youngtsai commited on
Commit
300f268
·
verified ·
1 Parent(s): 923839a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -18
app.py CHANGED
@@ -1,20 +1,52 @@
1
- import random
2
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
- # 定義抽籤函數
5
- def draw_lots(max_number):
6
- max_number = int(max_number)
7
- winner = random.randint(1, max_number)
8
- return f"中獎號碼是: {winner}"
9
-
10
- # 建立 Gradio 介面
11
- iface = gr.Interface(
12
- fn=draw_lots,
13
- inputs=gr.Number(label="輸入全班的號碼上限"),
14
- outputs=gr.Textbox(label="中獎號碼"),
15
- title="抽籤系統",
16
- description="輸入全班的號碼上限,系統將隨機選出一個號碼"
17
- )
18
-
19
- # 啟動介面
20
- iface.launch()
 
 
1
  import gradio as gr
2
+ import random
3
+
4
+ class LotterySystem:
5
+ def __init__(self):
6
+ self.items = []
7
+
8
+ def add_item(self, item):
9
+ if item.strip():
10
+ self.items.append(item.strip())
11
+ return f"已添加: {item}\n當前選項: {', '.join(self.items)}"
12
+ return "請輸入有效的選項"
13
+
14
+ def draw_lottery(self):
15
+ if self.items:
16
+ result = random.choice(self.items)
17
+ return f"抽籤結果: {result}"
18
+ return "請先添加一些選項"
19
+
20
+ def get_items(self):
21
+ return f"當前選項: {', '.join(self.items)}"
22
+
23
+ lottery = LotterySystem()
24
+
25
+ def add_item_interface(item):
26
+ return lottery.add_item(item)
27
+
28
+ def draw_lottery_interface():
29
+ return lottery.draw_lottery()
30
+
31
+ def get_items_interface():
32
+ return lottery.get_items()
33
+
34
+ with gr.Blocks(title="抽籤系統") as demo:
35
+ gr.Markdown("# 抽籤系統")
36
+
37
+ with gr.Row():
38
+ input_item = gr.Textbox(label="輸入選項")
39
+ add_button = gr.Button("添加選項")
40
+
41
+ output_add = gr.Textbox(label="添加結果")
42
+ add_button.click(add_item_interface, inputs=input_item, outputs=output_add)
43
+
44
+ draw_button = gr.Button("抽籤")
45
+ output_draw = gr.Textbox(label="抽籤結果")
46
+ draw_button.click(draw_lottery_interface, outputs=output_draw)
47
+
48
+ show_items_button = gr.Button("顯示所有選項")
49
+ output_items = gr.Textbox(label="當前選項")
50
+ show_items_button.click(get_items_interface, outputs=output_items)
51
 
52
+ demo.launch()