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