Commit
·
fe90914
1
Parent(s):
52fe9f8
function 3
Browse files
app.py
CHANGED
@@ -1,36 +1,35 @@
|
|
1 |
-
from tensorflow.keras.models import load_model
|
2 |
-
import numpy as np
|
3 |
-
import cv2
|
4 |
import gradio as gr
|
|
|
|
|
5 |
|
6 |
-
#
|
7 |
-
model = load_model('sentimentality.h5')
|
8 |
-
|
9 |
-
# Function to preprocess the input text
|
10 |
def preprocess(text):
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
# Convert the words to indices in the vocabulary
|
17 |
-
word_indices = [vocab.index(word) if word in vocab else 0 for word in words]
|
18 |
-
# Pad the sequence with zeros to a fixed length of 500
|
19 |
-
padded_indices = np.zeros(500, dtype=np.int32)
|
20 |
-
padded_indices[:len(word_indices)] = word_indices
|
21 |
-
# Convert the sequence to a tensor
|
22 |
-
tensor = np.expand_dims(padded_indices, axis=0)
|
23 |
-
return tensor
|
24 |
|
25 |
-
#
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
# Create a Gradio interface
|
34 |
-
|
35 |
-
|
36 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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')
|
15 |
+
|
16 |
+
# Define a function to make a prediction on the input text
|
17 |
+
def predict_sentiment(text):
|
18 |
+
# Preprocess the text
|
19 |
+
text = preprocess(text)
|
20 |
+
# Make a prediction using the loaded model
|
21 |
+
proba = model.predict(text)[0]
|
22 |
+
# Normalize the probabilities
|
23 |
+
proba /= proba.sum()
|
24 |
+
# Return the probability distribution
|
25 |
+
return {"Positive": float(proba[0]), "Negative": float(proba[1]), "Neutral": float(proba[2])}
|
26 |
|
27 |
# Create a Gradio interface
|
28 |
+
iface = gr.Interface(
|
29 |
+
fn=predict_sentiment,
|
30 |
+
inputs=gr.inputs.Textbox(label="Enter text here"),
|
31 |
+
outputs=gr.outputs.Bar(label="Sentiment Distribution", num_top_classes=3)
|
32 |
+
)
|
33 |
+
|
34 |
+
# Launch the interface
|
35 |
+
iface.launch()
|