2022-09-03 16:32:45 +00:00
|
|
|
import modules.scripts as scripts
|
|
|
|
import gradio as gr
|
2023-03-29 06:52:34 +00:00
|
|
|
import ast
|
|
|
|
import copy
|
2022-09-03 16:32:45 +00:00
|
|
|
|
|
|
|
from modules.processing import Processed
|
|
|
|
from modules.shared import opts, cmd_opts, state
|
|
|
|
|
2023-03-29 06:52:34 +00:00
|
|
|
|
|
|
|
def convertExpr2Expression(expr):
|
|
|
|
expr.lineno = 0
|
|
|
|
expr.col_offset = 0
|
|
|
|
result = ast.Expression(expr.value, lineno=0, col_offset = 0)
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def exec_with_return(code, module):
|
|
|
|
"""
|
|
|
|
like exec() but can return values
|
|
|
|
https://stackoverflow.com/a/52361938/5862977
|
|
|
|
"""
|
|
|
|
code_ast = ast.parse(code)
|
|
|
|
|
|
|
|
init_ast = copy.deepcopy(code_ast)
|
|
|
|
init_ast.body = code_ast.body[:-1]
|
|
|
|
|
|
|
|
last_ast = copy.deepcopy(code_ast)
|
|
|
|
last_ast.body = code_ast.body[-1:]
|
|
|
|
|
|
|
|
exec(compile(init_ast, "<ast>", "exec"), module.__dict__)
|
|
|
|
if type(last_ast.body[0]) == ast.Expr:
|
|
|
|
return eval(compile(convertExpr2Expression(last_ast.body[0]), "<ast>", "eval"), module.__dict__)
|
|
|
|
else:
|
|
|
|
exec(compile(last_ast, "<ast>", "exec"), module.__dict__)
|
|
|
|
|
|
|
|
|
2022-09-03 16:32:45 +00:00
|
|
|
class Script(scripts.Script):
|
2022-09-16 21:06:21 +00:00
|
|
|
|
2022-09-03 16:32:45 +00:00
|
|
|
def title(self):
|
|
|
|
return "Custom code"
|
|
|
|
|
2022-09-04 10:08:06 +00:00
|
|
|
def show(self, is_img2img):
|
2022-09-03 16:32:45 +00:00
|
|
|
return cmd_opts.allow_code
|
|
|
|
|
|
|
|
def ui(self, is_img2img):
|
2023-03-29 06:52:34 +00:00
|
|
|
example = """from modules.processing import process_images
|
|
|
|
|
|
|
|
p.width = 768
|
|
|
|
p.height = 768
|
|
|
|
p.batch_size = 2
|
|
|
|
p.steps = 10
|
|
|
|
|
|
|
|
return process_images(p)
|
|
|
|
"""
|
|
|
|
|
2022-09-03 16:32:45 +00:00
|
|
|
|
2023-03-29 06:52:34 +00:00
|
|
|
code = gr.Code(value=example, language="python", label="Python code", elem_id=self.elem_id("code"))
|
|
|
|
indent_level = gr.Number(label='Indent level', value=2, precision=0, elem_id=self.elem_id("indent_level"))
|
2022-09-03 16:32:45 +00:00
|
|
|
|
2023-03-29 06:52:34 +00:00
|
|
|
return [code, indent_level]
|
2022-09-16 21:06:21 +00:00
|
|
|
|
2023-03-29 06:52:34 +00:00
|
|
|
def run(self, p, code, indent_level):
|
2022-09-04 10:08:06 +00:00
|
|
|
assert cmd_opts.allow_code, '--allow-code option must be enabled'
|
2022-09-03 16:32:45 +00:00
|
|
|
|
|
|
|
display_result_data = [[], -1, ""]
|
|
|
|
|
|
|
|
def display(imgs, s=display_result_data[1], i=display_result_data[2]):
|
|
|
|
display_result_data[0] = imgs
|
|
|
|
display_result_data[1] = s
|
|
|
|
display_result_data[2] = i
|
|
|
|
|
|
|
|
from types import ModuleType
|
|
|
|
module = ModuleType("testmodule")
|
|
|
|
module.__dict__.update(globals())
|
|
|
|
module.p = p
|
|
|
|
module.display = display
|
2023-03-29 06:52:34 +00:00
|
|
|
|
|
|
|
indent = " " * indent_level
|
|
|
|
indented = code.replace('\n', '\n' + indent)
|
|
|
|
body = f"""def __webuitemp__():
|
|
|
|
{indent}{indented}
|
|
|
|
__webuitemp__()"""
|
|
|
|
|
|
|
|
result = exec_with_return(body, module)
|
|
|
|
|
|
|
|
if isinstance(result, Processed):
|
|
|
|
return result
|
2022-09-03 16:32:45 +00:00
|
|
|
|
|
|
|
return Processed(p, *display_result_data)
|