Spaces:
Runtime error
Runtime error
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 | |
import spacy.cli | |
spacy.cli.download("en_core_web_sm") | |
nltk.download('punkt_tab') | |
nltk.download('stopwords') | |
stop_words = set(stopwords.words('english')) | |
nlp = spacy.load('en_core_web_sm') | |
# Available backend options are: "jax", "torch", "tensorflow". | |
import os | |
os.environ["KERAS_BACKEND"] = "jax" | |
import keras | |
model = keras.saving.load_model("hf://ARI-HIPA-AI-Team/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() |