Commit
·
60ae5e6
1
Parent(s):
84accd9
another commit with custom model
Browse files
app.py
CHANGED
@@ -1,3 +1,37 @@
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from tensorflow import keras
|
2 |
+
import tensorflow as tf
|
3 |
+
from tensorflow.keras.datasets import imdb
|
4 |
+
import numpy as np
|
5 |
import gradio as gr
|
6 |
|
7 |
+
number_of_words = 3000
|
8 |
+
words_per_view = 200
|
9 |
+
|
10 |
+
loaded_model = tf.keras.models.load_model('sentimentality.h5')
|
11 |
+
word_to_index = imdb.get_word_index()
|
12 |
+
|
13 |
+
def get_predict(userInputString, model):
|
14 |
+
words = userInputString.split()
|
15 |
+
#print(len(words))
|
16 |
+
encoded_word = np.zeros(words_per_view).astype(int)
|
17 |
+
encoded_word[words_per_view -len(words) - 1] = 1
|
18 |
+
for i, word in enumerate(words):
|
19 |
+
index = words_per_view - len(words) + i
|
20 |
+
encoded_word[index] = word_to_index.get(word, 0) + 3
|
21 |
+
encoded_word = np.expand_dims(encoded_word, axis=0)
|
22 |
+
prediction = model.predict(encoded_word)
|
23 |
+
return prediction
|
24 |
+
|
25 |
+
def analyze_sentiment(userInputString):
|
26 |
+
result = get_predict(userInputString, loaded_model)[0][0]
|
27 |
+
if result > 0.5:
|
28 |
+
answer = 'positive review'
|
29 |
+
else: answer = 'negative review'
|
30 |
+
return answer
|
31 |
+
UserInputPage = gr.Interface(
|
32 |
+
fn=analyze_sentiment,
|
33 |
+
inputs = ["text"],
|
34 |
+
outputs=["text"]
|
35 |
+
)
|
36 |
+
tabbed_Interface = gr.TabbedInterface([UserInputPage], ["Check user input"])
|
37 |
+
tabbed_Interface.launch()
|