Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,23 +6,15 @@ from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
| 6 |
|
| 7 |
MODEL_DIR = "./model"
|
| 8 |
|
| 9 |
-
# 如果 model 資料夾不存在,就解壓縮 model.zip
|
| 10 |
if not os.path.exists(MODEL_DIR):
|
| 11 |
with zipfile.ZipFile("model.zip", "r") as zip_ref:
|
| 12 |
zip_ref.extractall(MODEL_DIR)
|
| 13 |
|
| 14 |
-
# 載入 tokenizer 和模型
|
| 15 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR)
|
| 16 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_DIR)
|
| 17 |
model.eval()
|
| 18 |
|
| 19 |
def predict(text):
|
| 20 |
-
romance_words = ["網戀", "交友", "寶貝", "老公", "老婆", "男友", "女友", "愛我", "喜歡我", "感情"]
|
| 21 |
-
money_words = ["匯款", "轉帳", "借錢", "還我", "投資", "入金", "儲值"]
|
| 22 |
-
|
| 23 |
-
if any(w in text for w in romance_words) and any(w in text for w in money_words):
|
| 24 |
-
return "愛情(規則判斷:感情關係 + 金錢要求)"
|
| 25 |
-
|
| 26 |
inputs = tokenizer(
|
| 27 |
text,
|
| 28 |
return_tensors="pt",
|
|
@@ -39,4 +31,13 @@ def predict(text):
|
|
| 39 |
pred_label = model.config.id2label[pred_id]
|
| 40 |
confidence = probs[pred_id].item() * 100
|
| 41 |
|
| 42 |
-
return f"{pred_label}(信心度:{confidence:.2f}%)"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
MODEL_DIR = "./model"
|
| 8 |
|
|
|
|
| 9 |
if not os.path.exists(MODEL_DIR):
|
| 10 |
with zipfile.ZipFile("model.zip", "r") as zip_ref:
|
| 11 |
zip_ref.extractall(MODEL_DIR)
|
| 12 |
|
|
|
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR)
|
| 14 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_DIR)
|
| 15 |
model.eval()
|
| 16 |
|
| 17 |
def predict(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
inputs = tokenizer(
|
| 19 |
text,
|
| 20 |
return_tensors="pt",
|
|
|
|
| 31 |
pred_label = model.config.id2label[pred_id]
|
| 32 |
confidence = probs[pred_id].item() * 100
|
| 33 |
|
| 34 |
+
return f"{pred_label}(信心度:{confidence:.2f}%)"
|
| 35 |
+
|
| 36 |
+
demo = gr.Interface(
|
| 37 |
+
fn=predict,
|
| 38 |
+
inputs=gr.Textbox(lines=5, placeholder="請輸入可疑文字..."),
|
| 39 |
+
outputs="text",
|
| 40 |
+
title="LINE Bot BERT 詐騙分類 API"
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|