File size: 1,237 Bytes
5328dbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import tensorflow as tf
from tensorflow import keras

# Load the sentiment analysis model
model = keras.models.load_model("sentimentality.h5")

# Load the tokenizer and max length used during training
tokenizer = keras.preprocessing.text.tokenizer_from_json(open("tokenizer.json").read())
max_len = 100

def predict_sentiment(text):
    # Preprocess the text
    text = [text]
    text = tf.keras.preprocessing.sequence.pad_sequences(tokenizer.texts_to_sequences(text), maxlen=max_len)

    # Make a prediction
    prediction = model.predict(text)[0][0]

    # Return the probabilities of each sentiment
    positive_prob = round(prediction * 100, 2)
    negative_prob = round((1 - prediction) * 100, 2)
    neutral_prob = 100 - positive_prob - negative_prob

    return f"Positive: {positive_prob}%\nNegative: {negative_prob}%\nNeutral: {neutral_prob}%"

# Define the interface of the Gradio app
iface = gr.Interface(
    fn=predict_sentiment,
    inputs=gr.inputs.Textbox(label="Enter text here:"),
    outputs=gr.outputs.Textbox(label="Sentiment probabilities:"),
    title="Sentiment Analysis",
    description="Enter some text and get the probabilities of the sentiment.",
)

# Run the Gradio app
iface.launch()