Dinesh310 commited on
Commit
714e3b2
ยท
verified ยท
1 Parent(s): d31a43f

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +52 -32
src/streamlit_app.py CHANGED
@@ -2,53 +2,70 @@ import streamlit as st
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:
@@ -56,15 +73,18 @@ if query:
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]}...\"")
 
 
 
 
2
  import os
3
  from rag_engine import ProjectRAGEngine
4
 
5
+ # -------------------------------
6
+ # Page configuration
7
+ # -------------------------------
8
+ st.set_page_config(
9
+ page_title="IIR Project Analyzer",
10
+ layout="wide"
11
+ )
12
+
13
  st.title("๐Ÿ“‚ Industrial Project Report Analyzer")
14
 
15
+ # -------------------------------
16
+ # Environment validation
17
+ # -------------------------------
18
+ openai_key = os.getenv("OPENAI_API_KEY")
19
+ openrouter_key = os.getenv("OPENROUTER_API_KEY")
20
 
21
+ if not openai_key or not openrouter_key:
22
+ st.error(
23
+ "Missing API keys.\n\n"
24
+ "Please set the following environment variables:\n"
25
+ "- OPENAI_API_KEY (for embeddings)\n"
26
+ "- OPENROUTER_API_KEY (for LLM)"
27
+ )
28
  st.stop()
29
 
30
+ # -------------------------------
31
+ # Session persistence
32
+ # -------------------------------
33
  if "engine" not in st.session_state:
34
+ st.session_state.engine = ProjectRAGEngine()
35
 
36
+ # -------------------------------
37
+ # File upload
38
+ # -------------------------------
39
  uploaded_files = st.sidebar.file_uploader(
40
+ "Upload Project PDFs",
41
+ type="pdf",
42
  accept_multiple_files=True
43
  )
44
 
45
  if uploaded_files:
46
+ if (
47
+ "processed_files" not in st.session_state
48
+ or set(st.session_state.processed_files) != {f.name for f in uploaded_files}
49
+ ):
50
  with st.spinner("Analyzing project reports..."):
 
51
  temp_dir = "temp"
52
+ os.makedirs(temp_dir, exist_ok=True)
53
+
 
54
  paths = []
55
  for f in uploaded_files:
56
  path = os.path.join(temp_dir, f.name)
57
  with open(path, "wb") as out:
58
  out.write(f.getbuffer())
59
  paths.append(path)
60
+
 
61
  st.session_state.engine.process_documents(paths)
62
  st.session_state.processed_files = [f.name for f in uploaded_files]
63
+
64
  st.success("Reports indexed. Ready for queries.")
65
 
66
+ # -------------------------------
67
+ # Chat interface
68
+ # -------------------------------
69
  query = st.chat_input("Ex: 'Compare the budgets of these projects'")
70
 
71
  if query:
 
73
  st.write(query)
74
 
75
  with st.chat_message("assistant"):
 
76
  answer, sources = st.session_state.engine.get_answer(query)
77
+
78
+ st.markdown("### Response")
79
+ st.write(answer)
80
+
81
  if sources:
82
+ with st.expander("๐Ÿ“Œ Source Attribution & Quotes"):
83
  for idx, s in enumerate(sources):
84
+ doc_name = os.path.basename(s["metadata"]["source"])
85
+ page_num = s["metadata"]["page"] + 1
86
+
87
+ st.markdown(
88
+ f"**Source {idx + 1}:** {doc_name} (Page {page_num})"
89
+ )
90
+ st.caption(f"\"{s['content'][:300]}...\"")