Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,69 +2,64 @@ import gradio as gr
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
api_key
|
| 7 |
-
|
| 8 |
-
if not api_key:
|
| 9 |
-
raise ValueError("API key missing")
|
| 10 |
-
|
| 11 |
-
genai.configure(api_key=api_key)
|
| 12 |
|
|
|
|
| 13 |
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 14 |
|
| 15 |
def fitness_coach(age, weight, goal, activity):
|
| 16 |
try:
|
| 17 |
prompt = f"""
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
response = model.generate_content(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
return response.text
|
| 29 |
|
| 30 |
except Exception as e:
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
return f"""
|
| 35 |
-
πͺ Personalized Fitness Plan
|
| 36 |
-
|
| 37 |
-
π€ Age: {age}
|
| 38 |
-
β Weight: {weight} kg
|
| 39 |
-
π― Goal: {goal}
|
| 40 |
-
π Activity Level: {activity}
|
| 41 |
-
|
| 42 |
-
π Workout Plan:
|
| 43 |
-
- 3-5 days exercise per week
|
| 44 |
-
- Cardio + Strength training
|
| 45 |
-
- Beginner β start light
|
| 46 |
-
|
| 47 |
-
π₯ Diet Plan:
|
| 48 |
-
- High protein foods
|
| 49 |
-
- Reduce junk food
|
| 50 |
-
- Drink 2-3L water daily
|
| 51 |
-
|
| 52 |
-
π‘ Tips:
|
| 53 |
-
- Sleep 7-8 hours
|
| 54 |
-
- Stay consistent
|
| 55 |
-
- Track progress weekly
|
| 56 |
-
"""
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
|
|
|
|
| 61 |
interface = gr.Interface(
|
| 62 |
fn=fitness_coach,
|
| 63 |
inputs=[
|
| 64 |
gr.Number(label="Age"),
|
| 65 |
gr.Number(label="Weight (kg)"),
|
| 66 |
gr.Dropdown(["Weight Loss", "Muscle Gain", "Maintenance"], label="Goal"),
|
| 67 |
-
gr.Dropdown(["Beginner", "Intermediate", "Advanced"], label="Activity Level")
|
| 68 |
],
|
| 69 |
outputs="text",
|
| 70 |
title="πͺ Personalized Fitness Coach",
|
|
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Configure API key
|
| 6 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Use correct model
|
| 9 |
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 10 |
|
| 11 |
def fitness_coach(age, weight, goal, activity):
|
| 12 |
try:
|
| 13 |
prompt = f"""
|
| 14 |
+
You are a professional fitness coach.
|
| 15 |
+
|
| 16 |
+
Generate a UNIQUE and personalized fitness plan based on the user details.
|
| 17 |
+
|
| 18 |
+
User Details:
|
| 19 |
+
Age: {age}
|
| 20 |
+
Weight: {weight} kg
|
| 21 |
+
Goal: {goal}
|
| 22 |
+
Activity Level: {activity}
|
| 23 |
+
|
| 24 |
+
Instructions:
|
| 25 |
+
- If goal is Weight Loss β focus on fat loss workouts
|
| 26 |
+
- If goal is Muscle Gain β focus on strength training
|
| 27 |
+
- If goal is Maintenance β balanced plan
|
| 28 |
+
- If activity is Beginner β simple plan
|
| 29 |
+
- If Intermediate β moderate plan
|
| 30 |
+
- If Advanced β intense plan
|
| 31 |
+
|
| 32 |
+
Include:
|
| 33 |
+
1. Workout Plan
|
| 34 |
+
2. Diet Plan
|
| 35 |
+
3. Tips
|
| 36 |
+
|
| 37 |
+
Make sure the response changes based on inputs.
|
| 38 |
+
"""
|
| 39 |
|
| 40 |
+
response = model.generate_content(
|
| 41 |
+
prompt,
|
| 42 |
+
generation_config={
|
| 43 |
+
"temperature": 0.9,
|
| 44 |
+
"top_p": 1,
|
| 45 |
+
"top_k": 1,
|
| 46 |
+
"max_output_tokens": 500,
|
| 47 |
+
}
|
| 48 |
+
)
|
| 49 |
|
| 50 |
+
return response.text
|
|
|
|
| 51 |
|
| 52 |
except Exception as e:
|
| 53 |
+
return f"Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
# Gradio UI
|
| 56 |
interface = gr.Interface(
|
| 57 |
fn=fitness_coach,
|
| 58 |
inputs=[
|
| 59 |
gr.Number(label="Age"),
|
| 60 |
gr.Number(label="Weight (kg)"),
|
| 61 |
gr.Dropdown(["Weight Loss", "Muscle Gain", "Maintenance"], label="Goal"),
|
| 62 |
+
gr.Dropdown(["Beginner", "Intermediate", "Advanced"], label="Activity Level")
|
| 63 |
],
|
| 64 |
outputs="text",
|
| 65 |
title="πͺ Personalized Fitness Coach",
|