Update app.py
Browse files
app.py
CHANGED
|
@@ -1,85 +1,69 @@
|
|
| 1 |
-
import sqlite3
|
| 2 |
import gradio as gr
|
| 3 |
-
import os
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
cursor.execute("SELECT * FROM Items WHERE category_id = ?", (category_id,))
|
| 57 |
-
items = [{"ID": row[0], "Name": row[1], "Description": row[2], "Price": row[3], "Image": row[5]} for row in cursor.fetchall()]
|
| 58 |
-
return items
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
def display_categories():
|
| 62 |
-
return get_categories()
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
def display_items(category_id):
|
| 66 |
-
return get_items(category_id)
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
# Gradio App
|
| 70 |
with gr.Blocks() as demo:
|
| 71 |
-
gr.Markdown("#
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
+
# Food item data
|
| 4 |
+
food_data = [
|
| 5 |
+
{
|
| 6 |
+
"name": "Apple",
|
| 7 |
+
"image": "apple.jpg",
|
| 8 |
+
"nutrition": {
|
| 9 |
+
"Calories": "52 kcal",
|
| 10 |
+
"Proteins": "0.3 g",
|
| 11 |
+
"Carbohydrates": "14 g"
|
| 12 |
+
}
|
| 13 |
+
},
|
| 14 |
+
{
|
| 15 |
+
"name": "Banana",
|
| 16 |
+
"image": "banana.jpg",
|
| 17 |
+
"nutrition": {
|
| 18 |
+
"Calories": "96 kcal",
|
| 19 |
+
"Proteins": "1.3 g",
|
| 20 |
+
"Carbohydrates": "27 g"
|
| 21 |
+
}
|
| 22 |
+
},
|
| 23 |
+
{
|
| 24 |
+
"name": "Chicken Breast",
|
| 25 |
+
"image": "chicken_breast.jpg",
|
| 26 |
+
"nutrition": {
|
| 27 |
+
"Calories": "165 kcal",
|
| 28 |
+
"Proteins": "31 g",
|
| 29 |
+
"Carbohydrates": "0 g"
|
| 30 |
+
}
|
| 31 |
+
},
|
| 32 |
+
{
|
| 33 |
+
"name": "Rice",
|
| 34 |
+
"image": "rice.jpg",
|
| 35 |
+
"nutrition": {
|
| 36 |
+
"Calories": "130 kcal",
|
| 37 |
+
"Proteins": "2.7 g",
|
| 38 |
+
"Carbohydrates": "28 g"
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
# Function to show food details
|
| 44 |
+
def food_details(food_name):
|
| 45 |
+
# Find the food item by name
|
| 46 |
+
food_item = next((food for food in food_data if food["name"] == food_name), None)
|
| 47 |
+
if food_item:
|
| 48 |
+
# Format nutrition details
|
| 49 |
+
nutrition_details = "\n".join(f"{key}: {value}" for key, value in food_item["nutrition"].items())
|
| 50 |
+
return f"### {food_item['name']}\n\n**Nutrition Details:**\n\n{nutrition_details}"
|
| 51 |
+
return "Food item not found."
|
| 52 |
+
|
| 53 |
+
# Gradio Interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
with gr.Blocks() as demo:
|
| 55 |
+
gr.Markdown("# Food Nutrition App")
|
| 56 |
|
| 57 |
+
# Food items gallery
|
| 58 |
+
with gr.Row():
|
| 59 |
+
for food in food_data:
|
| 60 |
+
with gr.Column():
|
| 61 |
+
gr.Markdown(f"### {food['name']}")
|
| 62 |
+
food_button = gr.Button(image=food["image"])
|
| 63 |
+
food_button.click(
|
| 64 |
+
food_details,
|
| 65 |
+
inputs=gr.Textbox(value=food["name"], visible=False),
|
| 66 |
+
outputs=gr.Markdown()
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
demo.launch()
|