Commit
·
f3335c0
1
Parent(s):
d278b23
fn8
Browse files
app.py
CHANGED
|
@@ -1,14 +1,5 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
-
import numpy as np
|
| 4 |
-
|
| 5 |
-
# Define a function to preprocess the text input
|
| 6 |
-
def preprocess(text):
|
| 7 |
-
tokenizer = tf.keras.preprocessing.text.Tokenizer()
|
| 8 |
-
tokenizer.fit_on_texts([text])
|
| 9 |
-
text = tokenizer.texts_to_sequences([text])
|
| 10 |
-
text = tf.keras.preprocessing.sequence.pad_sequences(text, maxlen=500, padding='post', truncating='post')
|
| 11 |
-
return text
|
| 12 |
|
| 13 |
# Load the pre-trained model
|
| 14 |
model = tf.keras.models.load_model('sentimentality.h5')
|
|
@@ -16,33 +7,27 @@ model = tf.keras.models.load_model('sentimentality.h5')
|
|
| 16 |
# Define a function to make a prediction on the input text
|
| 17 |
def predict_sentiment(text):
|
| 18 |
# Preprocess the text
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
# Make a prediction using the loaded model
|
| 21 |
proba = model.predict(text)[0]
|
| 22 |
# Normalize the probabilities
|
| 23 |
proba /= proba.sum()
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
# Determine the color based on the sentiment label
|
| 27 |
-
if sentiment_label == 'Positive':
|
| 28 |
-
color = '#2a9d8f'
|
| 29 |
-
elif sentiment_label == 'Negative':
|
| 30 |
-
color = '#e76f51'
|
| 31 |
-
else:
|
| 32 |
-
color = '#264653'
|
| 33 |
-
# Return the sentiment label and color
|
| 34 |
-
return {'label': sentiment_label, 'color': color}
|
| 35 |
|
| 36 |
-
#
|
| 37 |
iface = gr.Interface(
|
| 38 |
fn=predict_sentiment,
|
| 39 |
-
inputs=gr.inputs.Textbox(label=
|
| 40 |
-
outputs=gr.outputs.Label(label=
|
| 41 |
-
font_size=30, font_family='Arial',
|
| 42 |
-
background_color='#f8f8f8',
|
| 43 |
-
color='black'),
|
| 44 |
-
title='SENTIMENT ANALYSIS'
|
| 45 |
)
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
# Launch the interface
|
| 48 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# Load the pre-trained model
|
| 5 |
model = tf.keras.models.load_model('sentimentality.h5')
|
|
|
|
| 7 |
# Define a function to make a prediction on the input text
|
| 8 |
def predict_sentiment(text):
|
| 9 |
# Preprocess the text
|
| 10 |
+
tokenizer = tf.keras.preprocessing.text.Tokenizer()
|
| 11 |
+
tokenizer.fit_on_texts([text])
|
| 12 |
+
text = tokenizer.texts_to_sequences([text])
|
| 13 |
+
text = tf.keras.preprocessing.sequence.pad_sequences(text, maxlen=500, padding='post', truncating='post')
|
| 14 |
# Make a prediction using the loaded model
|
| 15 |
proba = model.predict(text)[0]
|
| 16 |
# Normalize the probabilities
|
| 17 |
proba /= proba.sum()
|
| 18 |
+
# Return the probability distribution
|
| 19 |
+
return {"Positive": float(proba[0]), "Negative": float(proba[1]), "Neutral": float(proba[2])}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# Create a Gradio interface
|
| 22 |
iface = gr.Interface(
|
| 23 |
fn=predict_sentiment,
|
| 24 |
+
inputs=gr.inputs.Textbox(label="Enter text here", lines=5, placeholder="Type here to analyze sentiment..."),
|
| 25 |
+
outputs=gr.outputs.Label(label="Sentiment", default="Neutral", font_size=30)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
)
|
| 27 |
|
| 28 |
+
# Add the possible classes to the output plot
|
| 29 |
+
classes = ["Positive", "Negative", "Neutral"]
|
| 30 |
+
iface.outputs[0].choices = classes
|
| 31 |
+
|
| 32 |
# Launch the interface
|
| 33 |
iface.launch()
|