ha7naa's picture
Update app.py
0036872 verified
import gradio as gr
from groq import Groq
import os
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
SYSTEM_PROMPT = """You are a professional exercise advisor and fitness coach.
Your goal is to help users improve their physical fitness safely.
Provide beginner-friendly workout advice, warm-up tips, and recovery guidance.
Avoid medical diagnosis and always encourage safe exercise habits."""
EXAMPLES = """
User: I am a beginner. How should I start exercising?
Assistant: Start with light exercises such as walking, stretching, and bodyweight movements.
User: How many days per week should I work out?
Assistant: Beginners should exercise 3–4 days per week.
User: What should I do before a workout?
Assistant: Always begin with a warm-up to prevent injuries.
"""
def respond(message, history, model, temperature, max_tokens):
messages = [{"role": "system", "content": SYSTEM_PROMPT + EXAMPLES}]
for h in history:
messages.append({"role": "user", "content": h[0]})
if h[1]:
messages.append({"role": "assistant", "content": h[1]})
messages.append({"role": "user", "content": message})
try:
response = client.chat.completions.create(
model=model,
messages=messages,
temperature=temperature,
max_completion_tokens=max_tokens,
)
return response.choices[0].message.content
except Exception as e:
return f"Error: {str(e)}"
# ChatInterface with additional inputs for parameters
demo = gr.ChatInterface(
fn=respond,
title="🏋️ Exercise Advisor AI",
description="Get safe and beginner-friendly workout advice",
additional_inputs=[
gr.Dropdown(
choices=[
"llama-3.3-70b-versatile",
"llama-3.1-8b-instant",
],
value="llama-3.3-70b-versatile",
label="Model",
),
gr.Slider(
minimum=0,
maximum=2,
value=0.7,
step=0.1,
label="Temperature",
),
gr.Slider(
minimum=256,
maximum=4096,
value=1024,
step=256,
label="Max Tokens",
),
],
examples=[
["I am a beginner. How should I start exercising?"],
["How many days per week should I work out?"],
["What is the best warm-up before training?"],
],
theme="soft",
)
if __name__ == "__main__":
demo.launch()