mirror of
https://github.com/wassname/Clover-Edition.git
synced 2026-09-09 11:13:26 +08:00
auto ml working
This commit is contained in:
Binary file not shown.
@@ -16,69 +16,78 @@
|
||||
import datetime
|
||||
from flask import g
|
||||
import os
|
||||
|
||||
os.environ['GOOGLE_APPLICATION_CREDENTIALS']="./AI-Adventure-2bb65e3a4e2f.json"
|
||||
|
||||
import googleapiclient.discovery
|
||||
from utils import *
|
||||
from google.cloud import storage
|
||||
from google import cloud
|
||||
import json
|
||||
from flask import Flask, render_template, request, abort
|
||||
storage_client = storage.Client()
|
||||
bucket = storage_client.get_bucket("dungeon-cache")
|
||||
from flask import Response
|
||||
import requests
|
||||
app = Flask(__name__)
|
||||
import pdb
|
||||
import gpt2.src.encoder as encoder
|
||||
|
||||
|
||||
# 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"]
|
||||
requested_map = {}
|
||||
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")
|
||||
|
||||
|
||||
def predict(context_tokens):
|
||||
|
||||
# Create the ML Engine service object.
|
||||
# To authenticate set the environment variable
|
||||
# GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account_file>
|
||||
service = googleapiclient.discovery.build('ml', 'v1')
|
||||
name = 'projects/{}/models/{}'.format(project, model)
|
||||
instance = json.loads(context_tokens)
|
||||
instance = context_tokens
|
||||
|
||||
if version is not None:
|
||||
name += '/versions/{}'.format(version)
|
||||
|
||||
response = service.projects(). predict(
|
||||
name=name,
|
||||
body={'instances': [instance]}
|
||||
body={'instances': [{'context': instance}]}
|
||||
).execute()
|
||||
|
||||
if 'error' in response:
|
||||
raise RuntimeError(response['error'])
|
||||
|
||||
return response['predictions']
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def generate(prompt):
|
||||
context_tokens = [enc.encode(prompt)]
|
||||
|
||||
|
||||
context_tokens = enc.encode(prompt)
|
||||
pred = predict(context_tokens)
|
||||
|
||||
|
||||
output = enc.decode(pred[0])
|
||||
pred = pred[0][len(context_tokens):]
|
||||
output = enc.decode(pred)
|
||||
return output
|
||||
|
||||
def generate_story_block(prompt):
|
||||
block = generate(prompt)
|
||||
block = cut_trailing_sentence(block)
|
||||
block = story_replace(block)
|
||||
|
||||
return block
|
||||
|
||||
def generate_action_result(prompt, phrase):
|
||||
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('/')
|
||||
@@ -146,9 +155,8 @@ def story_request():
|
||||
if action_results is not None:
|
||||
response = action_results
|
||||
else:
|
||||
response = requests.post(gen_ip + "/generate",
|
||||
data={"actions":"true","seed":seed, "prompt_num":prompt_num, "prompt": prompt, "choices": json.dumps(choices)})
|
||||
response = response.text
|
||||
action_results = [generate_action_result(prompt, phrase) for phrase in phrases]
|
||||
response = json.dumps(action_results)
|
||||
cache_file(seed, prompt_num, choices, response, "choices")
|
||||
else:
|
||||
|
||||
@@ -158,8 +166,7 @@ def story_request():
|
||||
if result is not None:
|
||||
response = result
|
||||
else:
|
||||
response = requests.post(gen_ip + "/generate", data={"actions":"false","seed":seed, "prompt_num":prompt_num})
|
||||
response=response.text
|
||||
response = generate_story_block(prompts[prompt_num])
|
||||
cache_file(seed, prompt_num, [], response, "story")
|
||||
|
||||
print("\nGenerated response is: \n", response)
|
||||
@@ -168,7 +175,8 @@ def story_request():
|
||||
return response
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', port=8080)
|
||||
|
||||
app.run(host='0.0.0.0', port=8080)
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -2,3 +2,4 @@ google-cloud-storage
|
||||
numpy
|
||||
flask
|
||||
gunicorn
|
||||
google-api-python-client
|
||||
|
||||
+19
-24
@@ -15,25 +15,21 @@ var action_list = ["You attack", "You tell", "You use", "You go"]
|
||||
var prompt_num = 0
|
||||
var seed_max = 1000
|
||||
var seed_min = 0
|
||||
var seed = Math.floor(Math.random() * (+seed_max - +seed_min)) + +seed_min;
|
||||
//var seed = 108
|
||||
//var seed = Math.floor(Math.random() * (+seed_max - +seed_min)) + +seed_min;
|
||||
var seed = 999
|
||||
console.log("Seed is ", seed)
|
||||
|
||||
function isMobileDevice() {
|
||||
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
|
||||
};
|
||||
|
||||
|
||||
function checkButtonDisplay()
|
||||
if(typing==false){
|
||||
console.log("Mobile device");
|
||||
document.getElementById('buttons').style.visibility='visible';
|
||||
|
||||
function buttonCheck(){
|
||||
if(typing == true){
|
||||
setTimeout(buttonCheck, 500);
|
||||
}
|
||||
else{
|
||||
setTimeout(checkButtonDisplay, 1000);
|
||||
console.log("Not mobile device");
|
||||
document.getElementById('buttons').style.visibility='hidden';
|
||||
|
||||
document.getElementById('buttons').style.visibility='visible';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,29 +58,29 @@ var StoryTracker = {
|
||||
|
||||
StoryTracker.makeActionRequests(StoryTracker.firstStory + StoryTracker.lastStory)
|
||||
Typer.appendToText(story)
|
||||
Typer.appendToText("\n\nOptions:")
|
||||
action_waiting = true
|
||||
setTimeout(StoryTracker.actionWait, 10000);
|
||||
},
|
||||
|
||||
actionWait:function(){
|
||||
|
||||
if(action_waiting == true){
|
||||
if(typing == true || acceptInput == true){
|
||||
setTimeout(StoryTracker.actionWait, 5000);
|
||||
setTimeout(StoryTracker.actionWait, 10000);
|
||||
}
|
||||
else{
|
||||
Typer.appendToText(" Generating...")
|
||||
Typer.appendToText("\n\n Generating options... (~20s)")
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
|
||||
addNextAction:function(action_result){
|
||||
|
||||
action_waiting = false
|
||||
|
||||
var action_results = JSON.parse(action_result)
|
||||
|
||||
Typer.appendToText("\n\nOptions:")
|
||||
for (i = 0; i < 4; i++){
|
||||
|
||||
action_result = action_results[i]
|
||||
@@ -102,10 +98,9 @@ var StoryTracker = {
|
||||
Typer.appendToText("\nWhich action do you choose? ")
|
||||
StoryTracker.action_int = 0
|
||||
acceptInput = true
|
||||
|
||||
if(isMobileDevice(){
|
||||
setTimeout(checkButtonDisplay, 1000);
|
||||
|
||||
|
||||
if(isMobileDevice()){
|
||||
setTimeout(buttonCheck, 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -133,7 +128,6 @@ var StoryTracker = {
|
||||
|
||||
processInput:function(){
|
||||
var choice_int = parseInt(inputStr, 10)
|
||||
|
||||
if(choice_int >= 0 && choice_int <= 3){
|
||||
|
||||
console.log("choice_int is %d", choice_int)
|
||||
@@ -142,10 +136,9 @@ var StoryTracker = {
|
||||
StoryTracker.lastStory = StoryTracker.results[choice_int]
|
||||
StoryTracker.makeActionRequests(StoryTracker.firstStory + StoryTracker.lastStory)
|
||||
action_waiting = true
|
||||
setTimeout(StoryTracker.actionWait, 4000);
|
||||
setTimeout(StoryTracker.actionWait, 10000);
|
||||
Typer.appendToText("\n")
|
||||
Typer.appendToText(StoryTracker.lastStory)
|
||||
Typer.appendToText("\n\nOptions:")
|
||||
}
|
||||
else{
|
||||
|
||||
@@ -200,7 +193,7 @@ var Typer={
|
||||
}
|
||||
var text=Typer.text.substring(0,Typer.index)
|
||||
var rtn= new RegExp("\n", "g")
|
||||
A solution would be to add position: relative for the button. This will move it above the label.
|
||||
|
||||
$("#console").html(text.replace(rtn,"<br/>"))
|
||||
}
|
||||
else{
|
||||
@@ -293,6 +286,8 @@ function start(){
|
||||
startTyping()
|
||||
Typer.startBlinker()
|
||||
|
||||
console.log("Not mobile device");
|
||||
document.getElementById('buttons').style.visibility='hidden';
|
||||
}
|
||||
|
||||
|
||||
|
||||
+1
-1
@@ -118,7 +118,7 @@ button:focus{
|
||||
text-align: center;
|
||||
padding: 10px 5px;
|
||||
text-decoration: none;
|
||||
font-size:calc(12px + 1.0vh );
|
||||
font-size:calc(14px + 0.5vh );
|
||||
min-font-size:
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user