Download app.py from shani67/bot: direct link, hf CLI and curl.
- Browser
- Download file 35 kB
-
https://huggingface.co/spaces/shani67/bot/resolve/main/app.py
- Command line
-
hf download hf://spaces/shani67/bot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/shani67/bot/resolve/main/app.py
35 kB
# import os
# import streamlit as st
# from dotenv import load_dotenv
# from langchain_pinecone import PineconeVectorStore
# from langchain_google_genai import ChatGoogleGenerativeAI
# from langchain.chains import create_retrieval_chain
# from langchain.chains.combine_documents import create_stuff_documents_chain
# from langchain_core.prompts import ChatPromptTemplate
# from src.helper import download_hugging_face_embeddings
# from src.prompt import system_prompt
# # =========================================================
# # PAGE CONFIG
# # =========================================================
# st.set_page_config(
# page_title="HealthSphere AI",
# page_icon="🩺",
# layout="centered"
# )
# # =========================================================
# # LOAD ENV VARIABLES
# # =========================================================
# load_dotenv()
# GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
# # =========================================================
# # CUSTOM CSS
# # =========================================================
# st.markdown("""
# <style>
# .stApp{
# background: linear-gradient(90deg,#243644 0%,#221f5b 100%);
# }
# .block-container{
# padding-top: 2rem;
# }
# .main-box{
# background: linear-gradient(90deg,#111926 0%,#17173d 100%);
# border-radius: 24px;
# padding: 20px;
# max-width: 900px;
# margin: auto;
# box-shadow: 0px 0px 25px rgba(0,0,0,0.4);
# }
# .header{
# display:flex;
# align-items:center;
# margin-bottom:20px;
# }
# .avatar-wrap{
# position:relative;
# margin-right:18px;
# }
# .avatar{
# width:85px;
# height:85px;
# border-radius:50%;
# }
# .dot{
# position:absolute;
# right:5px;
# bottom:5px;
# width:18px;
# height:18px;
# background:#58d84f;
# border-radius:50%;
# border:2px solid #111926;
# }
# .title{
# color:white;
# font-size:30px;
# font-weight:600;
# }
# .subtitle{
# color:white;
# font-size:19px;
# opacity:0.9;
# }
# .chat-area{
# height:500px;
# overflow-y:auto;
# padding:10px;
# }
# .user-msg{
# background:#2d5cff;
# color:white;
# padding:14px 18px;
# border-radius:16px;
# width:fit-content;
# max-width:75%;
# margin-left:auto;
# margin-bottom:15px;
# font-size:16px;
# }
# .bot-msg{
# background:#1b2535;
# color:white;
# padding:14px 18px;
# border-radius:16px;
# width:fit-content;
# max-width:75%;
# margin-bottom:15px;
# font-size:16px;
# }
# .stTextInput input{
# background:#060b18 !important;
# color:white !important;
# border-radius:12px !important;
# border:1px solid rgba(255,255,255,0.1) !important;
# padding:14px !important;
# }
# .stButton button{
# height:55px;
# width:65px;
# border-radius:12px;
# background:#0b1020;
# color:white;
# border:1px solid white;
# font-size:22px;
# }
# </style>
# """, unsafe_allow_html=True)
# # =========================================================
# # EMBEDDINGS
# # =========================================================
# embeddings = download_hugging_face_embeddings()
# # =========================================================
# # VECTOR STORE
# # =========================================================
# docsearch = PineconeVectorStore.from_existing_index(
# index_name="medical-chat-bot",
# embedding=embeddings
# )
# retriever = docsearch.as_retriever(
# search_type="similarity",
# search_kwargs={"k": 3}
# )
# # =========================================================
# # GEMINI
# # =========================================================
# llm = ChatGoogleGenerativeAI(
# model="gemini-2.5-flash",
# temperature=0.3,
# google_api_key=GOOGLE_API_KEY
# )
# # =========================================================
# # PROMPT
# # =========================================================
# prompt = ChatPromptTemplate.from_messages([
# ("system", system_prompt),
# ("human", "{input}")
# ])
# question_answer_chain = create_stuff_documents_chain(
# llm,
# prompt
# )
# rag_chain = create_retrieval_chain(
# retriever,
# question_answer_chain
# )
# # =========================================================
# # SESSION STATE
# # =========================================================
# if "messages" not in st.session_state:
# st.session_state.messages = []
# # =========================================================
# # HEADER
# # =========================================================
# st.markdown("""
# <div class="main-box">
# <div class="header">
# <div class="avatar-wrap">
# <img class="avatar"
# src="https://cdn-icons-png.flaticon.com/512/387/387561.png">
# <div class="dot"></div>
# </div>
# <div>
# <div class="title">HealthSphere AI</div>
# <div class="subtitle">Your Intelligent Medical Assistant</div>
# </div>
# </div>
# """, unsafe_allow_html=True)
# # =========================================================
# # CHAT AREA
# # =========================================================
# chat_html = '<div class="chat-area">'
# for msg in st.session_state.messages:
# if msg["role"] == "user":
# chat_html += f"""
# <div class="user-msg">
# {msg["content"]}
# </div>
# """
# else:
# chat_html += f"""
# <div class="bot-msg">
# {msg["content"]}
# </div>
# """
# chat_html += "</div>"
# st.markdown(chat_html, unsafe_allow_html=True)
# # =========================================================
# # INPUT
# # =========================================================
# col1, col2 = st.columns([8,1])
# with col1:
# user_input = st.text_input(
# "",
# placeholder="Ask your medical question...",
# label_visibility="collapsed"
# )
# with col2:
# send = st.button("➤")
# # =========================================================
# # CLOSE MAIN BOX
# # =========================================================
# st.markdown("</div>", unsafe_allow_html=True)
# # =========================================================
# # CHAT LOGIC
# # =========================================================
# if send and user_input:
# st.session_state.messages.append({
# "role":"user",
# "content":user_input
# })
# try:
# response = rag_chain.invoke({
# "input": user_input
# })
# answer = response["answer"]
# except Exception as e:
# answer = str(e)
# st.session_state.messages.append({
# "role":"assistant",
# "content":answer
# })
# st.rerun()
# import os
# import streamlit as st
# from dotenv import load_dotenv
# from langchain_pinecone import PineconeVectorStore
# from langchain_google_genai import ChatGoogleGenerativeAI
# from langchain.chains import create_retrieval_chain
# from langchain.chains.combine_documents import create_stuff_documents_chain
# from langchain_core.prompts import ChatPromptTemplate
# from src.helper import download_hugging_face_embeddings
# from src.prompt import system_prompt
# # =========================================================
# # PAGE CONFIG
# # =========================================================
# st.set_page_config(
# page_title="HealthSphere AI",
# page_icon="🩺",
# layout="centered"
# )
# # =========================================================
# # ENV
# # =========================================================
# load_dotenv()
# GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
# # =========================================================
# # CSS
# # =========================================================
# st.markdown("""
# <style>
# .stApp{
# background: linear-gradient(90deg,#243644 0%,#221f5b 100%);
# }
# .block-container{
# padding-top:2rem;
# }
# .main-box{
# background: linear-gradient(90deg,#111926 0%,#17173d 100%);
# border-radius:24px;
# padding:20px;
# max-width:900px;
# margin:auto;
# box-shadow:0px 0px 25px rgba(0,0,0,0.4);
# }
# .header{
# display:flex;
# align-items:center;
# margin-bottom:20px;
# }
# .avatar{
# width:85px;
# height:85px;
# border-radius:50%;
# margin-right:18px;
# }
# .title{
# color:white;
# font-size:32px;
# font-weight:600;
# }
# .subtitle{
# color:white;
# font-size:20px;
# opacity:0.9;
# }
# .stChatMessage{
# background:#1b2535;
# border-radius:16px;
# padding:10px;
# margin-bottom:10px;
# }
# [data-testid="stChatMessageContent"]{
# color:white;
# }
# .stTextInput input{
# background:#060b18 !important;
# color:white !important;
# border-radius:12px !important;
# }
# </style>
# """, unsafe_allow_html=True)
# # =========================================================
# # EMBEDDINGS
# # =========================================================
# embeddings = download_hugging_face_embeddings()
# # =========================================================
# # VECTOR STORE
# # =========================================================
# docsearch = PineconeVectorStore.from_existing_index(
# index_name="medical-chat-bot",
# embedding=embeddings
# )
# retriever = docsearch.as_retriever(
# search_type="similarity",
# search_kwargs={"k": 3}
# )
# # =========================================================
# # GEMINI
# # =========================================================
# llm = ChatGoogleGenerativeAI(
# model="gemini-2.5-flash",
# temperature=0.3,
# google_api_key=GOOGLE_API_KEY
# )
# # =========================================================
# # PROMPT
# # =========================================================
# prompt = ChatPromptTemplate.from_messages([
# ("system", system_prompt),
# ("human", "{input}")
# ])
# question_answer_chain = create_stuff_documents_chain(
# llm,
# prompt
# )
# rag_chain = create_retrieval_chain(
# retriever,
# question_answer_chain
# )
# # =========================================================
# # SESSION
# # =========================================================
# if "messages" not in st.session_state:
# st.session_state.messages = []
# # =========================================================
# # HEADER
# # =========================================================
# st.markdown("""
# <div class="main-box">
# <div class="header">
# <img class="avatar"
# src="https://cdn-icons-png.flaticon.com/512/387/387561.png">
# <div>
# <div class="title">HealthSphere AI</div>
# <div class="subtitle">Your Intelligent Medical Assistant</div>
# </div>
# </div>
# """, unsafe_allow_html=True)
# # =========================================================
# # CHAT DISPLAY
# # =========================================================
# for msg in st.session_state.messages:
# with st.chat_message(msg["role"]):
# st.write(msg["content"])
# # =========================================================
# # USER INPUT
# # =========================================================
# user_input = st.chat_input(
# "Ask your medical question..."
# )
# # =========================================================
# # CHAT LOGIC
# # =========================================================
# if user_input:
# st.session_state.messages.append({
# "role": "user",
# "content": user_input
# })
# with st.chat_message("user"):
# st.write(user_input)
# with st.chat_message("assistant"):
# with st.spinner("Thinking..."):
# try:
# response = rag_chain.invoke({
# "input": user_input
# })
# answer = response["answer"]
# except Exception as e:
# answer = str(e)
# st.write(answer)
# st.session_state.messages.append({
# "role": "assistant",
# "content": answer
# })
# # =========================================================
# # END CONTAINER
# # =========================================================
# st.markdown("</div>", unsafe_allow_html=True)
# import streamlit as st
# from langchain_pinecone import PineconeVectorStore
# from langchain_google_genai import ChatGoogleGenerativeAI
# from langchain.chains import create_retrieval_chain
# from langchain.chains.combine_documents import (
# create_stuff_documents_chain
# )
# from helper import load_embeddings
# from prompt import prompt
# from config import (
# GOOGLE_API_KEY,
# INDEX_NAME,
# GEMINI_MODEL
# )
# # =========================================================
# # PAGE CONFIG
# # =========================================================
# st.set_page_config(
# page_title="HealthSphere AI",
# page_icon="🩺",
# layout="centered"
# )
# # =========================================================
# # CUSTOM CSS
# # =========================================================
# st.markdown("""
# <style>
# .stApp{
# background: linear-gradient(
# 90deg,
# #243644 0%,
# #221f5b 100%
# );
# }
# .main-title{
# text-align:center;
# color:white;
# font-size:42px;
# font-weight:bold;
# }
# .sub-title{
# text-align:center;
# color:white;
# margin-bottom:30px;
# }
# [data-testid="stChatMessage"]{
# background-color:#1b2535;
# border-radius:15px;
# padding:10px;
# margin-bottom:10px;
# }
# [data-testid="stChatMessageContent"]{
# color:white;
# }
# </style>
# """, unsafe_allow_html=True)
# # =========================================================
# # HEADER
# # =========================================================
# st.markdown(
# "<div class='main-title'>🩺 HealthSphere AI</div>",
# unsafe_allow_html=True
# )
# st.markdown(
# "<div class='sub-title'>AI Powered Medical Assistant</div>",
# unsafe_allow_html=True
# )
# # =========================================================
# # LOAD EMBEDDINGS
# # =========================================================
# embeddings = load_embeddings()
# # =========================================================
# # VECTOR STORE
# # =========================================================
# @st.cache_resource
# def load_vectorstore():
# return PineconeVectorStore.from_existing_index(
# index_name=INDEX_NAME,
# embedding=embeddings
# )
# docsearch = load_vectorstore()
# retriever = docsearch.as_retriever(
# search_type="similarity",
# search_kwargs={"k": 3}
# )
# # =========================================================
# # GEMINI MODEL
# # =========================================================
# llm = ChatGoogleGenerativeAI(
# model=GEMINI_MODEL,
# temperature=0.3,
# google_api_key=GOOGLE_API_KEY
# )
# # =========================================================
# # RAG CHAIN
# # =========================================================
# question_answer_chain = create_stuff_documents_chain(
# llm,
# prompt
# )
# rag_chain = create_retrieval_chain(
# retriever,
# question_answer_chain
# )
# # =========================================================
# # SESSION STATE
# # =========================================================
# if "messages" not in st.session_state:
# st.session_state.messages = []
# # =========================================================
# # DISPLAY MESSAGES
# # =========================================================
# for message in st.session_state.messages:
# with st.chat_message(message["role"]):
# st.markdown(message["content"])
# # =========================================================
# # USER INPUT
# # =========================================================
# user_input = st.chat_input(
# "Ask your medical question..."
# )
# # =========================================================
# # CHATBOT RESPONSE
# # =========================================================
# if user_input:
# st.session_state.messages.append({
# "role": "user",
# "content": user_input
# })
# with st.chat_message("user"):
# st.markdown(user_input)
# with st.chat_message("assistant"):
# with st.spinner("Thinking..."):
# try:
# response = rag_chain.invoke({
# "input": user_input
# })
# answer = response["answer"]
# except Exception as e:
# answer = f"Error: {str(e)}"
# st.markdown(answer)
# st.session_state.messages.append({
# "role": "assistant",
# "content": answer
# })
# import streamlit as st
# from langchain_pinecone import PineconeVectorStore
# from langchain_google_genai import ChatGoogleGenerativeAI
# from langchain.chains import create_retrieval_chain
# from langchain.chains.combine_documents import (
# create_stuff_documents_chain
# )
# from helper import load_embeddings
# from prompt import prompt
# from config import (
# GOOGLE_API_KEY,
# INDEX_NAME,
# GEMINI_MODEL
# )
# # =========================================================
# # PAGE CONFIG
# # =========================================================
# st.set_page_config(
# page_title="HealthSphere AI",
# page_icon="🩺",
# layout="wide"
# )
# # =========================================================
# # CSS
# # =========================================================
# st.markdown("""
# <style>
# /* =========================================================
# MAIN BACKGROUND
# ========================================================= */
# .stApp{
# background:#0b1120;
# color:white;
# }
# /* Hide Streamlit Branding */
# header{
# visibility:hidden;
# }
# footer{
# visibility:hidden;
# }
# #MainMenu{
# visibility:hidden;
# }
# /* =========================================================
# MAIN CONTAINER
# ========================================================= */
# .block-container{
# padding-top:2rem;
# max-width:1100px;
# }
# /* =========================================================
# TITLE
# ========================================================= */
# .main-title{
# text-align:center;
# font-size:52px;
# font-weight:700;
# color:white;
# margin-bottom:5px;
# }
# .sub-title{
# text-align:center;
# font-size:18px;
# color:#94a3b8;
# margin-bottom:40px;
# }
# /* =========================================================
# REMOVE CHAT DEFAULT BACKGROUND
# ========================================================= */
# [data-testid="stChatMessage"]{
# background:transparent !important;
# border:none !important;
# padding:0px !important;
# }
# /* =========================================================
# USER MESSAGE
# ========================================================= */
# .user-message{
# background:#1e293b;
# color:white;
# padding:16px;
# border-radius:12px;
# margin-bottom:15px;
# width:fit-content;
# max-width:75%;
# margin-left:auto;
# border:1px solid #334155;
# font-size:16px;
# line-height:1.7;
# }
# /* =========================================================
# BOT MESSAGE
# ========================================================= */
# .bot-message{
# background:#2563eb;
# color:white;
# padding:16px;
# border-radius:12px;
# margin-bottom:15px;
# width:fit-content;
# max-width:75%;
# margin-right:auto;
# border:1px solid #3b82f6;
# font-size:16px;
# line-height:1.7;
# }
# /* =========================================================
# CHAT INPUT AREA
# ========================================================= */
# .stChatInputContainer{
# background:#0b1120;
# border-top:none;
# padding-top:12px;
# }
# /* Input */
# .stChatInputContainer textarea{
# background:#111827 !important;
# color:white !important;
# border:2px solid black !important;
# border-radius:14px !important;
# font-size:16px !important;
# padding:14px !important;
# }
# /* Placeholder */
# .stChatInputContainer textarea::placeholder{
# color:#94a3b8 !important;
# }
# /* =========================================================
# SEND BUTTON
# ========================================================= */
# .stChatInputContainer button{
# background:#10b981 !important;
# color:white !important;
# border:none !important;
# border-radius:12px !important;
# transition:0.3s ease;
# }
# /* Hover */
# .stChatInputContainer button:hover{
# background:#059669 !important;
# }
# /* =========================================================
# SPINNER
# ========================================================= */
# .stSpinner > div{
# color:white !important;
# }
# /* =========================================================
# SCROLLBAR
# ========================================================= */
# ::-webkit-scrollbar{
# width:8px;
# }
# ::-webkit-scrollbar-track{
# background:#111827;
# }
# ::-webkit-scrollbar-thumb{
# background:#475569;
# border-radius:10px;
# }
# </style>
# """, unsafe_allow_html=True)
# # =========================================================
# # HEADER
# # =========================================================
# st.markdown("""
# <div class="main-title">
# 🩺 HealthSphere AI
# </div>
# <div class="sub-title">
# Your Intelligent Medical Assistant
# </div>
# """, unsafe_allow_html=True)
# # =========================================================
# # LOAD EMBEDDINGS
# # =========================================================
# embeddings = load_embeddings()
# # =========================================================
# # VECTOR STORE
# # =========================================================
# @st.cache_resource
# def load_vectorstore():
# return PineconeVectorStore.from_existing_index(
# index_name=INDEX_NAME,
# embedding=embeddings
# )
# docsearch = load_vectorstore()
# retriever = docsearch.as_retriever(
# search_type="similarity",
# search_kwargs={"k": 3}
# )
# # =========================================================
# # LLM
# # =========================================================
# llm = ChatGoogleGenerativeAI(
# model=GEMINI_MODEL,
# temperature=0.3,
# google_api_key=GOOGLE_API_KEY
# )
# # =========================================================
# # RAG CHAIN
# # =========================================================
# question_answer_chain = create_stuff_documents_chain(
# llm,
# prompt
# )
# rag_chain = create_retrieval_chain(
# retriever,
# question_answer_chain
# )
# # =========================================================
# # SESSION STATE
# # =========================================================
# if "messages" not in st.session_state:
# st.session_state.messages = []
# # =========================================================
# # DISPLAY CHAT
# # =========================================================
# for message in st.session_state.messages:
# if message["role"] == "user":
# st.markdown(
# f"""
# <div class="user-message">
# {message["content"]}
# </div>
# """,
# unsafe_allow_html=True
# )
# else:
# st.markdown(
# f"""
# <div class="bot-message">
# {message["content"]}
# </div>
# """,
# unsafe_allow_html=True
# )
# # =========================================================
# # USER INPUT
# # =========================================================
# user_input = st.chat_input(
# "Ask your medical question..."
# )
# # =========================================================
# # CHAT LOGIC
# # =========================================================
# if user_input:
# # USER MESSAGE
# st.session_state.messages.append({
# "role": "user",
# "content": user_input
# })
# st.markdown(
# f"""
# <div class="user-message">
# {user_input}
# </div>
# """,
# unsafe_allow_html=True
# )
# # BOT RESPONSE
# with st.spinner(
# "Analyzing medical information..."
# ):
# try:
# response = rag_chain.invoke({
# "input": user_input
# })
# answer = response["answer"]
# except Exception as e:
# answer = f"""
# Error:
# {str(e)}
# """
# st.markdown(
# f"""
# <div class="bot-message">
# {answer}
# </div>
# """,
# unsafe_allow_html=True
# )
# # SAVE RESPONSE
# st.session_state.messages.append({
# "role": "assistant",
# "content": answer
# })
import streamlit as st
from langchain_pinecone import PineconeVectorStore
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain.chains import create_retrieval_chain
from langchain.chains.combine_documents import (
create_stuff_documents_chain
)
from helper import load_embeddings
from prompt import prompt
from config import (
GOOGLE_API_KEY,
INDEX_NAME,
GEMINI_MODEL
)
# =========================================================
# PAGE CONFIG
# =========================================================
st.set_page_config(
page_title="HealthSphere AI",
page_icon="🩺",
layout="wide"
)
# =========================================================
# CUSTOM CSS
# =========================================================
st.markdown("""
<style>
/* =========================================================
MAIN APP
========================================================= */
.stApp{
background:#0b1120;
color:white;
}
/* Hide Streamlit Header/Footer */
header{
visibility:hidden;
}
footer{
visibility:hidden;
}
#MainMenu{
visibility:hidden;
}
/* =========================================================
MAIN CONTAINER
========================================================= */
.block-container{
max-width:1100px;
padding-top:2rem;
}
/* =========================================================
TITLE
========================================================= */
.main-title{
text-align:center;
font-size:52px;
font-weight:700;
color:white;
margin-bottom:5px;
}
.sub-title{
text-align:center;
font-size:18px;
color:#94a3b8;
margin-bottom:35px;
}
/* =========================================================
REMOVE DEFAULT CHAT BACKGROUND
========================================================= */
[data-testid="stChatMessage"]{
background:transparent !important;
border:none !important;
padding:0px !important;
}
/* =========================================================
USER MESSAGE
========================================================= */
.user-container{
display:flex;
justify-content:flex-end;
margin-bottom:16px;
}
.user-message{
background:#1e293b;
color:white;
padding:15px 18px;
border-radius:14px;
max-width:75%;
border:1px solid #334155;
font-size:16px;
line-height:1.7;
}
/* =========================================================
BOT MESSAGE
========================================================= */
.bot-container{
display:flex;
justify-content:flex-start;
margin-bottom:16px;
}
.bot-message{
background:#2563eb;
color:white;
padding:15px 18px;
border-radius:14px;
max-width:75%;
border:1px solid #3b82f6;
font-size:16px;
line-height:1.7;
}
/* =========================================================
CHAT INPUT
========================================================= */
.stChatInputContainer{
background:#0b1120;
border-top:none;
padding-top:12px;
}
/* Input Box */
.stChatInputContainer textarea{
background:#111827 !important;
color:white !important;
border:2px solid black !important;
border-radius:14px !important;
padding:14px !important;
font-size:16px !important;
}
/* Placeholder */
.stChatInputContainer textarea::placeholder{
color:#94a3b8 !important;
}
/* =========================================================
SEND BUTTON
========================================================= */
.stChatInputContainer button{
background:#10b981 !important;
color:white !important;
border:none !important;
border-radius:12px !important;
transition:0.3s ease;
}
/* Hover */
.stChatInputContainer button:hover{
background:#059669 !important;
}
/* =========================================================
SCROLLBAR
========================================================= */
::-webkit-scrollbar{
width:8px;
}
::-webkit-scrollbar-track{
background:#111827;
}
::-webkit-scrollbar-thumb{
background:#475569;
border-radius:10px;
}
/* =========================================================
SPINNER
========================================================= */
.stSpinner > div{
color:white !important;
}
</style>
""", unsafe_allow_html=True)
# =========================================================
# HEADER
# =========================================================
st.markdown("""
<div class="main-title">
🩺 HealthSphere AI
</div>
<div class="sub-title">
🤖 Your Intelligent Medical Assistant
</div>
""", unsafe_allow_html=True)
# =========================================================
# LOAD EMBEDDINGS
# =========================================================
embeddings = load_embeddings()
# =========================================================
# VECTOR STORE
# =========================================================
@st.cache_resource
def load_vectorstore():
return PineconeVectorStore.from_existing_index(
index_name=INDEX_NAME,
embedding=embeddings
)
docsearch = load_vectorstore()
retriever = docsearch.as_retriever(
search_type="similarity",
search_kwargs={"k": 3}
)
# =========================================================
# GEMINI MODEL
# =========================================================
llm = ChatGoogleGenerativeAI(
model=GEMINI_MODEL,
temperature=0.3,
google_api_key=GOOGLE_API_KEY
)
# =========================================================
# RAG CHAIN
# =========================================================
question_answer_chain = create_stuff_documents_chain(
llm,
prompt
)
rag_chain = create_retrieval_chain(
retriever,
question_answer_chain
)
# =========================================================
# SESSION STATE
# =========================================================
if "messages" not in st.session_state:
st.session_state.messages = []
# =========================================================
# DISPLAY CHAT HISTORY
# =========================================================
for message in st.session_state.messages:
if message["role"] == "user":
st.markdown(
f"""
<div class="user-container">
<div class="user-message">
👨💻 {message["content"]}
</div>
</div>
""",
unsafe_allow_html=True
)
else:
st.markdown(
f"""
<div class="bot-container">
<div class="bot-message">
🩺🤖 {message["content"]}
</div>
</div>
""",
unsafe_allow_html=True
)
# =========================================================
# USER INPUT
# =========================================================
user_input = st.chat_input(
"💬 Ask your medical question..."
)
# =========================================================
# CHATBOT RESPONSE
# =========================================================
if user_input:
# USER MESSAGE
st.session_state.messages.append({
"role": "user",
"content": user_input
})
st.markdown(
f"""
<div class="user-container">
<div class="user-message">
👨💻 {user_input}
</div>
</div>
""",
unsafe_allow_html=True
)
# BOT RESPONSE
with st.spinner(
"🧠 Analyzing medical information..."
):
try:
response = rag_chain.invoke({
"input": user_input
})
answer = response["answer"]
except Exception as e:
answer = f"""
❌ Error:
{str(e)}
"""
st.markdown(
f"""
<div class="bot-container">
<div class="bot-message">
🩺🤖 {answer}
</div>
</div>
""",
unsafe_allow_html=True
)
# SAVE CHAT
st.session_state.messages.append({
"role": "assistant",
"content": answer
})
# import os
# import streamlit as st
# from dotenv import load_dotenv
# from langchain_pinecone import PineconeVectorStore
# from langchain_google_genai import ChatGoogleGenerativeAI
# from langchain.chains import create_retrieval_chain
# from langchain.chains.combine_documents import create_stuff_documents_chain
# from langchain_core.prompts import ChatPromptTemplate
# from src.helper import download_hugging_face_embeddings
# from src.prompt import system_prompt
# # =========================================================
# # PAGE CONFIG
# # =========================================================
# st.set_page_config(
# page_title="HealthSphere AI",
# page_icon="🩺",
# layout="centered"
# )
# # =========================================================
# # LOAD ENV VARIABLES
# # =========================================================
# load_dotenv()
# GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
# # =========================================================
# # CUSTOM CSS
# # =========================================================
# st.markdown("""
# <style>
# .stApp{
# background: linear-gradient(90deg,#243644 0%,#221f5b 100%);
# }
# .block-container{
# padding-top: 2rem;
# }
# .main-box{
# background: linear-gradient(90deg,#111926 0%,#17173d 100%);
# border-radius: 24px;
# padding: 20px;
# max-width: 900px;
# margin: auto;
# box-shadow: 0px 0px 25px rgba(0,0,0,0.4);
# }
# .header{
# display:flex;
# align-items:center;
# margin-bottom:20px;
# }
# .avatar-wrap{
# position:relative;
# margin-right:18px;
# }
# .avatar{
# width:85px;
# height:85px;
# border-radius:50%;
# }
# .dot{
# position:absolute;
# right:5px;
# bottom:5px;
# width:18px;
# height:18px;
# background:#58d84f;
# border-radius:50%;
# border:2px solid #111926;
# }
# .title{
# color:white;
# font-size:30px;
# font-weight:600;
# }
# .subtitle{
# color:white;
# font-size:19px;
# opacity:0.9;
# }