dindizz commited on
Commit
077d4ce
·
verified ·
1 Parent(s): 3192101

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -22
app.py CHANGED
@@ -5,40 +5,60 @@ from scipy.optimize import linprog
5
  # Example data: Nutritional Information and Cost for the healthiest dishes
6
  nutritional_data = {
7
  "Idli with Vegetable Sambar": {
8
- "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": 50
 
 
9
  },
10
  "Khichdi": {
11
- "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
 
 
12
  },
13
  "Tandoori Chicken": {
14
- "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
 
 
15
  },
16
  "Palak Paneer": {
17
- "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
 
 
18
  },
19
  "Raita": {
20
- "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": 30
 
 
21
  },
22
  "Rajma": {
23
- "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
 
 
24
  },
25
  "Baingan Bharta": {
26
- "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": 50
 
 
27
  },
28
  "Besan Chilla": {
29
- "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": 60
 
 
30
  },
31
  "Masoor Dal": {
32
- "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": 70
 
 
33
  },
34
  "Upma": {
35
- "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": 40
 
 
36
  }
37
  }
38
 
39
- def optimize_nutrition(target_calories, target_protein, max_cost):
40
- # Extracting cost, calories, and protein data
41
- costs = [nutritional_data[dish]["Cost"] for dish in nutritional_data]
42
  calories = [nutritional_data[dish]["Energy (kcal)"] for dish in nutritional_data]
43
  proteins = [nutritional_data[dish]["Protein (g)"] for dish in nutritional_data]
44
 
@@ -46,7 +66,6 @@ def optimize_nutrition(target_calories, target_protein, max_cost):
46
  c = costs
47
 
48
  # Inequality constraints (A_ub @ x <= b_ub)
