Commit
·
4386646
1
Parent(s):
9811bd2
codex codes
Browse files
app.py
CHANGED
@@ -1,41 +1,33 @@
|
|
1 |
import gradio as gr
|
2 |
-
import numpy as np
|
3 |
import tensorflow as tf
|
4 |
-
from tensorflow.keras.datasets import imdb
|
5 |
|
6 |
-
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
encoded_word = np.expand_dims(encoded_word, axis=0)
|
21 |
|
22 |
-
# Make the prediction
|
23 |
-
prediction =
|
24 |
|
25 |
-
# Return the sentiment
|
26 |
if prediction > 0.5:
|
27 |
-
return "
|
28 |
else:
|
29 |
-
return "
|
30 |
|
31 |
# Create the Gradio interface
|
32 |
-
iface = gr.Interface(
|
33 |
-
fn=predict_sentiment,
|
34 |
-
inputs=["textbox", gr.inputs.NumberRange(minimum=1, maximum=10000, default=3000, label="Number of words"), gr.inputs.NumberRange(minimum=1, maximum=100, default=30, label="Words per view")],
|
35 |
-
outputs="text",
|
36 |
-
title="Sentiment Analysis",
|
37 |
-
description="Enter a text and get the sentiment prediction"
|
38 |
-
)
|
39 |
|
40 |
-
#
|
41 |
-
iface.launch()
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import tensorflow as tf
|
|
|
3 |
|
4 |
+
# Load the saved model
|
5 |
+
model = tf.keras.models.load_model("sentimentality.h5")
|
6 |
|
7 |
+
def predict_sentiment(text):
|
8 |
+
"""
|
9 |
+
This function takes a text input and returns the predicted sentiment using a saved model.
|
10 |
+
|
11 |
+
Parameters:
|
12 |
+
text (str): The input text to be analyzed
|
13 |
+
|
14 |
+
Returns:
|
15 |
+
str: The predicted sentiment of the input text (either "positive" or "negative")
|
16 |
+
"""
|
17 |
+
# Preprocess the input text
|
18 |
+
text = preprocess_text(text)
|
|
|
19 |
|
20 |
+
# Make the prediction using the loaded model
|
21 |
+
prediction = model.predict([text])[0][0]
|
22 |
|
23 |
+
# Return the predicted sentiment
|
24 |
if prediction > 0.5:
|
25 |
+
return "positive"
|
26 |
else:
|
27 |
+
return "negative"
|
28 |
|
29 |
# Create the Gradio interface
|
30 |
+
iface = gr.Interface(fn=predict_sentiment, inputs="text", outputs="text")
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
+
# Run the interface
|
33 |
+
iface.launch()
|