Refactored the class
This commit is contained in:
@@ -18,6 +18,15 @@
|
||||
import sys
|
||||
from random import Random
|
||||
import re
|
||||
import server
|
||||
from aiohttp import web
|
||||
|
||||
|
||||
@server.PromptServer.instance.routes.post("/stable-wildcards/process")
|
||||
async def process_stable_wildcards(req):
|
||||
json_data = await req.json()
|
||||
prompt = StableWildcard.process_wildcards(json_data['prompt'], json_data['seed'])
|
||||
return web.json_response({"prompt": prompt}, content_type='application/json')
|
||||
|
||||
|
||||
class StableWildcard:
|
||||
@@ -57,20 +66,19 @@ class StableWildcard:
|
||||
"optional": {}
|
||||
}
|
||||
|
||||
def execute(self, prompt, seed, **kwargs):
|
||||
@staticmethod
|
||||
def process_wildcards(prompt, seed):
|
||||
"""
|
||||
Process wildcards using a seed to produce stable output.
|
||||
To achieve stable results a new random object is created
|
||||
using the given seed.
|
||||
"""
|
||||
|
||||
# Setup RNG
|
||||
rng = Random(int(seed))
|
||||
|
||||
# Search & replace matches
|
||||
match = self.WILDCARD_PATTERN.search(prompt)
|
||||
match = StableWildcard.WILDCARD_PATTERN.search(prompt)
|
||||
while match:
|
||||
|
||||
# Get the options - Remove the {}, split by | character
|
||||
# Because the search pattern requires at least one character,
|
||||
# ops is guaranteed to have at least one option
|
||||
@@ -84,7 +92,17 @@ class StableWildcard:
|
||||
prompt = prompt.replace(match.group(), pick, 1)
|
||||
|
||||
# Search for more wildcards
|
||||
match = self.WILDCARD_PATTERN.search(prompt)
|
||||
match = StableWildcard.WILDCARD_PATTERN.search(prompt)
|
||||
|
||||
return prompt
|
||||
|
||||
def execute(self, prompt, seed, **kwargs):
|
||||
"""
|
||||
Process the wildcards for execution
|
||||
"""
|
||||
|
||||
# Process the wildcards
|
||||
prompt = self.process_wildcards(prompt, seed)
|
||||
|
||||
# Output result console
|
||||
print('\033[96m Stable Wildcard: ({}) "{}"\033[0m'.format(seed, prompt))
|
||||
|
||||
Reference in New Issue
Block a user