File size: 1,759 Bytes
6a51c08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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()