dindizz's picture
Create app.py
01c1eaf verified
import gradio as gr
import matplotlib.pyplot as plt
import numpy as np
from io import BytesIO
# Data: Nutritional Information for the healthiest dishes
nutritional_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
},
"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
},
"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
},
"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
},
"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
},
"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
},
"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
},
"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
},
"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
},
"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
}
}
def plot_nutritional_comparison(dish):
"""Plots a radar chart comparing the nutritional values of the selected dish."""
labels = list(nutritional_data[dish].keys())
values = list(nutritional_data[dish].values())
# Normalize values for better comparison on the radar chart
max_values = [max(nutritional_data[d][label] for d in nutritional_data) for label in labels]
normalized_values = [value / max_val if max_val > 0 else 0 for value, max_val in zip(values, max_values)]
# Setup the radar chart
angles = np.linspace(0, 2 * np.pi, len(labels), endpoint=False).tolist()
values += values[:1] # repeat the first value to close the circle
normalized_values += normalized_values[:1]
angles += angles[:1]
fig, ax = plt.subplots(figsize=(6, 6), subplot_kw=dict(polar=True))
ax.fill(angles, normalized_values, color='blue', alpha=0.25)
ax.plot(angles, normalized_values, color='blue', linewidth=2)
ax.set_yticklabels([])
ax.set_xticks(angles[:-1])
ax.set_xticklabels(labels)
ax.set_title(f'Nutritional Profile for {dish}', size=15, color='blue', y=1.1)
return fig
def download_image(dish, file_format):
"""Generates the image in the specified format and returns it as a downloadable file."""
fig = plot_nutritional_comparison(dish)
buf = BytesIO()
fig.savefig(buf, format=file_format)
buf.seek(0)
return buf
# Gradio Interface
def create_interface():
dishes = list(nutritional_data.keys())
formats = ["png", "jpeg"]
with gr.Blocks() as demo:
gr.Markdown("# Nutritional Comparison of Healthiest Dishes")
dish_selector = gr.Dropdown(choices=dishes, label="Select a Dish", value=dishes[0])
nutritional_comparison_output = gr.Plot(label="Nutritional Comparison")
format_selector = gr.Radio(choices=formats, label="Select Image Format", value="png")
download_button = gr.File(label="Download Image")
# Update the plot based on the selected dish
def update_nutritional_comparison(dish):
return plot_nutritional_comparison(dish)
# Provide the download option
def get_image(dish, file_format):
return download_image(dish, file_format)
dish_selector.change(fn=update_nutritional_comparison, inputs=dish_selector, outputs=nutritional_comparison_output)
gr.Row(dish_selector, nutritional_comparison_output)
format_selector.change(fn=get_image, inputs=[dish_selector, format_selector], outputs=download_button)
return demo
# Launch the interface
demo = create_interface()
demo.launch(share=True)