Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -11,10 +11,8 @@ from urllib.parse import quote_plus
|
|
11 |
user = quote_plus(os.getenv("MONGO_USER"))
|
12 |
password = quote_plus(os.getenv("MONGO_PASS"))
|
13 |
cluster = os.getenv("MONGO_CLUSTER")
|
14 |
-
# db_name = "grant_docs"
|
15 |
db_name = os.environ.get("MONGO_DB", "grant_docs")
|
16 |
mongo_uri = f"mongodb+srv://{user}:{password}@{cluster}/{db_name}?retryWrites=true&w=majority&tls=true&tlsAllowInvalidCertificates=true"
|
17 |
-
# mongo_uri = os.environ["MONGO_URI"]
|
18 |
client = MongoClient(mongo_uri, tls=True, tlsAllowInvalidCertificates=True, serverSelectionTimeoutMS=20000)
|
19 |
db = client[db_name]
|
20 |
|
@@ -33,17 +31,17 @@ with st.sidebar:
|
|
33 |
except Exception as e:
|
34 |
st.error(f"Failed to list collections: {e}")
|
35 |
selected_collection = "doc_chunks_cat"
|
|
|
36 |
if selected_collection == "Create New Collection":
|
37 |
-
selected_collection = st.sidebar.text_input("Enter Collection Name:
|
38 |
if not selected_collection:
|
39 |
st.warning("⚠️ Enter a collection name to proceed.")
|
40 |
st.stop()
|
|
|
41 |
is_grant_app = st.toggle("Is this a Grant Application?", value=False)
|
42 |
|
43 |
-
# uploaded_file = st.file_uploader("Upload a DOCX or TXT file", type=["docx", "txt"])
|
44 |
uploaded_file = st.file_uploader("Upload a DOCX, TXT, or PDF file", type=["docx", "txt", "pdf"])
|
45 |
|
46 |
-
|
47 |
if uploaded_file:
|
48 |
temp_path = Path(tempfile.gettempdir()) / uploaded_file.name
|
49 |
with open(temp_path, "wb") as f:
|
@@ -69,10 +67,9 @@ if uploaded_file:
|
|
69 |
"is_grant_app": is_grant_app,
|
70 |
})
|
71 |
collection.insert_one(chunk)
|
72 |
-
|
73 |
st.success(f"✅ {len(chunks)} chunks inserted into `{selected_collection}`")
|
74 |
|
75 |
-
# Show a few previews
|
76 |
for i, c in enumerate(chunks[:3]):
|
77 |
st.subheader(f"Chunk {i+1}: {c['metadata'].get('header') or 'No Header'}")
|
78 |
st.markdown(c['text'][:400] + "...")
|
@@ -81,13 +78,14 @@ if uploaded_file:
|
|
81 |
|
82 |
if len(chunks) > 3:
|
83 |
st.info(f"... and {len(chunks)-3} more chunks processed.")
|
84 |
-
# st.success(f"✅ {len(chunks)} chunks inserted into `{selected_collection}`")
|
85 |
-
try:
|
86 |
-
os.remove(temp_path)
|
87 |
-
except Exception as e:
|
88 |
-
st.warning(f"⚠️ Could not delete temp file: {e}")
|
89 |
-
# Rerun the app to refresh the UI
|
90 |
-
st.rerun()
|
91 |
|
92 |
else:
|
93 |
st.warning("⚠️ No chunks were generated.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
user = quote_plus(os.getenv("MONGO_USER"))
|
12 |
password = quote_plus(os.getenv("MONGO_PASS"))
|
13 |
cluster = os.getenv("MONGO_CLUSTER")
|
|
|
14 |
db_name = os.environ.get("MONGO_DB", "grant_docs")
|
15 |
mongo_uri = f"mongodb+srv://{user}:{password}@{cluster}/{db_name}?retryWrites=true&w=majority&tls=true&tlsAllowInvalidCertificates=true"
|
|
|
16 |
client = MongoClient(mongo_uri, tls=True, tlsAllowInvalidCertificates=True, serverSelectionTimeoutMS=20000)
|
17 |
db = client[db_name]
|
18 |
|
|
|
31 |
except Exception as e:
|
32 |
st.error(f"Failed to list collections: {e}")
|
33 |
selected_collection = "doc_chunks_cat"
|
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)
|
42 |
|
|
|
43 |
uploaded_file = st.file_uploader("Upload a DOCX, TXT, or PDF file", type=["docx", "txt", "pdf"])
|
44 |
|
|
|
45 |
if uploaded_file:
|
46 |
temp_path = Path(tempfile.gettempdir()) / uploaded_file.name
|
47 |
with open(temp_path, "wb") as f:
|
|
|
67 |
"is_grant_app": is_grant_app,
|
68 |
})
|
69 |
collection.insert_one(chunk)
|
70 |
+
|
71 |
st.success(f"✅ {len(chunks)} chunks inserted into `{selected_collection}`")
|
72 |
|
|
|
73 |
for i, c in enumerate(chunks[:3]):
|
74 |
st.subheader(f"Chunk {i+1}: {c['metadata'].get('header') or 'No Header'}")
|
75 |
st.markdown(c['text'][:400] + "...")
|
|
|
78 |
|
79 |
if len(chunks) > 3:
|
80 |
st.info(f"... and {len(chunks)-3} more chunks processed.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
else:
|
83 |
st.warning("⚠️ No chunks were generated.")
|
84 |
+
|
85 |
+
try:
|
86 |
+
if temp_path.exists():
|
87 |
+
os.remove(temp_path)
|
88 |
+
except Exception as e:
|
89 |
+
st.warning(f"⚠️ Could not delete temp file: {e}")
|
90 |
+
|
91 |
+
st.rerun()
|