Spaces:
Runtime error
Runtime error
Tirath5504
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import string
|
4 |
+
import nltk
|
5 |
+
from nltk.tokenize import word_tokenize
|
6 |
+
from nltk.corpus import stopwords
|
7 |
+
from nltk.stem import WordNetLemmatizer
|
8 |
+
import tensorflow as tf
|
9 |
+
from tensorflow import keras
|
10 |
+
from keras import layers
|
11 |
+
from tensorflow.keras.preprocessing.text import Tokenizer
|
12 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
13 |
+
import joblib
|
14 |
+
|
15 |
+
nltk.download('stopwords')
|
16 |
+
nltk.download('omw-1.4')
|
17 |
+
nltk.download('wordnet')
|
18 |
+
nltk.download('punkt')
|
19 |
+
|
20 |
+
tokenizer, model = joblib.load("lstm_model.pkl")
|
21 |
+
|
22 |
+
def preprocess(text, tokenizer):
|
23 |
+
lemmatizer = WordNetLemmatizer()
|
24 |
+
vocab = set()
|
25 |
+
stop_words = set(stopwords.words('english'))
|
26 |
+
tokens = word_tokenize(text)
|
27 |
+
tokens = [word for word in tokens if word.lower() not in stop_words and word not in string.punctuation]
|
28 |
+
tokens = [lemmatizer.lemmatize(word.lower()) for word in tokens]
|
29 |
+
vocab.update(tokens)
|
30 |
+
preprocessed_text = ' '.join(tokens)
|
31 |
+
X = tokenizer.texts_to_sequences(preprocessed_text)
|
32 |
+
max_len = max(len(y) for y in X)
|
33 |
+
X = pad_sequences(X, maxlen=max_len)
|
34 |
+
return X
|
35 |
+
|
36 |
+
def predict(text):
|
37 |
+
X = preprocess(text, tokenizer)
|
38 |
+
pred = model.predict(X)
|
39 |
+
probabilities = np.mean(pred, axis=0)
|
40 |
+
final_class = np.argmax(probabilities)
|
41 |
+
if final_class == 0:
|
42 |
+
prediction = "The string is classified as hate speech."
|
43 |
+
else:
|
44 |
+
prediction = "The string is classified as normal speech."
|
45 |
+
return {"prediction": prediction, "probability": probabilities.tolist()}
|
46 |
+
|
47 |
+
iface = gr.Interface(
|
48 |
+
fn=predict,
|
49 |
+
inputs=gr.inputs.Textbox(lines=2, placeholder="Enter text here..."),
|
50 |
+
outputs=[gr.outputs.Textbox(label="Prediction"), gr.outputs.Textbox(label="Probabilities")],
|
51 |
+
title="Hate Speech Classifier",
|
52 |
+
description="A classifier to detect hate speech in a given text.",
|
53 |
+
)
|
54 |
+
|
55 |
+
if __name__ == "__main__":
|
56 |
+
iface.launch()
|