File size: 1,215 Bytes
9724286
fe90914
52fe9f8
fe90914
 
 
 
 
 
f3335c0
 
 
 
fe90914
 
 
 
f3335c0
 
6ed2700
f3335c0
fe90914
6ed2700
f3335c0
 
fe90914
 
f3335c0
 
 
 
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
import gradio as gr
import tensorflow as tf

# 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
    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')
    # Make a prediction using the loaded model
    proba = model.predict(text)[0]
    # Normalize the probabilities
    proba /= proba.sum()
    # Return the probability distribution
    return {"Positive": float(proba[0]), "Negative": float(proba[1]), "Neutral": float(proba[2])}

# Create a Gradio interface
iface = gr.Interface(
    fn=predict_sentiment,
    inputs=gr.inputs.Textbox(label="Enter text here", lines=5, placeholder="Type here to analyze sentiment..."),
    outputs=gr.outputs.Label(label="Sentiment", default="Neutral", font_size=30)
)

# Add the possible classes to the output plot
classes = ["Positive", "Negative", "Neutral"]
iface.outputs[0].choices = classes

# Launch the interface
iface.launch()