Dinesh310 commited on
Commit
e2b3fc7
·
verified ·
1 Parent(s): e3f3e21

Create streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +90 -0
streamlit_app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
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:
72
+ with st.chat_message("user"):
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]}...\"")