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()