Spaces:
Build error
Build error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""app.py
|
3 |
+
|
4 |
+
Automatically generated by Colab.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1mHMq54a9glSyzg_CVIP0wshX9T8NUtmx
|
8 |
+
"""
|
9 |
+
|
10 |
+
import gradio as gr
|
11 |
+
from transformers import pipeline
|
12 |
+
|
13 |
+
pipe = pipeline(model="delarosajav95/HateSpeech-BETO-cased-v2")
|
14 |
+
#function that Gradio will use to classify
|
15 |
+
def classify_text(inputs):
|
16 |
+
result = pipe(inputs, return_all_scores=True)
|
17 |
+
output = []
|
18 |
+
label_mapping = {
|
19 |
+
'LABEL_0': 'Non-Hate Speech',
|
20 |
+
'LABEL_1': 'Hate Speech'
|
21 |
+
}
|
22 |
+
for i, predictions in enumerate(result):
|
23 |
+
for pred in predictions:
|
24 |
+
label = label_mapping.get(pred['label'], pred['label'])
|
25 |
+
score = pred['score']
|
26 |
+
output.append(f"{label}: {score:.2%}")
|
27 |
+
|
28 |
+
return "\n".join(output)
|
29 |
+
#defining Gradio interface
|
30 |
+
textbox = gr.Textbox(lines=3, placeholder="Type a statement, comment, or review for evaluation (e.g., 'Such people don't deserve respect.')",
|
31 |
+
label="User Comment/Post:")
|
32 |
+
|
33 |
+
output_box = gr.Textbox(label="Results:")
|
34 |
+
|
35 |
+
iface = gr.Interface(
|
36 |
+
fn=classify_text,
|
37 |
+
inputs=textbox,
|
38 |
+
outputs=output_box,
|
39 |
+
live=True,
|
40 |
+
title="Spanish Hate Speech Classifier for User Content",
|
41 |
+
allow_flagging="never",
|
42 |
+
)
|
43 |
+
|
44 |
+
# Launch the interface
|
45 |
+
iface.launch()
|