Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
S
Stable Diffusion Webui
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
novelai-storage
Stable Diffusion Webui
Commits
7fc10e04
Commit
7fc10e04
authored
Apr 29, 2023
by
AUTOMATIC1111
Committed by
GitHub
Apr 29, 2023
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #9134 from space-nuko/improve-custom-code-extension
Improve custom code script
parents
5a666f39
79d57d02
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
56 additions
and
7 deletions
+56
-7
scripts/custom_code.py
scripts/custom_code.py
+56
-7
No files found.
scripts/custom_code.py
View file @
7fc10e04
import
modules.scripts
as
scripts
import
gradio
as
gr
import
ast
import
copy
from
modules.processing
import
Processed
from
modules.shared
import
opts
,
cmd_opts
,
state
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__
)
class
Script
(
scripts
.
Script
):
def
title
(
self
):
...
...
@@ -13,12 +44,23 @@ class Script(scripts.Script):
return
cmd_opts
.
allow_code
def
ui
(
self
,
is_img2img
):
code
=
gr
.
Textbox
(
label
=
"Python code"
,
lines
=
1
,
elem_id
=
self
.
elem_id
(
"code"
))
example
=
"""from modules.processing import process_images
p.width = 768
p.height = 768
p.batch_size = 2
p.steps = 10
return process_images(p)
"""
return
[
code
]
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"
))
return
[
code
,
indent_level
]
def
run
(
self
,
p
,
code
):
def
run
(
self
,
p
,
code
,
indent_level
):
assert
cmd_opts
.
allow_code
,
'--allow-code option must be enabled'
display_result_data
=
[[],
-
1
,
""
]
...
...
@@ -29,13 +71,20 @@ class Script(scripts.Script):
display_result_data
[
2
]
=
i
from
types
import
ModuleType
compiled
=
compile
(
code
,
''
,
'exec'
)
module
=
ModuleType
(
"testmodule"
)
module
.
__dict__
.
update
(
globals
())
module
.
p
=
p
module
.
display
=
display
exec
(
compiled
,
module
.
__dict__
)
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
return
Processed
(
p
,
*
display_result_data
)
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment