Spaces:
Runtime error
Runtime error
requirements.txt
Browse filestransformers
torch
gradio
app.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# 載入 tokenizer 和模型
|
| 6 |
+
tokenizer = BertTokenizer.from_pretrained("ckiplab/bert-base-chinese")
|
| 7 |
+
model = BertForSequenceClassification.from_pretrained("ckiplab/bert-base-chinese", num_labels=2)
|
| 8 |
+
model.eval()
|
| 9 |
+
|
| 10 |
+
# 預測函式
|
| 11 |
+
def predict(text):
|
| 12 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 13 |
+
with torch.no_grad():
|
| 14 |
+
outputs = model(**inputs)
|
| 15 |
+
logits = outputs.logits
|
| 16 |
+
prediction = torch.argmax(logits, dim=1).item()
|
| 17 |
+
return "詐騙訊息" if prediction == 1 else "正常訊息"
|
| 18 |
+
|
| 19 |
+
# Gradio UI
|
| 20 |
+
iface = gr.Interface(fn=predict, inputs="text", outputs="text", title="Line詐騙訊息辨識器")
|
| 21 |
+
iface.launch()
|