Bennie12 commited on
Commit
efadcd3
·
verified ·
1 Parent(s): 953dd4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -25
app.py CHANGED
@@ -1,25 +1,38 @@
1
- import gradio as gr
2
- from bert_explainer import analyze_text
3
-
4
- def predict_fn(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
- iface =gr.Interface(
12
- fn=predict_fn,
13
- inputs = [
14
- gr.TextArea(label="輸入訊息"),
15
- gr.Radio(choices=["cnn", "bert", "both"], label="分析模式", value="cnn")
16
- ],
17
- outputs = [
18
- gr.Textbox(label = "判斷結果"),
19
- gr.Textbox(label = "可疑分數"),
20
- gr.Textbox(label = "可疑詞彙")
21
- ],
22
- title= "預判詐騙訊息",
23
- description="輸入訊息,AI 將判定是否為詐騙並標記可疑詞 "
24
- )
25
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ with gr.Blocks() as demo:
20
+ gr.Markdown("## 📩 預測詐騙訊息 (文字 + 圖片)")
21
+
22
+ with gr.Tab("文字預測"):
23
+ text_inputs = gr.TextArea(label="輸入訊息"),
24
+ mode_radio_1 = gr.Radio(choices=["cnn", "bert", "both"], label="分析模式", value="cnn")
25
+ text_buotton = gr.Button("開始分析")
26
+ text_result =[gr.Textbox(label = "判斷結果"),gr.Textbox(label = "可疑分數"),gr.Textbox(label = "可疑詞彙")]
27
+ text_buotton.click(fn=predict_text, inputs=[text_inputs, mode_radio_1], outputs=text_result)
28
+
29
+ with gr.Tab("圖片預測"):
30
+ image_inputs = gr.TextArea(label="上傳圖片"),
31
+ mode_radio_2 = gr.Radio(choices=["cnn", "bert", "both"], label="分析模式", value="cnn")
32
+ image_buotton = gr.Button("開始分析")
33
+ image_result =[gr.Textbox(label = "判斷結果"),gr.Textbox(label = "可疑分數"),gr.Textbox(label = "可疑詞彙")]
34
+ image_buotton.click(fn=predict_image, inputs=[image_inputs, mode_radio_2], outputs=image_result)
35
+
36
+
37
+
38
+ demo.launch()