49
- # Ensuring that the total calories and protein are at least the target values
50
  A_ub = [
51
  [-cal for cal in calories], # -calories to ensure sum(calories * x) >= target_calories
52
  [-prot for prot in proteins], # -protein to ensure sum(proteins * x) >= target_protein
@@ -67,7 +86,7 @@ def optimize_nutrition(target_calories, target_protein, max_cost):
67
  total_calories = sum(cal * q for cal, q in zip(calories, quantities))
68
  total_protein = sum(prot * q for prot, q in zip(proteins, quantities))
69
 
70
- result_str = f"### Selected Dishes (Quantities):\n"
71
  for dish, qty in zip(selected_dishes, quantities):
72
  if qty > 1e-5:
73
  result_str += f"- **{dish}**: {qty:.2f} portions\n"
@@ -78,14 +97,17 @@ def optimize_nutrition(target_calories, target_protein, max_cost):
78
 
79
  return result_str
80
  else:
81
- return "No feasible solution found that meets the nutritional goals within the cost constraints."
82
 
83
  # Gradio Interface
84
  def create_interface():
 
 
85
  with gr.Blocks() as demo:
86
- gr.Markdown("# Cost vs. Nutrition Trade-off Optimization")
87
 
88
- # User inputs for nutritional targets and cost limit
 
89
  energy_target = gr.Number(label="Target Energy (kcal)", value=500)
90
  protein_target = gr.Number(label="Target Protein (g)", value=25)
91
  cost_limit = gr.Number(label="Maximum Cost (₹)", value=300)
@@ -94,12 +116,12 @@ def create_interface():
94
  optimization_output = gr.Markdown(label="Optimization Results")
95
 
96
  # Function to handle optimization
97
- def run_optimization(target_calories, target_protein, max_cost):
98
- return optimize_nutrition(target_calories, target_protein, max_cost)
99
 
100
- optimize_button.click(fn=run_optimization, inputs=[energy_target, protein_target, cost_limit], outputs=optimization_output)
101
 
102
- gr.Row([energy_target, protein_target, cost_limit])
103
  gr.Row(optimize_button, optimization_output)
104
 
105
  return demo
 
5
  # Example data: Nutritional Information and Cost for the healthiest dishes
6
  nutritional_data = {
7
  "Idli with Vegetable Sambar": {
8
+ "Energy (kcal)": 100, "Protein (g)": 5.5, "Fat (g)": 1.5, "Carbohydrate (g)": 17.0, "Fiber (g)": 3.0,
9
+ "Calcium (mg)": 35, "Iron (mg)": 1.0, "Vitamin C (mg)": 8.0,
10
+ "Chennai": 50, "Bengaluru": 55, "Hyderabad": 50, "New Delhi": 60
11
  },
12
  "Khichdi": {
13
+ "Energy (kcal)": 120, "Protein (g)": 4.0, "Fat (g)": 2.5, "Carbohydrate (g)": 20.0, "Fiber (g)": 2.5,
14
+ "Calcium (mg)": 40, "Iron (mg)": 1.0, "Vitamin C (mg)": 5.0,
15
+ "Chennai": 60, "Bengaluru": 65, "Hyderabad": 60, "New Delhi": 70
16
  },
17
  "Tandoori Chicken": {
18
+ "Energy (kcal)": 150, "Protein (g)": 18.0, "Fat (g)": 7.0, "Carbohydrate (g)": 3.0, "Fiber (g)": 0.5,
19
+ "Calcium (mg)": 15, "Iron (mg)": 1.2, "Vitamin C (mg)": 1.5,
20
+ "Chennai": 150, "Bengaluru": 160, "Hyderabad": 150, "New Delhi": 180
21
  },
22
  "Palak Paneer": {
23
+ "Energy (kcal)": 140, "Protein (g)": 7.5, "Fat (g)": 10.0, "Carbohydrate (g)": 6.0, "Fiber (g)": 3.0,
24
+ "Calcium (mg)": 200, "Iron (mg)": 3.0, "Vitamin C (mg)": 15.0,
25
+ "Chennai": 100, "Bengaluru": 110, "Hyderabad": 100, "New Delhi": 120
26
  },
27
  "Raita": {
28
+ "Energy (kcal)": 60, "Protein (g)": 3.5, "Fat (g)": 2.0, "Carbohydrate (g)": 6.5, "Fiber (g)": 0.5,
29
+ "Calcium (mg)": 100, "Iron (mg)": 0.5, "Vitamin C (mg)": 2.0,
30
+ "Chennai": 30, "Bengaluru": 35, "Hyderabad": 30, "New Delhi": 40
31
  },
32
  "Rajma": {
33
+ "Energy (kcal)": 140, "Protein (g)": 7.5, "Fat (g)": 5.0, "Carbohydrate (g)": 20.0, "Fiber (g)": 6.0,
34
+ "Calcium (mg)": 50, "Iron (mg)": 3.5, "Vitamin C (mg)": 4.0,
35
+ "Chennai": 80, "Bengaluru": 90, "Hyderabad": 80, "New Delhi": 100
36
  },
37
  "Baingan Bharta": {
38
+ "Energy (kcal)": 70, "Protein (g)": 2.5, "Fat (g)": 3.0, "Carbohydrate (g)": 10.0, "Fiber (g)": 4.0,
39
+ "Calcium (mg)": 30, "Iron (mg)": 0.7, "Vitamin C (mg)": 6.0,
40
+ "Chennai": 50, "Bengaluru": 55, "Hyderabad": 50, "New Delhi": 60
41
  },
42
  "Besan Chilla": {
43
+ "Energy (kcal)": 180, "Protein (g)": 8.0, "Fat (g)": 7.0, "Carbohydrate (g)": 20.0, "Fiber (g)": 5.0,
44
+ "Calcium (mg)": 30, "Iron (mg)": 2.5, "Vitamin C (mg)": 1.0,
45
+ "Chennai": 60, "Bengaluru": 65, "Hyderabad": 60, "New Delhi": 70
46
  },
47
  "Masoor Dal": {
48
+ "Energy (kcal)": 110, "Protein (g)": 7.5, "Fat (g)": 2.0, "Carbohydrate (g)": 16.0, "Fiber (g)": 5.5,
49
+ "Calcium (mg)": 25, "Iron (mg)": 3.0, "Vitamin C (mg)": 4.0,
50
+ "Chennai": 70, "Bengaluru": 75, "Hyderabad": 70, "New Delhi": 80
51
  },
52
  "Upma": {
53
+ "Energy (kcal)": 150, "Protein (g)": 4.0, "Fat (g)": 6.0, "Carbohydrate (g)": 25.0, "Fiber (g)": 3.0,
54
+ "Calcium (mg)": 30, "Iron (mg)": 1.0, "Vitamin C (mg)": 3.0,
55
+ "Chennai": 40, "Bengaluru": 45, "Hyderabad": 40, "New Delhi": 50
56
  }
57
  }
58
 
59
+ def optimize_nutrition_in_city(city, target_calories, target_protein, max_cost):
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
 
 
66
  c = costs
67
 
68
  # Inequality constraints (A_ub @ x <= b_ub)
 
69
  A_ub = [
70
  [-cal for cal in calories], # -calories to ensure sum(calories * x) >= target_calories
71
  [-prot for prot in proteins], # -protein to ensure sum(proteins * x) >= target_protein
 
86
  total_calories = sum(cal * q for cal, q in zip(calories, quantities))
87
  total_protein = sum(prot * q for prot, q in zip(proteins, quantities))
88
 
89
+ result_str = f"### Selected Dishes in {city} (Quantities):\n"
90
  for dish, qty in zip(selected_dishes, quantities):
91
  if qty > 1e-5:
92
  result_str += f"- **{dish}**: {qty:.2f} portions\n"
 
97
 
98
  return result_str
99
  else:
100
+ return f"No feasible solution found for {city} that meets the nutritional goals within the cost constraints."
101
 
102
  # Gradio Interface
103
  def create_interface():
104
+ cities = ["Chennai", "Bengaluru", "Hyderabad", "New Delhi"]
105
+
106
  with gr.Blocks() as demo:
107
+ gr.Markdown("# Cost vs. Nutrition Trade-off Optimization in Different Cities")
108
 
109
+ # User inputs for city, nutritional targets, and cost limit
110
+ city_selector = gr.Dropdown(choices=cities, label="Select City")
111
  energy_target = gr.Number(label="Target Energy (kcal)", value=500)
112
  protein_target = gr.Number(label="Target Protein (g)", value=25)
113
  cost_limit = gr.Number(label="Maximum Cost (₹)", value=300)
 
116
  optimization_output = gr.Markdown(label="Optimization Results")
117
 
118
  # Function to handle optimization
119
+ def run_optimization(city, target_calories, target_protein, max_cost):
120
+ return optimize_nutrition_in_city(city, target_calories, target_protein, max_cost)
121
 
122
+ optimize_button.click(fn=run_optimization, inputs=[city_selector, energy_target, protein_target, cost_limit], outputs=optimization_output)
123
 
124
+ gr.Row([city_selector, energy_target, protein_target, cost_limit])
125
  gr.Row(optimize_button, optimization_output)
126
 
127
  return demo