Dinesh310 commited on
Commit
4928a8c
·
verified ·
1 Parent(s): 1d17bf5

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +47 -39
src/streamlit_app.py CHANGED
@@ -1,40 +1,48 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
5
-
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  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]}...\"")