File size: 2,056 Bytes
d5e5818
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
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
import gradio as gr
import random

# 晚餐清單,每個選項包含名稱和圖片連結
# 圖片連結可以是你自己的圖片庫,或是來自網路上的公開圖片
dinner_options = {
    "紅燒牛肉麵": "https://images.unsplash.com/photo-1563823439066-88b901614f04?ixlib=rb-4.0.3&q=85&fm=jpg&crop=entropy&cs=srgb",
    "蒜香奶油蝦義大利麵": "https://images.unsplash.com/photo-1627878347895-df5698b67272?ixlib=rb-4.0.3&q=85&fm=jpg&crop=entropy&cs=srgb",
    "日式豬排咖哩飯": "https://images.unsplash.com/photo-1548694086-63d08f5d0232?ixlib=rb-4.0.3&q=85&fm=jpg&crop=entropy&cs=srgb",
    "麻婆豆腐": "https://images.unsplash.com/photo-1596796530661-d7072a394879?ixlib=rb-4.0.3&q=85&fm=jpg&crop=entropy&cs=srgb",
    "三杯雞": "https://images.unsplash.com/photo-1563823439066-88b901614f04?ixlib=rb-4.0.3&q=85&fm=jpg&crop=entropy&cs=srgb",
    # 你可以從這裡開始增加更多選項,確保至少有50個!
    # 記得為每道菜換上不同的圖片連結
}

# 挑選晚餐的函式
def pick_dinner():
    # 從字典中隨機挑選一組鍵值對 (晚餐名稱, 圖片連結)
    dish, image_url = random.choice(list(dinner_options.items()))
    return f"今晚吃 **{dish}**!", image_url

# 建立 Gradio 介面
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("# 🍜 晚餐挑選器")
    gr.Markdown("選擇困難?交給我來決定你今晚要吃什麼!")
    
    with gr.Row():
        with gr.Column(scale=1):
            # 建立一個按鈕,點擊後觸發 pick_dinner 函式
            btn = gr.Button("隨機挑選")
            
        with gr.Column(scale=2):
            # 顯示結果的文字框
            output_text = gr.Markdown()
            # 顯示結果圖片的圖片框
            output_image = gr.Image(label="圖片參考", interactive=False)
            
    # 將按鈕的點擊事件與函式和輸出元件連結
    btn.click(
        fn=pick_dinner,
        outputs=[output_text, output_image]
    )

if __name__ == "__main__":
    demo.launch()