Arshad112's picture
Update app.py
2e68f66 verified
import os
from groq import Groq
import streamlit as st
from dotenv import load_dotenv
# Load API key from .env file
load_dotenv()
# Retrieve the API key from environment variables
api_key = os.getenv("GROQ_API_KEY")
# Ensure the API key is loaded correctly
if not api_key:
st.error("API key is missing. Please set the GROQ_API_KEY in the .env file.")
else:
# Initialize the Groq client
client = Groq(api_key=api_key)
# Define the health diet topics for the chatbot
diet_topics = [
"weight loss plan", "balanced diet", "high-protein diet", "low-carb diet",
"vegetarian diet", "keto diet", "intermittent fasting", "healthy meal plans",
"heart-healthy diet", "diabetes-friendly diet", "meal prepping", "nutrition for muscle gain",
"detox diet", "vitamin-rich foods", "hydration tips", "healthy snacks",
"immune-boosting foods", "foods for better digestion", "foods for glowing skin",
"healthy breakfasts", "meal plans for athletes", "diet for weight maintenance",
"mindful eating", "portion control", "anti-inflammatory foods", "meal planning for busy people"
]
# 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 Diet Plan Chatbot")
# Let the user choose a diet plan or type a custom query
topic = st.selectbox("Choose a diet plan topic", diet_topics)
user_input = st.text_area("Or ask a diet-related question:", "")
# If the user provides a query, we use that
query = user_input if user_input else f"Tell me about {topic} for a healthy body"
# 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(topic in user_input.lower() for topic in diet_topics):
st.write("Sorry, I can only answer diet-related questions.")
# Ensure the main function is called when the script is run
if __name__ == "__main__":
main()