Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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]}...\"")
|