Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,68 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
|
| 3 |
+
from keras.datasets import imdb
|
| 4 |
+
from keras.preprocessing import sequence
|
| 5 |
+
import keras
|
| 6 |
+
import tensorflow as tf
|
| 7 |
+
import os
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
+
VOCAB_SIZE = 88584
|
| 11 |
+
|
| 12 |
+
MAXLEN = 250
|
| 13 |
+
BATCH_SIZE = 64
|
| 14 |
+
|
| 15 |
+
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words = VOCAB_SIZE)
|
| 16 |
+
train_data[1]
|
| 17 |
+
|
| 18 |
+
train_data = sequence.pad_sequences(train_data, MAXLEN)
|
| 19 |
+
test_data = sequence.pad_sequences(test_data, MAXLEN)
|
| 20 |
+
|
| 21 |
+
model = tf.keras.Sequential([
|
| 22 |
+
tf.keras.layers.Embedding(VOCAB_SIZE, 32),
|
| 23 |
+
tf.keras.layers.LSTM(32),
|
| 24 |
+
tf.keras.layers.Dense(1, activation="sigmoid")
|
| 25 |
+
])
|
| 26 |
+
|
| 27 |
+
model.compile(loss="binary_crossentropy",optimizer="rmsprop",metrics=['acc'])
|
| 28 |
+
|
| 29 |
+
history = model.fit(train_data, train_labels, epochs=20, validation_split=0.2)
|
| 30 |
+
|
| 31 |
+
results = model.evaluate(test_data, test_labels)
|
| 32 |
+
print(results)
|
| 33 |
+
|
| 34 |
+
word_index = imdb.get_word_index()
|
| 35 |
+
|
| 36 |
+
def encode_text(text):
|
| 37 |
+
tokens = keras.preprocessing.text.text_to_word_sequence(text)
|
| 38 |
+
tokens = [word_index[word] if word in word_index else 0 for word in tokens]
|
| 39 |
+
return sequence.pad_sequences([tokens], MAXLEN)[0]
|
| 40 |
+
|
| 41 |
+
text = "that movie was just amazing, so amazing"
|
| 42 |
+
encoded = encode_text(text)
|
| 43 |
+
print(encoded)
|
| 44 |
+
|
| 45 |
+
reverse_word_index = {value: key for (key, value) in word_index.items()}
|
| 46 |
+
|
| 47 |
+
def decode_integers(integers):
|
| 48 |
+
PAD = 0
|
| 49 |
+
text = ""
|
| 50 |
+
for num in integers:
|
| 51 |
+
if num != PAD:
|
| 52 |
+
text += reverse_word_index[num] + " "
|
| 53 |
+
|
| 54 |
+
return text[:-1]
|
| 55 |
+
|
| 56 |
+
print(decode_integers(encoded))
|
| 57 |
+
|
| 58 |
+
def predict(text):
|
| 59 |
+
encoded_text = encode_text(text)
|
| 60 |
+
pred = np.zeros((1,250))
|
| 61 |
+
pred[0] = encoded_text
|
| 62 |
+
result = model.predict(pred)
|
| 63 |
+
print(result[0])
|
| 64 |
+
|
| 65 |
+
positive_review = "That movie was! really loved it and would great watch it again because it was amazingly great"
|
| 66 |
+
st.write(predict(positive_review))
|
| 67 |
+
negative_review = "that movie really sucked. I hated it and wouldn't watch it again. Was one of the worst things I've ever watched"
|
| 68 |
+
st.write(predict(negative_review))
|