risingodegua commited on
Commit
85574a0
1 Parent(s): 76b43c9

Add app.py with model predictor

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+
4
+ from transformers import AutoTokenizer, TFAutoModelForSequenceClassification
5
+
6
+ tokenizer = AutoTokenizer.from_pretrained("uhhlt/bert-based-uncased-hatespeech-movies")
7
+
8
+ model = TFAutoModelForSequenceClassification.from_pretrained("uhhlt/bert-based-uncased-hatespeech-movies")
9
+
10
+ def make_prediction(text):
11
+ '''
12
+ This function takes a string as input and returns a prediction for the hate speech class.
13
+ Hate speech class labels are: Normal(0), Offensive(1), and Hate speech(2).
14
+
15
+ Parameters:
16
+ text (str): The text to be classified.
17
+
18
+ Returns:
19
+ str: The predicted class label.
20
+ '''
21
+ input_ids = tokenizer.encode(text)
22
+ input_ids = np.array(input_ids)
23
+ input_ids = np.expand_dims(input_ids, axis=0)
24
+ prediction_arr = model.predict(input_ids)[0][0]
25
+
26
+ labels = ["Normal", "Offensive", "Hate Speech"]
27
+ prediction = labels[np.argmax(prediction_arr)]
28
+ return prediction
29
+
30
+
31
+
32
+ iface = gr.Interface(
33
+ fn=make_prediction,
34
+ inputs=gr.inputs.Textbox(lines=3, placeholder="Enter your text here..."),
35
+ outputs="text",
36
+ title="Hate Speech Detector",
37
+ description="A model for detecting if a given text is an hate speech.",
38
+ )
39
+ iface.launch()