Spaces:
Build error
Build error
| import gradio as gr | |
| import numpy as np | |
| # Data: Nutritional Information and Cost by City for all dishes | |
| dish_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": {"Street": "20-60", "Local": "40-90", "Upscale": "80-180"}, | |
| "Bengaluru": {"Street": "25-60", "Local": "50-100", "Upscale": "80-200"}, | |
| "Hyderabad": {"Street": "20-50", "Local": "40-90", "Upscale": "80-180"}, | |
| "New Delhi": {"Street": "30-70", "Local": "60-120", "Upscale": "90-200"} | |
| }, | |
| "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": {"Street": "30-60", "Local": "50-120", "Upscale": "120-200"}, | |
| "Bengaluru": {"Street": "35-70", "Local": "60-130", "Upscale": "120-220"}, | |
| "Hyderabad": {"Street": "30-60", "Local": "50-110", "Upscale": "110-200"}, | |
| "New Delhi": {"Street": "40-80", "Local": "70-140", "Upscale": "130-250"} | |
| }, | |
| "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": {"Street": "100-150", "Local": "150-300", "Upscale": "250-500"}, | |
| "Bengaluru": {"Street": "110-160", "Local": "160-320", "Upscale": "270-550"}, | |
| "Hyderabad": {"Street": "100-150", "Local": "150-300", "Upscale": "250-500"}, | |
| "New Delhi": {"Street": "120-180", "Local": "180-350", "Upscale": "300-600"} | |
| }, | |
| "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": {"Street": "60-100", "Local": "100-200", "Upscale": "180-350"}, | |
| "Bengaluru": {"Street": "70-110", "Local": "110-220", "Upscale": "200-380"}, | |
| "Hyderabad": {"Street": "60-100", "Local": "100-200", "Upscale": "180-350"}, | |
| "New Delhi": {"Street": "80-120", "Local": "120-250", "Upscale": "200-400"} | |
| }, | |
| "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": {"Street": "20-40", "Local": "50-80", "Upscale": "100-150"}, | |
| "Bengaluru": {"Street": "25-50", "Local": "55-85", "Upscale": "110-160"}, | |
| "Hyderabad": {"Street": "20-40", "Local": "50-80", "Upscale": "100-150"}, | |
| "New Delhi": {"Street": "30-50", "Local": "60-100", "Upscale": "120-180"} | |
| }, | |
| "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": {"Street": "50-80", "Local": "100-150", "Upscale": "180-300"}, | |
| "Bengaluru": {"Street": "60-90", "Local": "110-160", "Upscale": "200-320"}, | |
| "Hyderabad": {"Street": "50-80", "Local": "100-150", "Upscale": "180-300"}, | |
| "New Delhi": {"Street": "70-100", "Local": "120-180", "Upscale": "220-350"} | |
| }, | |
| "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": {"Street": "40-70", "Local": "80-120", "Upscale": "150-250"}, | |
| "Bengaluru": {"Street": "45-80", "Local": "90-130", "Upscale": "160-270"}, | |
| "Hyderabad": {"Street": "40-70", "Local": "80-120", "Upscale": "150-250"}, | |
| "New Delhi": {"Street": "50-90", "Local": "100-150", "Upscale": "180-280"} | |
| }, | |
| "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": {"Street": "20-40", "Local": "50-80", "Upscale": "100-150"}, | |
| "Bengaluru": {"Street": "25-45", "Local": "55-85", "Upscale": "110-160"}, | |
| "Hyderabad": {"Street": "20-40", "Local": "50-80", "Upscale": "100-150"}, | |
| "New Delhi": {"Street": "30-50", "Local": "60-90", "Upscale": "120-180"} | |
| }, | |
| "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": {"Street": "40-70", "Local": "80-120", "Upscale": "150-250"}, | |
| "Bengaluru": {"Street": "45-75", "Local": "85-130", "Upscale": "160-270"}, | |
| "Hyderabad": {"Street": "40-70", "Local": "80-120", "Upscale": "150-250"}, | |
| "New Delhi": {"Street": "50-90", "Local": "100-150", "Upscale": "180-280"} | |
| }, | |
| "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": {"Street": "20-50", "Local": "50-100", "Upscale": "100-200"}, | |
| "Bengaluru": {"Street": "25-60", "Local": "55-110", "Upscale": "120-220"}, | |
| "Hyderabad": {"Street": "20-50", "Local": "50-100", "Upscale": "100-200"}, | |
| "New Delhi": {"Street": "30-60", "Local": "60-120", "Upscale": "120-250"} | |
| } | |
| } | |
| def extract_average_cost(cost_range): | |
| """Extracts the average cost from a cost range string.""" | |
| costs = [int(cost) for cost in cost_range.split('-')] | |
| return np.mean(costs) | |
| def normalize(values): | |
| """Normalizes a list of values to a scale from 0 to 1.""" | |
| min_val = min(values) | |
| max_val = max(values) | |
| return [(v - min_val) / (max_val - min_val) for v in values] | |
| def optimize_nutrition_and_cost(city, segment, nutrition_weight, cost_weight): | |
| """Optimizes dishes based on both nutrition and cost with custom weights.""" | |
| nutrition_scores = [] | |
| costs = [] | |
| dishes = [] | |
| for dish, info in dish_data.items(): | |
| nutrition_score = ( | |
| info["Protein (g)"] * 2 + # Higher protein is better | |
| info["Fiber (g)"] * 1.5 + # Higher fiber is better | |
| -info["Energy (kcal)"] # Lower calories are better | |
| ) | |
| nutrition_scores.append(nutrition_score) | |
| avg_cost = extract_average_cost(info[city][segment]) | |
| costs.append(avg_cost) | |
| dishes.append(dish) | |
| # Normalize both nutrition and cost | |
| normalized_nutrition_scores = normalize(nutrition_scores) | |
| normalized_costs = normalize(costs) | |
| # Calculate combined score (higher is better) | |
| combined_scores = [ | |
| (nutrition_weight * n_score + cost_weight * (1 - c_score)) | |
| for n_score, c_score in zip(normalized_nutrition_scores, normalized_costs) | |
| ] | |
| # Sort by combined score | |
| ranked_dishes = sorted(zip(dishes, combined_scores), key=lambda x: x[1], reverse=True) | |
| return ranked_dishes | |
| def display_optimized_ranking(city, segment, nutrition_weight, cost_weight): | |
| """Displays the optimized ranking of dishes based on nutrition and cost.""" | |
| ranked_dishes = optimize_nutrition_and_cost(city, segment, nutrition_weight, cost_weight) | |
| output = "" | |
| for i, (dish, combined_score) in enumerate(ranked_dishes, 1): | |
| output += f"{i}. {dish} - Combined Score: {combined_score:.2f}\n" | |
| return output | |
| # Gradio Interface | |
| def create_interface(): | |
| cities = ["Chennai", "Bengaluru", "Hyderabad", "New Delhi"] | |
| segments = ["Street", "Local", "Upscale"] | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Healthy Indian Dishes: Optimized by Cost and Nutrition") | |
| with gr.Row(): | |
| city_selector = gr.Dropdown(choices=cities, label="Select a City", value="Chennai") | |
| segment_selector = gr.Dropdown(choices=segments, label="Select an Eateries Segment", value="Street") | |
| with gr.Row(): | |
| nutrition_weight_slider = gr.Slider(0, 1, value=0.7, label="Nutrition Weight") | |
| cost_weight_slider = gr.Slider(0, 1, value=0.3, label="Cost Weight") | |
| with gr.Row(): | |
| optimized_ranking_output = gr.Textbox(label="Optimized Dish Ranking", interactive=False) | |
| # Update the output based on the selections and weight sliders | |
| def update_ranking(city, segment, nutrition_weight, cost_weight): | |
| return display_optimized_ranking(city, segment, nutrition_weight, cost_weight) | |
| inputs = [city_selector, segment_selector, nutrition_weight_slider, cost_weight_slider] | |
| city_selector.change(fn=update_ranking, inputs=inputs, outputs=optimized_ranking_output) | |
| segment_selector.change(fn=update_ranking, inputs=inputs, outputs=optimized_ranking_output) | |
| nutrition_weight_slider.change(fn=update_ranking, inputs=inputs, outputs=optimized_ranking_output) | |
| cost_weight_slider.change(fn=update_ranking, inputs=inputs, outputs=optimized_ranking_output) | |
| gr.Row(city_selector, segment_selector, nutrition_weight_slider, cost_weight_slider, optimized_ranking_output) | |
| return demo | |
| # Launch the interface | |
| demo = create_interface() | |
| demo.launch(share=True) | |