Dua Rajper
Create app.py
2dd08e5 verified
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)