dindizz commited on
Commit
12f1609
·
verified ·
1 Parent(s): fee6ea2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -60
app.py CHANGED
@@ -56,38 +56,18 @@ nutritional_data = {
56
  }
57
  }
58
 
59
- def display_dishes_in_city(city):
60
- """Displays all dishes available in the selected city with their nutritional information and cost."""
61
- result_str = f"### Available Dishes in {city}:\n"
62
- for dish, info in nutritional_data.items():
63
- result_str += f"- **{dish}**\n"
64
- result_str += f" - Cost: ₹{info[city]}\n"
65
- result_str += f" - Energy: {info['Energy (kcal)']} kcal\n"
66
- result_str += f" - Protein: {info['Protein (g)']} g\n"
67
- result_str += f" - Fat: {info['Fat (g)']} g\n"
68
- result_str += f" - Carbohydrate: {info['Carbohydrate (g)']} g\n"
69
- result_str += f" - Fiber: {info['Fiber (g)']} g\n"
70
- result_str += f" - Calcium: {info['Calcium (mg)']} mg\n"
71
- result_str += f" - Iron: {info['Iron (mg)']} mg\n"
72
- result_str += f" - Vitamin C: {info['Vitamin C (mg)']} mg\n\n"
73
- return result_str
74
-
75
- def optimize_nutrition_in_city(city, target_calories, target_protein, max_cost):
76
  # Extracting cost, calories, and protein data for the selected city
77
  costs = [nutritional_data[dish][city] for dish in nutritional_data]
78
  calories = [nutritional_data[dish]["Energy (kcal)"] for dish in nutritional_data]
79
  proteins = [nutritional_data[dish]["Protein (g)"] for dish in nutritional_data]
80
 
81
- # Objective function: Minimize cost
82
- c = costs
83
 
84
- # Inequality constraints (A_ub @ x <= b_ub)
85
- A_ub = [
86
- [-cal for cal in calories], # -calories to ensure sum(calories * x) >= target_calories
87
- [-prot for prot in proteins], # -protein to ensure sum(proteins * x) >= target_protein
88
- costs # to ensure sum(costs * x) <= max_cost
89
- ]
90
- b_ub = [-target_calories, -target_protein, max_cost]
91
 
92
  # Bounds for each dish (x >= 0)
93
  bounds = [(0, None) for _ in costs]
@@ -98,11 +78,11 @@ def optimize_nutrition_in_city(city, target_calories, target_protein, max_cost):
98
  if result.success:
99
  selected_dishes = [dish for i, dish in enumerate(nutritional_data) if result.x[i] > 1e-5]
100
  quantities = result.x
101
- total_cost = result.fun
102
- total_calories = sum(cal * q for cal, q in zip(calories, quantities))
103
- total_protein = sum(prot * q for prot, q in zip(proteins, quantities))
104
 
105
- result_str = f"### Selected Dishes in {city} (Quantities):\n"
106
  for dish, qty in zip(selected_dishes, quantities):
107
  if qty > 1e-5:
108
  result_str += f"- **{dish}**: {qty:.2f} portions\n"
@@ -113,50 +93,30 @@ def optimize_nutrition_in_city(city, target_calories, target_protein, max_cost):
113
 
114
  return result_str
115
  else:
116
- return f"No feasible solution found for {city} that meets the nutritional goals within the cost constraints."
117
 
118
  # Gradio Interface
119
  def create_interface():
120
  cities = ["Chennai", "Bengaluru", "Hyderabad", "New Delhi"]
121
 
122
  with gr.Blocks() as demo:
123
- gr.Markdown("# Cost vs. Nutrition Trade-off Optimization in Different Cities")
124
 
125
- # User inputs for city, nutritional targets, and cost limit
126
  city_selector = gr.Dropdown(choices=cities, label="Select City")
127
- energy_target = gr.Number(label="Target Energy (kcal)", value=500)
128
- protein_target = gr.Number(label="Target Protein (g)", value=25)
129
- cost_limit = gr.Number(label="Maximum Cost (₹)", value=300)
130
-
131
- toggle_dishes_button = gr.Button("Display Dishes")
132
- optimize_button = gr.Button("Optimize")
133
 
134
- dishes_output = gr.Markdown(label="Available Dishes")
135
  optimization_output = gr.Markdown(label="Optimization Results")
136
 
137
- # Function to toggle the display of dishes
138
- dishes_visible = False
139
-
140
- def toggle_dishes(city):
141
- nonlocal dishes_visible
142
- dishes_visible = not dishes_visible
143
- if dishes_visible:
144
- toggle_dishes_button.label = "Hide Dishes"
145
- return display_dishes_in_city(city)
146
- else:
147
- toggle_dishes_button.label = "Display Dishes"
148
- return ""
149
-
150
  # Function to handle optimization
151
- def run_optimization(city, target_calories, target_protein, max_cost):
152
- return optimize_nutrition_in_city(city, target_calories, target_protein, max_cost)
153
 
154
- toggle_dishes_button.click(fn=toggle_dishes, inputs=[city_selector], outputs=dishes_output)
155
- optimize_button.click(fn=run_optimization, inputs=[city_selector, energy_target, protein_target, cost_limit], outputs=optimization_output)
156
 
157
- gr.Row([city_selector, energy_target, protein_target, cost_limit])
158
- gr.Row(toggle_dishes_button, optimize_button)
159
- gr.Row(dishes_output)
160
  gr.Row(optimization_output)
161
 
162
  return demo
 
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]
63
  proteins = [nutritional_data[dish]["Protein (g)"] for dish in nutritional_data]
64
 
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 (x >= 0)
73
  bounds = [(0, None) for _ in costs]
 
78
  if result.success:
79
  selected_dishes = [dish for i, dish in enumerate(nutritional_data) if result.x[i] > 1e-5]
80
  quantities = result.x
81
+ total_cost = sum(cost * qty for cost, qty in zip(costs, quantities))
82
+ total_calories = sum(cal * qty for cal, qty in zip(calories, quantities))
83
+ total_protein = sum(prot * qty for prot, qty in zip(proteins, quantities))
84
 
85
+ result_str = f"### Best Combination of Dishes for ₹{daily_budget:.2f} in {city}:\n"
86
  for dish, qty in zip(selected_dishes, quantities):
87
  if qty > 1e-5:
88
  result_str += f"- **{dish}**: {qty:.2f} portions\n"
 
93
 
94
  return result_str
95
  else:
96
+ return f"No feasible solution found for {daily_budget:.2f} in {city}."
97
 
98
  # Gradio Interface
99
  def create_interface():
100
  cities = ["Chennai", "Bengaluru", "Hyderabad", "New Delhi"]
101
 
102
  with gr.Blocks() as demo:
103
+ gr.Markdown("# Daily Budget Optimization for Best Nutrition")
104
 
105
+ # User inputs for city and daily budget
106
  city_selector = gr.Dropdown(choices=cities, label="Select City")
107
+ budget_input = gr.Number(label="Daily Budget ()", value=300)
 
 
 
 
 
108
 
109
+ optimize_button = gr.Button("Optimize Nutrition")
110
  optimization_output = gr.Markdown(label="Optimization Results")
111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  # Function to handle optimization
113
+ def run_optimization(city, daily_budget):
114
+ return optimize_dishes_for_budget(city, daily_budget)
115
 
116
+ optimize_button.click(fn=run_optimization, inputs=[city_selector, budget_input], outputs=optimization_output)
 
117
 
118
+ gr.Row([city_selector, budget_input])
119
+ gr.Row(optimize_button)
 
120
  gr.Row(optimization_output)
121
 
122
  return demo