molehh commited on
Commit
8d53316
·
1 Parent(s): 6b6f731

modified app

Browse files
Files changed (1) hide show
  1. app.py +37 -26
app.py CHANGED
@@ -1,8 +1,11 @@
1
  import streamlit as st
2
  from groq import Groq
3
 
4
- # Set up the Groq API client (Replace with your valid API key)
5
- client = Groq(api_key="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") # Replace with a secure method for storing API keys
 
 
 
6
 
7
  # Streamlit UI Setup
8
  st.title("🥗 Diet and Nutrition Chatbot")
@@ -11,7 +14,14 @@ st.write("Welcome! I’m here to help you with diet tips, meal planning, and nut
11
  # Initialize chat history
12
  if "messages" not in st.session_state:
13
  st.session_state.messages = [
14
- {"role": "system", "content": "You are a helpful Diet and Nutrition Chatbot. Provide meal plans, healthy eating tips, information about food benefits, and encourage balanced diets. Tailor your suggestions to different dietary preferences when asked."}
 
 
 
 
 
 
 
15
  ]
16
 
17
  # Display chat history
@@ -24,29 +34,30 @@ for message in st.session_state.messages:
24
  user_input = st.chat_input("Ask me about healthy eating, diets, or meal planning...")
25
 
26
  if user_input:
27
- # Validate the input: check if the question is related to diet or nutrition
28
- keywords = ["diet", "nutrition", "meal", "food", "healthy", "calories", "protein", "vegetarian", "vegan", "keto", "weight"]
29
- if any(keyword in user_input.lower() for keyword in keywords):
30
- # Append user message
31
- st.session_state.messages.append({"role": "user", "content": user_input})
32
- with st.chat_message("user"):
33
- st.write(user_input)
34
 
 
35
  # Get AI Response
36
- try:
37
- response = client.chat.completions.create(
38
- model="llama-3.3-70b-versatile", # Adjust the model as per API availability
39
- messages=st.session_state.messages
 
 
 
 
 
 
 
40
  )
41
- bot_reply = response.choices[0].message.content
42
- except Exception as e:
43
- bot_reply = "Sorry, I'm having trouble processing your request right now. Please try again later."
44
-
45
- # Append AI Response
46
- st.session_state.messages.append({"role": "assistant", "content": bot_reply})
47
- with st.chat_message("assistant"):
48
- st.write(bot_reply)
49
- else:
50
- # Notify the user that their question is not related to the topic
51
- with st.chat_message("assistant"):
52
- st.write("❗ Please ask questions related to diet, nutrition, or healthy eating.")
 
1
  import streamlit as st
2
  from groq import Groq
3
 
4
+ # Hardcoded API Key (Replace with a valid key)
5
+ API_KEY = "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"
6
+
7
+ # Initialize Groq client
8
+ client = Groq(api_key=API_KEY)
9
 
10
  # Streamlit UI Setup
11
  st.title("🥗 Diet and Nutrition Chatbot")
 
14
  # Initialize chat history
15
  if "messages" not in st.session_state:
16
  st.session_state.messages = [
17
+ {
18
+ "role": "system",
19
+ "content": (
20
+ "You are a highly knowledgeable chatbot specializing in diet and nutrition. "
21
+ "Answer only questions related to nutrition, meal planning, food benefits, and healthy eating. "
22
+ "Do not respond to questions outside of diet and nutrition. If asked an unrelated question, politely ask the user to focus on diet and nutrition topics."
23
+ )
24
+ }
25
  ]
26
 
27
  # Display chat history
 
34
  user_input = st.chat_input("Ask me about healthy eating, diets, or meal planning...")
35
 
36
  if user_input:
37
+ # Append user message
38
+ st.session_state.messages.append({"role": "user", "content": user_input})
39
+ with st.chat_message("user"):
40
+ st.write(user_input)
 
 
 
41
 
42
+ try:
43
  # Get AI Response
44
+ response = client.chat.completions.create(
45
+ model="llama-3.3-70b-versatile", # Adjust the model as per API availability
46
+ messages=st.session_state.messages
47
+ )
48
+
49
+ bot_reply = response.choices[0].message.content
50
+
51
+ # Redirect if the AI response is off-topic
52
+ if "diet" not in bot_reply.lower() and "nutrition" not in bot_reply.lower():
53
+ bot_reply = (
54
+ "❗ I specialize in diet and nutrition. Please ask about meal plans, healthy eating, or food-related topics."
55
  )
56
+
57
+ except Exception as e:
58
+ bot_reply = "Sorry, I'm having trouble processing your request right now. Please try again later."
59
+
60
+ # Append AI Response
61
+ st.session_state.messages.append({"role": "assistant", "content": bot_reply})
62
+ with st.chat_message("assistant"):
63
+ st.write(bot_reply)