|
|
|
|
|
import os |
|
|
from typing import Dict |
|
|
|
|
|
import gradio as gr |
|
|
import numpy as np |
|
|
import tensorflow as tf |
|
|
|
|
|
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2" |
|
|
|
|
|
import tensorflow_text |
|
|
|
|
|
|
|
|
try: |
|
|
import tensorflow_hub as hub |
|
|
except ImportError as e: |
|
|
|
|
|
print(f"Warning: TensorFlow Hub import issue: {e}") |
|
|
hub = None |
|
|
|
|
|
|
|
|
try: |
|
|
import tf_keras |
|
|
from tf_keras.optimizers import Adam |
|
|
|
|
|
keras = tf_keras |
|
|
except ImportError: |
|
|
keras = tf.keras |
|
|
|
|
|
|
|
|
try: |
|
|
from official.nlp.optimization import AdamWeightDecay, WarmUp |
|
|
except ImportError: |
|
|
|
|
|
AdamWeightDecay = None |
|
|
WarmUp = None |
|
|
|
|
|
np.set_printoptions(suppress=True) |
|
|
|
|
|
labels = ["hate speech", "offensive language", "neither"] |
|
|
|
|
|
|
|
|
|
|
|
custom_objects = {} |
|
|
if hub is not None: |
|
|
custom_objects["KerasLayer"] = hub.KerasLayer |
|
|
if AdamWeightDecay is not None: |
|
|
custom_objects["AdamWeightDecay"] = AdamWeightDecay |
|
|
if WarmUp is not None: |
|
|
custom_objects["WarmUp"] = WarmUp |
|
|
|
|
|
classifier_model = keras.models.load_model("classifier_model.h5", custom_objects=custom_objects) |
|
|
|
|
|
|
|
|
def run_model(text: str) -> Dict[str, float]: |
|
|
prediction = classifier_model.predict([text])[0] |
|
|
confidences = {labels[i]: float(prediction[i]) for i in range(len(labels))} |
|
|
return confidences |
|
|
|
|
|
|
|
|
examples = [ |
|
|
["This is wonderful!"], |
|
|
] |
|
|
|
|
|
short_description = ( |
|
|
"This application classifies text into three categories: hate speech, offensive language, " |
|
|
"and neither, using a deep learning model trained on the Hate Speech and Offensive Language Dataset. " |
|
|
"Enter a sentence and the model will predict its category." |
|
|
) |
|
|
|
|
|
demo = gr.Interface( |
|
|
fn=run_model, |
|
|
inputs=gr.Textbox(lines=5, placeholder="Enter a sentence here...", label="Input Text"), |
|
|
outputs=gr.Label(), |
|
|
examples=examples, |
|
|
title="Hate Speech and Offensive Language Classifier", |
|
|
description=short_description, |
|
|
) |
|
|
|
|
|
demo.launch() |
|
|
|