Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,6 @@ import shutil
|
|
| 3 |
import zipfile
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
-
import spaces
|
| 7 |
import torch
|
| 8 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 9 |
|
|
@@ -11,6 +10,7 @@ from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
|
| 11 |
ZIP_PATH = "model.zip"
|
| 12 |
EXTRACT_DIR = "./model"
|
| 13 |
MODEL_DIR = os.path.join(EXTRACT_DIR, "final_model")
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
def prepare_model_files():
|
|
@@ -63,6 +63,7 @@ model = AutoModelForSequenceClassification.from_pretrained(
|
|
| 63 |
local_files_only=True
|
| 64 |
)
|
| 65 |
|
|
|
|
| 66 |
model.eval()
|
| 67 |
|
| 68 |
print("模型載入成功。", flush=True)
|
|
@@ -74,19 +75,12 @@ LABEL_MAP = {
|
|
| 74 |
}
|
| 75 |
|
| 76 |
|
| 77 |
-
@spaces.GPU(duration=60)
|
| 78 |
def predict(text: str) -> str:
|
| 79 |
text = text.strip()
|
| 80 |
|
| 81 |
if not text:
|
| 82 |
return "請先輸入要判斷的訊息。"
|
| 83 |
|
| 84 |
-
device = torch.device(
|
| 85 |
-
"cuda" if torch.cuda.is_available() else "cpu"
|
| 86 |
-
)
|
| 87 |
-
|
| 88 |
-
model.to(device)
|
| 89 |
-
|
| 90 |
inputs = tokenizer(
|
| 91 |
text,
|
| 92 |
return_tensors="pt",
|
|
@@ -96,7 +90,7 @@ def predict(text: str) -> str:
|
|
| 96 |
)
|
| 97 |
|
| 98 |
inputs = {
|
| 99 |
-
key: value.to(
|
| 100 |
for key, value in inputs.items()
|
| 101 |
}
|
| 102 |
|
|
@@ -104,7 +98,7 @@ def predict(text: str) -> str:
|
|
| 104 |
outputs = model(**inputs)
|
| 105 |
probabilities = torch.softmax(outputs.logits, dim=-1)[0]
|
| 106 |
|
| 107 |
-
probabilities = probabilities.
|
| 108 |
|
| 109 |
pred_id = int(torch.argmax(probabilities).item())
|
| 110 |
confidence = float(probabilities[pred_id].item() * 100)
|
|
@@ -130,7 +124,7 @@ def predict(text: str) -> str:
|
|
| 130 |
|
| 131 |
return (
|
| 132 |
f"🔍 判斷結果:{pred_label}\n\n"
|
| 133 |
-
f"📊 信心度:{confidence:.2f}%\n\n"
|
| 134 |
f"🚨 詐騙機率:{scam_probability:.2f}%\n"
|
| 135 |
f"✅ 真實機率:{real_probability:.2f}%\n\n"
|
| 136 |
f"⚠️ 提醒:{advice}"
|
|
@@ -151,6 +145,7 @@ demo = gr.Interface(
|
|
| 151 |
title="LINE Bot BERT 詐騙/真實訊息辨識 API",
|
| 152 |
description=(
|
| 153 |
"輸入文字後,系統會判斷內容較接近詐騙訊息或真實訊息。"
|
|
|
|
| 154 |
),
|
| 155 |
api_name="predict"
|
| 156 |
)
|
|
|
|
| 3 |
import zipfile
|
| 4 |
|
| 5 |
import gradio as gr
|
|
|
|
| 6 |
import torch
|
| 7 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 8 |
|
|
|
|
| 10 |
ZIP_PATH = "model.zip"
|
| 11 |
EXTRACT_DIR = "./model"
|
| 12 |
MODEL_DIR = os.path.join(EXTRACT_DIR, "final_model")
|
| 13 |
+
DEVICE = torch.device("cpu")
|
| 14 |
|
| 15 |
|
| 16 |
def prepare_model_files():
|
|
|
|
| 63 |
local_files_only=True
|
| 64 |
)
|
| 65 |
|
| 66 |
+
model.to(DEVICE)
|
| 67 |
model.eval()
|
| 68 |
|
| 69 |
print("模型載入成功。", flush=True)
|
|
|
|
| 75 |
}
|
| 76 |
|
| 77 |
|
|
|
|
| 78 |
def predict(text: str) -> str:
|
| 79 |
text = text.strip()
|
| 80 |
|
| 81 |
if not text:
|
| 82 |
return "請先輸入要判斷的訊息。"
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
inputs = tokenizer(
|
| 85 |
text,
|
| 86 |
return_tensors="pt",
|
|
|
|
| 90 |
)
|
| 91 |
|
| 92 |
inputs = {
|
| 93 |
+
key: value.to(DEVICE)
|
| 94 |
for key, value in inputs.items()
|
| 95 |
}
|
| 96 |
|
|
|
|
| 98 |
outputs = model(**inputs)
|
| 99 |
probabilities = torch.softmax(outputs.logits, dim=-1)[0]
|
| 100 |
|
| 101 |
+
probabilities = probabilities.cpu()
|
| 102 |
|
| 103 |
pred_id = int(torch.argmax(probabilities).item())
|
| 104 |
confidence = float(probabilities[pred_id].item() * 100)
|
|
|
|
| 124 |
|
| 125 |
return (
|
| 126 |
f"🔍 判斷結果:{pred_label}\n\n"
|
| 127 |
+
f"📊 模型信心度:{confidence:.2f}%\n\n"
|
| 128 |
f"🚨 詐騙機率:{scam_probability:.2f}%\n"
|
| 129 |
f"✅ 真實機率:{real_probability:.2f}%\n\n"
|
| 130 |
f"⚠️ 提醒:{advice}"
|
|
|
|
| 145 |
title="LINE Bot BERT 詐騙/真實訊息辨識 API",
|
| 146 |
description=(
|
| 147 |
"輸入文字後,系統會判斷內容較接近詐騙訊息或真實訊息。"
|
| 148 |
+
"建議輸入較完整的內容,文字過短可能降低辨識效果。"
|
| 149 |
),
|
| 150 |
api_name="predict"
|
| 151 |
)
|