File size: 4,022 Bytes
89d3c48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import os
import gradio as gr
from dotenv import load_dotenv
from google import genai

# Load environment variables
load_dotenv()
api_key = os.getenv("GEMINI_API_KEY")

# Initialize Gemini client
client = genai.Client(api_key=api_key)

# Function to calculate daily calorie requirements
def calculate_calorie_requirements(age, gender, weight, height, fitness_goal):
    if gender == "Male":
        bmr = 10 * weight + 6.25 * height - 5 * age + 5
    else:
        bmr = 10 * weight + 6.25 * height - 5 * age - 161

    if fitness_goal == "Weight Loss":
        return bmr * 1.2
    elif fitness_goal == "Weight Gain":
        return bmr * 1.5
    else:
        return bmr * 1.375

# Generate personalized plan
def generate_plan(name, age, gender, weight, height, fitness_goal, dietary_preference,
                  food_allergies, local_cuisine, month, include_ayurveda):
    bmi = round(weight / (height / 100) ** 2, 2)
    health_status = "Underweight" if bmi < 18.5 else "Normal weight" if bmi <= 24.9 else "Overweight"
    daily_calories = calculate_calorie_requirements(age, gender, weight, height, fitness_goal)

    metrics = {
        "name": name,
        "age": age,
        "gender": gender,
        "bmi": bmi,
        "health_status": health_status,
        "fitness_goal": fitness_goal,
        "dietary_preference": dietary_preference,
        "food_allergies": food_allergies,
        "daily_calories": int(daily_calories),
        "local_cuisine": local_cuisine,
        "month": month,
    }

    if include_ayurveda:
        prompt = f"""
        You are a health expert specializing in both modern medicine and Ayurveda.
        Generate a personalized weekly diet and exercise plan for {name}, a {age}-year-old {gender}
        with a BMI of {bmi} ({health_status}).
        Goal: {fitness_goal}. Daily Calorie Requirement: {int(daily_calories)} kcal.
        Dietary Preference: {dietary_preference}. Allergies: {food_allergies}.
        Local Cuisine: {local_cuisine}. Month: {month}.
        Include Ayurvedic insights and dosha-based recommendations.
        """
    else:
        prompt = f"""
        You are a health expert.
        Generate a personalized weekly diet and exercise plan for {name}, a {age}-year-old {gender}
        with a BMI of {bmi} ({health_status}).
        Goal: {fitness_goal}. Daily Calorie Requirement: {int(daily_calories)} kcal.
        Dietary Preference: {dietary_preference}. Allergies: {food_allergies}.
        Local Cuisine: {local_cuisine}. Month: {month}.
        """

    try:
        response = client.models.generate_content(
            model="gemini-2.5-flash",
            contents=prompt
        )
        return f"**Your BMI:** {bmi} ({health_status})\n\n" + response.text
    except Exception as e:
        return f"Error calling Gemini API: {e}"

# Gradio UI
iface = gr.Interface(
    fn=generate_plan,
    inputs=[
        gr.Textbox(label="Name"),
        gr.Number(label="Age", value=25),
        gr.Radio(["Male", "Female", "Other"], label="Gender"),
        gr.Number(label="Weight (kg)", value=70),
        gr.Number(label="Height (cm)", value=170),
        gr.Radio(["Weight Loss", "Weight Gain", "Maintenance"], label="Fitness Goal"),
        gr.Dropdown(["Vegetarian", "Vegan", "Keto", "Halal", "None"], label="Dietary Preference"),
        gr.Textbox(label="Food Allergies (if any)"),
        gr.Textbox(label="Preferred Local Cuisine"),
        gr.Dropdown(
            ["January", "February", "March", "April", "May", "June",
             "July", "August", "September", "October", "November", "December"],
            label="Month"
        ),
        gr.Checkbox(label="Include Ayurvedic insights", value=True),
    ],
    outputs=gr.Markdown(label="Personalized Health Plan"),
    title="AI-Based Personalized Weekly Diet and Exercise Planner (Gemini 2.5 Flash Pro)",
    description="Uses Google Gemini AI to generate a custom health and fitness plan integrating Ayurvedic insights."
)

if __name__ == "__main__":
    iface.launch()