import gradio as gr import time import csv from datetime import datetime import os import pandas as pd # ========== 菜单数据结构 ========== MENU_DATA = { "Appetizers": [ {"id": "A1", "name": "Spring Rolls", "desc": "(Cabbage, carrots, wrapper) – Crispy fried rolls with vegetables.", "price": 4.5}, {"id": "A2", "name": "Dumplings", "desc": "(Pork, chives, soy sauce) – Steamed dumplings with savory filling.", "price": 5.0}, {"id": "A3", "name": "Salad", "desc": "(Lettuce, cucumber, tomato) – Fresh vegetable salad with light dressing.", "price": 3.5}, {"id": "A4", "name": "Tofu Skewers", "desc": "(Tofu, peppers, glaze) – Grilled tofu skewers with sweet sauce.", "price": 4.0}, {"id": "A5", "name": "Edamame", "desc": "(Soybeans, salt) – Lightly boiled and salted green soybeans.", "price": 3.0}, ], "Main Dishes": [ {"id": "B1", "name": "Beef Pho", "desc": "(Beef, rice noodles, herbs) – Traditional Vietnamese noodle soup.", "price": 9.5}, {"id": "B2", "name": "Chicken Curry", "desc": "(Chicken, coconut milk, curry) – Mild spicy curry with rice.", "price": 10.0}, {"id": "B3", "name": "Fried Rice", "desc": "(Rice, egg, veggies) – Wok-fried rice with soy sauce and greens.", "price": 8.0}, {"id": "B4", "name": "Grilled Fish", "desc": "(Fish, lime, herbs) – Grilled fillet with citrus-herb flavor.", "price": 11.0}, {"id": "B5", "name": "Veggie Stir Fry", "desc": "(Broccoli, carrots, tofu) – Mixed vegetables stir-fried in sauce.", "price": 8.5}, ], "Desserts": [ {"id": "C1", "name": "Mango Sticky Rice", "desc": "(Mango, sticky rice, coconut) – Thai-style sweet dessert.", "price": 5.0}, {"id": "C2", "name": "Taro Cake", "desc": "(Taro, rice flour, sugar) – Soft steamed traditional treat.", "price": 4.5}, {"id": "C3", "name": "Coconut Jelly", "desc": "(Coconut milk, agar) – Light and refreshing cold dessert.", "price": 4.0}, {"id": "C4", "name": "Sweet Bean Soup", "desc": "(Red beans, sugar, tapioca) – Warm comforting dessert soup.", "price": 3.5}, {"id": "C5", "name": "Sesame Balls", "desc": "(Glutinous rice, sesame, red bean) – Crispy chewy snack.", "price": 3.5}, ], "Drinks": [ {"id": "D1", "name": "Lime Soda", "desc": "(Lime, soda water, sugar) – Refreshing fizzy lime drink.", "price": 2.5}, {"id": "D2", "name": "Iced Tea", "desc": "(Black tea, lemon, ice) – Chilled lemon tea beverage.", "price": 2.0}, {"id": "D3", "name": "Coconut Water", "desc": "(Coconut juice) – Natural hydrating drink.", "price": 3.0}, {"id": "D4", "name": "Soy Milk", "desc": "(Soybeans, water, sugar) – Sweet and smooth traditional drink.", "price": 2.0}, {"id": "D5", "name": "Bottled Water", "desc": "(Mineral water) – Still bottled water.", "price": 1.5}, ], } # ========== 功能函数 ========== def record_time(start_time): duration = round(time.time() - start_time, 2) timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") with open("decision_times.csv", "a", newline="", encoding="utf-8") as f: writer = csv.writer(f) writer.writerow([timestamp, duration]) return f"✅ Thank you! Your decision time was {duration} seconds.\nPlease proceed to the next page and enter your selected item IDs." # ========== Gradio 界面 ========== with gr.Blocks(css=".footer, .prose a {display: none !important}") as demo: start_time = time.time() gr.Markdown("# 📋 Digital Menu\nPlease browse the menu and decide what you'd like to order.") with gr.Tabs(): for category, items in MENU_DATA.items(): with gr.Tab(label=category): for item in items: with gr.Row(): gr.Image(value="user_avatar.jpg", width=100, height=100) gr.Markdown(f"**{item['id']} – {item['name']}** \n{item['desc']} \n💰 €{item['price']:.2f}") submit_btn = gr.Button("✅ I have made my selection") output_box = gr.Textbox(visible=False) submit_btn.click(fn=record_time, inputs=[gr.State(value=start_time)], outputs=output_box) demo.launch()