%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from google.colab import drive
drive.mount('/content/drive')
Mounted at /content/drive
%cd '/content/drive/My Drive/Colab Notebooks'
/content/drive/My Drive/Colab Notebooks
from tensorflow.keras.models import model_from_json
model = model_from_json(open('imdb_model_architecture.json').read())
model.load_weights('imdb_model_weights.h5')
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.summary()
Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= embedding (Embedding) (None, None, 128) 1280000 lstm (LSTM) (None, 128) 131584 dense (Dense) (None, 1) 129 ================================================================= Total params: 1,411,713 Trainable params: 1,411,713 Non-trainable params: 0 _________________________________________________________________
from tensorflow.keras.datasets.imdb import get_word_index
word_index = get_word_index()
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/imdb_word_index.json 1646592/1641221 [==============================] - 0s 0us/step 1654784/1641221 [==============================] - 0s 0us/step
word_index['this']
11
text = "this movie is worth seeing"
text.split()
['this', 'movie', 'is', 'worth', 'seeing']
seq = [word_index[x] for x in text.split()]
seq
[11, 17, 6, 287, 316]
model.predict([seq])
array([[0.98741376]], dtype=float32)
text = "could of been so much better if properly cast directed and a better script"
seq = [word_index[x] for x in text.split()]
model.predict([seq])
array([[0.9204154]], dtype=float32)
# 在自己的電腦上就不需要再安裝一次了
!pip install gradio
import gradio as gr
def imdb_score(text):
seq = [word_index[x] for x in text.split()]
score = model.predict([seq])[0][0]
return score
iface = gr.Interface(imdb_score, inputs="text", outputs="number",
title="IMDb情意分析")
iface.launch()
# 在自己的電腦上記得要設定share=True,這樣別人才看得到
# iface.launch(share=True)