import gradio as gr import random class LotterySystem: def __init__(self): self.items = [] def add_item(self, item): if item.strip(): self.items.append(item.strip()) return f"已添加: {item}\n當前選項: {', '.join(self.items)}" return "請輸入有效的選項" def draw_lottery(self): if self.items: result = random.choice(self.items) return f"抽籤結果: {result}" return "請先添加一些選項" def get_items(self): return f"當前選項: {', '.join(self.items)}" lottery = LotterySystem() def add_item_interface(item): return lottery.add_item(item) def draw_lottery_interface(): return lottery.draw_lottery() def get_items_interface(): return lottery.get_items() with gr.Blocks(title="抽籤系統") as demo: gr.Markdown("# 抽籤系統") with gr.Row(): input_item = gr.Textbox(label="輸入選項") add_button = gr.Button("添加選項") output_add = gr.Textbox(label="添加結果") add_button.click(add_item_interface, inputs=input_item, outputs=output_add) draw_button = gr.Button("抽籤") output_draw = gr.Textbox(label="抽籤結果") draw_button.click(draw_lottery_interface, outputs=output_draw) show_items_button = gr.Button("顯示所有選項") output_items = gr.Textbox(label="當前選項") show_items_button.click(get_items_interface, outputs=output_items) demo.launch()