Dinesh310 commited on
Commit
ad75b70
·
verified ·
1 Parent(s): bd75a92

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +42 -20
src/streamlit_app.py CHANGED
@@ -2,47 +2,69 @@ import streamlit as st
2
  import os
3
  from rag_engine import ProjectRAGEngine
4
 
5
- st.set_page_config(page_title="Project Analyzer", layout="wide")
 
6
  st.title("📂 Industrial Project Report Analyzer")
7
 
8
- # Handling OpenAI API Key from Hugging Face Secrets or User Input
 
9
  api_key = os.environ.get("OPENAI_API_KEY")
10
  if not api_key:
11
  api_key = st.sidebar.text_input("Enter OpenAI API Key", type="password")
12
 
13
  if not api_key:
14
- st.warning("Please provide an OpenAI API Key to continue.")
15
  st.stop()
16
 
 
17
  if "engine" not in st.session_state:
18
  st.session_state.engine = ProjectRAGEngine(api_key)
19
 
20
- # Document Upload Section [cite: 29]
21
- uploaded_files = st.sidebar.file_uploader("Upload Project PDFs", type="pdf", accept_multiple_files=True)
 
 
 
 
22
 
23
  if uploaded_files:
 
24
  if "processed_files" not in st.session_state or len(st.session_state.processed_files) != len(uploaded_files):
25
- with st.spinner("Processing documents..."):
26
- if not os.path.exists("temp"): os.makedirs("temp")
 
 
 
 
27
  paths = []
28
  for f in uploaded_files:
29
- p = os.path.join("temp", f.name)
30
- with open(p, "wb") as out: out.write(f.getbuffer())
31
- paths.append(p)
 
 
 
32
  st.session_state.engine.process_documents(paths)
33
  st.session_state.processed_files = [f.name for f in uploaded_files]
34
- st.success("Indexing complete. You can now ask questions.")
35
 
36
- # Chat Interface [cite: 30, 31]
37
  query = st.chat_input("Ex: 'Compare the budgets of these projects'")
 
38
  if query:
39
- with st.chat_message("user"): st.write(query)
 
 
40
  with st.chat_message("assistant"):
 
41
  answer, sources = st.session_state.engine.get_answer(query)
42
- st.write(answer)
43
- with st.expander("Source Attribution & Quotes"):
44
- for s in sources:
45
- doc = os.path.basename(s['metadata']['source'])
46
- page = s['metadata']['page']
47
- st.markdown(f"**{doc} (Page {page})**")
48
- st.caption(f"\"{s['content'][:200]}...\"")
 
 
 
 
2
  import os
3
  from rag_engine import ProjectRAGEngine
4
 
5
+ # Page configuration to ensure wide display for citations [cite: 31]
6
+ st.set_page_config(page_title="IIR Project Analyzer", layout="wide")
7
  st.title("📂 Industrial Project Report Analyzer")
8
 
9
+ # 1. API Key Security Logic [cite: 21]
10
+ # Checks Hugging Face Secrets first, then falls back to user input
11
  api_key = os.environ.get("OPENAI_API_KEY")
12
  if not api_key:
13
  api_key = st.sidebar.text_input("Enter OpenAI API Key", type="password")
14
 
15
  if not api_key:
16
+ st.warning("Please provide an OpenAI API Key in the sidebar to proceed.")
17
  st.stop()
18
 
19
+ # 2. Session Persistence [cite: 8, 44]
20
  if "engine" not in st.session_state:
21
  st.session_state.engine = ProjectRAGEngine(api_key)
22
 
23
+ # 3. Document Management Interface [cite: 6, 29]
24
+ uploaded_files = st.sidebar.file_uploader(
25
+ "Upload Project PDFs",
26
+ type="pdf",
27
+ accept_multiple_files=True
28
+ )
29
 
30
  if uploaded_files:
31
+ # Check if files are already processed to prevent redundant API calls [cite: 8]
32
  if "processed_files" not in st.session_state or len(st.session_state.processed_files) != len(uploaded_files):
33
+ with st.spinner("Analyzing project reports..."):
34
+ # Ensure the temp directory has write permissions to avoid 403/Permission errors
35
+ temp_dir = "temp"
36
+ if not os.path.exists(temp_dir):
37
+ os.makedirs(temp_dir, mode=0o777)
38
+
39
  paths = []
40
  for f in uploaded_files:
41
+ path = os.path.join(temp_dir, f.name)
42
+ with open(path, "wb") as out:
43
+ out.write(f.getbuffer())
44
+ paths.append(path)
45
+
46
+ # Simultaneous querying setup [cite: 7]
47
  st.session_state.engine.process_documents(paths)
48
  st.session_state.processed_files = [f.name for f in uploaded_files]
49
+ st.success("Reports indexed. Ready for queries.")
50
 
51
+ # 4. Question Answering Interface [cite: 30-31]
52
  query = st.chat_input("Ex: 'Compare the budgets of these projects'")
53
+
54
  if query:
55
+ with st.chat_message("user"):
56
+ st.write(query)
57
+
58
  with st.chat_message("assistant"):
59
+ # Handle cases with no documents [cite: 25]
60
  answer, sources = st.session_state.engine.get_answer(query)
61
+ st.markdown(f"### Response\n{answer}") # cite: 16
62
+
63
+ # Mandatory Source Attribution Display [cite: 11, 17, 31]
64
+ if sources:
65
+ with st.expander("View Source Attribution & Quotes"): # cite: 18
66
+ for idx, s in enumerate(sources):
67
+ doc_name = os.path.basename(s['metadata']['source'])
68
+ page_num = s['metadata']['page'] + 1 # PDF index starts at 0
69
+ st.markdown(f"**Source {idx+1}:** {doc_name} (Page {page_num})")
70
+ st.caption(f"Verbatim Quote: \"{s['content'][:300]}...\"")