Tesneem commited on
Commit
7242cc4
·
verified ·
1 Parent(s): dc82c6a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -16
app.py CHANGED
@@ -25,17 +25,19 @@ with st.sidebar:
25
 
26
  # Fetch collection names for dropdown
27
  try:
28
- existing_collections = db.list_collection_names()
29
- existing_collections.append("Create New Collection")
30
- selected_collection = st.selectbox("Choose MongoDB Collection", existing_collections, index=existing_collections.index("default_collection") if "default_collection" in existing_collections else 0)
31
- except Exception as e:
32
- st.error(f"Failed to list collections: {e}")
33
- selected_collection = "default_collection"
34
-
35
- if selected_collection == "Create New Collection":
36
- selected_collection = st.sidebar.text_input("Enter Collection Name:")
37
- if not selected_collection:
38
- st.warning("⚠️ Enter a collection name to proceed.")
 
 
39
  st.stop()
40
 
41
  is_grant_app = st.toggle("Is this a Grant Application?", value=False)
@@ -50,10 +52,14 @@ if uploaded_file:
50
  st.success(f"Uploaded `{uploaded_file.name}`")
51
 
52
  modified_time = datetime.now().isoformat()
53
- collection = db[selected_collection]
54
-
55
- if collection.find_one({"metadata.title": uploaded_file.name}):
56
- st.warning("⚠️ This file already exists in the collection. Skipping...")
 
 
 
 
57
  else:
58
  st.write("⏳ Processing with DocumentChunker...")
59
  chunker = DocumentChunker()
@@ -61,6 +67,7 @@ if uploaded_file:
61
 
62
  if chunks:
63
  for chunk in chunks:
 
64
  chunk['metadata'].update({
65
  "title": uploaded_file.name,
66
  "uploaded_at": modified_time,
@@ -68,7 +75,7 @@ if uploaded_file:
68
  })
69
  collection.insert_one(chunk)
70
 
71
- st.success(f"✅ {len(chunks)} chunks inserted into `{selected_collection}`")
72
 
73
  # Show a few previews
74
  for i, c in enumerate(chunks[:3]):
 
25
 
26
  # Fetch collection names for dropdown
27
  try:
28
+ existing_categories = db["final_chunks"].distinct("collection_category") or []
29
+ except Exception:
30
+ existing_categories = []
31
+ existing_categories=sorted([c for c in existing_categories if c])+["Create New Category"]
32
+ selected_category = st.selectbox(
33
+ "Choose Category (collection_category)",
34
+ existing_categories,
35
+ index=existing_categories.index("Create New Category") if "Create New Category" in existing_categories else 0
36
+ )
37
+ if selected_category == "Create New Category":
38
+ selected_category = st.sidebar.text_input("Enter Category Name:")
39
+ if not selected_category:
40
+ st.warning("⚠️ Enter a category name to proceed.")
41
  st.stop()
42
 
43
  is_grant_app = st.toggle("Is this a Grant Application?", value=False)
 
52
  st.success(f"Uploaded `{uploaded_file.name}`")
53
 
54
  modified_time = datetime.now().isoformat()
55
+ collection = db['final_chunks']
56
+ already = final_col.find_one({
57
+ "metadata.title": uploaded_file.name,
58
+ "collection_category": selected_category
59
+ })
60
+
61
+ if already:
62
+ st.warning(f"⚠️ `{uploaded_file.name}` already exists in category `{selected_category}`. Skipping…")
63
  else:
64
  st.write("⏳ Processing with DocumentChunker...")
65
  chunker = DocumentChunker()
 
67
 
68
  if chunks:
69
  for chunk in chunks:
70
+ chunk['collection_category']=selected_category
71
  chunk['metadata'].update({
72
  "title": uploaded_file.name,
73
  "uploaded_at": modified_time,
 
75
  })
76
  collection.insert_one(chunk)
77
 
78
+ st.success(f"✅ {len(chunks)} chunks inserted into `final_chunks` (category: `{selected_category}`)")
79
 
80
  # Show a few previews
81
  for i, c in enumerate(chunks[:3]):