NightPrince's picture
Update app.py
6a51c08 verified
raw
history blame
1.76 kB
import gradio as gr
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
import json
# Load configurations
NUM_WORDS = 1000
MAXLEN = 120
PADDING = 'post'
OOV_TOKEN = "<OOV>"
with open('tokenizer.json', 'r') as f:
tokenizer = tf.keras.preprocessing.text.tokenizer_from_json(f.read())
# Load the trained model
model = tf.keras.models.load_model("model.h5")
# Function to convert sentences to padded sequences
def seq_and_pad(sentences, tokenizer, padding, maxlen):
sequences = tokenizer.texts_to_sequences(sentences)
padded_sequences = pad_sequences(sequences, maxlen=maxlen, padding=padding)
return padded_sequences
# Function to predict the class of a sentence
def predict_sport_class(sentence):
# Convert the sentence to a padded sequence
sentence_seq = seq_and_pad([sentence], tokenizer, PADDING, MAXLEN)
# Make a prediction
prediction = model.predict(sentence_seq)
# Get the predicted label
predicted_label = np.argmax(prediction)
# Mapping the label value back to the original label
label_mapping = {0: "sport", 1: "business", 2: "politics", 3: "tech", 4: "entertainment"}
# Get the predicted class label
predicted_class = label_mapping[predicted_label]
return predicted_class
# Create the Gradio interface
interface = gr.Interface(
fn=predict_sport_class,
inputs=gr.Textbox(lines=2, placeholder="Enter a sentence here..."),
outputs=gr.Label(num_top_classes=1),
title="Text Classification App",
description="Enter a sentence to classify it into one of the following categories: sport, business, politics, tech, entertainment.",
)
interface.launch()