fitness_chatbot / app.py
Dua Rajper
Update app.py
bf90edc verified
import os
import streamlit as st
from groq import Groq
from dotenv import load_dotenv
# Load API key from .env file
load_dotenv()
api_key = os.getenv("GROQ_API_KEY")
# Check if API key is set
if not api_key:
st.error("⚠️ Error: GROQ_API_KEY is missing. Please set it in the .env file.")
st.stop()
# Initialize the Groq client
client = Groq(api_key=api_key)
# Function to get chatbot response
def get_fitness_response(user_question):
# Define keywords related to fitness, weight loss, and health
fitness_keywords = [
"health", "fitness", "exercise", "diet", "nutrition", "weight loss", "workout", "calories",
"meal plan", "cardio", "strength training", "fat loss", "BMI", "hydration", "metabolism",
"protein intake", "muscle gain", "recovery", "supplements", "healthy habits", "intermittent fasting"
]
if any(keyword in user_question.lower() for keyword in fitness_keywords):
# Call Groq API to generate a response
completion = client.chat.completions.create(
model="deepseek-r1-distill-llama-70b",
messages=[{"role": "user", "content": user_question}],
temperature=0.6,
max_completion_tokens=512,
top_p=0.95,
stream=False,
)
return completion.choices[0].message['content']
else:
return "⚠️ This chatbot only answers fitness, health, and weight loss-related questions."
# Streamlit UI
st.set_page_config(page_title="Health & Fitness Chatbot", layout="centered")
st.title("💪 Health & Fitness Chatbot")
st.write("Ask me anything about fitness, weight loss, workouts, and nutrition!")
# User Input
user_input = st.text_input("Type your question:")
if user_input:
response = get_fitness_response(user_input)
st.write(response)