Commit
·
2e71df7
1
Parent(s):
21a1937
fn9
Browse files
app.py
CHANGED
@@ -1,3 +1,35 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
|
5 |
+
# Load the pre-trained model
|
6 |
+
model = tf.keras.models.load_model('sentimentality.h5')
|
7 |
+
|
8 |
+
# Define a function to preprocess the text input
|
9 |
+
def preprocess(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 |
+
return text
|
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.Label(label="Sentiment", default="Neutral")
|
32 |
+
)
|
33 |
+
|
34 |
+
# Launch the interface
|
35 |
+
iface.launch()
|