Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import numpy as np | |
| from scipy.optimize import linprog | |
| # Data: Nutritional Information 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, "Cost": 40 | |
| }, | |
| "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, "Cost": 60 | |
| }, | |
| "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, "Cost": 150 | |
| }, | |
| "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, "Cost": 100 | |
| }, | |
| "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, "Cost": 20 | |
| }, | |
| "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, "Cost": 80 | |
| }, | |
| "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, "Cost": 70 | |
| }, | |
| "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, "Cost": 50 | |
| }, | |
| "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, "Cost": 40 | |
| }, | |
| "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, "Cost": 30 | |
| } | |
| } | |
| def optimize_nutrition(target_calories, target_protein, max_cost): | |
| """Optimizes the selection of dishes to meet nutritional goals.""" | |
| # Objective: Minimize the total deviation from nutritional goals | |
| # Constraints: Total cost <= max_cost, total nutritional values should try to meet or exceed targets | |
| dishes = list(nutritional_data.keys()) | |
| # Coefficients for the linear programming (negative values for maximization) | |
| c = [-nutritional_data[dish]['Energy (kcal)'] for dish in dishes] + \ | |
| [-nutritional_data[dish]['Protein (g)'] for dish in dishes] | |
| # Inequality constraints matrix | |
| A_ub = [ | |
| [nutritional_data[dish]['Energy (kcal)'] for dish in dishes] + \ | |
| [0 for _ in dishes], # Total calories | |
| [0 for _ in dishes] + [nutritional_data[dish]['Protein (g)'] for dish in dishes] # Total protein | |
| ] | |
| # Inequality constraints vector | |
| b_ub = [target_calories, target_protein] | |
| # Equality constraints (cost) | |
| A_eq = [[nutritional_data[dish]['Cost'] for dish in dishes]] | |
| b_eq = [max_cost] | |
| # Bounds for decision variables (dish quantities, we assume only integers are allowed) | |
| bounds = [(0, 1) for _ in range(len(dishes) * 2)] | |
| # Linear programming to optimize the nutritional intake | |
| result = linprog(c, A_ub=A_ub, b_ub=b_ub, A_eq=A_eq, b_eq=b_eq, bounds=bounds, method='simplex') | |
| if result.success: | |
| quantities = result.x[:len(dishes)] | |
| selected_dishes = {dishes[i]: quantities[i] for i in range(len(dishes)) if quantities[i] > 0.1} | |
| return f"Selected Dishes: {selected_dishes}" | |
| else: | |
| return "No optimal solution found with the given constraints." | |
| # Gradio Interface | |
| def create_interface(): | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Nutritional Optimization Tool") | |
| # Input widgets for nutritional targets | |
| target_calories = gr.Number(label="Target Calories", value=2000) | |
| target_protein = gr.Number(label="Target Protein (g)", value=50) | |
| max_cost = gr.Number(label="Maximum Cost (₹)", value=500) | |
| # Output widget | |
| optimization_output = gr.Textbox(label="Optimization Result") | |
| # Optimization function to run on button click | |
| def run_optimization(target_calories, target_protein, max_cost): | |
| return optimize_nutrition(target_calories, target_protein, max_cost) | |
| optimize_button = gr.Button("Optimize") | |
| optimize_button.click(fn=run_optimization, inputs=[target_calories, target_protein, max_cost], outputs=optimization_output) | |
| gr.Row(target_calories, target_protein, max_cost, optimize_button, optimization_output) | |
| return demo | |
| # Launch the interface | |
| demo = create_interface() | |
| demo.launch(share=True) | |