# 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(""" # # """, 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(""" #
#
#
# #
#
#
#
HealthSphere AI
#
Your Intelligent Medical Assistant
#
#
# """, unsafe_allow_html=True) # # ========================================================= # # CHAT AREA # # ========================================================= # chat_html = '
' # for msg in st.session_state.messages: # if msg["role"] == "user": # chat_html += f""" #
# {msg["content"]} #
# """ # else: # chat_html += f""" #
# {msg["content"]} #
# """ # chat_html += "
" # 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("
", 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(""" # # """, 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(""" #
#
# #
#
HealthSphere AI
#
Your Intelligent Medical Assistant
#
#
# """, 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("
", 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(""" # # """, unsafe_allow_html=True) # # ========================================================= # # HEADER # # ========================================================= # st.markdown( # "
🩺 HealthSphere AI
", # unsafe_allow_html=True # ) # st.markdown( # "
AI Powered Medical Assistant
", # 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(""" # # """, unsafe_allow_html=True) # # ========================================================= # # HEADER # # ========================================================= # st.markdown(""" #
# 🩺 HealthSphere AI #
#
# Your Intelligent Medical Assistant #
# """, 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""" #
# {message["content"]} #
# """, # unsafe_allow_html=True # ) # else: # st.markdown( # f""" #
# {message["content"]} #
# """, # 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""" #
# {user_input} #
# """, # 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""" #
# {answer} #
# """, # 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(""" """, unsafe_allow_html=True) # ========================================================= # HEADER # ========================================================= st.markdown("""
🩺 HealthSphere AI
πŸ€– Your Intelligent Medical Assistant
""", 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"""
πŸ‘¨β€πŸ’» {message["content"]}
""", unsafe_allow_html=True ) else: st.markdown( f"""
πŸ©ΊπŸ€– {message["content"]}
""", 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"""
πŸ‘¨β€πŸ’» {user_input}
""", 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"""
πŸ©ΊπŸ€– {answer}
""", unsafe_allow_html=True ) # SAVE CHAT st.session_state.messages.append({ "role": "assistant", "content": answer })