Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from langchain_core.prompts import PromptTemplate | |
| from langchain_groq import ChatGroq | |
| from langchain_community.vectorstores import FAISS | |
| from langchain_community.embeddings import HuggingFaceEmbeddings | |
| import os | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| def initialize_groq_llm(): | |
| return ChatGroq( | |
| groq_api_key=os.getenv("GROQ_API_KEY"), | |
| model_name="llama-3.3-70b-versatile", | |
| max_tokens=512 | |
| ) | |
| embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2") | |
| faiss_index = FAISS.load_local( | |
| "medical_faiss_index", | |
| embedding_model, | |
| allow_dangerous_deserialization=True | |
| ) | |
| prompt_template = """ | |
| You are a healthcare professional built by Parthib Karak an AI engineering from Institute of Engineering and management,kolkata, and you can assist users with health-related issues. | |
| Use the following pieces of information along with the LLM's knowledge to answer the user's question about diseases or healthcare. | |
| If the following pieces provide some information, combine it with your existing knowledge to craft the most accurate and helpful response. | |
| Include relevant details such as home remedies, medications, and other necessary actions in a clear, point-wise manner for quick readability. | |
| If any other related questions arise, just say, "I am a healthcare professional.How may i assist you today?" | |
| If you don't know the answer, just say that you don't know. Don't try to make up an answer. | |
| Context: {context} | |
| Question: {question} | |
| Only return the helpful answer below and nothing else. | |
| Helpful answer: | |
| """ | |
| def generate_response(question): | |
| retriever = faiss_index.as_retriever(search_kwargs={'k': 1}) | |
| docs = retriever.invoke(question) | |
| context = "\n".join([doc.page_content for doc in docs]) | |
| llm = initialize_groq_llm() | |
| prompt = PromptTemplate( | |
| input_variables=["context", "question"], | |
| template=prompt_template | |
| ) | |
| formatted_prompt = prompt.format(context=context, question=question) | |
| response = llm.invoke(formatted_prompt) | |
| return response.content | |
| st.set_page_config(page_title="HealthCare ChatBot", page_icon="π€", layout="centered") | |
| st.markdown(""" | |
| <style> | |
| body { | |
| background: linear-gradient(135deg, #E3F2FD 0%, #F3E5F5 100%); | |
| font-family: 'Poppins', sans-serif; | |
| } | |
| .main-title { | |
| text-align: center; | |
| font-size: 38px; | |
| font-weight: 700; | |
| color: #2C3E50; | |
| margin-top: 10px; | |
| } | |
| .sub-title { | |
| text-align: center; | |
| font-size: 16px; | |
| color: #5D6D7E; | |
| margin-bottom: 30px; | |
| } | |
| .stTextInput > div > div > input { | |
| border: 2px solid #9C27B0; | |
| border-radius: 12px; | |
| padding: 10px; | |
| } | |
| .response-box { | |
| background-color: #FFFFFF; | |
| padding: 20px; | |
| border-radius: 15px; | |
| box-shadow: 0px 2px 10px rgba(0,0,0,0.1); | |
| color: #333333; | |
| line-height: 1.6; | |
| } | |
| .footer { | |
| text-align: center; | |
| font-size: 13px; | |
| color: #777; | |
| margin-top: 40px; | |
| } | |
| .stButton>button { | |
| background: linear-gradient(90deg, #6A1B9A, #8E24AA); | |
| color: white; | |
| border-radius: 10px; | |
| font-weight: 600; | |
| padding: 0.6rem 1.2rem; | |
| border: none; | |
| transition: 0.3s; | |
| } | |
| .stButton>button:hover { | |
| background: linear-gradient(90deg, #8E24AA, #AB47BC); | |
| transform: scale(1.05); | |
| } | |
| </style> | |
| """, unsafe_allow_html=True) | |
| st.markdown('<div class="main-title">π©Ί HealthCare ChatBot</div>', unsafe_allow_html=True) | |
| st.markdown('<div class="sub-title">An AI-powered health assistant</div>', unsafe_allow_html=True) | |
| user_input = st.text_input("π¬ Ask a healthcare-related question below:", "") | |
| col1, col2, col3 = st.columns([1, 2, 1]) | |
| with col2: | |
| generate_clicked = st.button("π Generate Response") | |
| if generate_clicked and user_input: | |
| with st.spinner('π Analyzing your question...'): | |
| response = generate_response(user_input.lower().strip()) | |
| st.markdown('<div class="response-box">', unsafe_allow_html=True) | |
| st.write(f"**Response:** {response}") | |
| st.markdown('</div>', unsafe_allow_html=True) | |