RAG / app.py
Rahbarnisa's picture
Update app.py
06f7b84 verified
import streamlit as st
import sys
import os
# Fix import issues in Hugging Face
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
# Page config (MUST be first Streamlit command)
st.set_page_config(
page_title="AI Pharmaceutical Support Assistant",
layout="wide"
)
st.title("πŸ’Š AI Pharmaceutical Support Assistant")
st.markdown("Ask questions from pharmaceutical documents and get AI-powered answers with sources.")
# Debug (to confirm app loads)
st.write("βœ… App started")
# Imports AFTER Streamlit setup
from src.loader import load_all_pdfs
from src.chunking import chunk_documents
from src.vector_store import create_vector_store
from src.rag_pipelines import retrieve_documents, build_context, generate_answer
from src.memory import add_to_memory, get_memory
# Cache system (VERY IMPORTANT for performance)
@st.cache_resource
def load_system():
docs = load_all_pdfs()
chunks = chunk_documents(docs)
db = create_vector_store(chunks)
return db
# Show loading spinner (important for UX)
with st.spinner("πŸ”„ Loading AI system... please wait (first run may take 30-60 seconds)"):
db = load_system()
# Initialize chat history
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
# User input
user_input = st.text_input("πŸ’¬ Ask your question:")
# Process input
if user_input:
with st.spinner("πŸ€– Thinking..."):
try:
# Retrieve relevant docs
docs = retrieve_documents(user_input, db)
# Build context
context, sources = build_context(docs)
# Get memory
memory = get_memory()
# Generate answer
answer = generate_answer(user_input, context, sources, memory)
# Save memory
add_to_memory(user_input, answer)
# Save to UI history
st.session_state.chat_history.append(("You", user_input))
st.session_state.chat_history.append(("AI", answer))
except Exception as e:
st.error(f"❌ Error: {str(e)}")
# Display chat history
st.markdown("### πŸ’¬ Conversation")
for role, message in st.session_state.chat_history:
if role == "You":
st.markdown(f"**πŸ§‘ You:** {message}")
else:
st.markdown(f"**πŸ€– AI:** {message}")
# Footer (for portfolio)
st.markdown("---")
st.markdown("πŸš€ Built by Rahbarnisa | RAG + LLM + Pharma Support System")