File size: 1,256 Bytes
dae66c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
947e05d
dae66c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# -*- coding: utf-8 -*-
"""app.py

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1mHMq54a9glSyzg_CVIP0wshX9T8NUtmx
"""

import gradio as gr
from transformers import pipeline

pipe = pipeline(model="delarosajav95/HateSpeech-BETO-cased-v2")
#function that Gradio will use to classify
def classify_text(inputs):
  result = pipe(inputs, return_all_scores=True)
  output = []
  label_mapping = {
    'LABEL_0': 'Non-Hate Speech',
    'LABEL_1': 'Hate Speech'
  }
  for i, predictions in enumerate(result):
    for pred in predictions:
      label = label_mapping.get(pred['label'], pred['label'])
      score = pred['score']
      output.append(f"{label}: {score:.2%}")

  return "\n".join(output)
#defining Gradio interface
textbox = gr.Textbox(lines=3, placeholder="Enter a Spanish user review, comment, or opinion to evaluate (e.g., 'Esas personas no merecen respeto.')",
                     label="User Comment/Post:")

output_box = gr.Textbox(label="Results:")

iface = gr.Interface(
    fn=classify_text,
    inputs=textbox,
    outputs=output_box,
    live=True,
    title="Spanish Hate Speech Classifier for User Content",
    allow_flagging="never",
)

# Launch the interface
iface.launch()