Bennie12's picture
Update app.py
90fb62b verified
raw
history blame
1.74 kB
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()