Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 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=
|
| 14 |
-
status
|
| 15 |
-
confidence = f"{result['confidence']}%"
|
| 16 |
-
keywords = ', '.join(result['suspicious_keywords'])
|
| 17 |
-
return status, confidence, keywords
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
inputs=[
|
| 22 |
-
|
| 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 |
-
|
| 34 |
-
|
| 35 |
-
|
| 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 |
-
#
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
)
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
|
|
|
| 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)
|