Commit
·
5328dbc
1
Parent(s):
e7041e9
created app.py
Browse filesfirst creation of app.py was done here.
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
from tensorflow import keras
|
4 |
+
|
5 |
+
# Load the sentiment analysis model
|
6 |
+
model = keras.models.load_model("sentimentality.h5")
|
7 |
+
|
8 |
+
# Load the tokenizer and max length used during training
|
9 |
+
tokenizer = keras.preprocessing.text.tokenizer_from_json(open("tokenizer.json").read())
|
10 |
+
max_len = 100
|
11 |
+
|
12 |
+
def predict_sentiment(text):
|
13 |
+
# Preprocess the text
|
14 |
+
text = [text]
|
15 |
+
text = tf.keras.preprocessing.sequence.pad_sequences(tokenizer.texts_to_sequences(text), maxlen=max_len)
|
16 |
+
|
17 |
+
# Make a prediction
|
18 |
+
prediction = model.predict(text)[0][0]
|
19 |
+
|
20 |
+
# Return the probabilities of each sentiment
|
21 |
+
positive_prob = round(prediction * 100, 2)
|
22 |
+
negative_prob = round((1 - prediction) * 100, 2)
|
23 |
+
neutral_prob = 100 - positive_prob - negative_prob
|
24 |
+
|
25 |
+
return f"Positive: {positive_prob}%\nNegative: {negative_prob}%\nNeutral: {neutral_prob}%"
|
26 |
+
|
27 |
+
# Define the interface of the Gradio app
|
28 |
+
iface = gr.Interface(
|
29 |
+
fn=predict_sentiment,
|
30 |
+
inputs=gr.inputs.Textbox(label="Enter text here:"),
|
31 |
+
outputs=gr.outputs.Textbox(label="Sentiment probabilities:"),
|
32 |
+
title="Sentiment Analysis",
|
33 |
+
description="Enter some text and get the probabilities of the sentiment.",
|
34 |
+
)
|
35 |
+
|
36 |
+
# Run the Gradio app
|
37 |
+
iface.launch()
|