Spaces:
Sleeping
Sleeping
| 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() | |