Spaces:
Runtime error
Runtime error
Update app.py
Browse files1224346789101112131415161718192021222324252627282930313256789101112131434351516373817181940414243444520212247482324505152252627285455565729305960616231import gradio as grimport random
class LotterySystem:class SimpleChatBot: def __init__(self): self.items = []
def add_items(self, items): new_items = [item.strip() for item in items.split('\n') if item.strip()] self.items.extend(new_items) return f"已添加: {len(new_items)} 個選項\n當前選項: {', '.join(self.items)}"
def draw_lottery(self): if self.items: result = random.choice(self.items) return f"抽籤結果: {result}" return "請先添加一些選項"
def get_items(self): return '\n'.join(self.items)
def update_items(self, items): self.items = [item.strip() for item in items.split('\n') if item.strip()] return f"已更新選項列表,當前共有 {len(self.items)} 個選項"
lottery = LotterySystem()
def add_items_interface(items): return lottery.add_items(items)
def draw_lottery_interface(): return lottery.draw_lottery() self.responses = { "你好": "你好!有什麼我可以幫助你的嗎?", "再見": "再見!希望很快能再見到你。", "你叫什麼名字": "我是你的Gradio聊天機器人。", "今天天氣如何": "我不太清楚,但你可以查一下當地的天氣預報。" } def get_response(self, message): return self.responses.get(message, "抱歉,我不太明白你的意思。")
def get_items_interface(): return lottery.get_items()chatbot = SimpleChatBot()
def update_items_interface(items): return lottery.update_items(items)def respond(message): return chatbot.get_response(message)
with gr.Blocks(title="改進的抽籤系統") as demo: gr.Markdown("# 改進的抽籤系統") with gr.Row(): input_items = gr.Textbox(label="輸入選項(每行一個)", lines=5) add_button = gr.Button("批次添加選項")with gr.Blocks(title="簡單的Gradio聊天機器人") as demo: gr.Markdown("# 簡單的Gradio聊天機器人") output_add = gr.Textbox(label="添加結果") add_button.click(add_items_interface, inputs=input_items, outputs=output_add) chatbot_interface = gr.Chatbot() draw_button = gr.Button("抽籤") output_draw = gr.Textbox(label="抽籤結果") draw_button.click(draw_lottery_interface, outputs=output_draw) with gr.Row(): user_input = gr.Textbox(placeholder="輸入訊息...", label="你的訊息") send_button = gr.Button("發送") show_items_button = gr.Button("顯示/編輯所有選項") edit_items = gr.Textbox(label="當前選項(可編輯)", lines=10) update_button = gr.Button("更新選項") update_result = gr.Textbox(label="更新結果") send_button.click(respond, inputs=user_input, outputs=chatbot_interface) show_items_button.click(get_items_interface, outputs=edit_items) update_button.click(update_items_interface, inputs=edit_items, outputs=update_result)
demo.launch() demo.launch()app.py+21-52