Subbu1304 commited on
Commit
99a998d
·
verified ·
1 Parent(s): 5b27ff3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -81
app.py CHANGED
@@ -1,85 +1,69 @@
1
- import sqlite3
2
  import gradio as gr
3
- import os
4
 
5
- DATABASE = 'restaurant.db'
6
-
7
-
8
- # Initialize database
9
- def init_db():
10
- with sqlite3.connect(DATABASE) as conn:
11
- cursor = conn.cursor()
12
- cursor.execute('''
13
- CREATE TABLE IF NOT EXISTS Categories (
14
- id INTEGER PRIMARY KEY AUTOINCREMENT,
15
- name TEXT NOT NULL,
16
- image TEXT NOT NULL
17
- )
18
- ''')
19
- cursor.execute('''
20
- CREATE TABLE IF NOT EXISTS Items (
21
- id INTEGER PRIMARY KEY AUTOINCREMENT,
22
- name TEXT NOT NULL,
23
- description TEXT,
24
- price REAL NOT NULL,
25
- category_id INTEGER NOT NULL,
26
- image TEXT NOT NULL,
27
- FOREIGN KEY (category_id) REFERENCES Categories(id)
28
- )
29
- ''')
30
- # Add sample data
31
- cursor.execute("INSERT OR IGNORE INTO Categories (id, name, image) VALUES (1, 'Breakfast', 'breakfast.png')")
32
- cursor.execute("INSERT OR IGNORE INTO Categories (id, name, image) VALUES (2, 'Ramen', 'ramen.png')")
33
- cursor.execute("INSERT OR IGNORE INTO Items (id, name, description, price, category_id, image) VALUES "
34
- "(1, 'Pancakes', 'Fluffy pancakes with syrup.', 5.99, 1, 'pancakes.png')")
35
- cursor.execute("INSERT OR IGNORE INTO Items (id, name, description, price, category_id, image) VALUES "
36
- "(2, 'Classic Ramen', 'Delicious ramen with pork and egg.', 12.99, 2, 'ramen.png')")
37
- conn.commit()
38
-
39
-
40
- if not os.path.exists(DATABASE):
41
- init_db()
42
-
43
-
44
- # Gradio Functions
45
- def get_categories():
46
- with sqlite3.connect(DATABASE) as conn:
47
- cursor = conn.cursor()
48
- cursor.execute("SELECT * FROM Categories")
49
- categories = [{"ID": row[0], "Name": row[1], "Image": row[2]} for row in cursor.fetchall()]
50
- return categories
51
-
52
-
53
- def get_items(category_id):
54
- with sqlite3.connect(DATABASE) as conn:
55
- cursor = conn.cursor()
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("# Restaurant App")
72
 
73
- with gr.Tab("Categories"):
74
- gr.Markdown("### View All Categories")
75
- categories_output = gr.JSON()
76
- gr.Button("Show Categories").click(display_categories, outputs=categories_output)
77
-
78
- with gr.Tab("Items"):
79
- gr.Markdown("### View Items by Category")
80
- category_id_input = gr.Number(label="Enter Category ID")
81
- items_output = gr.JSON()
82
- gr.Button("Show Items").click(display_items, inputs=category_id_input, outputs=items_output)
83
-
84
- demo.launch(share=True)
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()