File size: 2,167 Bytes
2dd08e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import streamlit as st
from groq import Groq
from dotenv import load_dotenv

# Load environment variables
load_dotenv()
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
if not GROQ_API_KEY:
    st.error("GROQ API Key not found. Please set the API key in a .env file and restart the app.")
    st.stop()

# Initialize Groq client
client = Groq(api_key=GROQ_API_KEY)

# Define allowed health topics
topic_responses = {
    "fever": "For fever, take paracetamol and stay hydrated. If symptoms persist, consult a doctor.",
    "malaria": "For malaria, take prescribed antimalarial drugs and rest. Visit a doctor for proper diagnosis.",
    "flu": "For flu, take antihistamines, drink warm fluids, and rest. Consider over-the-counter pain relievers.",
    "cough": "For cough, drink warm honey tea, use cough syrup, and avoid cold drinks. If severe, see a doctor.",
    "typhoid": "For typhoid, take antibiotics as prescribed by a doctor and maintain good hydration. Avoid contaminated food."
}

def get_health_advice(user_input):
    user_input = user_input.lower()
    for topic, response in topic_responses.items():
        if topic in user_input:
            return response
    return "Sorry, I don't have knowledge about that. Ask me only health-related matters."

# Streamlit UI setup
st.title("Health Assistance Chatbot")
st.write("Ask about Fever, Malaria, Flu, Cough, or Typhoid for symptoms and treatment suggestions.")

# Store chat history
if "messages" not in st.session_state:
    st.session_state.messages = []

# Display chat history
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

# User input
user_input = st.chat_input("Type your question here...")
if user_input:
    st.session_state.messages.append({"role": "user", "content": user_input})
    
    # Check if input is health-related
    bot_response = get_health_advice(user_input)
    
    # Append bot response to chat history
    st.session_state.messages.append({"role": "assistant", "content": bot_response})
    
    # Display bot response
    with st.chat_message("assistant"):
        st.markdown(bot_response)