Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,59 +5,66 @@ from dotenv import load_dotenv
|
|
| 5 |
|
| 6 |
# Load API key from .env file
|
| 7 |
load_dotenv()
|
|
|
|
|
|
|
| 8 |
api_key = os.getenv("GROQ_API_KEY")
|
| 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 |
-
|
| 60 |
-
|
| 61 |
-
#
|
| 62 |
-
if
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Load API key from .env file
|
| 7 |
load_dotenv()
|
| 8 |
+
|
| 9 |
+
# Retrieve the API key from environment variables
|
| 10 |
api_key = os.getenv("GROQ_API_KEY")
|
| 11 |
|
| 12 |
+
# Ensure the API key is loaded correctly
|
| 13 |
+
if not api_key:
|
| 14 |
+
st.error("API key is missing. Please set the GROQ_API_KEY in the .env file.")
|
| 15 |
+
else:
|
| 16 |
+
# Initialize the Groq client
|
| 17 |
+
client = Groq(api_key=api_key)
|
| 18 |
+
|
| 19 |
+
# Define the health diet topics for the chatbot
|
| 20 |
+
diet_topics = [
|
| 21 |
+
"weight loss plan", "balanced diet", "high-protein diet", "low-carb diet",
|
| 22 |
+
"vegetarian diet", "keto diet", "intermittent fasting", "healthy meal plans",
|
| 23 |
+
"heart-healthy diet", "diabetes-friendly diet", "meal prepping", "nutrition for muscle gain",
|
| 24 |
+
"detox diet", "vitamin-rich foods", "hydration tips", "healthy snacks",
|
| 25 |
+
"immune-boosting foods", "foods for better digestion", "foods for glowing skin",
|
| 26 |
+
"healthy breakfasts", "meal plans for athletes", "diet for weight maintenance",
|
| 27 |
+
"mindful eating", "portion control", "anti-inflammatory foods", "meal planning for busy people"
|
| 28 |
+
]
|
| 29 |
+
|
| 30 |
+
# Function to fetch chatbot completion from Groq API
|
| 31 |
+
def get_response(query):
|
| 32 |
+
completion = client.chat.completions.create(
|
| 33 |
+
model="llama-3.3-70b-versatile",
|
| 34 |
+
messages=[{"role": "user", "content": query}],
|
| 35 |
+
temperature=0.7,
|
| 36 |
+
max_completion_tokens=1024,
|
| 37 |
+
top_p=1,
|
| 38 |
+
)
|
| 39 |
+
response = completion.choices[0].message.content
|
| 40 |
+
return response
|
| 41 |
+
|
| 42 |
+
def main():
|
| 43 |
+
st.title("Health Diet Plan Chatbot")
|
| 44 |
+
|
| 45 |
+
# Let the user choose a diet plan or type a custom query
|
| 46 |
+
topic = st.selectbox("Choose a diet plan topic", diet_topics)
|
| 47 |
+
user_input = st.text_area("Or ask a diet-related question:", "")
|
| 48 |
+
|
| 49 |
+
# If the user provides a query, we use that
|
| 50 |
+
query = user_input if user_input else f"Tell me about {topic} for a healthy body"
|
| 51 |
+
|
| 52 |
+
# Create a submit button
|
| 53 |
+
submit_button = st.button("Submit")
|
| 54 |
+
|
| 55 |
+
# Call the Groq API to get the response if the button is clicked
|
| 56 |
+
if submit_button and query:
|
| 57 |
+
response = get_response(query)
|
| 58 |
+
|
| 59 |
+
# Display the response
|
| 60 |
+
st.write("### Response:")
|
| 61 |
+
st.write(response)
|
| 62 |
+
|
| 63 |
+
# Handle unrelated queries
|
| 64 |
+
if user_input and not any(topic in user_input.lower() for topic in diet_topics):
|
| 65 |
+
st.write("Sorry, I can only answer diet-related questions.")
|
| 66 |
+
|
| 67 |
+
# Ensure the main function is called when the script is run
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
main()
|
| 70 |
+
|