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