Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,36 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from bert_explainer import analyze_text, analyze_image
|
| 3 |
|
| 4 |
-
#
|
| 5 |
def predict_text(text, mode):
|
| 6 |
result = analyze_text(text=text, explain_mode=mode)
|
| 7 |
-
return result[
|
| 8 |
|
| 9 |
-
# ⛳ 分析圖片
|
| 10 |
def predict_image(file_path, mode):
|
| 11 |
with open(file_path, "rb") as f:
|
| 12 |
result = analyze_image(f.read(), explain_mode=mode)
|
| 13 |
-
return result[
|
| 14 |
|
| 15 |
-
# 🧩
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
gr.
|
| 20 |
-
gr.
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
gr.Textbox(label="
|
| 24 |
-
|
| 25 |
-
gr.Textbox(label="可疑詞彙")
|
| 26 |
-
],
|
| 27 |
-
title="詐騙文字分析",
|
| 28 |
-
flagging_mode="never"
|
| 29 |
-
)
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
gr.
|
| 36 |
-
gr.
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
gr.Textbox(label="判斷結果"),
|
| 40 |
-
gr.Textbox(label="置信度"),
|
| 41 |
-
gr.Textbox(label="可疑詞彙")
|
| 42 |
-
],
|
| 43 |
-
title="詐騙圖片分析",
|
| 44 |
-
flagging_mode="never"
|
| 45 |
-
)
|
| 46 |
|
| 47 |
-
# ✅
|
| 48 |
-
demo = gr.TabbedInterface(
|
| 49 |
-
interface_list=[text_api, image_api],
|
| 50 |
-
tab_names=["文字模式", "圖片模式"]
|
| 51 |
-
)
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
# ✅ 加上這段,Hugging Face 才能啟動 Space
|
| 55 |
if __name__ == "__main__":
|
| 56 |
-
demo.queue().launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from bert_explainer import analyze_text, analyze_image
|
| 3 |
|
| 4 |
+
# 🧠 預測函式:供 UI 和 API 共用
|
| 5 |
def predict_text(text, mode):
|
| 6 |
result = analyze_text(text=text, explain_mode=mode)
|
| 7 |
+
return result["status"], f"{result['confidence']}%", ", ".join(result["suspicious_keywords"])
|
| 8 |
|
|
|
|
| 9 |
def predict_image(file_path, mode):
|
| 10 |
with open(file_path, "rb") as f:
|
| 11 |
result = analyze_image(f.read(), explain_mode=mode)
|
| 12 |
+
return result["status"], f"{result['confidence']}%", ", ".join(result["suspicious_keywords"])
|
| 13 |
|
| 14 |
+
# 🧩 Gradio UI:兩個 Tab(文字 & 圖片)
|
| 15 |
+
with gr.Blocks() as demo:
|
| 16 |
+
with gr.Tab("文字模式"):
|
| 17 |
+
text_input = gr.Textbox(lines=3, label="輸入文字")
|
| 18 |
+
text_mode = gr.Radio(["cnn", "bert", "both"], value="cnn", label="分析模式")
|
| 19 |
+
text_btn = gr.Button("提交")
|
| 20 |
+
text_output1 = gr.Textbox(label="判斷結果")
|
| 21 |
+
text_output2 = gr.Textbox(label="置信度")
|
| 22 |
+
text_output3 = gr.Textbox(label="可疑詞彙")
|
| 23 |
+
text_btn.click(fn=predict_text, inputs=[text_input, text_mode], outputs=[text_output1, text_output2, text_output3])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
with gr.Tab("圖片模式"):
|
| 26 |
+
image_input = gr.Image(type="filepath", label="上傳圖片")
|
| 27 |
+
image_mode = gr.Radio(["cnn", "bert", "both"], value="cnn", label="分析模式")
|
| 28 |
+
image_btn = gr.Button("提交")
|
| 29 |
+
image_output1 = gr.Textbox(label="判斷結果")
|
| 30 |
+
image_output2 = gr.Textbox(label="置信度")
|
| 31 |
+
image_output3 = gr.Textbox(label="可疑詞彙")
|
| 32 |
+
image_btn.click(fn=predict_image, inputs=[image_input, image_mode], outputs=[image_output1, image_output2, image_output3])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# ✅ 讓 Gradio 支援 API 路由 + 互動功能
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
if __name__ == "__main__":
|
| 36 |
+
demo.queue().launch()
|