File size: 1,658 Bytes
9724286 fe90914 5328dbc fe90914 52fe9f8 fe90914 52fe9f8 fe90914 b4a2a0b 6ed2700 fe90914 6ed2700 b4a2a0b 6ed2700 fe90914 |
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 |
import gradio as gr
import tensorflow as tf
import numpy as np
# Define a function to preprocess the text input
def preprocess(text):
tokenizer = tf.keras.preprocessing.text.Tokenizer()
tokenizer.fit_on_texts([text])
text = tokenizer.texts_to_sequences([text])
text = tf.keras.preprocessing.sequence.pad_sequences(text, maxlen=500, padding='post', truncating='post')
return text
# Load the pre-trained model
model = tf.keras.models.load_model('sentimentality.h5')
# Define a function to make a prediction on the input text
def predict_sentiment(text):
# Preprocess the text
text = preprocess(text)
# Make a prediction using the loaded model
proba = model.predict(text)[0]
# Normalize the probabilities
proba /= proba.sum()
# Get the predicted sentiment label
sentiment_label = ['Positive', 'Negative', 'Neutral'][np.argmax(proba)]
# Determine the color based on the sentiment label
if sentiment_label == 'Positive':
color = '#2a9d8f'
elif sentiment_label == 'Negative':
color = '#e76f51'
else:
color = '#264653'
# Return the sentiment label and color
return {'label': sentiment_label, 'color': color}
# Define the Gradio interface
iface = gr.Interface(
fn=predict_sentiment,
inputs=gr.inputs.Textbox(label='Enter text here'),
outputs=gr.outputs.Label(label='Sentiment', default='Neutral',
font_size=30, font_family='Arial',
background_color='#f8f8f8',
color='black', value=None),
title='SENTIMENT ANALYSIS'
)
# Launch the interface
iface.launch()
|