Muzamilahmed's picture
Create app.py
b42b0b1 verified
import os
from groq import Groq
import streamlit as st
from dotenv import load_dotenv
# Load API key from .env file
load_dotenv()
api_key = os.getenv("GROQ_API_KEY")
# Initialize the Groq client
client = Groq(api_key=api_key)
# Define the health issues for the chatbot
health_issues = [
"fever", "malaria", "skin infection", "flu", "cough", "diabetes", "hypertension",
"cold", "asthma", "arthritis", "chickenpox", "measles", "pneumonia", "migraine",
"anxiety", "depression", "heart disease", "cancer", "high cholesterol", "stroke",
"obesity", "insomnia", "constipation", "acid reflux", "eczema", "psoriasis",
"allergy", "cold sores", "chronic fatigue", "UTI", "kidney disease", "hepatitis",
"tuberculosis", "stomach ulcers", "gout", "HIV/AIDS", "malnutrition", "blood pressure",
"menstrual irregularities", "pregnancy", "anemia", "vitamin deficiency", "chronic pain"
]
# Function to fetch chatbot completion from Groq API
def get_response(query):
completion = client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[{"role": "user", "content": query}],
temperature=0.7,
max_completion_tokens=1024,
top_p=1,
)
response = completion.choices[0].message.content
return response
def main():
st.title("Health Assistant Chatbot")
# Let the user choose a health topic or type a custom query
topic = st.selectbox("Choose a health issue", health_issues)
user_input = st.text_area("Or ask a health-related question:", "")
# If the user provides a query, we use that
query = user_input if user_input else f"Tell me about {topic}"
# Create a submit button
submit_button = st.button("Submit")
# Call the Groq API to get the response if the button is clicked
if submit_button and query:
response = get_response(query)
# Display the response
st.write("### Response:")
st.write(response)
# Handle unrelated queries
if user_input and not any(health_issue in user_input.lower() for health_issue in health_issues):
st.write("Sorry, I can only answer health-related questions.")
if __name__ == "__main__":
main()