Bennie12 commited on
Commit
2100448
·
verified ·
1 Parent(s): 1f90a29

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -14
app.py CHANGED
@@ -3,29 +3,51 @@ from bert_explainer import analyze_text, analyze_image
3
  import fastapi
4
  import uvicorn
5
 
6
- # 定義函式
7
  def predict_text(text, mode):
8
  result = analyze_text(text=text, explain_mode=mode)
9
  return result['status'], f"{result['confidence']}%", ', '.join(result['suspicious_keywords'])
10
 
11
- def predict_image(file, mode):
12
- result = analyze_image(file.read(), explain_mode=mode)
 
 
13
  return result['status'], f"{result['confidence']}%", ', '.join(result['suspicious_keywords'])
14
 
15
- # 定義 Gradio 接口
16
- text_api = gr.Interface(fn=predict_text,
17
- inputs=[gr.Textbox(), gr.Radio(["cnn", "bert", "both"], value="cnn")],
18
- outputs=["text", "text", "text"])
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- image_api = gr.Interface(fn=predict_image,
21
- inputs=[gr.Image(type="filepath"), gr.Radio(["cnn", "bert", "both"], value="cnn")],
22
- outputs=["text", "text", "text"])
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
- # FastAPI 設定(取代 launch)
25
  app = fastapi.FastAPI()
26
  app = gr.mount_gradio_app(app, text_api, path="/run/predict_text")
27
  app = gr.mount_gradio_app(app, image_api, path="/run/predict_image")
28
 
29
- # 本地測試時使用
30
- if __name__ == "__main__":
31
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
3
  import fastapi
4
  import uvicorn
5
 
6
+ # ⛳ 分析文字
7
  def predict_text(text, mode):
8
  result = analyze_text(text=text, explain_mode=mode)
9
  return result['status'], f"{result['confidence']}%", ', '.join(result['suspicious_keywords'])
10
 
11
+ # 分析圖片
12
+ def predict_image(file_path, mode):
13
+ with open(file_path, "rb") as f:
14
+ result = analyze_image(f.read(), explain_mode=mode)
15
  return result['status'], f"{result['confidence']}%", ', '.join(result['suspicious_keywords'])
16
 
17
+ # 🚀 建立文字 API
18
+ text_api = gr.Interface(
19
+ fn=predict_text,
20
+ inputs=[
21
+ gr.Textbox(lines=3, placeholder="請輸入訊息", label="輸入文字"),
22
+ gr.Radio(["cnn", "bert", "both"], value="cnn", label="分析模式")
23
+ ],
24
+ outputs=[
25
+ gr.Textbox(label="判斷結果"),
26
+ gr.Textbox(label="置信度"),
27
+ gr.Textbox(label="可疑詞彙")
28
+ ],
29
+ title="詐騙文字分析 API",
30
+ flagging_mode="never"
31
+ )
32
 
33
+ # 🚀 建立圖片 API
34
+ image_api = gr.Interface(
35
+ fn=predict_image,
36
+ inputs=[
37
+ gr.Image(type="filepath", label="上傳圖片"),
38
+ gr.Radio(["cnn", "bert", "both"], value="cnn", label="分析模式")
39
+ ],
40
+ outputs=[
41
+ gr.Textbox(label="判斷結果"),
42
+ gr.Textbox(label="置信度"),
43
+ gr.Textbox(label="可疑詞彙")
44
+ ],
45
+ title="詐騙圖片分析 API",
46
+ flagging_mode="never"
47
+ )
48
 
49
+ # 🌐 FastAPI
50
  app = fastapi.FastAPI()
51
  app = gr.mount_gradio_app(app, text_api, path="/run/predict_text")
52
  app = gr.mount_gradio_app(app, image_api, path="/run/predict_image")
53