Merge pull request #3 from nickwalton/refactor

Refactor
This commit is contained in:
Nick Walton
2019-09-14 20:24:48 -06:00
committed by GitHub
56 changed files with 394 additions and 789 deletions
+1 -1
View File
@@ -1,3 +1,3 @@
**/__pychache__
.idea
RL
*.json
-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"
}
+1 -1
View File
@@ -1 +1 @@
# Keys
#AIDungeon Code
Binary file not shown.
Binary file not shown.
+80
View File
@@ -0,0 +1,80 @@
import os
from story.utils import *
from google.cloud import storage
import json
from story.story_manager 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)))
def play_unconstrained():
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)
console_print(str(story_manager.story))
while (True):
action = input("> ")
action = "You " + action
result = story_manager.act(action)
console_print(action + result)
#
#
#
# def act(self, action_choice):
#
# action, result = self.possible_action_results[action_choice]
# self.story.add_to_story(action, result)
# self.possible_action_results = self.get_action_results()
# return result, self.possible_action_results
#
# def story_context(self):
# return self.story.latest_result()
#
# def get_action_results(self):
# return [self.generate_action_result(self.story_context(), phrase) for phrase in self.action_phrases]
#
# def generate_action_result(self, prompt, phrase):
# action = phrase + self.generator.generate(prompt + phrase)
# action_result = cut_trailing_sentence(action)
#
# action, result = split_first_sentence(action_result)
# result = story_replace(action_result)
# action = action_replace(action)
#
# return action, result
def play_constrained():
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 = ConstrainedStoryManager(generator, prompt)
console_print(str(story_manager.story))
possible_actions = story_manager.get_possible_actions()
while (True):
console_print("\nOptions:")
for i, action in enumerate(possible_actions):
console_print(str(i) + ") " + action)
result = None
while(result == None):
action_choice = input("Which action do you choose? ")
print("\n")
result, possible_actions = story_manager.act(action_choice)
console_print(result)
if __name__ == '__main__':
play_constrained()
-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
Binary file not shown.
Binary file not shown.
@@ -3,16 +3,16 @@ import os
import numpy as np
import tensorflow as tf
import gpt2.src.model as model
from src.model import *
from tensorflow.contrib import predictor
import gpt2.src.sample as sample
import gpt2.src.encoder as encoder
from utils import *
from src.sample import *
from src.encoder import *
import pdb
pos_action_starts = ["You attack", "You tell", "You use", "You go"]
class StoryGenerator():
class LocalGenerator():
def __init__(self, sess, length=75, temperature=0.9, top_k=40):
@@ -24,7 +24,9 @@ class StoryGenerator():
self.enc = encoder.get_encoder(model_path)
hparams = model.default_hparams()
with open(os.path.join(model_path, 'hparams.json')) as f:
hparams.override_from_dict(json.load(f))
hparams.override_from_dict(json.load(f))
pdb.set_trace()
self.context = tf.placeholder(tf.int32, [batch_size, None])
np.random.seed(seed)
@@ -38,8 +40,7 @@ class StoryGenerator():
saver = tf.train.Saver()
ckpt = tf.train.latest_checkpoint(model_path)
saver.restore(self.sess, ckpt)
def generate(self, prompt):
context_tokens = self.enc.encode(prompt)
out = self.sess.run(self.output, feed_dict={
@@ -48,53 +49,26 @@ class StoryGenerator():
text = self.enc.decode(out[0])
return text
def generate_story_block(self, prompt):
block = self.generate(prompt)
block = cut_trailing_sentence(block)
block = story_replace(block)
return block
def generate_action_options(self, prompt, action_starts=pos_action_starts):
possible_actions = []
for phrase in action_starts:
action = phrase + self.generate(prompt + phrase)
action = first_sentence(action)
possible_actions.append(action)
return possible_actions
def generate_action_result(self, prompt, phrase):
action = phrase + self.generate(prompt + phrase)
action_result = cut_trailing_sentence(action)
action_result = story_replace(action_result)
action = first_sentence(action)
return action, action_result
def save_model():
length=75
temperature=0.9
top_k=40
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
with tf.Session() as sess:
seed = None
batch_size=None
model_path='gpt2/models/117M'
model_path='models/774M'
hparams = model.default_hparams()
hparams = default_hparams()
with open(os.path.join(model_path, 'hparams.json')) as f:
hparams.override_from_dict(json.load(f))
context = tf.placeholder(tf.int32, [batch_size, None])
np.random.seed(seed)
tf.set_random_seed(seed)
output = sample.sample_sequence(
output = sample_sequence(
hparams=hparams, length=length,
context=context,
batch_size=batch_size,
@@ -105,33 +79,20 @@ def save_model():
ckpt = tf.train.latest_checkpoint(model_path)
saver.restore(sess, ckpt)
tf.saved_model.simple_save(sess, "./saved2", inputs={"context": context}, outputs={"output": output})
def generate_gpu_config(memory_fraction):
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.gpu_options.per_process_gpu_memory_fraction = memory_fraction
return config
def run_interactive():
pass
tf.saved_model.simple_save(sess, "./saved_model", inputs={"context": context}, outputs={"output": output})
def load_model():
# Set your memory fraction equal to a value less than 1, 0.6 is a good starting point.
# If no fraction is defined, the tensorflow algorithm may run into gpu out of memory problems.
fraction = 0.6
config = config=generate_gpu_config(fraction)
config = config = generate_gpu_config(fraction)
path_to_graph = "./saved"
#tf.saved_model.loader.load(
# session,
# [tf.saved_model.tag_constants.SERVING],
# path_to_graph)
# tf.saved_model.loader.load(
# session,
# [tf.saved_model.tag_constants.SERVING],
# path_to_graph)
#output = session.graph.get_tensor_by_name('output:0')
#context = session.graph.get_tensor_by_name('context:0')
# output = session.graph.get_tensor_by_name('output:0')
# context = session.graph.get_tensor_by_name('context:0')
model_path = 'gpt2/models/117M'
enc = encoder.get_encoder(model_path)
@@ -143,6 +104,7 @@ def load_model():
return (output, session)
if __name__ == '__main__':
save_model()
+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.
View File
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]),
],
+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
-1
View File
@@ -1 +0,0 @@
model*
-7
View File
@@ -1,7 +0,0 @@
{
"n_vocab": 50257,
"n_ctx": 1024,
"n_embd": 768,
"n_head": 12,
"n_layer": 12
}
Binary file not shown.
Binary file not shown.
+18 -218
View File
@@ -1,133 +1,17 @@
# Copyright 2018 Google LLC
#
# 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.
# [START gae_python37_render_template]
import datetime
from flask import g
from flask import session
import os
import googleapiclient.discovery
from utils import *
from google.cloud import storage
from google import cloud
from story.utils import *
import json
from flask import Flask, render_template, request, abort
from flask import Response
import requests
import pdb
import sys
from generator import StoryGenerator
import gpt2.src.encoder as encoder
from story.story_manager import *
from generator.web.web_generator import *
from other.caching import *
# App Info
phrases = [" You attack", " You use", " You tell", " You go"]
prompts = ["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"]
continuing_prompts = ["You are in a dungeon with your sword and shield. You are on a quest to defeat the necromancer. This dungeon is full of zombie and skeletons."]
app = Flask(__name__)
# Encoder Info
encoder_path='gpt2/models/117M'
enc = encoder.get_encoder(encoder_path)
# Model/Cache Info
project = "ai-adventure"
model = "generator_v1"
version = "version2"
os.environ['GOOGLE_APPLICATION_CREDENTIALS']="./AI-Adventure-2bb65e3a4e2f.json"
storage_client = storage.Client()
bucket = storage_client.get_bucket("dungeon-cache")
# Local generator functionality
RUN_LOCAL = True
session = None
local_generator = None
def get_local_generator():
if "gen" not in g:
if "sess" not in g:
g.sess = tf.Session()
g.gen = StoryGenerator(g.sess)
return g.gen
app.secret_key = '#d\xe0\xd1\xfb\xee\xa4\xbb\xd0\xf0/e)\xb5g\xdd<`\xc7\xa5\xb0-\xb8d0S'
@app.teardown_appcontext
def teardown_sess(_):
sess = g.pop("sess", None)
if sess is not None:
sess.close()
def predict(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(prompt):
while(True):
context_tokens = enc.encode(prompt)
try:
pred = predict(context_tokens)
pred = pred[0]["output"][len(context_tokens):]
output = enc.decode(pred)
return output
except:
print("generate request failed, trying again")
continue
def generate_story_block(prompt, local=False):
if local:
generator = get_local_generator()
block = generator.generate(prompt)
else:
block = generate(prompt)
block = cut_trailing_sentence(block)
block = story_replace(block)
return block
def generate_action_result(prompt, phrase, local=False):
if local:
generator = get_local_generator()
action = phrase + generator.generate(prompt + phrase)
else:
action = phrase + generate(prompt + phrase)
action_result = cut_trailing_sentence(action)
action_result = story_replace(action_result)
action = first_sentence(action)
return action, action_result
@app.route('/')
def root():
seed = -1
@@ -142,49 +26,21 @@ def rootseed(seed):
else:
seed = int(seed)
data = {'seed': seed}
session["seed"] = seed
return render_template('index.html', data=data)
@app.route('/index.html')
def index():
data = {'seed': -1}
return render_template('index.html', data=data)
@app.route('/about.html')
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****")
@@ -192,15 +48,17 @@ def story_request():
prompt_num = int(request.form["prompt_num"])
gen_actions = request.form["actions"]
print("Session Seed is ", session["seed"])
if int(seed) < 0 or int(seed) > 100:
print("Invalid seed: " + seed)
#print("Invalid seed: " + seed)
abort(404)
if gen_actions == "true":
#prompt = request.form["prompt"]
choices = json.loads(request.form["choices"])
print("Getting response for seed ", seed, " prompt_num ", prompt_num, " and choices ", choices)
#print("Getting response for seed ", seed, " prompt_num ", prompt_num, " and choices ", choices)
action_results = retrieve_from_cache(seed, prompt_num, choices, "choices")
@@ -209,13 +67,13 @@ def story_request():
else:
last_action_result = request.form["last_action_result"]
prompt = continuing_prompts[prompt_num] + last_action_result
print("\n\nAction prompt is \n ", prompt)
#print("\n\nAction prompt is \n ", prompt)
action_results = [generate_action_result(prompt, phrase, local=RUN_LOCAL) for phrase in phrases]
response = json.dumps(action_results)
cache_file(seed, prompt_num, choices, response, "choices")
else:
print("Getting response for seed ", seed, " prompt_num ", prompt_num)
#print("Getting response for seed ", seed, " prompt_num ", prompt_num)
result = retrieve_from_cache(seed, prompt_num, [], "story")
if result is not None:
@@ -225,68 +83,10 @@ def story_request():
response = generate_story_block(prompt, local=RUN_LOCAL)
cache_file(seed, prompt_num, [], response, "story")
print("\nGenerated response is: \n", response)
print("")
#print("\nGenerated response is: \n", response)
#print("")
return response
def generate_cache():
start_seed = int(sys.argv[1])
end_seed = int(sys.argv[2])
# Generate story sections
prompt_num = 0
action_queue = []
prompt = prompts[prompt_num]
for seed in range(start_seed,end_seed):
result = retrieve_from_cache(seed, prompt_num, [], "story")
if result is not None:
response = result
else:
prompt = prompts[prompt_num]
#print("\n Story prompt is ", prompt)
response = generate_story_block(prompt)
#print("\n Story response is ", response)
cache_file(seed, prompt_num, [], response, "story")
action_queue.append([seed,0,[],response])
while(True):
next_gen = action_queue.pop(0)
seed = next_gen[0]
prompt_num = next_gen[1]
choices = next_gen[2]
last_action_result = next_gen[3]
action_results = retrieve_from_cache(seed, prompt_num, choices, "choices")
if action_results is not None:
response = action_results
else:
if len(choices) is 0:
prompt = prompts[prompt_num] + last_action_result
else:
prompt = continuing_prompts[prompt_num] + last_action_result
#print("\n\n Action prompt is \n ", prompt)
action_results = [generate_action_result(prompt, phrase) for phrase in phrases]
response = json.dumps(action_results)
#print("\n\n Action
cache_file(seed, prompt_num, choices, response, "choices")
un_jsoned = json.loads(response)
for j in range(4):
new_choices = choices[:]
new_choices.append(j)
action_queue.append([seed, 0, new_choices, un_jsoned[j][1]])
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
# [START gae_python37_render_template]
app.run(host='0.0.0.0', port=8080)
+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
View File
+51
View File
@@ -0,0 +1,51 @@
def generate_cache():
start_seed = int(sys.argv[1])
end_seed = int(sys.argv[2])
# Generate story sections
prompt_num = 0
action_queue = []
prompt = prompts[prompt_num]
for seed in range(start_seed, end_seed):
result = retrieve_from_cache(seed, prompt_num, [], "story")
if result is not None:
response = result
else:
prompt = prompts[prompt_num]
# print("\n Story prompt is ", prompt)
response = generate_story_block(prompt)
# print("\n Story response is ", response)
cache_file(seed, prompt_num, [], response, "story")
action_queue.append([seed, 0, [], response])
while (True):
next_gen = action_queue.pop(0)
seed = next_gen[0]
prompt_num = next_gen[1]
choices = next_gen[2]
last_action_result = next_gen[3]
action_results = retrieve_from_cache(seed, prompt_num, choices, "choices")
if action_results is not None:
response = action_results
else:
if len(choices) is 0:
prompt = prompts[prompt_num] + last_action_result
else:
prompt = continuing_prompts[prompt_num] + last_action_result
# print("\n\n Action prompt is \n ", prompt)
action_results = [generate_action_result(prompt, phrase) for phrase in phrases]
response = json.dumps(action_results)
# print("\n\n Action
cache_file(seed, prompt_num, choices, response, "choices")
un_jsoned = json.loads(response)
for j in range(4):
new_choices = choices[:]
new_choices.append(j)
action_queue.append([seed, 0, new_choices, un_jsoned[j][1]])
Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+112
View File
@@ -0,0 +1,112 @@
from story.utils import *
class Story():
def __init__(self, story_start):
self.story_start = story_start
# list of actions. First action is the prompt length should always equal that of story blocks
self.actions = []
# list of story blocks first story block follows prompt and is intro story
self.results = []
def add_to_story(self, action, story_block):
self.actions.append(action)
self.results.append(story_block)
def latest_result(self):
if len(self.results) > 0:
return self.results[-1]
else:
return ""
def __str__(self):
story_list = [self.story_start]
for i in range(len(self.results)):
story_list.append(self.actions[i])
story_list.append(self.results[i])
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)
def act(self, action_choice):
result = self.generate_result(action_choice)
self.story.add_to_story(action_choice, result)
return result
def story_context(self):
return self.story.latest_result()
def generate_result(self, action):
block = self.generator.generate(self.story_context() + action)
block = cut_trailing_sentence(block)
block = story_replace(block)
return block
class ConstrainedStoryManager():
def __init__(self, generator, story_prompt):
self.generator = generator
self.action_phrases = ["You attack", "You tell", "You use", "You go"]
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.possible_action_results = None
def get_possible_actions(self):
if self.possible_action_results is None:
self.possible_action_results = self.get_action_results()
return [action_result[0] for action_result in self.possible_action_results]
def act(self, action_choice_str):
try:
action_choice = int(action_choice_str)
except:
print("Error invalid choice.")
return None, None
if action_choice < 0 or action_choice >= len(self.action_phrases):
print("Error invalid choice.")
return None, None
action, result = self.possible_action_results[action_choice]
self.story.add_to_story(action, result)
self.possible_action_results = self.get_action_results()
return result, self.get_possible_actions()
def story_context(self):
return self.story.latest_result()
def get_action_results(self):
return [self.generate_action_result(self.story_context(), phrase) for phrase in self.action_phrases]
def generate_action_result(self, prompt, phrase):
action = phrase + self.generator.generate(prompt + phrase)
action_result = cut_trailing_sentence(action)
action, result = split_first_sentence(action_result)
result = story_replace(action_result)
action = action_replace(action)
return action, result
+6 -5
View File
@@ -52,18 +52,19 @@ def text_replace(text):
return text
def first_sentence(text):
def split_first_sentence(text):
first_period = text.find('.')
first_exclamation = text.find('!')
if first_exclamation < first_period and first_exclamation > 0:
text = text[0:first_exclamation+1]
split_point = first_exclamation+1
elif first_period > 0:
text = text[0:first_period+1]
split_point = first_period+1
else:
return text[0:20]
split_point = text[0:20]
return text
return text[0:split_point], text[split_point:]
def all_but_first(text):
first_period = text.find('.')
+2 -1
View File
@@ -13,7 +13,8 @@
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->
<title>Dungeon</title>
<link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
<title>AI Dungeon</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
var seed = {{ data.seed }}