cryogenic22 commited on
Commit
eef990a
·
verified ·
1 Parent(s): a010f3e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -8
app.py CHANGED
@@ -18,7 +18,7 @@ from googleapiclient.discovery import build
18
  from googleapiclient.http import MediaIoBaseDownload
19
  from google.oauth2 import service_account
20
  import tempfile
21
- import os
22
 
23
 
24
  # SQLite Database Functions (database.py)
@@ -205,11 +205,6 @@ def main():
205
  create_tables(conn)
206
  else:
207
  st.error("Error! Cannot create the database connection.")
208
- # ... (other imports and functions) ...
209
-
210
- # Streamlit App Interface (app.py)
211
- def main():
212
- # ... (other code) ...
213
 
214
  # Dashboard Overview Tab
215
  st.sidebar.markdown("<h2 style='color: #1E3A8A;'>Dashboard Overview</h2>", unsafe_allow_html=True)
@@ -266,12 +261,56 @@ def main():
266
  # Initialize QA System for Selected Documents
267
  qa_system = initialize_qa_system(vector_store)
268
  if qa_system:
269
- # ... (rest of your query processing code) ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270
 
 
 
271
  except Exception as e:
272
  st.error(f"Error retrieving documents from database: {e}")
273
 
274
- # ... (rest of your code) ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
275
 
276
  # URL Input Section
277
  st.markdown("<h2 style='color: #1E3A8A;'>Or Provide a URL</h2>", unsafe_allow_html=True)
 
18
  from googleapiclient.http import MediaIoBaseDownload
19
  from google.oauth2 import service_account
20
  import tempfile
21
+ import os
22
 
23
 
24
  # SQLite Database Functions (database.py)
 
205
  create_tables(conn)
206
  else:
207
  st.error("Error! Cannot create the database connection.")
 
 
 
 
 
208
 
209
  # Dashboard Overview Tab
210
  st.sidebar.markdown("<h2 style='color: #1E3A8A;'>Dashboard Overview</h2>", unsafe_allow_html=True)
 
261
  # Initialize QA System for Selected Documents
262
  qa_system = initialize_qa_system(vector_store)
263
  if qa_system:
264
+ # Query Input
265
+ user_query = st.text_input("Enter your query about the RFPs:", placeholder="e.g., What are the evaluation criteria?", label_visibility='visible')
266
+ if user_query:
267
+ st.markdown("<p style='color: #1E3A8A;'>Retrieving answer...</p>", unsafe_allow_html=True)
268
+ try:
269
+ response, source_documents = qa_system.run(user_query, return_source_documents=True)
270
+ st.markdown("<h4 style='color: #1E3A8A;'>Answer:</h4>", unsafe_allow_html=True)
271
+ st.write(response)
272
+
273
+ # Store Query and Response in Database
274
+ with conn:
275
+ for doc in source_documents:
276
+ source_name = doc.metadata["source"]
277
+ document_id = conn.execute("SELECT id FROM documents WHERE name = ?", (source_name,)).fetchone()
278
+ if document_id:
279
+ conn.execute("INSERT INTO queries (query, response, document_id) VALUES (?, ?, ?)", (user_query, response, document_id[0]))
280
+
281
+ # Display Source Information
282
+ st.markdown("<h4 style='color: #1E3A8A;'>Sources:</h4>", unsafe_allow_html=True)
283
+ for doc in source_documents:
284
+ source_name = doc.metadata["source"]
285
+ matched_text = doc.page_content
286
+ st.write(f"- Source Document: {source_name}")
287
+ # Display the matching text with highlighting
288
+ for idx, page_content in enumerate(document_pages[document_names.index(source_name)]):
289
+ if matched_text in page_content:
290
+ highlighted_content = re.sub(re.escape(matched_text), f"<mark>{matched_text}</mark>", page_content)
291
+ st.write(f" - Page {idx + 1}: {highlighted_content}")
292
 
293
+ except Exception as e:
294
+ st.error(f"Error generating response: {e}")
295
  except Exception as e:
296
  st.error(f"Error retrieving documents from database: {e}")
297
 
298
+ # Document Upload Section
299
+ st.markdown("<h2 style='color: #1E3A8A;'>Upload RFP Documents</h2>", unsafe_allow_html=True)
300
+ uploaded_documents = st.file_uploader("Upload PDF documents", type="pdf", accept_multiple_files=True)
301
+ if uploaded_documents:
302
+ st.write(f"Uploaded {len(uploaded_documents)} documents.")
303
+ all_texts, document_names, document_pages = upload_and_parse_documents(uploaded_documents)
304
+ if all_texts:
305
+ # Store Documents in Database
306
+ if conn is not None:
307
+ try:
308
+ with conn:
309
+ for doc, doc_name in zip(all_texts, document_names):
310
+ conn.execute("INSERT INTO documents (name, content) VALUES (?, ?)", (doc_name, doc))
311
+ st.success("Documents uploaded and parsed successfully.", icon="✅")
312
+ except Exception as e:
313
+ st.error(f"Error saving documents to database: {e}")
314
 
315
  # URL Input Section
316
  st.markdown("<h2 style='color: #1E3A8A;'>Or Provide a URL</h2>", unsafe_allow_html=True)