import gradio as gr import numpy as np from scipy.optimize import linprog # Example data: Nutritional Information and Cost for the healthiest dishes nutritional_data = { "Idli with Vegetable Sambar": { "Energy (kcal)": 100, "Protein (g)": 5.5, "Fat (g)": 1.5, "Carbohydrate (g)": 17.0, "Fiber (g)": 3.0, "Calcium (mg)": 35, "Iron (mg)": 1.0, "Vitamin C (mg)": 8.0, "Chennai": 50, "Bengaluru": 55, "Hyderabad": 50, "New Delhi": 60 }, "Khichdi": { "Energy (kcal)": 120, "Protein (g)": 4.0, "Fat (g)": 2.5, "Carbohydrate (g)": 20.0, "Fiber (g)": 2.5, "Calcium (mg)": 40, "Iron (mg)": 1.0, "Vitamin C (mg)": 5.0, "Chennai": 60, "Bengaluru": 65, "Hyderabad": 60, "New Delhi": 70 }, "Tandoori Chicken": { "Energy (kcal)": 150, "Protein (g)": 18.0, "Fat (g)": 7.0, "Carbohydrate (g)": 3.0, "Fiber (g)": 0.5, "Calcium (mg)": 15, "Iron (mg)": 1.2, "Vitamin C (mg)": 1.5, "Chennai": 150, "Bengaluru": 160, "Hyderabad": 150, "New Delhi": 180 }, "Palak Paneer": { "Energy (kcal)": 140, "Protein (g)": 7.5, "Fat (g)": 10.0, "Carbohydrate (g)": 6.0, "Fiber (g)": 3.0, "Calcium (mg)": 200, "Iron (mg)": 3.0, "Vitamin C (mg)": 15.0, "Chennai": 100, "Bengaluru": 110, "Hyderabad": 100, "New Delhi": 120 }, "Raita": { "Energy (kcal)": 60, "Protein (g)": 3.5, "Fat (g)": 2.0, "Carbohydrate (g)": 6.5, "Fiber (g)": 0.5, "Calcium (mg)": 100, "Iron (mg)": 0.5, "Vitamin C (mg)": 2.0, "Chennai": 30, "Bengaluru": 35, "Hyderabad": 30, "New Delhi": 40 }, "Rajma": { "Energy (kcal)": 140, "Protein (g)": 7.5, "Fat (g)": 5.0, "Carbohydrate (g)": 20.0, "Fiber (g)": 6.0, "Calcium (mg)": 50, "Iron (mg)": 3.5, "Vitamin C (mg)": 4.0, "Chennai": 80, "Bengaluru": 90, "Hyderabad": 80, "New Delhi": 100 }, "Baingan Bharta": { "Energy (kcal)": 70, "Protein (g)": 2.5, "Fat (g)": 3.0, "Carbohydrate (g)": 10.0, "Fiber (g)": 4.0, "Calcium (mg)": 30, "Iron (mg)": 0.7, "Vitamin C (mg)": 6.0, "Chennai": 50, "Bengaluru": 55, "Hyderabad": 50, "New Delhi": 60 }, "Besan Chilla": { "Energy (kcal)": 180, "Protein (g)": 8.0, "Fat (g)": 7.0, "Carbohydrate (g)": 20.0, "Fiber (g)": 5.0, "Calcium (mg)": 30, "Iron (mg)": 2.5, "Vitamin C (mg)": 1.0, "Chennai": 60, "Bengaluru": 65, "Hyderabad": 60, "New Delhi": 70 }, "Masoor Dal": { "Energy (kcal)": 110, "Protein (g)": 7.5, "Fat (g)": 2.0, "Carbohydrate (g)": 16.0, "Fiber (g)": 5.5, "Calcium (mg)": 25, "Iron (mg)": 3.0, "Vitamin C (mg)": 4.0, "Chennai": 70, "Bengaluru": 75, "Hyderabad": 70, "New Delhi": 80 }, "Upma": { "Energy (kcal)": 150, "Protein (g)": 4.0, "Fat (g)": 6.0, "Carbohydrate (g)": 25.0, "Fiber (g)": 3.0, "Calcium (mg)": 30, "Iron (mg)": 1.0, "Vitamin C (mg)": 3.0, "Chennai": 40, "Bengaluru": 45, "Hyderabad": 40, "New Delhi": 50 } } def optimize_dishes_for_budget(city, daily_budget): # Extracting cost, calories, and protein data for the selected city costs = [nutritional_data[dish][city] for dish in nutritional_data] calories = [nutritional_data[dish]["Energy (kcal)"] for dish in nutritional_data] proteins = [nutritional_data[dish]["Protein (g)"] for dish in nutritional_data] # Objective function: Maximize nutritional value (calories + protein) c = [-1 * (cal + prot) for cal, prot in zip(calories, proteins)] # Minimize negative of nutrition for maximization # Constraint: Total cost must not exceed the daily budget A_ub = [costs] # Sum of costs * portions <= daily_budget b_ub = [daily_budget] # Bounds for each dish: minimum 1 portion bounds = [(1, None) for _ in costs] # Minimum 1 portion for each dish # Solve the optimization problem result = linprog(c, A_ub=A_ub, b_ub=b_ub, bounds=bounds, method='highs') if result.success: selected_dishes = [] for i, qty in enumerate(result.x): if qty >= 1: dish_name = list(nutritional_data.keys())[i] selected_dishes.append({ "dish": dish_name, "quantity": qty, "cost": nutritional_data[dish_name][city] * qty, "energy": nutritional_data[dish_name]["Energy (kcal)"] * qty, "protein": nutritional_data[dish_name]["Protein (g)"] * qty }) total_cost = sum(d['cost'] for d in selected_dishes) total_calories = sum(d['energy'] for d in selected_dishes) total_protein = sum(d['protein'] for d in selected_dishes) # Create the summary of the budget allocation result_str = f"### For ₹{daily_budget:.2f}, you can have:\n" for dish in selected_dishes: result_str += f"- **{dish['quantity']:.2f} portions of {dish['dish']}** at ₹{nutritional_data[dish['dish']][city]} per portion\n" result_str += f" - Total Cost: ₹{dish['cost']:.2f}\n" result_str += f" - Total Energy: {dish['energy']:.2f} kcal\n" result_str += f" - Total Protein: {dish['protein']:.2f} g\n\n" result_str += f"### Total Cost: ₹{total_cost:.2f}\n" result_str += f"### Total Calories: {total_calories:.2f} kcal\n" result_str += f"### Total Protein: {total_protein:.2f} g\n" return result_str else: return f"No feasible solution found for ₹{daily_budget:.2f} in {city}." def display_dishes_in_city(city): """Displays all dishes available in the selected city with their nutritional information and cost.""" result_str = f"### Available Dishes in {city} (Data pulled from leading food aggregators):\n" for dish, info in nutritional_data.items(): result_str += f"- **{dish}**\n" result_str += f" - Cost: ₹{info[city]}\n" result_str += f" - Energy: {info['Energy (kcal)']} kcal\n" result_str += f" - Protein: {info['Protein (g)']} g\n" result_str += f" - Fat: {info['Fat (g)']} g\n" result_str += f" - Carbohydrate: {info['Carbohydrate (g)']} g\n" result_str += f" - Fiber: {info['Fiber (g)']} g\n" result_str += f" - Calcium: {info['Calcium (mg)']} mg\n" result_str += f" - Iron: {info['Iron (mg)']} mg\n" result_str += f" - Vitamin C: {info['Vitamin C (mg)']} mg\n\n" return result_str # Gradio Interface def create_interface(): cities = ["Chennai", "Bengaluru", "Hyderabad", "New Delhi"] with gr.Blocks() as demo: gr.Markdown("# Daily Budget Optimization for Best Nutrition") # User inputs for city and daily budget city_selector = gr.Dropdown(choices=cities, label="Select City") budget_input = gr.Number(label="Daily Budget (₹)", value=500) show_all_dishes_button = gr.Button("Show All Available Dishes") optimize_button = gr.Button("Optimize Nutrition") all_dishes_output = gr.Markdown(label="All Available Dishes") optimization_output = gr.Markdown(label="Optimization Results") # Function to handle showing all dishes def show_all_dishes(city): return display_dishes_in_city(city) # Function to handle optimization def run_optimization(city, daily_budget): return optimize_dishes_for_budget(city, daily_budget) show_all_dishes_button.click(fn=show_all_dishes, inputs=[city_selector], outputs=all_dishes_output) optimize_button.click(fn=run_optimization, inputs=[city_selector, budget_input], outputs=optimization_output) gr.Row([city_selector, budget_input]) gr.Row(optimize_button) gr.Row(optimization_output) gr.Row(show_all_dishes_button) gr.Row(all_dishes_output) return demo # Launch the interface demo = create_interface() demo.launch(share=True)