migo0722 commited on
Commit
d5e5818
·
verified ·
1 Parent(s): 3b5be66

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -0
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import random
3
+
4
+ # 晚餐清單,每個選項包含名稱和圖片連結
5
+ # 圖片連結可以是你自己的圖片庫,或是來自網路上的公開圖片
6
+ dinner_options = {
7
+ "紅燒牛肉麵": "https://images.unsplash.com/photo-1563823439066-88b901614f04?ixlib=rb-4.0.3&q=85&fm=jpg&crop=entropy&cs=srgb",
8
+ "蒜香奶油蝦義大利麵": "https://images.unsplash.com/photo-1627878347895-df5698b67272?ixlib=rb-4.0.3&q=85&fm=jpg&crop=entropy&cs=srgb",
9
+ "日式豬排咖哩飯": "https://images.unsplash.com/photo-1548694086-63d08f5d0232?ixlib=rb-4.0.3&q=85&fm=jpg&crop=entropy&cs=srgb",
10
+ "麻婆豆腐": "https://images.unsplash.com/photo-1596796530661-d7072a394879?ixlib=rb-4.0.3&q=85&fm=jpg&crop=entropy&cs=srgb",
11
+ "三杯雞": "https://images.unsplash.com/photo-1563823439066-88b901614f04?ixlib=rb-4.0.3&q=85&fm=jpg&crop=entropy&cs=srgb",
12
+ # 你可以從這裡開始增加更多選項,確保至少有50個!
13
+ # 記得為每道菜換上不同的圖片連結
14
+ }
15
+
16
+ # 挑選晚餐的函式
17
+ def pick_dinner():
18
+ # 從字典中隨機挑選一組鍵值對 (晚餐名稱, 圖片連結)
19
+ dish, image_url = random.choice(list(dinner_options.items()))
20
+ return f"今晚吃 **{dish}**!", image_url
21
+
22
+ # 建立 Gradio 介面
23
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
24
+ gr.Markdown("# 🍜 晚餐挑選器")
25
+ gr.Markdown("選擇困難?交給我來決定你今晚要吃什麼!")
26
+
27
+ with gr.Row():
28
+ with gr.Column(scale=1):
29
+ # 建立一個按鈕,點擊後觸發 pick_dinner 函式
30
+ btn = gr.Button("隨機挑選")
31
+
32
+ with gr.Column(scale=2):
33
+ # 顯示結果的文字框
34
+ output_text = gr.Markdown()
35
+ # 顯示結果圖片的圖片框
36
+ output_image = gr.Image(label="圖片參考", interactive=False)
37
+
38
+ # 將按鈕的點擊事件與函式和輸出元件連結
39
+ btn.click(
40
+ fn=pick_dinner,
41
+ outputs=[output_text, output_image]
42
+ )
43
+
44
+ if __name__ == "__main__":
45
+ demo.launch()