Spaces:
Build error
Build error
| import gradio as gr | |
| # Replace the CSV data loading with in-memory data | |
| data = [ | |
| {"蛛絲馬跡": 1, "嫌疑犯": "5冰淇淋師傅", "直接證據": "SAS", "間接證據": "RHS SSS", "嫌疑犯圖片": "./5冰淇淋師傅.png"}, | |
| {"蛛絲馬跡": 2, "嫌疑犯": "7候選人", "直接證據": "SSS", "間接證據": "SAS ASA AAS", "嫌疑犯圖片": "./7候選人.png"}, | |
| {"蛛絲馬跡": 3, "嫌疑犯": "8列車長", "直接證據": "SAS", "間接證據": "無", "嫌疑犯圖片": "./8列車長.png"}, | |
| {"蛛絲馬跡": 4, "嫌疑犯": "2農夫", "直接證據": "SSS", "間接證據": "無", "嫌疑犯圖片": "2農夫.png"}, | |
| {"蛛絲馬跡": 5, "嫌疑犯": "1漁夫", "直接證據": "RHS", "間接證據": "SAS SSS", "嫌疑犯圖片": "1漁夫.png"}, | |
| {"蛛絲馬跡": 6, "嫌疑犯": "4廚師", "直接證據": "ASA", "間接證據": "無", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/pSbKqG.png"}, | |
| {"蛛絲馬跡": 7, "嫌疑犯": "3女僕", "直接證據": "RHS", "間接證據": "SAS SSS", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/XGoFYq.png"}, | |
| {"蛛絲馬跡": 8, "嫌疑犯": "6棋士", "直接證據": "AAS", "間接證據": "ASA", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/dMZ4I8.png"}, | |
| {"蛛絲馬跡": 9, "嫌疑犯": "10芭蕾舞者", "直接證據": "SSS", "間接證據": "SAS AAS ASA", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/nFohKW.png"}, | |
| {"蛛絲馬跡": 10, "嫌疑犯": "9 DJ", "直接證據": "SAS", "間接證據": "無", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/5fwCDV.png"}, | |
| {"蛛絲馬跡": 11, "嫌疑犯": "16造型師", "直接證據": "RHS", "間接證據": "SSS", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/zd4iXu.png"}, | |
| {"蛛絲馬跡": 12, "嫌疑犯": "12教宗", "直接證據": "SSS", "間接證據": "無", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/3q4ICp.png"}, | |
| {"蛛絲馬跡": 13, "嫌疑犯": "14音樂家", "直接證據": "ASA AAS", "間接證據": "無", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/CIKmMs.png"}, | |
| {"蛛絲馬跡": 14, "嫌疑犯": "13軍人", "直接證據": "ASA", "間接證據": "無", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/BfGyn5.png"}, | |
| {"蛛絲馬跡": 15, "嫌疑犯": "17和尚", "直接證據": "ASA AAS", "間接證據": "無", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/prwQVq.png"}, | |
| {"蛛絲馬跡": 16, "嫌疑犯": "18歌手", "直接證據": "AAS", "間接證據": "ASA", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/mtBa2p.png"}, | |
| {"蛛絲馬跡": 17, "嫌疑犯": "11醫生", "直接證據": "SAS", "間接證據": "無", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/T8kgnq.png"}, | |
| {"蛛絲馬跡": 18, "嫌疑犯": "15壽司師傅", "直接證據": "SAS", "間接證據": "AAS ASA SSS", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/fWMhki.png"}, | |
| {"蛛絲馬跡": 19, "嫌疑犯": "21郵差", "直接證據": "RHS", "間接證據": "SAS SSS", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/kUuriv.png"}, | |
| {"蛛絲馬跡": 20, "嫌疑犯": "19籃球員", "直接證據": "RHS", "間接證據": "SSS SAS", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/uhman9.png"}, | |
| {"蛛絲馬跡": 21, "嫌疑犯": "22法官", "直接證據": "ASA AAS", "間接證據": "無", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/R1ZipX.png"}, | |
| {"蛛絲馬跡": 22, "嫌疑犯": "20漫畫家", "直接證據": "ASA AAS", "間接證據": "無", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/yaZhP7.png"}, | |
| {"蛛絲馬跡": 23, "嫌疑犯": "25殺手", "直接證據": "ASA AAS", "間接證據": "無", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/5dVfQW.png"}, | |
| {"蛛絲馬跡": 24, "嫌疑犯": "28科學家", "直接證據": "ASA AAS", "間接證據": "無", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/Vj8MfX.png"}, | |
| {"蛛絲馬跡": 25, "嫌疑犯": "27太空人", "直接證據": "SSS", "間接證據": "ASA AAS RHS", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/hdso0N.png"}, | |
| {"蛛絲馬跡": 26, "嫌疑犯": "23酒保", "直接證據": "AAS", "間接證據": "ASA", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/UFJMlD.png"}, | |
| {"蛛絲馬跡": 27, "嫌疑犯": "24魔術師", "直接證據": "ASA", "間接證據": "AAS", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/jEAikS.png"}, | |
| {"蛛絲馬跡": 28, "嫌疑犯": "26保全", "直接證據": "ASA", "間接證據": "AAS", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/zh7qco.png"} | |
| ] | |
| # To store the indirect evidence dynamically | |
| indirect_evidence_store = "" | |
| def query_data(clue_number): | |
| global indirect_evidence_store | |
| try: | |
| clue_number = int(clue_number) | |
| row = next((item for item in data if item['蛛絲馬跡'] == clue_number), None) | |
| if row: | |
| suspect = row['嫌疑犯'] | |
| direct_evidence = row['直接證據'] | |
| indirect_evidence_store = row['間接證據'] if row['間接證據'] else "無間接證據" | |
| image_url = row['嫌疑犯圖片'] if row['嫌疑犯圖片'] else None | |
| return ( | |
| f"嫌疑犯: {suspect}\n直接證據: {direct_evidence}", | |
| gr.update(visible=True), | |
| gr.update(value="", visible=False), | |
| image_url if image_url else None | |
| ) | |
| else: | |
| return ( | |
| "未找到相關資料,請檢查蛛絲馬跡號碼是否正確。", | |
| gr.update(visible=False), | |
| gr.update(value="", visible=False), | |
| None | |
| ) | |
| except ValueError: | |
| return ( | |
| "請輸入有效的數字。", | |
| gr.update(visible=False), | |
| gr.update(value="", visible=False), | |
| None | |
| ) | |
| def show_indirect_evidence(): | |
| global indirect_evidence_store | |
| return gr.update(value=indirect_evidence_store, visible=True) | |
| def reset_fields(): | |
| global indirect_evidence_store | |
| indirect_evidence_store = "" | |
| return ( | |
| "", | |
| gr.update(visible=False), | |
| gr.update(value="", visible=False), | |
| gr.update(value="", visible=True), | |
| None | |
| ) | |
| # Create the Gradio interface | |
| with gr.Blocks() as demo: | |
| gr.Markdown("<h1>嫌疑犯ABC的現身</h1>") | |
| gr.Markdown("<h3>法官系統</h3>") | |
| with gr.Row(): | |
| input_box = gr.Textbox(label="請輸入蛛絲馬跡號碼", placeholder="例如: 11", lines=1) | |
| query_button = gr.Button("查詢") | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| output_box = gr.Textbox(label="查詢結果", interactive=False) | |
| with gr.Column(scale=1): | |
| image_output = gr.Image(label="嫌疑犯圖片", visible=True, elem_id="suspect-image") | |
| indirect_button = gr.Button("間接證據", visible=False) | |
| indirect_output = gr.Textbox(label="間接證據內容", interactive=False, visible=False) | |
| reset_button = gr.Button("重新查詢") | |
| # Bind actions to functions | |
| query_button.click( | |
| query_data, | |
| inputs=input_box, | |
| outputs=[output_box, indirect_button, indirect_output, image_output] | |
| ) | |
| indirect_button.click( | |
| show_indirect_evidence, | |
| inputs=None, | |
| outputs=indirect_output | |
| ) | |
| reset_button.click( | |
| reset_fields, | |
| inputs=None, | |
| outputs=[input_box, indirect_button, indirect_output, output_box, image_output] | |
| ) | |
| # Add inline CSS to adjust image size | |
| demo.css = """ | |
| #suspect-image img { | |
| max-width: 150px; | |
| max-height: 150px; | |
| } | |
| """ | |
| # Launch the Gradio app | |
| demo.launch() |