fitora-space / app.py
elloxli's picture
changed api
a3b6a00 verified
import os
import gradio as gr
from huggingface_hub import InferenceClient
import random
# --- Hugging Face API setup ---
HF_TOKEN = os.environ.get("HF_TOKEN") # Safely get from secrets
if not HF_TOKEN:
raise ValueError("❌ HF_TOKEN not found. Please set it in Hugging Face Space settings.")
client = InferenceClient("microsoft/phi-4", token=HF_TOKEN)
# --- Chatbot function ---
def respond(message, history):
messages = [
{
"role": "system",
"content": (
"You are Fitora, an upbeat, energetic personal fitness trainer AI. "
"You give motivational, supportive responses with specific workout tips, "
"nutrition advice, and healthy lifestyle encouragement. Always be positive "
"and help the user stay consistent in their fitness journey."
)
}
]
if history:
messages.extend(history)
messages.append({"role": "user", "content": message})
response = client.chat_completion(messages=messages, max_tokens=500)
return response['choices'][0]['message']['content'].strip()
# --- Personalized workout planner ---
def make_workout_plan(name, goal, style, days):
plan = f"""
🌸 **Hi {name}! Here’s Your Personalized Plan** 🌸
**Goal:** {goal}
**Style:** {style}
**Days per week:** {days}
**Plan:**
- Warm-up: 5 min light cardio
- Main workout: Mix of {style}-focused exercises ({days} days/week)
- Nutrition tip: Stay hydrated and eat balanced meals 🥗
- Motivation: Progress over perfection, always! ✨
"""
return plan
# --- Daily motivation generator ---
quotes = [
"💪 Push yourself, because no one else is going to do it for you.",
"🌸 Progress, not perfection.",
"🏋️‍♀️ The only bad workout is the one you didn’t do.",
"✨ Your body can stand almost anything. It’s your mind you have to convince."
]
def random_quote():
return random.choice(quotes)
# --- Build UI ---
with gr.Blocks(theme=gr.themes.Soft(primary_hue="pink", secondary_hue="rose")) as demo:
gr.Image(value="8e79ec27-2f8b-4ebf-a249-114a8c0dbc15.png", show_label=False, elem_id="logo")
gr.Markdown(
"""
## 🌸 **Fitora - Be Your Own Inspiration** 🌸
Welcome to your cute, colorful fitness buddy!
Let’s make workouts fun, personal, and motivating 💪✨
"""
)
with gr.Tab("💬 Chat with Fitora"):
gr.ChatInterface(respond, type="messages", title="Fitora - AI Fitness Coach")
with gr.Tab("📝 Personalized Workout Planner"):
name_input = gr.Textbox(label="Your Name", placeholder="e.g., Ella")
goal_input = gr.Textbox(label="Your Fitness Goal", placeholder="e.g., tone arms, lose weight")
style_input = gr.Dropdown(["Yoga", "HIIT", "Strength Training", "Pilates", "Mixed"], label="Preferred Workout Style")
days_slider = gr.Slider(1, 7, step=1, label="Days per Week")
plan_btn = gr.Button("Generate My Plan ✨")
plan_output = gr.Markdown()
plan_btn.click(make_workout_plan, [name_input, goal_input, style_input, days_slider], plan_output)
with gr.Tab("✨ Daily Motivation"):
quote_btn = gr.Button("Get Inspired! 🌟")
quote_output = gr.Markdown()
quote_btn.click(lambda: random_quote(), outputs=quote_output)
demo.launch()