Commit
·
da8c970
1
Parent(s):
c107058
Add application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class ToxicCommentClassification:
|
| 6 |
+
def __init__(self, model_name: str):
|
| 7 |
+
self.model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 8 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
+
self.pipeline = pipeline(
|
| 10 |
+
"text-classification",
|
| 11 |
+
model=self.model,
|
| 12 |
+
tokenizer=self.tokenizer,
|
| 13 |
+
return_all_scores=True,
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
def predict(self, text):
|
| 17 |
+
res = self.pipeline(text)[0]
|
| 18 |
+
results = dict()
|
| 19 |
+
is_normal = True
|
| 20 |
+
for x in res:
|
| 21 |
+
results[x['label']] = x['score']
|
| 22 |
+
|
| 23 |
+
if float(x['score']) > 0.8:
|
| 24 |
+
is_normal = False
|
| 25 |
+
|
| 26 |
+
if is_normal:
|
| 27 |
+
results['normal'] = 1
|
| 28 |
+
return results
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def main():
|
| 32 |
+
model = ToxicCommentClassification("DuongTrongChi/facebook-commet-classification-filter-v4")
|
| 33 |
+
iface = gr.Interface(
|
| 34 |
+
fn=model.predict,
|
| 35 |
+
inputs=gr.Textbox(
|
| 36 |
+
lines=3,
|
| 37 |
+
placeholder="Hãy nhập nội dung vào đây",
|
| 38 |
+
label="Input Text",
|
| 39 |
+
),
|
| 40 |
+
outputs="label",
|
| 41 |
+
title="Toxic Comment Classification",
|
| 42 |
+
examples=[
|
| 43 |
+
"Ôi chú chó này nhìn dễ thương thế!",
|
| 44 |
+
"Cái lúc óc chó sống làm chi cho chật đất",
|
| 45 |
+
"Cầm con dao này và đâm chết con chó này đi!",
|
| 46 |
+
"Tôi dắt con mèo cưng của tôi đi đạo phố ở công viên.",
|
| 47 |
+
"Việt Nam là một nơi bạo loạn, người dân thì cướp bốc giết người. Quan chức nhà nước thì toàn bốc lộc nhân dân."
|
| 48 |
+
],
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
iface.launch()
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
main()
|