Bennie12 commited on
Commit
7511b3c
·
verified ·
1 Parent(s): 90fb62b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -43
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['status'], f"{result['confidence']}%", ', '.join(result['suspicious_keywords'])
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['status'], f"{result['confidence']}%", ', '.join(result['suspicious_keywords'])
14
 
15
- # 🧩 文字分析介面
16
- text_api = gr.Interface(
17
- fn=predict_text,
18
- inputs=[
19
- gr.Textbox(lines=3, placeholder="請輸入訊息", label="輸入文字"),
20
- gr.Radio(["cnn", "bert", "both"], value="cnn", label="分析模式")
21
- ],
22
- outputs=[
23
- gr.Textbox(label="判斷結果"),
24
- gr.Textbox(label="置信度"),
25
- gr.Textbox(label="可疑詞彙")
26
- ],
27
- title="詐騙文字分析",
28
- flagging_mode="never"
29
- )
30
 
31
- # 🧩 圖片分析介面
32
- image_api = gr.Interface(
33
- fn=predict_image,
34
- inputs=[
35
- gr.Image(type="filepath", label="上傳圖片"),
36
- gr.Radio(["cnn", "bert", "both"], value="cnn", label="分析模式")
37
- ],
38
- outputs=[
39
- gr.Textbox(label="判斷結果"),
40
- gr.Textbox(label="置信度"),
41
- gr.Textbox(label="可疑詞彙")
42
- ],
43
- title="詐騙圖片分析",
44
- flagging_mode="never"
45
- )
46
 
47
- # ✅ 這是 Hugging Face Space 預設會尋找的變數,會觸發自動啟動 + 文件頁
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()