Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,31 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
| 3 |
import torch
|
| 4 |
|
| 5 |
model_name = "naot97/vietnamese-toxicity-detection_3"
|
|
|
|
|
|
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
-
model = AutoModelForSequenceClassification.from_pretrained(
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def detect_toxic(text):
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
with torch.no_grad():
|
| 12 |
outputs = model(**inputs)
|
| 13 |
probs = torch.softmax(outputs.logits, dim=1)
|
| 14 |
toxic_score = probs[0][1].item()
|
|
|
|
| 15 |
return f"Toxicity Score: {toxic_score:.4f}"
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
import torch
|
| 4 |
|
| 5 |
model_name = "naot97/vietnamese-toxicity-detection_3"
|
| 6 |
+
|
| 7 |
+
# Load tokenizer và model
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
+
model = AutoModelForSequenceClassification.from_pretrained(
|
| 10 |
+
model_name, trust_remote_code=False # đảm bảo không dùng custom model sai
|
| 11 |
+
)
|
| 12 |
|
| 13 |
def detect_toxic(text):
|
| 14 |
+
# Tokenize input
|
| 15 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 16 |
+
|
| 17 |
with torch.no_grad():
|
| 18 |
outputs = model(**inputs)
|
| 19 |
probs = torch.softmax(outputs.logits, dim=1)
|
| 20 |
toxic_score = probs[0][1].item()
|
| 21 |
+
|
| 22 |
return f"Toxicity Score: {toxic_score:.4f}"
|
| 23 |
|
| 24 |
+
# Giao diện Gradio
|
| 25 |
+
gr.Interface(
|
| 26 |
+
fn=detect_toxic,
|
| 27 |
+
inputs="text",
|
| 28 |
+
outputs="text",
|
| 29 |
+
title="Vietnamese Toxicity Detector",
|
| 30 |
+
description="Nhận diện mức độ độc hại của văn bản tiếng Việt."
|
| 31 |
+
).launch()
|