Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from scipy.optimize import linprog
|
| 4 |
+
|
| 5 |
+
# Data: Nutritional Information 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": 40
|
| 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": 20
|
| 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": 70
|
| 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": 50
|
| 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": 40
|
| 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": 30
|
| 36 |
+
}
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
def optimize_nutrition(target_calories, target_protein, max_cost):
|
| 40 |
+
"""Optimizes the selection of dishes to meet nutritional goals."""
|
| 41 |
+
# Objective: Minimize the total deviation from nutritional goals
|
| 42 |
+
# Constraints: Total cost <= max_cost, total nutritional values should try to meet or exceed targets
|
| 43 |
+
|
| 44 |
+
dishes = list(nutritional_data.keys())
|
| 45 |
+
|
| 46 |
+
# Coefficients for the linear programming (negative values for maximization)
|
| 47 |
+
c = [-nutritional_data[dish]['Energy (kcal)'] for dish in dishes] + \
|
| 48 |
+
[-nutritional_data[dish]['Protein (g)'] for dish in dishes]
|
| 49 |
+
|
| 50 |
+
# Inequality constraints matrix
|
| 51 |
+
A_ub = [
|
| 52 |
+
[nutritional_data[dish]['Energy (kcal)'] for dish in dishes] + \
|
| 53 |
+
[0 for _ in dishes], # Total calories
|
| 54 |
+
[0 for _ in dishes] + [nutritional_data[dish]['Protein (g)'] for dish in dishes] # Total protein
|
| 55 |
+
]
|
| 56 |
+
|
| 57 |
+
# Inequality constraints vector
|
| 58 |
+
b_ub = [target_calories, target_protein]
|
| 59 |
+
|
| 60 |
+
# Equality constraints (cost)
|
| 61 |
+
A_eq = [[nutritional_data[dish]['Cost'] for dish in dishes]]
|
| 62 |
+
b_eq = [max_cost]
|
| 63 |
+
|
| 64 |
+
# Bounds for decision variables (dish quantities, we assume only integers are allowed)
|
| 65 |
+
bounds = [(0, 1) for _ in range(len(dishes) * 2)]
|
| 66 |
+
|
| 67 |
+
# Linear programming to optimize the nutritional intake
|
| 68 |
+
result = linprog(c, A_ub=A_ub, b_ub=b_ub, A_eq=A_eq, b_eq=b_eq, bounds=bounds, method='simplex')
|
| 69 |
+
|
| 70 |
+
if result.success:
|
| 71 |
+
quantities = result.x[:len(dishes)]
|
| 72 |
+
selected_dishes = {dishes[i]: quantities[i] for i in range(len(dishes)) if quantities[i] > 0.1}
|
| 73 |
+
return f"Selected Dishes: {selected_dishes}"
|
| 74 |
+
else:
|
| 75 |
+
return "No optimal solution found with the given constraints."
|
| 76 |
+
|
| 77 |
+
# Gradio Interface
|
| 78 |
+
def create_interface():
|
| 79 |
+
with gr.Blocks() as demo:
|
| 80 |
+
gr.Markdown("# Nutritional Optimization Tool")
|
| 81 |
+
|
| 82 |
+
# Input widgets for nutritional targets
|
| 83 |
+
target_calories = gr.Number(label="Target Calories", value=2000)
|
| 84 |
+
target_protein = gr.Number(label="Target Protein (g)", value=50)
|
| 85 |
+
max_cost = gr.Number(label="Maximum Cost (₹)", value=500)
|
| 86 |
+
|
| 87 |
+
# Output widget
|
| 88 |
+
optimization_output = gr.Textbox(label="Optimization Result")
|
| 89 |
+
|
| 90 |
+
# Optimization function to run on button click
|
| 91 |
+
def run_optimization(target_calories, target_protein, max_cost):
|
| 92 |
+
return optimize_nutrition(target_calories, target_protein, max_cost)
|
| 93 |
+
|
| 94 |
+
optimize_button = gr.Button("Optimize")
|
| 95 |
+
optimize_button.click(fn=run_optimization, inputs=[target_calories, target_protein, max_cost], outputs=optimization_output)
|
| 96 |
+
|
| 97 |
+
gr.Row(target_calories, target_protein, max_cost, optimize_button, optimization_output)
|
| 98 |
+
|
| 99 |
+
return demo
|
| 100 |
+
|
| 101 |
+
# Launch the interface
|
| 102 |
+
demo = create_interface()
|
| 103 |
+
demo.launch(share=True)
|