Maverick2708 commited on
Commit
f6cc98e
·
verified ·
1 Parent(s): c1d4944

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -21
app.py CHANGED
@@ -1,26 +1,29 @@
1
- from transformers import AutoTokenizer, AutoModelForSequenceClassification, TextClassificationPipeline
 
 
2
 
3
- # Model ID hợp lệ
4
- model_id = "trituenhantaoio/bert-base-vietnamese-uncased"
 
5
 
6
- # Tải tokenizer và model
7
- tokenizer = AutoTokenizer.from_pretrained(model_id)
8
- model = AutoModelForSequenceClassification.from_pretrained(model_id)
9
 
10
- # Tạo pipeline
11
- pipe = TextClassificationPipeline(model=model, tokenizer=tokenizer, return_all_scores=True)
 
 
 
 
 
12
 
13
- # Danh sách bình luận để kiểm tra
14
- comments = [
15
- "Bạn thật sự ngu ngốc và kém cỏi.",
16
- "Hôm nay là một ngày đẹp trời.",
17
- "Cái này tệ kinh khủng, không ai muốn dùng đâu!"
18
- ]
19
 
20
- # In kết quả
21
- for comment in comments:
22
- results = pipe(comment)
23
- print(f"Comment: {comment}")
24
- for label in results[0]:
25
- print(f" {label['label']}: {label['score']:.4f}")
26
- print("------")
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, T5ForConditionalGeneration
3
+ import torch
4
 
5
+ # Dùng đúng tokenizer gốc: t5-small
6
+ tokenizer = AutoTokenizer.from_pretrained("t5-small")
7
+ model = T5ForConditionalGeneration.from_pretrained("naot97/vietnamese-toxicity-detection_3")
8
 
9
+ def detect_toxic(text):
10
+ prompt = f"toxic classification: {text}"
11
+ inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
12
 
13
+ with torch.no_grad():
14
+ outputs = model.generate(
15
+ input_ids=inputs["input_ids"],
16
+ attention_mask=inputs["attention_mask"],
17
+ max_length=10
18
+ )
19
+ decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
20
 
21
+ return f"Toxicity: {decoded}"
 
 
 
 
 
22
 
23
+ gr.Interface(
24
+ fn=detect_toxic,
25
+ inputs="text",
26
+ outputs="text",
27
+ title="Vietnamese Toxicity Detector",
28
+ description="Dựa trên mô hình T5 phát hiện độc hại tiếng Việt"
29
+ ).launch()