Spaces:
Sleeping
Sleeping
Dua Rajper commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from groq import Groq
|
| 4 |
+
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 |
+
# Check if API key is set
|
| 11 |
+
if not api_key:
|
| 12 |
+
st.error("⚠️ Error: GROQ_API_KEY is missing. Please set it in the .env file.")
|
| 13 |
+
st.stop()
|
| 14 |
+
|
| 15 |
+
# Initialize the Groq client
|
| 16 |
+
client = Groq(api_key=api_key)
|
| 17 |
+
|
| 18 |
+
# Function to get chatbot response
|
| 19 |
+
def get_health_response(user_question):
|
| 20 |
+
# Check if the question is related to health, fitness, or diet
|
| 21 |
+
health_keywords = ["health", "fitness", "exercise", "diet", "nutrition", "weight loss", "workout", "calories", "meal plan"]
|
| 22 |
+
|
| 23 |
+
if any(keyword in user_question.lower() for keyword in health_keywords):
|
| 24 |
+
# Call Groq API to generate a response
|
| 25 |
+
completion = client.chat.completions.create(
|
| 26 |
+
model="deepseek-r1-distill-llama-70b",
|
| 27 |
+
messages=[{"role": "user", "content": user_question}],
|
| 28 |
+
temperature=0.6,
|
| 29 |
+
max_completion_tokens=512,
|
| 30 |
+
top_p=0.95,
|
| 31 |
+
stream=False,
|
| 32 |
+
)
|
| 33 |
+
return completion.choices[0].message['content']
|
| 34 |
+
else:
|
| 35 |
+
return "⚠️ This chatbot only answers health, fitness, and diet-related questions."
|
| 36 |
+
|
| 37 |
+
# Streamlit UI
|
| 38 |
+
st.set_page_config(page_title="Health & Fitness Chatbot", layout="centered")
|
| 39 |
+
st.title("💪 Health & Fitness Chatbot")
|
| 40 |
+
st.write("Ask me about health, fitness, weight loss, and diet plans!")
|
| 41 |
+
|
| 42 |
+
# User Input
|
| 43 |
+
user_input = st.text_input("Type your question:")
|
| 44 |
+
|
| 45 |
+
if user_input:
|
| 46 |
+
response = get_health_response(user_input)
|
| 47 |
+
st.write(response)
|