Create streamlit_app3.py
Browse files- streamlit_app3.py +71 -0
streamlit_app3.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import uuid
|
| 3 |
+
import tempfile
|
| 4 |
+
from src.core.embeddings import load_embeddings
|
| 5 |
+
from src.core.llm import load_llm
|
| 6 |
+
from src.ingestion.document_processor import DocumentProcessor
|
| 7 |
+
from src.graph.rag_graph import ProjectRAGGraph
|
| 8 |
+
|
| 9 |
+
# ---------------- Page Config ----------------
|
| 10 |
+
st.set_page_config(page_title="π Project RAG Chat", layout="wide")
|
| 11 |
+
st.title("π Industrial Project Report Chatbot")
|
| 12 |
+
|
| 13 |
+
# ---------------- Session State ----------------
|
| 14 |
+
if "thread_id" not in st.session_state:
|
| 15 |
+
st.session_state.thread_id = str(uuid.uuid4())
|
| 16 |
+
|
| 17 |
+
if "chat_history" not in st.session_state:
|
| 18 |
+
st.session_state.chat_history = []
|
| 19 |
+
|
| 20 |
+
if "rag_graph" not in st.session_state:
|
| 21 |
+
st.session_state.rag_graph = None
|
| 22 |
+
|
| 23 |
+
# ---------------- Sidebar ----------------
|
| 24 |
+
st.sidebar.header("π Upload PDFs")
|
| 25 |
+
uploaded_files = st.sidebar.file_uploader(
|
| 26 |
+
"Upload one or more project PDFs",
|
| 27 |
+
type="pdf",
|
| 28 |
+
accept_multiple_files=True
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
if uploaded_files and st.sidebar.button("π Process Documents"):
|
| 32 |
+
with st.spinner("Processing documents..."):
|
| 33 |
+
temp_paths = []
|
| 34 |
+
|
| 35 |
+
for file in uploaded_files:
|
| 36 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
|
| 37 |
+
tmp.write(file.read())
|
| 38 |
+
temp_paths.append(tmp.name)
|
| 39 |
+
|
| 40 |
+
embeddings = load_embeddings()
|
| 41 |
+
llm = load_llm()
|
| 42 |
+
|
| 43 |
+
processor = DocumentProcessor(embeddings)
|
| 44 |
+
vector_store = processor.process_pdfs(temp_paths)
|
| 45 |
+
|
| 46 |
+
st.session_state.rag_graph = ProjectRAGGraph(llm, vector_store)
|
| 47 |
+
st.success("Documents processed successfully!")
|
| 48 |
+
|
| 49 |
+
# ---------------- Chat UI ----------------
|
| 50 |
+
st.subheader("π¬ Ask Questions")
|
| 51 |
+
|
| 52 |
+
user_question = st.chat_input("Ask something about the uploaded reports...")
|
| 53 |
+
|
| 54 |
+
if user_question and st.session_state.rag_graph:
|
| 55 |
+
st.session_state.chat_history.append(("user", user_question))
|
| 56 |
+
|
| 57 |
+
with st.spinner("Thinking..."):
|
| 58 |
+
answer = st.session_state.rag_graph.query(
|
| 59 |
+
user_question,
|
| 60 |
+
st.session_state.thread_id
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
st.session_state.chat_history.append(("assistant", answer))
|
| 64 |
+
|
| 65 |
+
# ---------------- Display Chat ----------------
|
| 66 |
+
for role, msg in st.session_state.chat_history:
|
| 67 |
+
with st.chat_message(role):
|
| 68 |
+
st.markdown(msg)
|
| 69 |
+
|
| 70 |
+
if not st.session_state.rag_graph:
|
| 71 |
+
st.info("β¬
οΈ Upload PDFs and click *Process Documents* to start chatting.")
|