|
|
import streamlit as st |
|
|
import os |
|
|
from src.rag_engine import ProjectRAGEngine |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
st.set_page_config( |
|
|
page_title="IIR Project Analyzer", |
|
|
layout="wide" |
|
|
) |
|
|
|
|
|
st.title("π Industrial Project Report Analyzer") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if "engine" not in st.session_state: |
|
|
st.session_state.engine = ProjectRAGEngine() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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]}...\"") |
|
|
|