Dinesh310 commited on
Commit
57bbabb
·
verified ·
1 Parent(s): 7f9f761

Create streamlit_app2.py

Browse files
Files changed (1) hide show
  1. streamlit_app2.py +98 -0
streamlit_app2.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ import tempfile
4
+ from src.RAG_builder import ProjectRAGGraph # Ensure your graph class is in your_filename.py
5
+
6
+ # --- Page Config ---
7
+ st.set_page_config(page_title="Project Analyst RAG", layout="wide")
8
+ st.title("📄 Professional Project Analyst Chat")
9
+
10
+ # --- Initialize Session State ---
11
+ if "rag_graph" not in st.session_state:
12
+ st.session_state.rag_graph = ProjectRAGGraph()
13
+ if "messages" not in st.session_state:
14
+ st.session_state.messages = []
15
+ if "thread_id" not in st.session_state:
16
+ st.session_state.thread_id = "default_user_1" # Hardcoded for demo, could be unique per session
17
+
18
+ # --- Sidebar: File Upload ---
19
+ with st.sidebar:
20
+ st.header("Upload Documents")
21
+ uploaded_files = st.file_uploader(
22
+ "Upload Project PDFs",
23
+ type="pdf",
24
+ accept_multiple_files=True
25
+ )
26
+
27
+ process_button = st.button("Process Documents")
28
+
29
+ if process_button and uploaded_files:
30
+ with st.spinner("Processing PDFs..."):
31
+ # Create temporary file paths to pass to your PDF Loader
32
+ pdf_paths = []
33
+ for uploaded_file in uploaded_files:
34
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
35
+ tmp.write(uploaded_file.getvalue())
36
+ pdf_paths.append(tmp.name)
37
+
38
+ # Use your existing process_documents method
39
+ st.session_state.rag_graph.process_documents(pdf_paths)
40
+
41
+ # Clean up temp files
42
+ for path in pdf_paths:
43
+ os.remove(path)
44
+
45
+ st.success("Documents Indexed Successfully!")
46
+
47
+ # --- Chat Interface ---
48
+ # Display existing messages
49
+ for message in st.session_state.messages:
50
+ with st.chat_message(message["role"]):
51
+ st.markdown(message["content"])
52
+ if "citations" in message and message["citations"]:
53
+ with st.expander("View Sources"):
54
+ for doc in message["citations"]:
55
+ st.caption(f"Source: {doc.metadata.get('source', 'Unknown')} - Page: {doc.metadata.get('page', 'N/A')}")
56
+ st.write(f"_{doc.page_content[:200]}..._")
57
+
58
+ # User Input
59
+ if prompt := st.chat_input("Ask a question about your projects..."):
60
+ # Check if vector store is ready
61
+ if st.session_state.rag_graph.vector_store is None:
62
+ st.error("Please upload and process documents first!")
63
+ else:
64
+ # Add user message to state
65
+ st.session_state.messages.append({"role": "user", "content": prompt})
66
+ with st.chat_message("user"):
67
+ st.markdown(prompt)
68
+
69
+ # Generate Response using the Graph
70
+ with st.chat_message("assistant"):
71
+ with st.spinner("Analyzing..."):
72
+ # We need to call the graph. We'll modify the query return slightly to get citations
73
+ config = {"configurable": {"thread_id": st.session_state.thread_id}}
74
+ inputs = {"question": prompt}
75
+
76
+ # Execute graph
77
+ result = st.session_state.rag_graph.workflow.invoke(inputs, config=config)
78
+
79
+ answer = result["answer"]
80
+ context = result["context"] # These are the retrieved Document objects
81
+
82
+ st.markdown(answer)
83
+
84
+ # Citations section
85
+ if context:
86
+ with st.expander("View Sources"):
87
+ for doc in context:
88
+ source_name = os.path.basename(doc.metadata.get('source', 'Unknown'))
89
+ page_num = doc.metadata.get('page', 0) + 1
90
+ st.caption(f"📄 {source_name} (Page {page_num})")
91
+ st.write(f"_{doc.page_content[:300]}..._")
92
+
93
+ # Add assistant response to state
94
+ st.session_state.messages.append({
95
+ "role": "assistant",
96
+ "content": answer,
97
+ "citations": context
98
+ })