Spaces:
Sleeping
Sleeping
modified app
Browse files
app.py
CHANGED
|
@@ -1,8 +1,11 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from groq import Groq
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 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 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
)
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|