Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -56,10 +56,7 @@ nutritional_data = {
|
|
| 56 |
}
|
| 57 |
}
|
| 58 |
|
| 59 |
-
def optimize_dishes_for_budget(city, daily_budget
|
| 60 |
-
# Calculate recommended daily protein intake
|
| 61 |
-
recommended_protein = 0.8 * weight
|
| 62 |
-
|
| 63 |
# Extracting cost, calories, and protein data for the selected city
|
| 64 |
costs = [nutritional_data[dish][city] for dish in nutritional_data]
|
| 65 |
calories = [nutritional_data[dish]["Energy (kcal)"] for dish in nutritional_data]
|
|
@@ -68,18 +65,15 @@ def optimize_dishes_for_budget(city, daily_budget, weight):
|
|
| 68 |
# Objective function: Maximize nutritional value (calories + protein)
|
| 69 |
c = [-1 * (cal + prot) for cal, prot in zip(calories, proteins)] # Minimize negative of nutrition for maximization
|
| 70 |
|
| 71 |
-
#
|
| 72 |
A_ub = [costs] # Sum of costs * portions <= daily_budget
|
| 73 |
b_ub = [daily_budget]
|
| 74 |
|
| 75 |
-
A_eq = [proteins] # Total protein must equal recommended intake
|
| 76 |
-
b_eq = [recommended_protein]
|
| 77 |
-
|
| 78 |
# Bounds for each dish: minimum 1 portion
|
| 79 |
bounds = [(1, None) for _ in costs] # Minimum 1 portion for each dish
|
| 80 |
|
| 81 |
# Solve the optimization problem
|
| 82 |
-
result = linprog(c, A_ub=A_ub, b_ub=b_ub,
|
| 83 |
|
| 84 |
if result.success:
|
| 85 |
selected_dishes = []
|
|
@@ -108,7 +102,7 @@ def optimize_dishes_for_budget(city, daily_budget, weight):
|
|
| 108 |
|
| 109 |
result_str += f"### Total Cost: ₹{total_cost:.2f}\n"
|
| 110 |
result_str += f"### Total Calories: {total_calories:.2f} kcal\n"
|
| 111 |
-
result_str += f"### Total Protein: {total_protein:.2f} g
|
| 112 |
|
| 113 |
return result_str
|
| 114 |
else:
|
|
@@ -137,9 +131,8 @@ def create_interface():
|
|
| 137 |
with gr.Blocks() as demo:
|
| 138 |
gr.Markdown("# Daily Budget Optimization for Best Nutrition")
|
| 139 |
|
| 140 |
-
# User inputs for city
|
| 141 |
city_selector = gr.Dropdown(choices=cities, label="Select City")
|
| 142 |
-
weight_input = gr.Number(label="Your Weight (kg)", value=70)
|
| 143 |
budget_input = gr.Number(label="Daily Budget (₹)", value=500)
|
| 144 |
|
| 145 |
show_all_dishes_button = gr.Button("Show All Available Dishes")
|
|
@@ -153,13 +146,13 @@ def create_interface():
|
|
| 153 |
return display_dishes_in_city(city)
|
| 154 |
|
| 155 |
# Function to handle optimization
|
| 156 |
-
def run_optimization(city, daily_budget
|
| 157 |
-
return optimize_dishes_for_budget(city, daily_budget
|
| 158 |
|
| 159 |
show_all_dishes_button.click(fn=show_all_dishes, inputs=[city_selector], outputs=all_dishes_output)
|
| 160 |
-
optimize_button.click(fn=run_optimization, inputs=[city_selector, budget_input
|
| 161 |
|
| 162 |
-
gr.Row([city_selector,
|
| 163 |
gr.Row(optimize_button)
|
| 164 |
gr.Row(optimization_output)
|
| 165 |
gr.Row(show_all_dishes_button)
|
|
|
|
| 56 |
}
|
| 57 |
}
|
| 58 |
|
| 59 |
+
def optimize_dishes_for_budget(city, daily_budget):
|
|
|
|
|
|
|
|
|
|
| 60 |
# Extracting cost, calories, and protein data for the selected city
|
| 61 |
costs = [nutritional_data[dish][city] for dish in nutritional_data]
|
| 62 |
calories = [nutritional_data[dish]["Energy (kcal)"] for dish in nutritional_data]
|
|
|
|
| 65 |
# Objective function: Maximize nutritional value (calories + protein)
|
| 66 |
c = [-1 * (cal + prot) for cal, prot in zip(calories, proteins)] # Minimize negative of nutrition for maximization
|
| 67 |
|
| 68 |
+
# Constraint: Total cost must not exceed the daily budget
|
| 69 |
A_ub = [costs] # Sum of costs * portions <= daily_budget
|
| 70 |
b_ub = [daily_budget]
|
| 71 |
|
|
|
|
|
|
|
|
|
|
| 72 |
# Bounds for each dish: minimum 1 portion
|
| 73 |
bounds = [(1, None) for _ in costs] # Minimum 1 portion for each dish
|
| 74 |
|
| 75 |
# Solve the optimization problem
|
| 76 |
+
result = linprog(c, A_ub=A_ub, b_ub=b_ub, bounds=bounds, method='highs')
|
| 77 |
|
| 78 |
if result.success:
|
| 79 |
selected_dishes = []
|
|
|
|
| 102 |
|
| 103 |
result_str += f"### Total Cost: ₹{total_cost:.2f}\n"
|
| 104 |
result_str += f"### Total Calories: {total_calories:.2f} kcal\n"
|
| 105 |
+
result_str += f"### Total Protein: {total_protein:.2f} g\n"
|
| 106 |
|
| 107 |
return result_str
|
| 108 |
else:
|
|
|
|
| 131 |
with gr.Blocks() as demo:
|
| 132 |
gr.Markdown("# Daily Budget Optimization for Best Nutrition")
|
| 133 |
|
| 134 |
+
# User inputs for city and daily budget
|
| 135 |
city_selector = gr.Dropdown(choices=cities, label="Select City")
|
|
|
|
| 136 |
budget_input = gr.Number(label="Daily Budget (₹)", value=500)
|
| 137 |
|
| 138 |
show_all_dishes_button = gr.Button("Show All Available Dishes")
|
|
|
|
| 146 |
return display_dishes_in_city(city)
|
| 147 |
|
| 148 |
# Function to handle optimization
|
| 149 |
+
def run_optimization(city, daily_budget):
|
| 150 |
+
return optimize_dishes_for_budget(city, daily_budget)
|
| 151 |
|
| 152 |
show_all_dishes_button.click(fn=show_all_dishes, inputs=[city_selector], outputs=all_dishes_output)
|
| 153 |
+
optimize_button.click(fn=run_optimization, inputs=[city_selector, budget_input], outputs=optimization_output)
|
| 154 |
|
| 155 |
+
gr.Row([city_selector, budget_input])
|
| 156 |
gr.Row(optimize_button)
|
| 157 |
gr.Row(optimization_output)
|
| 158 |
gr.Row(show_all_dishes_button)
|