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+
# Streamlit App Interface (app.py)
|
| 3 |
+
def main():
|
| 4 |
+
st.title("**SYNAPTYX - RFP Analysis Agent**")
|
| 5 |
+
st.markdown("<h3 style='color: #1E3A8A;'>Upload RFP documents, provide a URL, search, and get intelligent answers.</h3>", unsafe_allow_html=True)
|
| 6 |
+
|
| 7 |
+
# Database Initialization
|
| 8 |
+
database = "rfp_agent.db"
|
| 9 |
+
conn = create_connection(database)
|
| 10 |
+
if conn is not None:
|
| 11 |
+
create_tables(conn)
|
| 12 |
+
else:
|
| 13 |
+
st.error("Error! Cannot create the database connection.")
|
| 14 |
+
|
| 15 |
+
# Dashboard Overview Tab
|
| 16 |
+
st.sidebar.markdown("<h2 style='color: #1E3A8A;'>Dashboard Overview</h2>", unsafe_allow_html=True)
|
| 17 |
+
if conn is not None:
|
| 18 |
+
try:
|
| 19 |
+
cursor = conn.cursor()
|
| 20 |
+
cursor.execute("SELECT COUNT(*) FROM documents")
|
| 21 |
+
total_documents = cursor.fetchone()[0]
|
| 22 |
+
|
| 23 |
+
cursor.execute("SELECT COUNT(*) FROM queries")
|
| 24 |
+
total_queries = cursor.fetchone()[0]
|
| 25 |
+
|
| 26 |
+
st.sidebar.write(f"Total Documents: {total_documents}")
|
| 27 |
+
st.sidebar.write(f"Total Queries: {total_queries}")
|
| 28 |
+
except Exception as e:
|
| 29 |
+
st.error(f"Error retrieving dashboard data: {e}")
|
| 30 |
+
|
| 31 |
+
# Sidebar Knowledge Base Tab
|
| 32 |
+
st.sidebar.markdown("<h2 style='color: #1E3A8A;'>Knowledge Base</h2>", unsafe_allow_html=True)
|
| 33 |
+
st.sidebar.markdown("<p style='color: #1E3A8A;'>View and select documents for search.</p>", unsafe_allow_html=True)
|
| 34 |
+
|
| 35 |
+
# Retrieve Documents from Database
|
| 36 |
+
if conn is not None:
|
| 37 |
+
try:
|
| 38 |
+
cursor = conn.cursor()
|
| 39 |
+
cursor.execute("SELECT id, name FROM documents")
|
| 40 |
+
documents_in_db = cursor.fetchall()
|
| 41 |
+
|
| 42 |
+
if documents_in_db:
|
| 43 |
+
# Use st.multiselect instead of st.selectbox
|
| 44 |
+
selected_doc_ids = st.sidebar.multiselect(
|
| 45 |
+
"Select documents to include in the search:",
|
| 46 |
+
options=[doc[0] for doc in documents_in_db],
|
| 47 |
+
format_func=lambda doc_id: next(doc[1] for doc in documents_in_db if doc[0] == doc_id),
|
| 48 |
+
default=[doc[0] for doc in documents_in_db] # Select all documents by default
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
if selected_doc_ids:
|
| 52 |
+
selected_documents = []
|
| 53 |
+
selected_doc_names = [] # Also keep track of the document names
|
| 54 |
+
for doc_id in selected_doc_ids:
|
| 55 |
+
cursor.execute("SELECT content, name FROM documents WHERE id = ?", (doc_id,))
|
| 56 |
+
result = cursor.fetchone()
|
| 57 |
+
selected_documents.append(result[0])
|
| 58 |
+
selected_doc_names.append(result[1]) # Add the name to the list
|
| 59 |
+
|
| 60 |
+
# Initialize FAISS and Store Embeddings for Selected Documents
|
| 61 |
+
embeddings = get_embeddings_model()
|
| 62 |
+
if embeddings:
|
| 63 |
+
vector_store = initialize_faiss(embeddings, selected_documents, selected_doc_names) # Use selected_doc_names here
|
| 64 |
+
if vector_store:
|
| 65 |
+
st.sidebar.success("Embeddings for selected documents stored successfully.", icon="📁")
|
| 66 |
+
|
| 67 |
+
# Initialize QA System for Selected Documents
|
| 68 |
+
qa_system = initialize_qa_system(vector_store)
|
| 69 |
+
if qa_system:
|
| 70 |
+
# Query Input
|
| 71 |
+
user_query = st.text_input("Enter your query about the RFPs:", placeholder="e.g., What are the evaluation criteria?", label_visibility='visible')
|
| 72 |
+
if user_query:
|
| 73 |
+
st.markdown("<p style='color: #1E3A8A;'>Retrieving answer...</p>", unsafe_allow_html=True)
|
| 74 |
+
try:
|
| 75 |
+
response, source_documents = qa_system.run(query=user_query, return_source_documents=True)
|
| 76 |
+
response = qa_system.run(query=user_query, return_source_documents=True)
|
| 77 |
+
st.markdown("<h4 style='color: #1E3A8A;'>Answer:</h4>", unsafe_allow_html=True)
|
| 78 |
+
st.write(response["result"]) # Access the answer text
|
| 79 |
+
st.write(response["source_documents"]) # Access the source documents
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
# Store Query and Response in Database
|
| 84 |
+
with conn:
|
| 85 |
+
for doc in source_documents:
|
| 86 |
+
source_name = doc.metadata["source"]
|
| 87 |
+
document_id = conn.execute("SELECT id FROM documents WHERE name = ?", (source_name,)).fetchone()
|
| 88 |
+
if document_id:
|
| 89 |
+
conn.execute("INSERT INTO queries (query, response, document_id) VALUES (?, ?, ?)", (user_query, response, document_id[0]))
|
| 90 |
+
|
| 91 |
+
# Display Source Information
|
| 92 |
+
st.markdown("<h4 style='color: #1E3A8A;'>Sources:</h4>", unsafe_allow_html=True)
|
| 93 |
+
for doc in source_documents:
|
| 94 |
+
source_name = doc.metadata["source"]
|
| 95 |
+
matched_text = doc.page_content
|
| 96 |
+
st.write(f"- Source Document: {source_name}")
|
| 97 |
+
# Display the matching text with highlighting
|
| 98 |
+
for idx, page_content in enumerate(document_pages[document_names.index(source_name)]):
|
| 99 |
+
if matched_text in page_content:
|
| 100 |
+
highlighted_content = re.sub(re.escape(matched_text), f"<mark>{matched_text}</mark>", page_content)
|
| 101 |
+
st.write(f" - Page {idx + 1}: {highlighted_content}")
|
| 102 |
+
|
| 103 |
+
except Exception as e:
|
| 104 |
+
st.error(f"Error generating response: {e}")
|
| 105 |
+
except Exception as e:
|
| 106 |
+
st.error(f"Error retrieving documents from database: {e}")
|
| 107 |
+
|
| 108 |
+
# Document Upload Section
|
| 109 |
+
st.markdown("<h2 style='color: #1E3A8A;'>Upload RFP Documents</h2>", unsafe_allow_html=True)
|
| 110 |
+
uploaded_documents = st.file_uploader("Upload PDF documents", type="pdf", accept_multiple_files=True)
|
| 111 |
+
if uploaded_documents:
|
| 112 |
+
st.write(f"Uploaded {len(uploaded_documents)} documents.")
|
| 113 |
+
all_texts, document_names, document_pages = upload_and_parse_documents(uploaded_documents)
|
| 114 |
+
if all_texts:
|
| 115 |
+
# Store Documents in Database
|
| 116 |
+
if conn is not None:
|
| 117 |
+
try:
|
| 118 |
+
with conn:
|
| 119 |
+
for doc, doc_name in zip(all_texts, document_names):
|
| 120 |
+
conn.execute("INSERT INTO documents (name, content) VALUES (?, ?)", (doc_name, doc))
|
| 121 |
+
st.success("Documents uploaded and parsed successfully.", icon="✅")
|
| 122 |
+
except Exception as e:
|
| 123 |
+
st.error(f"Error saving documents to database: {e}")
|
| 124 |
+
|
| 125 |
+
# URL Input Section
|
| 126 |
+
st.markdown("<h2 style='color: #1E3A8A;'>Or Provide a URL</h2>", unsafe_allow_html=True)
|
| 127 |
+
url = st.text_input("Enter the URL of a PDF document:")
|
| 128 |
+
if url:
|
| 129 |
+
all_texts, document_name = parse_pdf_from_url(url)
|
| 130 |
+
if all_texts:
|
| 131 |
+
# Store Document in Database
|
| 132 |
+
if conn is not None:
|
| 133 |
+
try:
|
| 134 |
+
with conn:
|
| 135 |
+
for doc in all_texts:
|
| 136 |
+
conn.execute("INSERT INTO documents (name, content) VALUES (?, ?)", (document_name, doc))
|
| 137 |
+
st.success("Document from URL uploaded and parsed successfully.", icon="✅")
|
| 138 |
+
except Exception as e:
|
| 139 |
+
st.error(f"Error saving document from URL to database: {e}")
|
| 140 |
+
|
| 141 |
+
# Google Drive Integration Section
|
| 142 |
+
st.markdown("<h2 style='color: #1E3A8A;'>Or Fetch from Google Drive</h2>", unsafe_allow_html=True)
|
| 143 |
+
gdrive_file_id = st.text_input("Enter the Google Drive File ID:")
|
| 144 |
+
if gdrive_file_id:
|
| 145 |
+
all_texts, document_name = parse_pdf_from_google_drive(gdrive_file_id)
|
| 146 |
+
if all_texts:
|
| 147 |
+
# Store Document in Database
|
| 148 |
+
if conn is not None:
|
| 149 |
+
try:
|
| 150 |
+
with conn:
|
| 151 |
+
for doc in all_texts:
|
| 152 |
+
conn.execute("INSERT INTO documents (name, content) VALUES (?, ?)", (document_name, doc))
|
| 153 |
+
st.success("Document from Google Drive uploaded and parsed successfully.", icon="✅")
|
| 154 |
+
except Exception as e:
|
| 155 |
+
st.error(f"Error saving document from Google Drive to database: {e}")
|
| 156 |
+
|
| 157 |
+
if __name__ == "__main__":
|
| 158 |
+
main()
|