import streamlit as st
import tempfile
from dspy_qa import DocQA
import os
os.system("pip install dspy-ai[faiss-cpu]")
st.set_page_config(page_title="DoC QA", layout="wide")
# st.title("📄 Chat over PDF using DSPy 🚀")
st.markdown("""
Document QA DSPY
""", unsafe_allow_html=True)
st.markdown("""
""", unsafe_allow_html=True)
# Initialize chat history
if "knowledge_base" not in st.session_state:
st.session_state.knowledge_base = None
if st.session_state.knowledge_base is None:
pdf_file = st.file_uploader("Upload a PDF file", "csv")
if pdf_file:
with tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as temp_file:
temp_file.write(pdf_file.getbuffer())
temp_file_path = temp_file.name
docqa = DocQA(temp_file_path)
if docqa:
st.success("PDF file uploaded successfully!")
st.session_state.knowledge_base = docqa
docqa = st.session_state.knowledge_base
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Display chat messages from history on app rerun
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Container for input and reset button
# Accept user input
if question := st.chat_input("What is up?"):
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": question})
# Display user message in chat message container
with st.chat_message("user"):
st.markdown(question)
if docqa is None:
with st.chat_message("assistant"):
st.markdown("Please upload a PDF file first.")
st.session_state.messages.append({"role": "assistant", "content": "Please upload a PDF file first!"})
st.stop()
# Display assistant response in chat message container
with st.chat_message("assistant"):
response = docqa.run(question)
st.markdown(response)
st.session_state.messages.append({"role": "assistant", "content": response})
if st.button("Reset"):
st.session_state.knowledge_base = None
st.session_state.messages = []
st.rerun() # Rerun the script