Rename app.py to Backend.py
Browse files- app.py → Backend.py +22 -173
app.py → Backend.py
RENAMED
|
@@ -21,8 +21,8 @@ import tempfile
|
|
| 21 |
import os
|
| 22 |
from langchain.llms import OpenAI # Import the OpenAI class
|
| 23 |
from langchain.chat_models import ChatOpenAI # Import ChatOpenAI
|
| 24 |
-
from langchain.agents import create_chat_conversational_react_description_agent
|
| 25 |
from langchain.memory import ConversationBufferMemory
|
|
|
|
| 26 |
|
| 27 |
|
| 28 |
|
|
@@ -196,180 +196,29 @@ def initialize_qa_system(_vector_store):
|
|
| 196 |
api_key=os.environ.get('OPENAI_API_KEY'),
|
| 197 |
)
|
| 198 |
|
| 199 |
-
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
|
|
|
|
|
|
|
|
|
| 215 |
except Exception as e:
|
| 216 |
st.error(f"Error initializing QA system: {e}")
|
| 217 |
return None
|
| 218 |
|
| 219 |
-
# Streamlit App Interface (app.py)
|
| 220 |
-
def main():
|
| 221 |
-
st.title("**SYNAPTYX - RFP Analysis Agent**")
|
| 222 |
-
st.markdown("<h3 style='color: #1E3A8A;'>Upload RFP documents, provide a URL, search, and get intelligent answers.</h3>", unsafe_allow_html=True)
|
| 223 |
-
|
| 224 |
-
# Database Initialization
|
| 225 |
-
database = "rfp_agent.db"
|
| 226 |
-
conn = create_connection(database)
|
| 227 |
-
if conn is not None:
|
| 228 |
-
create_tables(conn)
|
| 229 |
-
else:
|
| 230 |
-
st.error("Error! Cannot create the database connection.")
|
| 231 |
-
|
| 232 |
-
# Dashboard Overview Tab
|
| 233 |
-
st.sidebar.markdown("<h2 style='color: #1E3A8A;'>Dashboard Overview</h2>", unsafe_allow_html=True)
|
| 234 |
-
if conn is not None:
|
| 235 |
-
try:
|
| 236 |
-
cursor = conn.cursor()
|
| 237 |
-
cursor.execute("SELECT COUNT(*) FROM documents")
|
| 238 |
-
total_documents = cursor.fetchone()[0]
|
| 239 |
-
|
| 240 |
-
cursor.execute("SELECT COUNT(*) FROM queries")
|
| 241 |
-
total_queries = cursor.fetchone()[0]
|
| 242 |
-
|
| 243 |
-
st.sidebar.write(f"Total Documents: {total_documents}")
|
| 244 |
-
st.sidebar.write(f"Total Queries: {total_queries}")
|
| 245 |
-
except Exception as e:
|
| 246 |
-
st.error(f"Error retrieving dashboard data: {e}")
|
| 247 |
-
|
| 248 |
-
# Sidebar Knowledge Base Tab
|
| 249 |
-
st.sidebar.markdown("<h2 style='color: #1E3A8A;'>Knowledge Base</h2>", unsafe_allow_html=True)
|
| 250 |
-
st.sidebar.markdown("<p style='color: #1E3A8A;'>View and select documents for search.</p>", unsafe_allow_html=True)
|
| 251 |
-
|
| 252 |
-
# Retrieve Documents from Database
|
| 253 |
-
if conn is not None:
|
| 254 |
-
try:
|
| 255 |
-
cursor = conn.cursor()
|
| 256 |
-
cursor.execute("SELECT id, name FROM documents")
|
| 257 |
-
documents_in_db = cursor.fetchall()
|
| 258 |
-
|
| 259 |
-
if documents_in_db:
|
| 260 |
-
# Use st.multiselect instead of st.selectbox
|
| 261 |
-
selected_doc_ids = st.sidebar.multiselect(
|
| 262 |
-
"Select documents to include in the search:",
|
| 263 |
-
options=[doc[0] for doc in documents_in_db],
|
| 264 |
-
format_func=lambda doc_id: next(doc[1] for doc in documents_in_db if doc[0] == doc_id),
|
| 265 |
-
default=[doc[0] for doc in documents_in_db] # Select all documents by default
|
| 266 |
-
)
|
| 267 |
-
|
| 268 |
-
if selected_doc_ids:
|
| 269 |
-
selected_documents = []
|
| 270 |
-
selected_doc_names = [] # Also keep track of the document names
|
| 271 |
-
for doc_id in selected_doc_ids:
|
| 272 |
-
cursor.execute("SELECT content, name FROM documents WHERE id = ?", (doc_id,))
|
| 273 |
-
result = cursor.fetchone()
|
| 274 |
-
selected_documents.append(result[0])
|
| 275 |
-
selected_doc_names.append(result[1]) # Add the name to the list
|
| 276 |
-
|
| 277 |
-
# Initialize FAISS and Store Embeddings for Selected Documents
|
| 278 |
-
embeddings = get_embeddings_model()
|
| 279 |
-
if embeddings:
|
| 280 |
-
vector_store = initialize_faiss(embeddings, selected_documents, selected_doc_names) # Use selected_doc_names here
|
| 281 |
-
if vector_store:
|
| 282 |
-
st.sidebar.success("Embeddings for selected documents stored successfully.", icon="📁")
|
| 283 |
-
|
| 284 |
-
# Initialize QA System for Selected Documents
|
| 285 |
-
qa_system = initialize_qa_system(vector_store)
|
| 286 |
-
if qa_system:
|
| 287 |
-
# Query Input
|
| 288 |
-
user_query = st.text_input("Enter your query about the RFPs:", placeholder="e.g., What are the evaluation criteria?", label_visibility='visible')
|
| 289 |
-
if user_query:
|
| 290 |
-
st.markdown("<p style='color: #1E3A8A;'>Retrieving answer...</p>", unsafe_allow_html=True)
|
| 291 |
-
try:
|
| 292 |
-
response, source_documents = qa_system.run(query=user_query, return_source_documents=True)
|
| 293 |
-
response = qa_system.run(query=user_query, return_source_documents=True)
|
| 294 |
-
st.markdown("<h4 style='color: #1E3A8A;'>Answer:</h4>", unsafe_allow_html=True)
|
| 295 |
-
st.write(response["result"]) # Access the answer text
|
| 296 |
-
st.write(response["source_documents"]) # Access the source documents
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
# Store Query and Response in Database
|
| 301 |
-
with conn:
|
| 302 |
-
for doc in source_documents:
|
| 303 |
-
source_name = doc.metadata["source"]
|
| 304 |
-
document_id = conn.execute("SELECT id FROM documents WHERE name = ?", (source_name,)).fetchone()
|
| 305 |
-
if document_id:
|
| 306 |
-
conn.execute("INSERT INTO queries (query, response, document_id) VALUES (?, ?, ?)", (user_query, response, document_id[0]))
|
| 307 |
-
|
| 308 |
-
# Display Source Information
|
| 309 |
-
st.markdown("<h4 style='color: #1E3A8A;'>Sources:</h4>", unsafe_allow_html=True)
|
| 310 |
-
for doc in source_documents:
|
| 311 |
-
source_name = doc.metadata["source"]
|
| 312 |
-
matched_text = doc.page_content
|
| 313 |
-
st.write(f"- Source Document: {source_name}")
|
| 314 |
-
# Display the matching text with highlighting
|
| 315 |
-
for idx, page_content in enumerate(document_pages[document_names.index(source_name)]):
|
| 316 |
-
if matched_text in page_content:
|
| 317 |
-
highlighted_content = re.sub(re.escape(matched_text), f"<mark>{matched_text}</mark>", page_content)
|
| 318 |
-
st.write(f" - Page {idx + 1}: {highlighted_content}")
|
| 319 |
-
|
| 320 |
-
except Exception as e:
|
| 321 |
-
st.error(f"Error generating response: {e}")
|
| 322 |
-
except Exception as e:
|
| 323 |
-
st.error(f"Error retrieving documents from database: {e}")
|
| 324 |
-
|
| 325 |
-
# Document Upload Section
|
| 326 |
-
st.markdown("<h2 style='color: #1E3A8A;'>Upload RFP Documents</h2>", unsafe_allow_html=True)
|
| 327 |
-
uploaded_documents = st.file_uploader("Upload PDF documents", type="pdf", accept_multiple_files=True)
|
| 328 |
-
if uploaded_documents:
|
| 329 |
-
st.write(f"Uploaded {len(uploaded_documents)} documents.")
|
| 330 |
-
all_texts, document_names, document_pages = upload_and_parse_documents(uploaded_documents)
|
| 331 |
-
if all_texts:
|
| 332 |
-
# Store Documents in Database
|
| 333 |
-
if conn is not None:
|
| 334 |
-
try:
|
| 335 |
-
with conn:
|
| 336 |
-
for doc, doc_name in zip(all_texts, document_names):
|
| 337 |
-
conn.execute("INSERT INTO documents (name, content) VALUES (?, ?)", (doc_name, doc))
|
| 338 |
-
st.success("Documents uploaded and parsed successfully.", icon="✅")
|
| 339 |
-
except Exception as e:
|
| 340 |
-
st.error(f"Error saving documents to database: {e}")
|
| 341 |
-
|
| 342 |
-
# URL Input Section
|
| 343 |
-
st.markdown("<h2 style='color: #1E3A8A;'>Or Provide a URL</h2>", unsafe_allow_html=True)
|
| 344 |
-
url = st.text_input("Enter the URL of a PDF document:")
|
| 345 |
-
if url:
|
| 346 |
-
all_texts, document_name = parse_pdf_from_url(url)
|
| 347 |
-
if all_texts:
|
| 348 |
-
# Store Document in Database
|
| 349 |
-
if conn is not None:
|
| 350 |
-
try:
|
| 351 |
-
with conn:
|
| 352 |
-
for doc in all_texts:
|
| 353 |
-
conn.execute("INSERT INTO documents (name, content) VALUES (?, ?)", (document_name, doc))
|
| 354 |
-
st.success("Document from URL uploaded and parsed successfully.", icon="✅")
|
| 355 |
-
except Exception as e:
|
| 356 |
-
st.error(f"Error saving document from URL to database: {e}")
|
| 357 |
-
|
| 358 |
-
# Google Drive Integration Section
|
| 359 |
-
st.markdown("<h2 style='color: #1E3A8A;'>Or Fetch from Google Drive</h2>", unsafe_allow_html=True)
|
| 360 |
-
gdrive_file_id = st.text_input("Enter the Google Drive File ID:")
|
| 361 |
-
if gdrive_file_id:
|
| 362 |
-
all_texts, document_name = parse_pdf_from_google_drive(gdrive_file_id)
|
| 363 |
-
if all_texts:
|
| 364 |
-
# Store Document in Database
|
| 365 |
-
if conn is not None:
|
| 366 |
-
try:
|
| 367 |
-
with conn:
|
| 368 |
-
for doc in all_texts:
|
| 369 |
-
conn.execute("INSERT INTO documents (name, content) VALUES (?, ?)", (document_name, doc))
|
| 370 |
-
st.success("Document from Google Drive uploaded and parsed successfully.", icon="✅")
|
| 371 |
-
except Exception as e:
|
| 372 |
-
st.error(f"Error saving document from Google Drive to database: {e}")
|
| 373 |
-
|
| 374 |
-
if __name__ == "__main__":
|
| 375 |
-
main()
|
|
|
|
| 21 |
import os
|
| 22 |
from langchain.llms import OpenAI # Import the OpenAI class
|
| 23 |
from langchain.chat_models import ChatOpenAI # Import ChatOpenAI
|
|
|
|
| 24 |
from langchain.memory import ConversationBufferMemory
|
| 25 |
+
from langchain.agents import create_openai_tools_agent, AgentExecutor
|
| 26 |
|
| 27 |
|
| 28 |
|
|
|
|
| 196 |
api_key=os.environ.get('OPENAI_API_KEY'),
|
| 197 |
)
|
| 198 |
|
| 199 |
+
# Define the prompt template
|
| 200 |
+
prompt = ChatPromptTemplate.from_messages([
|
| 201 |
+
("system", "You are a helpful assistant"),
|
| 202 |
+
MessagesPlaceholder(variable_name="chat_history"),
|
| 203 |
|
| 204 |
+
("human", "{input}"),
|
| 205 |
+
])
|
| 206 |
+
|
| 207 |
+
# Define the tools
|
| 208 |
+
tools = [
|
| 209 |
+
Tool(
|
| 210 |
+
name="Search",
|
| 211 |
+
func=_vector_store.as_retriever(search_kwargs={"k": 2}).get_relevant_documents,
|
| 212 |
+
description="useful for when you need to answer questions about the documents you have been uploaded. Input should be a fully formed question.",
|
| 213 |
+
)
|
| 214 |
+
]
|
| 215 |
+
|
| 216 |
+
# Create the agent and executor
|
| 217 |
+
agent = create_openai_tools_agent(llm=llm, tools=tools, prompt=prompt)
|
| 218 |
+
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True, memory=ConversationBufferMemory(memory_key="chat_history"))
|
| 219 |
+
|
| 220 |
+
return agent_executor # Return the agent executor
|
| 221 |
except Exception as e:
|
| 222 |
st.error(f"Error initializing QA system: {e}")
|
| 223 |
return None
|
| 224 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|