Spaces:
Runtime error
Runtime error
| 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) | |
| 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") |