Dinesh310 commited on
Commit
3a8d617
Β·
verified Β·
1 Parent(s): 6ae5065

Update streamlit_app.py

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