Spaces:
Sleeping
Sleeping
File size: 8,046 Bytes
86a293a 3192101 86a293a 077d4ce 86a293a 077d4ce 86a293a 077d4ce 86a293a 077d4ce 86a293a 077d4ce 86a293a 077d4ce 86a293a 077d4ce 86a293a 077d4ce 86a293a 077d4ce 86a293a 077d4ce 86a293a 3cd35eb 077d4ce 3192101 86a293a 4714c60 86a293a 3cd35eb 12f1609 86a293a 265222e 3192101 3cd35eb 86a293a 74412c7 265222e 74412c7 3192101 2f2e2ed 74412c7 3192101 74412c7 3192101 3cd35eb 3192101 86a293a 12f1609 86a293a 74412c7 86a293a 077d4ce 86a293a 12f1609 86a293a 3cd35eb 077d4ce 2f2e2ed 71879f6 b4993ec 12f1609 b4993ec 3192101 86a293a b4993ec 3192101 3cd35eb 86a293a b4993ec 3cd35eb 86a293a 3cd35eb 12f1609 71879f6 b4993ec 86a293a |
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
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)
|