import gradio as gr from tensorflow import keras import pandas as pd import tensorflow as tf import nltk import spacy import re from nltk.corpus import stopwords from nltk.tokenize import word_tokenize from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing.sequence import pad_sequences nltk.download('punkt_tab') nltk.download('stopwords') stop_words = set(stopwords.words('english')) nlp = spacy.load('en_core_web_sm') model = tf.keras.models.load_model("path_to_your_model/my_keras_model") def preprocess_text(text): text = re.sub(r'[^a-zA-Z0-9\s]', '', text) # Only remove non-alphanumeric characters except spaces # Tokenize and remove stopwords tokens = word_tokenize(text.lower()) tokens = [word for word in tokens if word not in stop_words] # Lemmatize doc = nlp(' '.join(tokens)) lemmas = [token.lemma_ for token in doc] return ' '.join(lemmas) def predict(text): inputs = preprocess_text(text) outputs = model(inputs) return "This text is a violation = " + outputs demo = gr.Interface(fn=predict, inputs="text", outputs="text") demo.launch()