|
|
import gradio as gr |
|
|
|
|
|
|
|
|
food_data = [ |
|
|
{ |
|
|
"name": "Apple", |
|
|
"image": "apple.jpg", |
|
|
"nutrition": { |
|
|
"Calories": "52 kcal", |
|
|
"Proteins": "0.3 g", |
|
|
"Carbohydrates": "14 g" |
|
|
} |
|
|
}, |
|
|
{ |
|
|
"name": "Banana", |
|
|
"image": "banana.jpg", |
|
|
"nutrition": { |
|
|
"Calories": "96 kcal", |
|
|
"Proteins": "1.3 g", |
|
|
"Carbohydrates": "27 g" |
|
|
} |
|
|
}, |
|
|
{ |
|
|
"name": "Chicken Breast", |
|
|
"image": "chicken_breast.jpg", |
|
|
"nutrition": { |
|
|
"Calories": "165 kcal", |
|
|
"Proteins": "31 g", |
|
|
"Carbohydrates": "0 g" |
|
|
} |
|
|
}, |
|
|
{ |
|
|
"name": "Rice", |
|
|
"image": "rice.jpg", |
|
|
"nutrition": { |
|
|
"Calories": "130 kcal", |
|
|
"Proteins": "2.7 g", |
|
|
"Carbohydrates": "28 g" |
|
|
} |
|
|
} |
|
|
] |
|
|
|
|
|
|
|
|
def food_details(food_name): |
|
|
|
|
|
food_item = next((food for food in food_data if food["name"] == food_name), None) |
|
|
if food_item: |
|
|
|
|
|
nutrition_details = "\n".join(f"{key}: {value}" for key, value in food_item["nutrition"].items()) |
|
|
return f"### {food_item['name']}\n\n**Nutrition Details:**\n\n{nutrition_details}" |
|
|
return "Food item not found." |
|
|
|
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
gr.Markdown("# Food Nutrition App") |
|
|
|
|
|
|
|
|
with gr.Row(): |
|
|
for food in food_data: |
|
|
with gr.Column(): |
|
|
gr.Markdown(f"### {food['name']}") |
|
|
food_button = gr.Button(image=food["image"]) |
|
|
food_button.click( |
|
|
food_details, |
|
|
inputs=gr.Textbox(value=food["name"], visible=False), |
|
|
outputs=gr.Markdown() |
|
|
) |
|
|
|
|
|
demo.launch() |
|
|
|