SankethHonavar's picture
Optimize Streamlit for Hugging Face - Cached FAISS Loading
1be4c03
import streamlit as st
from time import sleep
from retriever import load_vector_store
from langgraph_graph import generate_answer
st.set_page_config("MedMCQA Chatbot", page_icon="🩺")
# πŸŒ— Sidebar
with st.sidebar:
st.title("🩺 MedMCQA Chatbot")
theme_mode = st.radio("πŸŒ“ Theme", ["Light", "Dark"], horizontal=True)
# πŸŒ“ Theme
if theme_mode == "Dark":
st.markdown("""
<style>
body, .stApp { background-color: #1e1e1e !important; color: #eee !important; }
.stTextInput input { background-color: #333 !important; color: #eee !important; }
input::placeholder { color: #bbb !important; }
.stButton>button { background-color: #444 !important; color: #eee !important; }
</style>
""", unsafe_allow_html=True)
else:
st.markdown("""
<style>
body, .stApp { background-color: #ffffff !important; color: #111 !important; }
.stTextInput input { background-color: #f0f0f0 !important; color: #111 !important; }
input::placeholder { color: #444 !important; }
.stButton>button { background-color: #e0e0e0 !important; color: #111 !important; }
</style>
""", unsafe_allow_html=True)
st.header("🩺 MedMCQA Chatbot")
st.caption("Ask a medical question and get answers from the MedMCQA dataset only. If not found, it will respond gracefully.")
# βœ… Cache FAISS loading to avoid blocking
@st.cache_resource(show_spinner="Loading FAISS Index... Please wait ⏳")
def get_vector_store():
return load_vector_store()
# Load vector store in the background
db = get_vector_store()
# ✏️ Query
query = st.text_input("πŸ” Enter your medical question:",
placeholder="e.g., What is the mechanism of Aspirin?")
# πŸš€ Answer generation
if query:
results = db.similarity_search(query, k=3)
context = "\n\n".join([doc.page_content for doc in results])
with st.spinner("🧠 Generating answer..."):
response = generate_answer(query, context)
# Animated answer
st.markdown("<h4>🧠 Answer:</h4>", unsafe_allow_html=True)
answer_placeholder = st.empty()
final_text = ""
for char in response:
final_text += char
answer_placeholder.markdown(final_text)
sleep(0.01)
with st.expander("πŸ”Ž Top Matches"):
for i, doc in enumerate(results, 1):
st.markdown(f"**Result {i}:**\n\n{doc.page_content}")
# πŸ“¬ Sidebar Contact
with st.sidebar:
st.markdown("---")
st.markdown("### πŸ“¬ Contact")
st.markdown("[πŸ“§ Email](mailto:[email protected])")
st.markdown("[πŸ”— LinkedIn](https://linkedin.com/in/sankethhonavar)")
st.markdown("[πŸ’» GitHub](https://github.com/SankethHonavar)")