Spaces:
Sleeping
Sleeping
| 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() | |