Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,192 +1,83 @@
|
|
| 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 |
-
|
| 84 |
-
#query = "What are GMP requirements for sterile manufacturing?"
|
| 85 |
-
|
| 86 |
-
#RAG pipeline
|
| 87 |
-
#retrieved_docs = retrieve_documents(query, db)
|
| 88 |
-
#context, sources = build_context(retrieved_docs)
|
| 89 |
-
#answer = generate_answer(query, context, sources)
|
| 90 |
-
|
| 91 |
-
#print("\n=== FINAL ANSWER ===\n")
|
| 92 |
-
#print(answer)
|
| 93 |
-
|
| 94 |
-
#App.py modified for memory
|
| 95 |
-
#from src.loader import load_all_pdfs
|
| 96 |
-
#from src.chunking import chunk_documents
|
| 97 |
-
#from src.vector_store import create_vector_store
|
| 98 |
-
#from src.rag_pipelines import retrieve_documents, build_context, generate_answer
|
| 99 |
-
#from src.memory import add_to_memory, get_memory
|
| 100 |
-
|
| 101 |
-
# Load system
|
| 102 |
-
#docs = load_all_pdfs()
|
| 103 |
-
#chunks = chunk_documents(docs)
|
| 104 |
-
#db = create_vector_store(chunks)
|
| 105 |
-
|
| 106 |
-
#print("AI Support Assistant Ready!\n")
|
| 107 |
-
|
| 108 |
-
#while True:
|
| 109 |
-
# query = input("You: ")
|
| 110 |
-
|
| 111 |
-
# if query.lower() == "exit":
|
| 112 |
-
# break
|
| 113 |
-
|
| 114 |
-
# Retrieve
|
| 115 |
-
#retrieved_docs = retrieve_documents(query, db)
|
| 116 |
-
|
| 117 |
-
# Build context
|
| 118 |
-
#context, sources = build_context(retrieved_docs)
|
| 119 |
-
|
| 120 |
-
# Get memory
|
| 121 |
-
#memory = get_memory()
|
| 122 |
-
|
| 123 |
-
# Generate answer
|
| 124 |
-
#answer = generate_answer(query, context, sources, memory)
|
| 125 |
-
|
| 126 |
-
#print("\nAI:", answer)
|
| 127 |
-
|
| 128 |
-
# Save memory
|
| 129 |
-
#add_to_memory(query, answer)
|
| 130 |
-
|
| 131 |
-
#STREAMLIT APP PY CODE
|
| 132 |
-
import streamlit as st
|
| 133 |
-
|
| 134 |
-
from src.loader import load_all_pdfs
|
| 135 |
-
from src.chunking import chunk_documents
|
| 136 |
-
from src.vector_store import create_vector_store
|
| 137 |
-
from src.rag_pipelines import retrieve_documents, build_context, generate_answer
|
| 138 |
-
from src.memory import add_to_memory, get_memory
|
| 139 |
-
|
| 140 |
-
st.set_page_config(page_title="AI Pharma Support", layout="wide")
|
| 141 |
-
|
| 142 |
-
st.title("π AI Pharmaceutical Support Assistant")
|
| 143 |
-
|
| 144 |
-
# Load system once (cache)
|
| 145 |
-
@st.cache_resource
|
| 146 |
-
def load_system():
|
| 147 |
-
docs = load_all_pdfs()
|
| 148 |
-
chunks = chunk_documents(docs)
|
| 149 |
-
db = create_vector_store(chunks)
|
| 150 |
-
return db
|
| 151 |
-
|
| 152 |
-
db = load_system()
|
| 153 |
-
|
| 154 |
-
# Session state for chat
|
| 155 |
-
if "chat_history" not in st.session_state:
|
| 156 |
-
st.session_state.chat_history = []
|
| 157 |
-
|
| 158 |
-
user_input = st.text_input("Ask your question:")
|
| 159 |
-
|
| 160 |
-
if user_input:
|
| 161 |
-
# Retrieve docs
|
| 162 |
-
docs = retrieve_documents(user_input, db)
|
| 163 |
-
|
| 164 |
-
# Build context
|
| 165 |
-
context, sources = build_context(docs)
|
| 166 |
-
|
| 167 |
-
# Get memory
|
| 168 |
-
memory = get_memory()
|
| 169 |
-
|
| 170 |
-
# Generate answer
|
| 171 |
-
answer = generate_answer(user_input, context, sources, memory)
|
| 172 |
-
|
| 173 |
-
# Save memory
|
| 174 |
-
add_to_memory(user_input, answer)
|
| 175 |
-
|
| 176 |
-
# Save to session UI
|
| 177 |
-
st.session_state.chat_history.append(("You", user_input))
|
| 178 |
-
st.session_state.chat_history.append(("AI", answer))
|
| 179 |
-
|
| 180 |
-
# Display chat
|
| 181 |
-
for role, message in st.session_state.chat_history:
|
| 182 |
-
if role == "You":
|
| 183 |
-
st.markdown(f"**π§ You:** {message}")
|
| 184 |
-
else:
|
| 185 |
-
st.markdown(f"**π€ AI:** {message}")
|
| 186 |
-
|
| 187 |
-
import streamlit as st
|
| 188 |
-
|
| 189 |
-
st.set_page_config(
|
| 190 |
-
page_title="AI Customer Support Assistant",
|
| 191 |
-
layout="centered"
|
| 192 |
-
)
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import sys
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Fix import issues in Hugging Face
|
| 6 |
+
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
| 7 |
+
|
| 8 |
+
# Page config (MUST be first Streamlit command)
|
| 9 |
+
st.set_page_config(
|
| 10 |
+
page_title="AI Pharmaceutical Support Assistant",
|
| 11 |
+
layout="wide"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
st.title("π AI Pharmaceutical Support Assistant")
|
| 15 |
+
st.markdown("Ask questions from pharmaceutical documents and get AI-powered answers with sources.")
|
| 16 |
+
|
| 17 |
+
# Debug (to confirm app loads)
|
| 18 |
+
st.write("β
App started")
|
| 19 |
+
|
| 20 |
+
# Imports AFTER Streamlit setup
|
| 21 |
+
from src.loader import load_all_pdfs
|
| 22 |
+
from src.chunking import chunk_documents
|
| 23 |
+
from src.vector_store import create_vector_store
|
| 24 |
+
from src.rag_pipelines import retrieve_documents, build_context, generate_answer
|
| 25 |
+
from src.memory import add_to_memory, get_memory
|
| 26 |
+
|
| 27 |
+
# Cache system (VERY IMPORTANT for performance)
|
| 28 |
+
@st.cache_resource
|
| 29 |
+
def load_system():
|
| 30 |
+
docs = load_all_pdfs()
|
| 31 |
+
chunks = chunk_documents(docs)
|
| 32 |
+
db = create_vector_store(chunks)
|
| 33 |
+
return db
|
| 34 |
+
|
| 35 |
+
# Show loading spinner (important for UX)
|
| 36 |
+
with st.spinner("π Loading AI system... please wait (first run may take 30-60 seconds)"):
|
| 37 |
+
db = load_system()
|
| 38 |
+
|
| 39 |
+
# Initialize chat history
|
| 40 |
+
if "chat_history" not in st.session_state:
|
| 41 |
+
st.session_state.chat_history = []
|
| 42 |
+
|
| 43 |
+
# User input
|
| 44 |
+
user_input = st.text_input("π¬ Ask your question:")
|
| 45 |
+
|
| 46 |
+
# Process input
|
| 47 |
+
if user_input:
|
| 48 |
+
with st.spinner("π€ Thinking..."):
|
| 49 |
+
try:
|
| 50 |
+
# Retrieve relevant docs
|
| 51 |
+
docs = retrieve_documents(user_input, db)
|
| 52 |
+
|
| 53 |
+
# Build context
|
| 54 |
+
context, sources = build_context(docs)
|
| 55 |
+
|
| 56 |
+
# Get memory
|
| 57 |
+
memory = get_memory()
|
| 58 |
+
|
| 59 |
+
# Generate answer
|
| 60 |
+
answer = generate_answer(user_input, context, sources, memory)
|
| 61 |
+
|
| 62 |
+
# Save memory
|
| 63 |
+
add_to_memory(user_input, answer)
|
| 64 |
+
|
| 65 |
+
# Save to UI history
|
| 66 |
+
st.session_state.chat_history.append(("You", user_input))
|
| 67 |
+
st.session_state.chat_history.append(("AI", answer))
|
| 68 |
+
|
| 69 |
+
except Exception as e:
|
| 70 |
+
st.error(f"β Error: {str(e)}")
|
| 71 |
+
|
| 72 |
+
# Display chat history
|
| 73 |
+
st.markdown("### π¬ Conversation")
|
| 74 |
+
|
| 75 |
+
for role, message in st.session_state.chat_history:
|
| 76 |
+
if role == "You":
|
| 77 |
+
st.markdown(f"**π§ You:** {message}")
|
| 78 |
+
else:
|
| 79 |
+
st.markdown(f"**π€ AI:** {message}")
|
| 80 |
+
|
| 81 |
+
# Footer (for portfolio)
|
| 82 |
+
st.markdown("---")
|
| 83 |
+
st.markdown("π Built by Rahbarnisa | RAG + LLM + Pharma Support System")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|