Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
from google import genai
|
| 5 |
+
|
| 6 |
+
# Load environment variables
|
| 7 |
+
load_dotenv()
|
| 8 |
+
api_key = os.getenv("GEMINI_API_KEY")
|
| 9 |
+
|
| 10 |
+
# Initialize Gemini client
|
| 11 |
+
client = genai.Client(api_key=api_key)
|
| 12 |
+
|
| 13 |
+
# Function to calculate daily calorie requirements
|
| 14 |
+
def calculate_calorie_requirements(age, gender, weight, height, fitness_goal):
|
| 15 |
+
if gender == "Male":
|
| 16 |
+
bmr = 10 * weight + 6.25 * height - 5 * age + 5
|
| 17 |
+
else:
|
| 18 |
+
bmr = 10 * weight + 6.25 * height - 5 * age - 161
|
| 19 |
+
|
| 20 |
+
if fitness_goal == "Weight Loss":
|
| 21 |
+
return bmr * 1.2
|
| 22 |
+
elif fitness_goal == "Weight Gain":
|
| 23 |
+
return bmr * 1.5
|
| 24 |
+
else:
|
| 25 |
+
return bmr * 1.375
|
| 26 |
+
|
| 27 |
+
# Generate personalized plan
|
| 28 |
+
def generate_plan(name, age, gender, weight, height, fitness_goal, dietary_preference,
|
| 29 |
+
food_allergies, local_cuisine, month, include_ayurveda):
|
| 30 |
+
bmi = round(weight / (height / 100) ** 2, 2)
|
| 31 |
+
health_status = "Underweight" if bmi < 18.5 else "Normal weight" if bmi <= 24.9 else "Overweight"
|
| 32 |
+
daily_calories = calculate_calorie_requirements(age, gender, weight, height, fitness_goal)
|
| 33 |
+
|
| 34 |
+
metrics = {
|
| 35 |
+
"name": name,
|
| 36 |
+
"age": age,
|
| 37 |
+
"gender": gender,
|
| 38 |
+
"bmi": bmi,
|
| 39 |
+
"health_status": health_status,
|
| 40 |
+
"fitness_goal": fitness_goal,
|
| 41 |
+
"dietary_preference": dietary_preference,
|
| 42 |
+
"food_allergies": food_allergies,
|
| 43 |
+
"daily_calories": int(daily_calories),
|
| 44 |
+
"local_cuisine": local_cuisine,
|
| 45 |
+
"month": month,
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
if include_ayurveda:
|
| 49 |
+
prompt = f"""
|
| 50 |
+
You are a health expert specializing in both modern medicine and Ayurveda.
|
| 51 |
+
Generate a personalized weekly diet and exercise plan for {name}, a {age}-year-old {gender}
|
| 52 |
+
with a BMI of {bmi} ({health_status}).
|
| 53 |
+
Goal: {fitness_goal}. Daily Calorie Requirement: {int(daily_calories)} kcal.
|
| 54 |
+
Dietary Preference: {dietary_preference}. Allergies: {food_allergies}.
|
| 55 |
+
Local Cuisine: {local_cuisine}. Month: {month}.
|
| 56 |
+
Include Ayurvedic insights and dosha-based recommendations.
|
| 57 |
+
"""
|
| 58 |
+
else:
|
| 59 |
+
prompt = f"""
|
| 60 |
+
You are a health expert.
|
| 61 |
+
Generate a personalized weekly diet and exercise plan for {name}, a {age}-year-old {gender}
|
| 62 |
+
with a BMI of {bmi} ({health_status}).
|
| 63 |
+
Goal: {fitness_goal}. Daily Calorie Requirement: {int(daily_calories)} kcal.
|
| 64 |
+
Dietary Preference: {dietary_preference}. Allergies: {food_allergies}.
|
| 65 |
+
Local Cuisine: {local_cuisine}. Month: {month}.
|
| 66 |
+
"""
|
| 67 |
+
|
| 68 |
+
try:
|
| 69 |
+
response = client.models.generate_content(
|
| 70 |
+
model="gemini-2.5-flash",
|
| 71 |
+
contents=prompt
|
| 72 |
+
)
|
| 73 |
+
return f"**Your BMI:** {bmi} ({health_status})\n\n" + response.text
|
| 74 |
+
except Exception as e:
|
| 75 |
+
return f"Error calling Gemini API: {e}"
|
| 76 |
+
|
| 77 |
+
# Gradio UI
|
| 78 |
+
iface = gr.Interface(
|
| 79 |
+
fn=generate_plan,
|
| 80 |
+
inputs=[
|
| 81 |
+
gr.Textbox(label="Name"),
|
| 82 |
+
gr.Number(label="Age", value=25),
|
| 83 |
+
gr.Radio(["Male", "Female", "Other"], label="Gender"),
|
| 84 |
+
gr.Number(label="Weight (kg)", value=70),
|
| 85 |
+
gr.Number(label="Height (cm)", value=170),
|
| 86 |
+
gr.Radio(["Weight Loss", "Weight Gain", "Maintenance"], label="Fitness Goal"),
|
| 87 |
+
gr.Dropdown(["Vegetarian", "Vegan", "Keto", "Halal", "None"], label="Dietary Preference"),
|
| 88 |
+
gr.Textbox(label="Food Allergies (if any)"),
|
| 89 |
+
gr.Textbox(label="Preferred Local Cuisine"),
|
| 90 |
+
gr.Dropdown(
|
| 91 |
+
["January", "February", "March", "April", "May", "June",
|
| 92 |
+
"July", "August", "September", "October", "November", "December"],
|
| 93 |
+
label="Month"
|
| 94 |
+
),
|
| 95 |
+
gr.Checkbox(label="Include Ayurvedic insights", value=True),
|
| 96 |
+
],
|
| 97 |
+
outputs=gr.Markdown(label="Personalized Health Plan"),
|
| 98 |
+
title="AI-Based Personalized Weekly Diet and Exercise Planner (Gemini 2.5 Flash Pro)",
|
| 99 |
+
description="Uses Google Gemini AI to generate a custom health and fitness plan integrating Ayurvedic insights."
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
if __name__ == "__main__":
|
| 103 |
+
iface.launch()
|