made significant rework. Console version works unconstrained

This commit is contained in:
Nick
2019-09-14 19:53:11 -06:00
parent a2bb707558
commit 1673afa190
43 changed files with 118 additions and 672 deletions
-12
View File
@@ -1,12 +0,0 @@
{
"type": "service_account",
"project_id": "ai-adventure",
"private_key_id": "2bb65e3a4e2f0c0ef073cb0f10c9c26e1bb1447b",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC7nTHn4hulYkNX\nrPmkZhKemEeCvURPB/6kJnfnNGCk/bxNfM4Jg3WS8zF0KvZsBpT/6ZCctMIynwMc\nAd1ABxpIACi8Ci9gobyDsuBRd3z2V78WTPZ8/MCKIIQC5X4prGvosyBp8APr6C9z\nsFFDwzf9tLBPg0F9JlgAtuxiXDRgQRXkxNwniqkGi95QNofsgmaJyj9DlBhtWU0K\n3jWN6P8eGaV+lyRz5qhgVdp3hI9ZFSweW4XeuVWAexKcDT6tY2OZyXzlftqwVW7L\ntcYJiKWDrjwszi7De8D0g/4tB9zd4L/mDtBhpJp1O0a4tbJ4dtbIeEIM+pcDeskF\nS6B4fv2HAgMBAAECggEADWORfS/osyvJDIe+pRnXuFO7vrvNFSrH5GUOZ4SYZQ3q\nk0kV1x33lQ4f+F9ciKcLmRvNAzHvLfelHCmR5ZDcMkk4nvrHGMlSD8ADPC0f0ODA\nNVI2wQK0pubdJOvFzimM5eVqqcTEMuC2CjaKBiD3bPapInOGh+zlmN91DSvxXPEI\nxue8mmW/gmUexOKxyVm8HREFLdDOvz4Z6Ta95OvOs1ez16ROI1DFPAg0K0cnJxrm\n5Z4XW8rZTMsLPrSVTfR/HvTyXmdzk0bviGEYWnGY1wZI+MvKhpADEmaYXnbMloWh\nWk2/3YitKMGeUwSVX/9+6VFiDhI+8uuNV/WzsKLFOQKBgQDrvGTTjJP30qH2Qosx\nOp4T9GdF2n77B1hjJ8pd6tPKbG5C8nWbogPVYqG0skEI3iSgyGbMNs9WXy84ghfU\n/QlFe9muUDWUv4bJOwRulYJhXqbYpE2aRsz09NcH9hJk0tY8fP6ow/QXUKXtjB2S\nq+DuU49bHMjmzE+1PmOHzgosbQKBgQDLvdSz6+YcwEbk0tuGNAaB1N98LwIlORLb\nzXnPOeWNMZ67F18D1RDTtqHo0rYQZpMqJqotGbLfWRuup5/qLe1aaT/sLRQuMzqK\nrDDIEf7AeH6MlKqnhArv/g1BQSFrRaa8ju9Nzl/1MF0hTwc3d86CjG4b5dUg9mNi\n16dA4NaxQwKBgQDS2hipK/Vx1iHt/ck4iEC6goIY9f1cFU7WqdGVPaSgS2o4UVwm\nj7y0EVuoH0SSZPgIJBfmkyPTEqJO/fXJGWdAki27f5zdZo5t0W7+ZpZO93VvE08i\nLi/G7nSjd1MugXMEeZziYRsj+4vetNQCy+AN7Amuqkl8PsUQzTQDVaS95QKBgAop\npON4n9jNSnv9fI+8JprxSFiEa8H5j9RA8pOAms+pKvbAGrAxP46f7IGViRlWVH7k\nwNZREtgLNNNIbzSaJ927pEGemgQX8BKcti40n5VwLTfL7ps+feEstfKMUiNsrP14\nz7z8z1KGoA6aR4zfwABJCvD/TNtBDwIOKbyVun/LAoGAGc1B0SFY2a3LyJ3CFMQD\n3+Y6Sam5w1dlkB/yECFeLN59fk0NErjHmlntIu6fgWMglvzvU5RESSOnctbh1wBi\nkyj/OdjrS76dAH4HfICJgS9gcLFixphU9JqOKd5rZfnZh52AFWs3aeEmpmoN5yyu\nnVir78ePOnyRJzLKm2iql1I=\n-----END PRIVATE KEY-----\n",
"client_email": "ai-adventure@appspot.gserviceaccount.com",
"client_id": "100390463556501175877",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/ai-adventure%40appspot.gserviceaccount.com"
}
+13 -7
View File
@@ -3,22 +3,28 @@ from story.utils import *
from google.cloud import storage
import json
from story.story_manager import *
from generator.tf_local.generator_local import *
from generator.web.web_generator import *
import tensorflow as tf
import textwrap
# Set the key
def console_print(str):
LINE_WIDTH=80
print((textwrap.fill(str, 80)))
if __name__ == '__main__':
sess = tf.Session()
generator = LocalGenerator(sess)
generator = WebGenerator("./AI-Adventure-2bb65e3a4e2f.json")
prompt = "You enter a dungeon with your trusty sword and shield. You are searching for the evil necromancer who killed your family. You've heard that he resides at the bottom of the dungeon, guarded by legions of the undead. You enter the first door and see"
story_manager = UnconstrainedStoryManager(generator, prompt)
print(story_manager.story)
console_print(str(story_manager.story))
while(True):
action = input("> ")
action = "You " + action
print(action)
result = story_manager.act(action)
print(result)
console_print(action + result)
-468
View File
@@ -1,468 +0,0 @@
#!/bin/bash
# Copyright 2019 Cortex Labs, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -e
####################
### FLAG PARSING ###
####################
flag_help=false
positional_args=()
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-c|--config)
export CORTEX_CONFIG="$2"
shift
shift
;;
-h|--help)
flag_help="true"
shift
;;
*)
positional_args+=("$1")
shift
;;
esac
done
set -- "${positional_args[@]}"
positional_args=()
for i in "$@"; do
case $i in
-c=*|--config=*)
export CORTEX_CONFIG="${i#*=}"
shift
;;
-h=*|--help=*)
flag_help="true"
;;
*)
positional_args+=("$1")
shift
;;
esac
done
set -- "${positional_args[@]}"
if [ "$flag_help" == "true" ]; then
show_help
exit 0
fi
for arg in "$@"; do
if [[ "$arg" == -* ]]; then
echo "unknown flag: $arg"
show_help
exit 1
fi
done
#####################
### CONFIGURATION ###
#####################
if [ "$CORTEX_CONFIG" != "" ]; then
if [ ! -f "$CORTEX_CONFIG" ]; then
echo "Cortex config file does not exist: $CORTEX_CONFIG"
exit 1
fi
source $CORTEX_CONFIG
fi
set -u
export CORTEX_VERSION_STABLE=0.7.3
# Defaults
export AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID:-""}"
if [ "$AWS_ACCESS_KEY_ID" = "" ]; then
echo -e "\nPlease set AWS_ACCESS_KEY_ID"
exit 1
fi
export AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY:-""}"
if [ "$AWS_SECRET_ACCESS_KEY" = "" ]; then
echo -e "\nPlease set AWS_SECRET_ACCESS_KEY"
exit 1
fi
export CORTEX_LOG_GROUP="${CORTEX_LOG_GROUP:-cortex}"
export CORTEX_BUCKET="${CORTEX_BUCKET:-""}"
export CORTEX_REGION="${CORTEX_REGION:-us-west-2}"
export CORTEX_ZONES="${CORTEX_ZONES:-""}"
export CORTEX_CLUSTER="${CORTEX_CLUSTER:-cortex}"
export CORTEX_NODE_TYPE="${CORTEX_NODE_TYPE:-t3.large}"
export CORTEX_NODES_MIN="${CORTEX_NODES_MIN:-2}"
export CORTEX_NODES_MAX="${CORTEX_NODES_MAX:-5}"
export CORTEX_NAMESPACE="${CORTEX_NAMESPACE:-cortex}"
export CORTEX_IMAGE_MANAGER="${CORTEX_IMAGE_MANAGER:-cortexlabs/manager:$CORTEX_VERSION_STABLE}"
export CORTEX_IMAGE_FLUENTD="${CORTEX_IMAGE_FLUENTD:-cortexlabs/fluentd:$CORTEX_VERSION_STABLE}"
export CORTEX_IMAGE_NGINX_BACKEND="${CORTEX_IMAGE_NGINX_BACKEND:-cortexlabs/nginx-backend:$CORTEX_VERSION_STABLE}"
export CORTEX_IMAGE_NGINX_CONTROLLER="${CORTEX_IMAGE_NGINX_CONTROLLER:-cortexlabs/nginx-controller:$CORTEX_VERSION_STABLE}"
export CORTEX_IMAGE_OPERATOR="${CORTEX_IMAGE_OPERATOR:-cortexlabs/operator:$CORTEX_VERSION_STABLE}"
export CORTEX_IMAGE_SPARK="${CORTEX_IMAGE_SPARK:-cortexlabs/spark:$CORTEX_VERSION_STABLE}"
export CORTEX_IMAGE_SPARK_OPERATOR="${CORTEX_IMAGE_SPARK_OPERATOR:-cortexlabs/spark-operator:$CORTEX_VERSION_STABLE}"
export CORTEX_IMAGE_TF_SERVE="${CORTEX_IMAGE_TF_SERVE:-cortexlabs/tf-serve:$CORTEX_VERSION_STABLE}"
export CORTEX_IMAGE_TF_TRAIN="${CORTEX_IMAGE_TF_TRAIN:-cortexlabs/tf-train:$CORTEX_VERSION_STABLE}"
export CORTEX_IMAGE_TF_API="${CORTEX_IMAGE_TF_API:-cortexlabs/tf-api:$CORTEX_VERSION_STABLE}"
export CORTEX_IMAGE_PYTHON_PACKAGER="${CORTEX_IMAGE_PYTHON_PACKAGER:-cortexlabs/python-packager:$CORTEX_VERSION_STABLE}"
export CORTEX_IMAGE_TF_SERVE_GPU="${CORTEX_IMAGE_TF_SERVE_GPU:-cortexlabs/tf-serve-gpu:$CORTEX_VERSION_STABLE}"
export CORTEX_IMAGE_TF_TRAIN_GPU="${CORTEX_IMAGE_TF_TRAIN_GPU:-cortexlabs/tf-train-gpu:$CORTEX_VERSION_STABLE}"
export CORTEX_IMAGE_ONNX_SERVE="${CORTEX_IMAGE_ONNX_SERVE:-cortexlabs/onnx-serve:$CORTEX_VERSION_STABLE}"
export CORTEX_IMAGE_ONNX_SERVE_GPU="${CORTEX_IMAGE_ONNX_SERVE_GPU:-cortexlabs/onnx-serve-gpu:$CORTEX_VERSION_STABLE}"
export CORTEX_IMAGE_CLUSTER_AUTOSCALER="${CORTEX_IMAGE_CLUSTER_AUTOSCALER:-cortexlabs/cluster-autoscaler:$CORTEX_VERSION_STABLE}"
export CORTEX_IMAGE_NVIDIA="${CORTEX_IMAGE_NVIDIA:-cortexlabs/nvidia:$CORTEX_VERSION_STABLE}"
export CORTEX_IMAGE_METRICS_SERVER="${CORTEX_IMAGE_METRICS_SERVER:-cortexlabs/metrics-server:$CORTEX_VERSION_STABLE}"
export CORTEX_ENABLE_TELEMETRY="${CORTEX_ENABLE_TELEMETRY:-""}"
##########################
### TOP-LEVEL COMMANDS ###
##########################
function install_eks() {
echo
docker run -it --entrypoint /root/install_eks.sh \
-e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID \
-e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY \
-e CORTEX_CLUSTER=$CORTEX_CLUSTER \
-e CORTEX_REGION=$CORTEX_REGION \
-e CORTEX_NODE_TYPE=$CORTEX_NODE_TYPE \
-e CORTEX_NODES_MIN=$CORTEX_NODES_MIN \
-e CORTEX_NODES_MAX=$CORTEX_NODES_MAX \
$CORTEX_IMAGE_MANAGER
}
function uninstall_eks() {
echo
docker run -it --entrypoint /root/uninstall_eks.sh \
-e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID \
-e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY \
-e CORTEX_CLUSTER=$CORTEX_CLUSTER \
-e CORTEX_REGION=$CORTEX_REGION \
$CORTEX_IMAGE_MANAGER
}
function install_cortex() {
echo
docker run -it --entrypoint /root/install_cortex.sh \
-e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID \
-e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY \
-e CORTEX_CLUSTER=$CORTEX_CLUSTER \
-e CORTEX_REGION=$CORTEX_REGION \
-e CORTEX_NAMESPACE=$CORTEX_NAMESPACE \
-e CORTEX_NODE_TYPE=$CORTEX_NODE_TYPE \
-e CORTEX_LOG_GROUP=$CORTEX_LOG_GROUP \
-e CORTEX_BUCKET=$CORTEX_BUCKET \
-e CORTEX_IMAGE_FLUENTD=$CORTEX_IMAGE_FLUENTD \
-e CORTEX_IMAGE_NGINX_BACKEND=$CORTEX_IMAGE_NGINX_BACKEND \
-e CORTEX_IMAGE_NGINX_CONTROLLER=$CORTEX_IMAGE_NGINX_CONTROLLER \
-e CORTEX_IMAGE_OPERATOR=$CORTEX_IMAGE_OPERATOR \
-e CORTEX_IMAGE_SPARK=$CORTEX_IMAGE_SPARK \
-e CORTEX_IMAGE_SPARK_OPERATOR=$CORTEX_IMAGE_SPARK_OPERATOR \
-e CORTEX_IMAGE_TF_SERVE=$CORTEX_IMAGE_TF_SERVE \
-e CORTEX_IMAGE_TF_TRAIN=$CORTEX_IMAGE_TF_TRAIN \
-e CORTEX_IMAGE_TF_API=$CORTEX_IMAGE_TF_API \
-e CORTEX_IMAGE_PYTHON_PACKAGER=$CORTEX_IMAGE_PYTHON_PACKAGER \
-e CORTEX_IMAGE_TF_SERVE_GPU=$CORTEX_IMAGE_TF_SERVE_GPU \
-e CORTEX_IMAGE_TF_TRAIN_GPU=$CORTEX_IMAGE_TF_TRAIN_GPU \
-e CORTEX_IMAGE_ONNX_SERVE=$CORTEX_IMAGE_ONNX_SERVE \
-e CORTEX_IMAGE_ONNX_SERVE_GPU=$CORTEX_IMAGE_ONNX_SERVE_GPU \
-e CORTEX_IMAGE_CLUSTER_AUTOSCALER=$CORTEX_IMAGE_CLUSTER_AUTOSCALER \
-e CORTEX_IMAGE_NVIDIA=$CORTEX_IMAGE_NVIDIA \
-e CORTEX_IMAGE_METRICS_SERVER=$CORTEX_IMAGE_METRICS_SERVER \
-e CORTEX_ENABLE_TELEMETRY=$CORTEX_ENABLE_TELEMETRY \
$CORTEX_IMAGE_MANAGER
}
function uninstall_operator() {
echo
docker run -it --entrypoint /root/uninstall_operator.sh \
-e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID \
-e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY \
-e CORTEX_CLUSTER=$CORTEX_CLUSTER \
-e CORTEX_REGION=$CORTEX_REGION \
-e CORTEX_NAMESPACE=$CORTEX_NAMESPACE \
$CORTEX_IMAGE_MANAGER
}
function info() {
echo
docker run -it --entrypoint /root/info.sh \
-e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID \
-e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY \
-e CORTEX_CLUSTER=$CORTEX_CLUSTER \
-e CORTEX_REGION=$CORTEX_REGION \
-e CORTEX_NAMESPACE=$CORTEX_NAMESPACE \
$CORTEX_IMAGE_MANAGER
}
################
### CHECK OS ###
################
case "$OSTYPE" in
darwin*) PARSED_OS="darwin" ;;
linux*) PARSED_OS="linux" ;;
*) echo -e "\nerror: only mac and linux are supported"; exit 1 ;;
esac
#############################
### DEPENDENCY MANAGEMENT ###
#############################
function check_dep_curl() {
if ! command -v curl >/dev/null; then
echo -e "\nerror: please install \`curl\`"
exit 1
fi
}
function install_cli() {
set -e
check_dep_curl
echo -e "\nInstalling the Cortex CLI (/usr/local/bin/cortex) ..."
CORTEX_SH_TMP_DIR="$HOME/.cortex-sh-tmp"
rm -rf $CORTEX_SH_TMP_DIR && mkdir -p $CORTEX_SH_TMP_DIR
curl -s -o $CORTEX_SH_TMP_DIR/cortex https://s3-us-west-2.amazonaws.com/get-cortex/$CORTEX_VERSION_STABLE/cli/$PARSED_OS/cortex
chmod +x $CORTEX_SH_TMP_DIR/cortex
if [ $(id -u) = 0 ]; then
mv -f $CORTEX_SH_TMP_DIR/cortex /usr/local/bin/cortex
else
ask_sudo
sudo mv -f $CORTEX_SH_TMP_DIR/cortex /usr/local/bin/cortex
fi
rm -rf $CORTEX_SH_TMP_DIR
echo "✓ Installed the Cortex CLI"
bash_profile_path=$(get_bash_profile)
if [ ! "$bash_profile_path" = "" ]; then
if ! grep -Fxq "source <(cortex completion)" "$bash_profile_path"; then
echo
read -p "Would you like to modify your bash profile ($bash_profile_path) to enable cortex command completion and the cx alias? [Y/n] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo -e "\nsource <(cortex completion)" >> $bash_profile_path
echo "✓ Your bash profile ($bash_profile_path) has been updated"
echo
echo "Note: \`bash_completion\` must be installed on your system for cortex command completion to function properly"
echo
echo "Command to update your current terminal session:"
echo " source $bash_profile_path"
else
echo "Your bash profile has not been modified. If you would like to modify it manually, add this line to your bash profile:"
echo " source <(cortex completion)"
echo "Note: \`bash_completion\` must be installed on your system for cortex command completion to function properly"
fi
fi
else
echo -e "\nIf your would like to enable cortex command completion and the cx alias, add this line to your bash profile:"
echo " source <(cortex completion)"
echo "Note: \`bash_completion\` must be installed on your system for cortex command completion to function properly"
fi
}
function uninstall_cli() {
set -e
rm -rf $HOME/.cortex
if ! command -v cortex >/dev/null; then
echo -e "\nThe Cortex CLI is not installed"
return
fi
if [[ ! -f /usr/local/bin/cortex ]]; then
echo -e "\nThe Cortex CLI was not found at /usr/local/bin/cortex, please uninstall it manually"
return
fi
if [ $(id -u) = 0 ]; then
rm /usr/local/bin/cortex
else
ask_sudo
sudo rm /usr/local/bin/cortex
fi
echo -e "\n✓ Uninstalled the Cortex CLI"
bash_profile_path=$(get_bash_profile)
if [ ! "$bash_profile_path" = "" ]; then
if grep -Fxq "source <(cortex completion)" "$bash_profile_path"; then
echo
read -p "Would you like to remove \"source <(cortex completion)\" from your bash profile ($bash_profile_path)? [Y/n] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
sed '/^source <(cortex completion)$/d' "$bash_profile_path" > "${bash_profile_path}_cortex_modified" && mv -f "${bash_profile_path}_cortex_modified" "$bash_profile_path"
echo "✓ Your bash profile ($bash_profile_path) has been updated"
fi
fi
fi
}
function get_bash_profile() {
if [ "$PARSED_OS" = "darwin" ]; then
if [ -f $HOME/.bash_profile ]; then
echo $HOME/.bash_profile
return
elif [ -f $HOME/.bashrc ]; then
echo $HOME/.bashrc
return
fi
else
if [ -f $HOME/.bashrc ]; then
echo $HOME/.bashrc
return
elif [ -f $HOME/.bash_profile ]; then
echo $HOME/.bash_profile
return
fi
fi
echo ""
}
function ask_sudo() {
if ! sudo -n true 2>/dev/null; then
echo -e "\nPlease enter your sudo password"
fi
}
function prompt_for_telemetry() {
if [ "$CORTEX_ENABLE_TELEMETRY" != "true" ] && [ "$CORTEX_ENABLE_TELEMETRY" != "false" ]; then
while true
do
echo
read -p "Would you like to help improve Cortex by anonymously sending error reports and usage stats to the dev team? [Y/n] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
export CORTEX_ENABLE_TELEMETRY=true
break
elif [[ $REPLY =~ ^[Nn]$ ]]; then
export CORTEX_ENABLE_TELEMETRY=false
break
fi
echo "Unexpected value, please enter \"Y\" or \"n\""
done
fi
}
############
### HELP ###
############
function show_help() {
echo "
Usage:
./cortex.sh command [sub-command] [flags]
Available Commands:
install install Cortex
uninstall uninstall Cortex
update update Cortex
info information about Cortex
install cli install the Cortex CLI
uninstall cli uninstall the Cortex CLI
Flags:
-c, --config path to a Cortex config file
-h, --help
"
}
######################
### ARG PROCESSING ###
######################
arg1=${1:-""}
arg2=${2:-""}
arg3=${3:-""}
if [ -z "$arg1" ]; then
show_help
exit 0
fi
if [ "$arg1" = "install" ]; then
if [ ! "$arg3" = "" ]; then
echo -e "\nerror: too many arguments for install command"
show_help
exit 1
elif [ "$arg2" = "" ]; then
prompt_for_telemetry && install_eks && install_cortex && info
elif [ "$arg2" = "cli" ]; then
install_cli
elif [ "$arg2" = "" ]; then
echo -e "\nerror: missing subcommand for install"
show_help
exit 1
else
echo -e "\nerror: invalid subcommand for install: $arg2"
show_help
exit 1
fi
elif [ "$arg1" = "uninstall" ]; then
if [ ! "$arg3" = "" ]; then
echo -e "\nerror: too many arguments for uninstall command"
show_help
exit 1
elif [ "$arg2" = "" ]; then
uninstall_eks
elif [ "$arg2" = "cli" ]; then
uninstall_cli
elif [ "$arg2" = "" ]; then
echo -e "\nerror: missing subcommand for uninstall"
show_help
exit 1
else
echo -e "\nerror: invalid subcommand for uninstall: $arg2"
show_help
exit 1
fi
elif [ "$arg1" = "update" ]; then
if [ ! "$arg2" = "" ]; then
echo -e "\nerror: too many arguments for get command"
show_help
exit 1
else
uninstall_operator && install_cortex
fi
elif [ "$arg1" = "info" ]; then
if [ ! "$arg2" = "" ]; then
echo -e "\nerror: too many arguments for get command"
show_help
exit 1
else
info
fi
else
echo -e "\nerror: unknown command: $arg1"
show_help
exit 1
fi
-11
View File
@@ -1,11 +0,0 @@
# cortex.yaml
- kind: deployment
name: text
- kind: api
name: generator
model: s3://cortex-examples/gpt-2/774M/1539234541.zip
request_handler: encoder.py
compute:
gpu: 1
min_replicas: 1
max_replicas: 1
-129
View File
@@ -1,129 +0,0 @@
# This file includes code which was modified from https://github.com/openai/gpt-2
import tensorflow as tf
import os
import json
import regex as re
from functools import lru_cache
import requests
import boto3
@lru_cache()
def bytes_to_unicode():
bs = (
list(range(ord("!"), ord("~") + 1))
+ list(range(ord("¡"), ord("¬") + 1))
+ list(range(ord("®"), ord("ÿ") + 1))
)
cs = bs[:]
n = 0
for b in range(2 ** 8):
if b not in bs:
bs.append(b)
cs.append(2 ** 8 + n)
n += 1
cs = [chr(n) for n in cs]
return dict(zip(bs, cs))
def get_pairs(word):
pairs = set()
prev_char = word[0]
for char in word[1:]:
pairs.add((prev_char, char))
prev_char = char
return pairs
class Encoder:
def __init__(self, encoder, bpe_merges, errors="replace"):
self.encoder = encoder
self.decoder = {v: k for k, v in self.encoder.items()}
self.errors = errors
self.byte_encoder = bytes_to_unicode()
self.byte_decoder = {v: k for k, v in self.byte_encoder.items()}
self.bpe_ranks = dict(zip(bpe_merges, range(len(bpe_merges))))
self.cache = {}
self.pat = re.compile(
r"""'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+"""
)
def bpe(self, token):
if token in self.cache:
return self.cache[token]
word = tuple(token)
pairs = get_pairs(word)
if not pairs:
return token
while True:
bigram = min(pairs, key=lambda pair: self.bpe_ranks.get(pair, float("inf")))
if bigram not in self.bpe_ranks:
break
first, second = bigram
new_word = []
i = 0
while i < len(word):
try:
j = word.index(first, i)
new_word.extend(word[i:j])
i = j
except:
new_word.extend(word[i:])
break
if word[i] == first and i < len(word) - 1 and word[i + 1] == second:
new_word.append(first + second)
i += 2
else:
new_word.append(word[i])
i += 1
new_word = tuple(new_word)
word = new_word
if len(word) == 1:
break
else:
pairs = get_pairs(word)
word = " ".join(word)
self.cache[token] = word
return word
def encode(self, text):
bpe_tokens = []
for token in re.findall(self.pat, text):
token = "".join(self.byte_encoder[b] for b in token.encode("utf-8"))
bpe_tokens.extend(self.encoder[bpe_token] for bpe_token in self.bpe(token).split(" "))
return bpe_tokens
def decode(self, tokens):
text = "".join([self.decoder[token] for token in tokens])
text = bytearray([self.byte_decoder[c] for c in text]).decode("utf-8", errors=self.errors)
return text
def get_encoder():
s3 = boto3.client("s3")
encoder = json.load(
s3.get_object(Bucket="cortex-examples", Key="gpt-2/774M/encoder.json")["Body"]
)
bpe_data = (
s3.get_object(Bucket="cortex-examples", Key="gpt-2/774M/vocab.bpe")["Body"]
.read()
.decode("utf-8")
)
bpe_merges = [tuple(merge_str.split()) for merge_str in bpe_data.split("\n")[1:-1]]
return Encoder(encoder=encoder, bpe_merges=bpe_merges)
encoder = get_encoder()
def pre_inference(sample, metadata):
context = encoder.encode(sample["text"])
return {"context": [context]}
def post_inference(prediction, metadata):
return {encoder.decode(prediction["response"]["sample"])}
Binary file not shown.
Binary file not shown.
+2
View File
@@ -0,0 +1,2 @@
model.ckpt.data-00000-of-00001
+7
View File
@@ -0,0 +1,7 @@
{
"n_vocab": 50257,
"n_ctx": 1024,
"n_embd": 1280,
"n_head": 20,
"n_layer": 36
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,6 +1,6 @@
import tensorflow as tf
import gpt2.src.model as model
from src.model import *
def top_k_logits(logits, k):
if k == 0:
@@ -30,11 +30,11 @@ def sample_sequence(*, hparams, length, start_token=None, batch_size=None, conte
context = tf.fill([batch_size, 1], start_token)
def step(hparams, tokens, past=None):
lm_output = model.model(hparams=hparams, X=tokens, past=past, reuse=tf.AUTO_REUSE)
lm_output = model(hparams=hparams, X=tokens, past=past, reuse=tf.AUTO_REUSE)
logits = lm_output['logits'][:, :, :hparams.n_vocab]
presents = lm_output['present']
presents.set_shape(model.past_shape(hparams=hparams, batch_size=batch_size))
presents.set_shape(past_shape(hparams=hparams, batch_size=batch_size))
return {
'logits': logits,
'presents': presents,
@@ -69,7 +69,7 @@ def sample_sequence(*, hparams, length, start_token=None, batch_size=None, conte
context,
],
shape_invariants=[
tf.TensorShape(model.past_shape(hparams=hparams, batch_size=batch_size)),
tf.TensorShape(past_shape(hparams=hparams, batch_size=batch_size)),
tf.TensorShape([batch_size]),
tf.TensorShape([batch_size, None]),
],
@@ -1 +0,0 @@
model*
@@ -1,7 +0,0 @@
{
"n_vocab": 50257,
"n_ctx": 1024,
"n_embd": 768,
"n_head": 12,
"n_layer": 12
}
+44
View File
@@ -0,0 +1,44 @@
from generator.tf.src.encoder import *
import googleapiclient.discovery
pos_action_starts = ["You attack", "You tell", "You use", "You go"]
project = "ai-adventure"
model = "generator_v1"
version = "version2"
class WebGenerator():
def __init__(self, credentials_file):
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credentials_file
model_path = './generator/tf/models/117M'
self.enc = get_encoder(model_path)
def predict(self, context_tokens):
service = googleapiclient.discovery.build('ml', 'v1')
name = 'projects/{}/models/{}'.format(project, model)
instance = context_tokens
if version is not None:
name += '/versions/{}'.format(version)
response = service.projects().predict(
name=name,
body={'instances': [{'context': instance}]}
).execute()
if 'error' in response:
raise RuntimeError(response['error'])
return response['predictions']
def generate(self, prompt):
while (True):
context_tokens = self.enc.encode(prompt)
try:
pred = self.predict(context_tokens)
pred = pred[0]["output"][len(context_tokens):]
output = self.enc.decode(pred)
return output
except:
print("generate request failed, trying again")
continue
-31
View File
@@ -134,37 +134,6 @@ def about():
return render_template('about.html')
def cache_file(seed, prompt_num, choices, response, tag):
return
# blob_file_name = "prompt" + str(prompt_num) + "/seed" + str(seed) + "/" + tag
# for action in choices:
# blob_file_name = blob_file_name + str(action)
# blob = bucket.blob(blob_file_name)
#
# blob.upload_from_string(response)
#
# print("File ", blob_file_name, " cached")
def retrieve_from_cache(seed, prompt_num, choices, tag):
return None
# blob_file_name = "prompt" + str(prompt_num) + "/seed" + str(seed) + "/" + tag
#
# for action in choices:
# blob_file_name = blob_file_name + str(action)
#
# blob = bucket.blob(blob_file_name)
#
# if blob.exists(storage_client):
# result = blob.download_as_string().decode("utf-8")
# print(blob_file_name, " found in cache")
# else:
# result = None
# print(blob_file_name, " not found in cache")
#
# return result
@app.route('/generate', methods=['POST'])
def story_request():
print("****Generating Story****")
+44
View File
@@ -0,0 +1,44 @@
from flask import g
from flask import session
import os
import googleapiclient.discovery
from story.utils import *
from google.cloud import storage
import json
from flask import Flask, render_template, request, abort
from generator import StoryGenerator
import gpt2.src.encoder as encoder
# Model/Cache Info
storage_client = storage.Client()
bucket = storage_client.get_bucket("dungeon-cache")
def cache_file(seed, prompt_num, choices, response, tag):
blob_file_name = "prompt" + str(prompt_num) + "/seed" + str(seed) + "/" + tag
for action in choices:
blob_file_name = blob_file_name + str(action)
blob = bucket.blob(blob_file_name)
blob.upload_from_string(response)
print("File ", blob_file_name, " cached")
def retrieve_from_cache(seed, prompt_num, choices, tag):
blob_file_name = "prompt" + str(prompt_num) + "/seed" + str(seed) + "/" + tag
for action in choices:
blob_file_name = blob_file_name + str(action)
blob = bucket.blob(blob_file_name)
if blob.exists(storage_client):
result = blob.download_as_string().decode("utf-8")
print(blob_file_name, " found in cache")
else:
result = None
print(blob_file_name, " not found in cache")
return result
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+4 -2
View File
@@ -1,5 +1,6 @@
from story.utils import *
class Story():
def __init__(self, story_start):
@@ -28,19 +29,20 @@ class Story():
story_list.append(self.actions[i])
story_list.append(self.results[i])
return sum(story_list)
return "".join(story_list)
class UnconstrainedStoryManager():
def __init__(self, generator, story_prompt):
self.generator = generator
block = self.generator.generate(story_prompt)
block = cut_trailing_sentence(block)
block = story_replace(block)
story_start = story_prompt + block
self.story = Story(story_start)
self.generator = generator
def act(self, action_choice):