File size: 2,427 Bytes
06f7b84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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")