File size: 3,436 Bytes
6a51c08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a60a26
479b69c
 
 
 
 
 
 
 
4a60a26
0faf193
 
4a60a26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0faf193
 
 
 
6a51c08
 
479b69c
6a51c08
479b69c
4a60a26
479b69c
0faf193
6a51c08
 
0faf193
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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

# Define examples
examples = [
    ["The team won the championship in a thrilling match!"],
    ["The stock market saw a significant drop today."],
    ["The prime minister announced new economic reforms."],
    ["The latest smartphone has cutting-edge features."],
    ["The actor delivered a stellar performance in the new movie."],
]

# Custom CSS for a fascinating dark theme
custom_css = """
body {
    background: linear-gradient(135deg, #1a1a2e, #16213e, #0f3460); /* Dark blue gradient */
    color: #ff3e3e; /* Vibrant red text color */
    font-family: 'Arial', sans-serif;
}

h1, h2, p {
    color: #ff3e3e; /* Red for headings and descriptions */
    text-shadow: 2px 2px 4px #000000; /* Text shadow for a glowing effect */
}

input[type="text"] {
    background-color: #16213e; /* Dark input background */
    color: #ffffff; /* White text in input fields */
    border: 2px solid #0f3460; /* Blue border */
    border-radius: 8px;
}

button {
    background: linear-gradient(45deg, #ff3e3e, #0f3460); /* Gradient button */
    color: #ffffff;
    border: none;
    border-radius: 8px;
    padding: 10px 20px;
    font-size: 16px;
    cursor: pointer;
    box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.5);
    transition: transform 0.2s;
}

button:hover {
    transform: scale(1.05); /* Slight zoom effect on hover */
    box-shadow: 2px 2px 15px rgba(255, 62, 62, 0.8);
}

.gradio-container {
    border-radius: 15px;
    padding: 20px;
    background: rgba(0, 0, 0, 0.7); /* Transparent black background for the main container */
    box-shadow: 0px 0px 15px #0f3460; /* Glowing effect for the container */
}
"""

# Interface definition
interface = gr.Interface(
    fn=predict_sport_class,
    inputs=gr.Textbox(lines=2, placeholder="Enter Article here..."),
    outputs=gr.Label(num_top_classes=1),
    title="Topic Classification App",
    description="Classify topics into one of these categories: sport, business, politics, tech, entertainment.",
    examples=examples,
    css=custom_css,  # Apply custom CSS
)

# Launch the interface
interface.launch()