Arshad112 commited on
Commit
2e68f66
·
verified ·
1 Parent(s): eff5350

Update app.py

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