mirror of
https://github.com/wassname/Clover-Edition.git
synced 2026-09-09 11:13:26 +08:00
made some changes
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -7,7 +7,7 @@ import gpt2.src.model as model
|
||||
from tensorflow.contrib import predictor
|
||||
import gpt2.src.sample as sample
|
||||
import gpt2.src.encoder as encoder
|
||||
from utils import *
|
||||
from story.utils import *
|
||||
import pdb
|
||||
|
||||
pos_action_starts = ["You attack", "You tell", "You use", "You go"]
|
||||
@@ -50,29 +50,29 @@ 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
|
||||
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,17 +1,11 @@
|
||||
import datetime
|
||||
from flask import g
|
||||
from flask import session
|
||||
import os
|
||||
import googleapiclient.discovery
|
||||
from utils import *
|
||||
from story.utils import *
|
||||
from google.cloud import storage
|
||||
from google import cloud
|
||||
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
|
||||
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
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 sum(story_list)
|
||||
|
||||
|
||||
class UnconstrainedStoryGenerator():
|
||||
|
||||
def __init__(self, generator, story_start):
|
||||
self.story = Story(story_start)
|
||||
self.generator = generator
|
||||
|
||||
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 ConstrainedStoryGenerator():
|
||||
|
||||
def __init__(self, generator, story_start):
|
||||
self.story = Story(story_start)
|
||||
self.generator = generator
|
||||
self.possible_action_results = self.get_action_results()
|
||||
self.action_phrases = ["You attack", "You tell", "You use", "You go"]
|
||||
|
||||
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
|
||||
@@ -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('.')
|
||||
Reference in New Issue
Block a user