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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -42
app.py CHANGED
@@ -1,53 +1,31 @@
1
  import gradio as gr
2
  from bert_explainer import analyze_text, analyze_image
 
 
3
 
 
4
  def predict_text(text, mode):
5
  result = analyze_text(text=text, explain_mode=mode)
6
- status = result['status']
7
- confidence = f"{result['confidence']}%"
8
- keywords = ', '.join(result['suspicious_keywords'])
9
- return status, confidence, keywords
10
-
11
 
12
  def predict_image(file, mode):
13
- result = analyze_image(file.read(), explain_mode= mode)
14
- status = result['status']
15
- confidence = f"{result['confidence']}%"
16
- keywords = ', '.join(result['suspicious_keywords'])
17
- return status, confidence, keywords
18
 
19
- text_interface = gr.Interface(
20
- fn=predict_text,
21
- inputs=[
22
- gr.Textbox(label="輸入訊息"),
23
- gr.Radio(choices=["cnn", "bert", "both"], label="分析模式", value="cnn")
24
- ],
25
- outputs=[
26
- gr.Textbox(label="判斷結果"),
27
- gr.Textbox(label="可疑分數"),
28
- gr.Textbox(label="可疑詞彙")
29
- ],
30
- allow_flagging="never"
31
- )
32
 
33
- image_interface = gr.Interface(
34
- fn=predict_image,
35
- inputs=[
36
- gr.Image(type="filepath", label="上傳圖片"),
37
- gr.Radio(choices=["cnn", "bert", "both"], label="分析模式", value="cnn")
38
- ],
39
- outputs=[
40
- gr.Textbox(label="判斷結果"),
41
- gr.Textbox(label="可疑分數"),
42
- gr.Textbox(label="可疑詞彙")
43
- ],
44
- allow_flagging="never"
45
- )
46
 
47
- # 整合成 Tab 介面
48
- demo = gr.TabbedInterface(
49
- interface_list=[text_interface, image_interface],
50
- tab_names=["文字預測", "圖片預測"]
51
- )
52
 
53
- demo.launch()
 
 
 
1
  import gradio as gr
2
  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)