PauloVBM commited on
Commit
aa53522
·
verified ·
1 Parent(s): 49af9e4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import gradio as gr
4
+
5
+ MODEL_NAME = "engsoftexperimental/modelos/modelo_final_distilbert2"
6
+
7
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
8
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
9
+
10
+ model.config.id2label = {0: "hate_speech", 1: "offensive", 2: "neither", 3: "spam"}
11
+ model.config.label2id = {"hate_speech": 0, "offensive": 1, "neither": 2, "spam": 3}
12
+
13
+ def classify_text(texto):
14
+ inputs = tokenizer(
15
+ texto,
16
+ return_tensors="pt",
17
+ truncation=True,
18
+ padding="max_length",
19
+ max_length=128
20
+ ).to(model.device)
21
+
22
+ with torch.no_grad():
23
+ logits = model(**inputs).logits
24
+ probs = torch.softmax(logits, dim=1)[0].cpu().numpy()
25
+ pred = int(torch.argmax(logits, dim=1))
26
+
27
+ return {
28
+ "classe_prevista": model.config.id2label[pred],
29
+ "prob_hate_speech": float(probs[0]),
30
+ "prob_offensive": float(probs[1]),
31
+ "prob_neither": float(probs[2])
32
+ "spam": float(probs[3])
33
+ }
34
+
35
+ def gradio_predict(text):
36
+ result = classify_text(text)
37
+ return (
38
+ result["classe_prevista"],
39
+ f"{result['prob_hate_speech']:.4f}",
40
+ f"{result['prob_offensive']:.4f}",
41
+ f"{result['prob_neither']:.4f}",
42
+ f"{result['prob_spam']:.4f}",
43
+ )
44
+
45
+ demo = gr.Interface(
46
+ fn=gradio_predict,
47
+ inputs=gr.Textbox(lines=5, label="Digite ou cole um texto"),
48
+ outputs=[
49
+ gr.Label(label="Classificação Prevista"),
50
+ gr.Textbox(label="Probabilidade (Hate Speech)"),
51
+ gr.Textbox(label="Probabilidade (Offensive)"),
52
+ gr.Textbox(label="Probabilidade (Neither)"),
53
+ gr.Textbox(label="Probabilidade (Spam)")
54
+ ],
55
+ title="Protótipo de Moderador de Conteúdo (HateBR + BERT)",
56
+ description="Ferramenta experimental utilizada na pesquisa para classificação de conteúdo em três classes: Hate Speech, Offensive, Neither e Spam."
57
+ )
58
+
59
+ demo.launch()