Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

standardized deployment with Docker #28

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ data/.DS_Store
*.gz
.spyproject/
.vscode/*
model.npz
chatbot.npz
37 changes: 37 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
FROM tensorflow/tensorflow:latest-py3

ARG TL_VERSION

RUN echo "Container Tag: ${TF_CONTAINER_VERSION}" \
&& apt-get update \
&& case $TF_CONTAINER_VERSION in \
latest-py3 | latest-gpu-py3) apt-get install -y python3-tk ;; \
*) apt-get install -y python-tk ;; \
esac \
&& if [ -z "$TL_VERSION" ]; then \
echo "Building a Nightly Release" \
&& apt-get install -y git \
&& mkdir /dist/ && cd /dist/ \
&& git clone https://github.com/tensorlayer/tensorlayer.git \
&& cd tensorlayer \
&& pip install --disable-pip-version-check --no-cache-dir --upgrade -e .[all]; \
else \
echo "Building Tag Release: $TL_VERSION" \
&& pip install --disable-pip-version-check --no-cache-dir --upgrade tensorlayer[all]=="$TL_VERSION"; \
fi \
&& apt-get autoremove -y \
&& rm -rf /var/lib/apt/lists/*

#Environment Variables for Click
ENV LC_ALL=C.UTF-8
ENV LANG=C.UTF-8

COPY requirements.txt /tmp/
RUN pip3 install --requirement /tmp/requirements.txt

COPY . /chatbot

WORKDIR /chatbot
RUN python3 main.py --batch-size 32 --num-epochs 1 -lr 0.001

CMD python3 main.py --inference-mode
Empty file added data/twitter/__init__.py
Empty file.
8 changes: 3 additions & 5 deletions data/twitter/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,9 @@ def process_data():

def load_data(PATH=''):
# read data control dictionaries
try:
with open(PATH + 'metadata.pkl', 'rb') as f:
metadata = pickle.load(f)
except:
metadata = None
with open(PATH + 'metadata.pkl', 'rb') as f:
metadata = pickle.load(f)

# read numpy arrays
idx_q = np.load(PATH + 'idx_q.npy')
idx_a = np.load(PATH + 'idx_a.npy')
Expand Down
7 changes: 7 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: '2.1'

services:
chatbot:
build: .
volumes:
- ./models:/chatbot/models/
23 changes: 12 additions & 11 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def train(data_corpus, batch_size, num_epochs, learning_rate, inference_mode):
encode_seqs = tf.placeholder(dtype=tf.int64, shape=[batch_size, None], name="encode_seqs")
decode_seqs = tf.placeholder(dtype=tf.int64, shape=[batch_size, None], name="decode_seqs")
target_seqs = tf.placeholder(dtype=tf.int64, shape=[batch_size, None], name="target_seqs")
target_mask = tf.placeholder(dtype=tf.int64, shape=[batch_size, None], name="target_mask")
target_mask = tf.placeholder(dtype=tf.int64, shape=[batch_size, None], name="target_mask")

net_out, _ = create_model(encode_seqs, decode_seqs, src_vocab_size, emb_dim, is_train=True, reuse=False)
net_out.print_params(False)
Expand All @@ -97,7 +97,7 @@ def train(data_corpus, batch_size, num_epochs, learning_rate, inference_mode):
y = tf.nn.softmax(net.outputs)

# Loss Function
loss = tl.cost.cross_entropy_seq_with_mask(logits=net_out.outputs, target_seqs=target_seqs,
loss = tl.cost.cross_entropy_seq_with_mask(logits=net_out.outputs, target_seqs=target_seqs,
input_mask=target_mask, return_details=False, name='cost')

# Optimizer
Expand All @@ -107,14 +107,14 @@ def train(data_corpus, batch_size, num_epochs, learning_rate, inference_mode):
sess.run(tf.global_variables_initializer())

# Load Model
tl.files.load_and_assign_npz(sess=sess, name='model.npz', network=net)
tl.files.load_and_assign_npz(sess=sess, name='/chatbot/models/chatbot.npz', network=net)

"""
Inference using pre-trained model
"""
def inference(seed):
seed_id = [word2idx.get(w, unk_id) for w in seed.split(" ")]

# Encode and get state
state = sess.run(net_rnn.final_state_encode,
{encode_seqs2: [seed_id]})
Expand Down Expand Up @@ -150,7 +150,7 @@ def inference(seed):
for epoch in range(num_epochs):
trainX, trainY = shuffle(trainX, trainY, random_state=0)
total_loss, n_iter = 0, 0
for X, Y in tqdm(tl.iterate.minibatches(inputs=trainX, targets=trainY, batch_size=batch_size, shuffle=False),
for X, Y in tqdm(tl.iterate.minibatches(inputs=trainX, targets=trainY, batch_size=batch_size, shuffle=False),
total=n_step, desc='Epoch[{}/{}]'.format(epoch + 1, num_epochs), leave=False):

X = tl.prepro.pad_sequences(X)
Expand All @@ -174,17 +174,17 @@ def inference(seed):

# printing average loss after every epoch
print('Epoch [{}/{}]: loss {:.4f}'.format(epoch + 1, num_epochs, total_loss / n_iter))

# inference after every epoch
for seed in seeds:
print("Query >", seed)
for _ in range(5):
sentence = inference(seed)
print(" >", ' '.join(sentence))

# saving the model
tl.files.save_npz(net.all_params, name='model.npz', sess=sess)
tl.files.save_npz(net.all_params, name='/chatbot/models/chatbot.npz', sess=sess)

# session cleanup
sess.close()

Expand All @@ -207,7 +207,7 @@ def create_model(encode_seqs, decode_seqs, src_vocab_size, emb_dim, is_train=Tru
vocabulary_size = src_vocab_size,
embedding_size = emb_dim,
name = 'seq_embedding')

net_rnn = Seq2Seq(net_encode, net_decode,
cell_fn = tf.nn.rnn_cell.LSTMCell,
n_hidden = emb_dim,
Expand All @@ -227,7 +227,8 @@ def create_model(encode_seqs, decode_seqs, src_vocab_size, emb_dim, is_train=Tru
Initial Setup
"""
def initial_setup(data_corpus):
metadata, idx_q, idx_a = data.load_data(PATH='data/{}/'.format(data_corpus))
#metadata, idx_q, idx_a = data.load_data(PATH='data/{}/'.format(data_corpus))
metadata, idx_q, idx_a = data.load_data(PATH='./data/twitter/')
(trainX, trainY), (testX, testY), (validX, validY) = data.split_dataset(idx_q, idx_a)
trainX = tl.prepro.remove_pad_sequences(trainX.tolist())
trainY = tl.prepro.remove_pad_sequences(trainY.tolist())
Expand Down
4 changes: 4 additions & 0 deletions models/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore