Commit
·
1a2c81f
1
Parent(s):
00d5b17
chatgpt attempt
Browse files
app.py
CHANGED
@@ -1,21 +1,41 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
import tensorflow as tf
|
3 |
-
from
|
4 |
|
5 |
-
|
6 |
-
learn = tf.keras.models.load_model('sentimentality.h5')
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
|
11 |
-
prediction = learn.predict(user_sentence)
|
12 |
-
# dict = {'1': 'Negative', '2': 'Neutral', '3': 'Positive'}
|
13 |
-
# return dict[prediction[0]]
|
14 |
-
return prediction
|
15 |
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
import tensorflow as tf
|
4 |
+
from tensorflow.keras.datasets import imdb
|
5 |
|
6 |
+
word_to_index = imdb.get_word_index()
|
|
|
7 |
|
8 |
+
# Load the pre-trained model from file
|
9 |
+
loaded_model = tf.keras.models.load_model('sentimentality.h5')
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
# Define a function to make predictions
|
12 |
+
def predict_sentiment(user_input, number_of_words, words_per_view):
|
13 |
+
# Encode the input text
|
14 |
+
words = user_input.split()
|
15 |
+
encoded_word = np.zeros(words_per_view).astype(int)
|
16 |
+
encoded_word[words_per_view - len(words) - 1] = 1
|
17 |
+
for i, word in enumerate(words):
|
18 |
+
index = words_per_view - len(words) + i
|
19 |
+
encoded_word[index] = word_to_index.get(word, 0) + 3
|
20 |
+
encoded_word = np.expand_dims(encoded_word, axis=0)
|
21 |
+
|
22 |
+
# Make the prediction
|
23 |
+
prediction = loaded_model.predict(encoded_word)[0][0]
|
24 |
+
|
25 |
+
# Return the sentiment label
|
26 |
+
if prediction > 0.5:
|
27 |
+
return "Positive"
|
28 |
+
else:
|
29 |
+
return "Negative"
|
30 |
|
31 |
+
# Create the Gradio interface
|
32 |
+
iface = gr.Interface(
|
33 |
+
fn=predict_sentiment,
|
34 |
+
inputs=["textbox", gr.inputs.Number(label="Number of words", default=3000, min_value=1, max_value=10000), gr.inputs.Number(label="Words per view", default=30, min_value=1, max_value=100)],
|
35 |
+
outputs="text",
|
36 |
+
title="Sentiment Analysis",
|
37 |
+
description="Enter a text and get the sentiment prediction"
|
38 |
+
)
|
39 |
|
40 |
+
# Launch the interface
|
41 |
+
iface.launch()
|