Commit
·
2f9f9d9
1
Parent(s):
ae7453b
fn10
Browse files
app.py
CHANGED
|
@@ -1,27 +1,38 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import tensorflow as tf
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
-
# Load the
|
| 6 |
-
model = tf.keras.models.load_model(
|
| 7 |
|
| 8 |
-
# Define a function to preprocess the text
|
| 9 |
-
def
|
| 10 |
-
|
| 11 |
-
tokenizer.
|
| 12 |
-
|
| 13 |
-
text =
|
|
|
|
|
|
|
|
|
|
| 14 |
return text
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
scores = model.predict(text)
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
examples=[["This is wonderful!"]]
|
|
|
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import tensorflow as tf
|
| 2 |
+
import gradio as gr
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
+
# Load the custom model
|
| 6 |
+
model = tf.keras.models.load_model("sentiment.h5")
|
| 7 |
|
| 8 |
+
# Define a function to preprocess the input text
|
| 9 |
+
def preprocess_text(text):
|
| 10 |
+
# Tokenize the text
|
| 11 |
+
tokenizer = tf.keras.preprocessing.text.Tokenizer(num_words=10000)
|
| 12 |
+
tokenizer.fit_on_texts(text)
|
| 13 |
+
text = tokenizer.texts_to_sequences(text)
|
| 14 |
+
# Pad the sequences to a fixed length
|
| 15 |
+
max_len = 30
|
| 16 |
+
text = tf.keras.preprocessing.sequence.pad_sequences(text, maxlen=max_len)
|
| 17 |
return text
|
| 18 |
|
| 19 |
+
# Define a function to get the sentiment scores from the model
|
| 20 |
+
def get_sentiment_scores(text):
|
| 21 |
+
# Preprocess the text
|
| 22 |
+
text = preprocess_text(text)
|
| 23 |
+
# Get the sentiment scores from the model
|
| 24 |
scores = model.predict(text)
|
| 25 |
+
# Postprocess the scores to obtain the sentiment label
|
| 26 |
+
label = "Positive" if np.round(scores) == 1 else "Negative"
|
| 27 |
+
return label
|
| 28 |
|
| 29 |
+
# Define the Gradio interface
|
| 30 |
+
interface = gr.Interface(
|
| 31 |
+
fn=get_sentiment_scores,
|
| 32 |
+
inputs=gr.inputs.Textbox(placeholder="Enter a positive or negative sentence here..."),
|
| 33 |
+
outputs=gr.outputs.Textbox(label="Sentiment Label"),
|
| 34 |
+
examples=[["This is wonderful!"], ["I hate this product."]]
|
| 35 |
+
)
|
| 36 |
|
| 37 |
+
# Launch the interface
|
| 38 |
+
interface.launch()
|