File size: 1,736 Bytes
efadcd3
b08d402
efadcd3
2100448
efadcd3
 
1f90a29
efadcd3
2100448
 
 
 
1f90a29
efadcd3
f9a8a9b
2100448
 
 
 
 
 
 
 
 
 
 
f9a8a9b
6285c3e
2100448
efadcd3
f9a8a9b
2100448
 
 
 
 
 
 
 
 
 
 
f9a8a9b
6285c3e
2100448
efadcd3
f9a8a9b
 
 
 
 
4a87731
 
 
 
90fb62b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import gradio as gr
from bert_explainer import analyze_text, analyze_image

# ⛳ 分析文字
def predict_text(text, mode):
    result = analyze_text(text=text, explain_mode=mode)
    return result['status'], f"{result['confidence']}%", ', '.join(result['suspicious_keywords'])

# ⛳ 分析圖片
def predict_image(file_path, mode):
    with open(file_path, "rb") as f:
        result = analyze_image(f.read(), explain_mode=mode)
    return result['status'], f"{result['confidence']}%", ', '.join(result['suspicious_keywords'])

# 🧩 文字分析介面
text_api = gr.Interface(
    fn=predict_text,
    inputs=[
        gr.Textbox(lines=3, placeholder="請輸入訊息", label="輸入文字"),
        gr.Radio(["cnn", "bert", "both"], value="cnn", label="分析模式")
    ],
    outputs=[
        gr.Textbox(label="判斷結果"),
        gr.Textbox(label="置信度"),
        gr.Textbox(label="可疑詞彙")
    ],
    title="詐騙文字分析",
    flagging_mode="never"
)

# 🧩 圖片分析介面
image_api = gr.Interface(
    fn=predict_image,
    inputs=[
        gr.Image(type="filepath", label="上傳圖片"),
        gr.Radio(["cnn", "bert", "both"], value="cnn", label="分析模式")
    ],
    outputs=[
        gr.Textbox(label="判斷結果"),
        gr.Textbox(label="置信度"),
        gr.Textbox(label="可疑詞彙")
    ],
    title="詐騙圖片分析",
    flagging_mode="never"
)

# ✅ 這是 Hugging Face Space 預設會尋找的變數,會觸發自動啟動 + 文件頁
demo = gr.TabbedInterface(
    interface_list=[text_api, image_api],
    tab_names=["文字模式", "圖片模式"]
)


# ✅ 加上這段,Hugging Face 才能啟動 Space
if __name__ == "__main__":
    demo.queue().launch()