File size: 4,414 Bytes
641fab9
cc06094
641fab9
737ff67
cc59299
641fab9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ddacf64
641fab9
 
 
 
 
 
 
 
 
 
 
 
 
 
c63bcf0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
641fab9
c63bcf0
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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)