Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,38 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import base64
|
| 3 |
-
import requests
|
| 4 |
-
import time
|
| 5 |
-
from io import BytesIO
|
| 6 |
-
from PIL import Image
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
def process_image(url):
|
| 10 |
-
try:
|
| 11 |
-
response = requests.get(url, timeout=10)
|
| 12 |
-
img = Image.open(BytesIO(response.content))
|
| 13 |
-
img.thumbnail((300, 300))
|
| 14 |
-
buffered = BytesIO()
|
| 15 |
-
img.save(buffered, format="PNG")
|
| 16 |
-
return f"data:image/png;base64,{base64.b64encode(buffered.getvalue()).decode()}"
|
| 17 |
-
except:
|
| 18 |
-
return None
|
| 19 |
-
|
| 20 |
-
# 嚴格保持桌遊原始資料
|
| 21 |
data = [
|
| 22 |
-
{"蛛絲馬跡": 1, "嫌疑犯": "5冰淇淋師傅", "直接證據": "SAS", "間接證據": "RHS SSS", "嫌疑犯圖片": "https://
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
]
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
print("初始化犯罪資料庫...")
|
| 28 |
-
for item in data:
|
| 29 |
-
item["嫌疑犯圖片"] = process_image(item["嫌疑犯圖片"])
|
| 30 |
-
|
| 31 |
indirect_evidence_store = ""
|
| 32 |
|
| 33 |
def query_data(clue_number):
|
|
@@ -36,24 +41,28 @@ def query_data(clue_number):
|
|
| 36 |
clue_number = int(clue_number)
|
| 37 |
row = next((item for item in data if item['蛛絲馬跡'] == clue_number), None)
|
| 38 |
if row:
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
return (
|
| 41 |
-
f"嫌疑犯: {
|
| 42 |
gr.update(visible=True),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
gr.update(visible=False),
|
| 44 |
-
|
|
|
|
| 45 |
)
|
| 46 |
-
return (
|
| 47 |
-
"未找到相關資料,請檢查蛛絲馬跡號碼是否正確。", # 原始錯誤訊息
|
| 48 |
-
gr.update(visible=False),
|
| 49 |
-
gr.update(visible=False),
|
| 50 |
-
None
|
| 51 |
-
)
|
| 52 |
except ValueError:
|
| 53 |
return (
|
| 54 |
-
"請輸入有效的數字。",
|
| 55 |
-
gr.update(visible=False),
|
| 56 |
gr.update(visible=False),
|
|
|
|
| 57 |
None
|
| 58 |
)
|
| 59 |
|
|
@@ -67,46 +76,31 @@ def reset_fields():
|
|
| 67 |
return (
|
| 68 |
"",
|
| 69 |
gr.update(visible=False),
|
| 70 |
-
gr.update(visible=False),
|
| 71 |
-
gr.update(value=
|
|
|
|
| 72 |
)
|
| 73 |
|
| 74 |
-
#
|
| 75 |
with gr.Blocks() as demo:
|
| 76 |
-
gr.Markdown("<h1
|
| 77 |
-
gr.Markdown("<h3
|
| 78 |
|
| 79 |
with gr.Row():
|
| 80 |
-
input_box = gr.Textbox(
|
| 81 |
-
label="請輸入蛛絲馬跡號碼", # 原始輸入提示
|
| 82 |
-
placeholder="例如: 11",
|
| 83 |
-
lines=1,
|
| 84 |
-
elem_classes="clue-input"
|
| 85 |
-
)
|
| 86 |
query_button = gr.Button("查詢")
|
| 87 |
|
| 88 |
with gr.Row():
|
| 89 |
with gr.Column(scale=1):
|
| 90 |
-
output_box = gr.Textbox(
|
| 91 |
-
label="查詢結果", # 原始結果標題
|
| 92 |
-
interactive=False,
|
| 93 |
-
elem_classes="result-box"
|
| 94 |
-
)
|
| 95 |
with gr.Column(scale=1):
|
| 96 |
-
image_output = gr.Image(
|
| 97 |
-
label="嫌疑犯圖片", # 原始圖片標題
|
| 98 |
-
elem_id="suspect-image",
|
| 99 |
-
show_label=True
|
| 100 |
-
)
|
| 101 |
|
| 102 |
indirect_button = gr.Button("間接證據", visible=False)
|
| 103 |
-
indirect_output = gr.Textbox(
|
| 104 |
-
label="間接證據內容", # 原始間接證據標題
|
| 105 |
-
interactive=False,
|
| 106 |
-
visible=False
|
| 107 |
-
)
|
| 108 |
reset_button = gr.Button("重新查詢")
|
| 109 |
|
|
|
|
| 110 |
query_button.click(
|
| 111 |
query_data,
|
| 112 |
inputs=input_box,
|
|
@@ -114,26 +108,22 @@ with gr.Blocks() as demo:
|
|
| 114 |
)
|
| 115 |
indirect_button.click(
|
| 116 |
show_indirect_evidence,
|
|
|
|
| 117 |
outputs=indirect_output
|
| 118 |
)
|
| 119 |
reset_button.click(
|
| 120 |
reset_fields,
|
| 121 |
-
|
|
|
|
| 122 |
)
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
font-family: "Microsoft JhengHei";
|
| 132 |
-
}
|
| 133 |
-
.result-box textarea {
|
| 134 |
-
font-family: "Microsoft JhengHei";
|
| 135 |
-
background-color: #F0F2F6;
|
| 136 |
-
}
|
| 137 |
-
"""
|
| 138 |
|
|
|
|
| 139 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
# Replace the CSV data loading with in-memory data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
data = [
|
| 5 |
+
{"蛛絲馬跡": 1, "嫌疑犯": "5冰淇淋師傅", "直接證據": "SAS", "間接證據": "RHS SSS", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/5JLMDX.png"},
|
| 6 |
+
{"蛛絲馬跡": 2, "嫌疑犯": "7候選人", "直接證據": "SSS", "間接證據": "SAS ASA AAS", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/WmOolp.png"},
|
| 7 |
+
{"蛛絲馬跡": 3, "嫌疑犯": "8列車長", "直接證據": "SAS", "間接證據": "無", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/Fk6fHM.png"},
|
| 8 |
+
{"蛛絲馬跡": 4, "嫌疑犯": "2農夫", "直接證據": "SSS", "間接證據": "無", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/9z0FQ1.png"},
|
| 9 |
+
{"蛛絲馬跡": 5, "嫌疑犯": "1漁夫", "直接證據": "RHS", "間接證據": "SAS SSS", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/91iAHn.png"},
|
| 10 |
+
{"蛛絲馬跡": 6, "嫌疑犯": "4廚師", "直接證據": "ASA", "間接證據": "無", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/pSbKqG.png"},
|
| 11 |
+
{"蛛絲馬跡": 7, "嫌疑犯": "3女僕", "直接證據": "RHS", "間接證據": "SAS SSS", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/XGoFYq.png"},
|
| 12 |
+
{"蛛絲馬跡": 8, "嫌疑犯": "6棋士", "直接證據": "AAS", "間接證據": "ASA", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/dMZ4I8.png"},
|
| 13 |
+
{"蛛絲馬跡": 9, "嫌疑犯": "10芭蕾舞者", "直接證據": "SSS", "間接證據": "SAS AAS ASA", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/nFohKW.png"},
|
| 14 |
+
{"蛛絲馬跡": 10, "嫌疑犯": "9 DJ", "直接證據": "SAS", "間接證據": "無", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/5fwCDV.png"},
|
| 15 |
+
{"蛛絲馬跡": 11, "嫌疑犯": "16造型師", "直接證據": "RHS", "間接證據": "SSS", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/zd4iXu.png"},
|
| 16 |
+
{"蛛絲馬跡": 12, "嫌疑犯": "12教宗", "直接證據": "SSS", "間接證據": "無", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/3q4ICp.png"},
|
| 17 |
+
{"蛛絲馬跡": 13, "嫌疑犯": "14音樂家", "直接證據": "ASA AAS", "間接證據": "無", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/CIKmMs.png"},
|
| 18 |
+
{"蛛絲馬跡": 14, "嫌疑犯": "13軍人", "直接證據": "ASA", "間接證據": "無", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/BfGyn5.png"},
|
| 19 |
+
{"蛛絲馬跡": 15, "嫌疑犯": "17和尚", "直接證據": "ASA AAS", "間接證據": "無", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/prwQVq.png"},
|
| 20 |
+
{"蛛絲馬跡": 16, "嫌疑犯": "18歌手", "直接證據": "AAS", "間接證據": "ASA", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/mtBa2p.png"},
|
| 21 |
+
{"蛛絲馬跡": 17, "嫌疑犯": "11醫生", "直接證據": "SAS", "間接證據": "無", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/T8kgnq.png"},
|
| 22 |
+
{"蛛絲馬跡": 18, "嫌疑犯": "15壽司師傅", "直接證據": "SAS", "間接證據": "AAS ASA SSS", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/fWMhki.png"},
|
| 23 |
+
{"蛛絲馬跡": 19, "嫌疑犯": "21郵差", "直接證據": "RHS", "間接證據": "SAS SSS", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/kUuriv.png"},
|
| 24 |
+
{"蛛絲馬跡": 20, "嫌疑犯": "19籃球員", "直接證據": "RHS", "間接證據": "SSS SAS", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/uhman9.png"},
|
| 25 |
+
{"蛛絲馬跡": 21, "嫌疑犯": "22法官", "直接證據": "ASA AAS", "間接證據": "無", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/R1ZipX.png"},
|
| 26 |
+
{"蛛絲馬跡": 22, "嫌疑犯": "20漫畫家", "直接證據": "ASA AAS", "間接證據": "無", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/yaZhP7.png"},
|
| 27 |
+
{"蛛絲馬跡": 23, "嫌疑犯": "25殺手", "直接證據": "ASA AAS", "間接證據": "無", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/5dVfQW.png"},
|
| 28 |
+
{"蛛絲馬跡": 24, "嫌疑犯": "28科學家", "直接證據": "ASA AAS", "間接證據": "無", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/Vj8MfX.png"},
|
| 29 |
+
{"蛛絲馬跡": 25, "嫌疑犯": "27太空人", "直接證據": "SSS", "間接證據": "ASA AAS RHS", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/hdso0N.png"},
|
| 30 |
+
{"蛛絲馬跡": 26, "嫌疑犯": "23酒保", "直接證據": "AAS", "間接證據": "ASA", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/UFJMlD.png"},
|
| 31 |
+
{"蛛絲馬跡": 27, "嫌疑犯": "24魔術師", "直接證據": "ASA", "間接證據": "AAS", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/jEAikS.png"},
|
| 32 |
+
{"蛛絲馬跡": 28, "嫌疑犯": "26保全", "直接證據": "ASA", "間接證據": "AAS", "嫌疑犯圖片": "https://upload.cc/i1/2024/12/24/zh7qco.png"}
|
| 33 |
]
|
| 34 |
|
| 35 |
+
# To store the indirect evidence dynamically
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
indirect_evidence_store = ""
|
| 37 |
|
| 38 |
def query_data(clue_number):
|
|
|
|
| 41 |
clue_number = int(clue_number)
|
| 42 |
row = next((item for item in data if item['蛛絲馬跡'] == clue_number), None)
|
| 43 |
if row:
|
| 44 |
+
suspect = row['嫌疑犯']
|
| 45 |
+
direct_evidence = row['直接證據']
|
| 46 |
+
indirect_evidence_store = row['間接證據'] if row['間接證據'] else "無間接證據"
|
| 47 |
+
image_url = row['嫌疑犯圖片'] if row['嫌疑犯圖片'] else None
|
| 48 |
return (
|
| 49 |
+
f"嫌疑犯: {suspect}\n直接證據: {direct_evidence}",
|
| 50 |
gr.update(visible=True),
|
| 51 |
+
gr.update(value="", visible=False),
|
| 52 |
+
image_url if image_url else None
|
| 53 |
+
)
|
| 54 |
+
else:
|
| 55 |
+
return (
|
| 56 |
+
"未找到相關資料,請檢查蛛絲馬跡號碼是否正確。",
|
| 57 |
gr.update(visible=False),
|
| 58 |
+
gr.update(value="", visible=False),
|
| 59 |
+
None
|
| 60 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
except ValueError:
|
| 62 |
return (
|
| 63 |
+
"請輸入有效的數字。",
|
|
|
|
| 64 |
gr.update(visible=False),
|
| 65 |
+
gr.update(value="", visible=False),
|
| 66 |
None
|
| 67 |
)
|
| 68 |
|
|
|
|
| 76 |
return (
|
| 77 |
"",
|
| 78 |
gr.update(visible=False),
|
| 79 |
+
gr.update(value="", visible=False),
|
| 80 |
+
gr.update(value="", visible=True),
|
| 81 |
+
None
|
| 82 |
)
|
| 83 |
|
| 84 |
+
# Create the Gradio interface
|
| 85 |
with gr.Blocks() as demo:
|
| 86 |
+
gr.Markdown("<h1>嫌疑犯ABC的現身</h1>")
|
| 87 |
+
gr.Markdown("<h3>法官系統</h3>")
|
| 88 |
|
| 89 |
with gr.Row():
|
| 90 |
+
input_box = gr.Textbox(label="請輸入蛛絲馬跡號碼", placeholder="例如: 11", lines=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
query_button = gr.Button("查詢")
|
| 92 |
|
| 93 |
with gr.Row():
|
| 94 |
with gr.Column(scale=1):
|
| 95 |
+
output_box = gr.Textbox(label="查詢結果", interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
with gr.Column(scale=1):
|
| 97 |
+
image_output = gr.Image(label="嫌疑犯圖片", visible=True, elem_id="suspect-image")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
indirect_button = gr.Button("間接證據", visible=False)
|
| 100 |
+
indirect_output = gr.Textbox(label="間接證據內容", interactive=False, visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
reset_button = gr.Button("重新查詢")
|
| 102 |
|
| 103 |
+
# Bind actions to functions
|
| 104 |
query_button.click(
|
| 105 |
query_data,
|
| 106 |
inputs=input_box,
|
|
|
|
| 108 |
)
|
| 109 |
indirect_button.click(
|
| 110 |
show_indirect_evidence,
|
| 111 |
+
inputs=None,
|
| 112 |
outputs=indirect_output
|
| 113 |
)
|
| 114 |
reset_button.click(
|
| 115 |
reset_fields,
|
| 116 |
+
inputs=None,
|
| 117 |
+
outputs=[input_box, indirect_button, indirect_output, output_box, image_output]
|
| 118 |
)
|
| 119 |
|
| 120 |
+
# Add inline CSS to adjust image size
|
| 121 |
+
demo.css = """
|
| 122 |
+
#suspect-image img {
|
| 123 |
+
max-width: 150px;
|
| 124 |
+
max-height: 150px;
|
| 125 |
+
}
|
| 126 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
+
# Launch the Gradio app
|
| 129 |
demo.launch()
|