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
a39c4cf7
Commit
a39c4cf7
authored
Feb 27, 2023
by
Vespinian
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
small refactor of api.py
parent
3b6de964
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
51 additions
and
74 deletions
+51
-74
modules/api/api.py
modules/api/api.py
+51
-74
No files found.
modules/api/api.py
View file @
a39c4cf7
...
@@ -172,56 +172,37 @@ class Api:
...
@@ -172,56 +172,37 @@ class Api:
return
script
,
script_idx
return
script
,
script_idx
def
get_script
(
self
,
script_name
,
script_runner
):
def
get_script
(
self
,
script_name
,
script_runner
):
for
script
in
script_runner
.
scripts
:
if
script_name
is
None
or
script_name
==
""
:
if
script_name
.
lower
()
==
script
.
title
()
.
lower
():
return
None
,
None
return
script
return
None
script_idx
=
script_name_to_index
(
script_name
,
script_runner
.
scripts
)
return
script_runner
.
scripts
[
script_idx
]
def
text2imgapi
(
self
,
txt2imgreq
:
StableDiffusionTxt2ImgProcessingAPI
):
script_runner
=
scripts
.
scripts_txt2img
if
not
script_runner
.
scripts
:
script_runner
.
initialize_scripts
(
False
)
ui
.
create_ui
()
api_selectable_scripts
,
api_selectable_script_idx
=
self
.
get_selectable_script
(
txt2imgreq
.
script_name
,
script_runner
)
populate
=
txt2imgreq
.
copy
(
update
=
{
# Override __init__ params
"sampler_name"
:
validate_sampler_name
(
txt2imgreq
.
sampler_name
or
txt2imgreq
.
sampler_index
),
"do_not_save_samples"
:
True
,
"do_not_save_grid"
:
True
}
)
if
populate
.
sampler_name
:
populate
.
sampler_index
=
None
# prevent a warning later on
args
=
vars
(
populate
)
args
.
pop
(
'script_name'
,
None
)
args
.
pop
(
'script_args'
,
None
)
# will refeed them later with script_args
args
.
pop
(
'alwayson_script_name'
,
None
)
args
.
pop
(
'alwayson_script_args'
,
None
)
def
init_script_args
(
self
,
request
,
selectable_scripts
,
selectable_idx
,
script_runner
):
#find max idx from the scripts in runner and generate a none array to init script_args
#find max idx from the scripts in runner and generate a none array to init script_args
last_arg_index
=
1
last_arg_index
=
1
for
script
in
script_runner
.
scripts
:
for
script
in
script_runner
.
scripts
:
if
last_arg_index
<
script
.
args_to
:
if
last_arg_index
<
script
.
args_to
:
last_arg_index
=
script
.
args_to
last_arg_index
=
script
.
args_to
# None everywhere ex
epc
t position 0 to initialize script args
# None everywhere ex
cep
t position 0 to initialize script args
script_args
=
[
None
]
*
last_arg_index
script_args
=
[
None
]
*
last_arg_index
# position 0 in script_arg is the idx+1 of the selectable script that is going to be run
# position 0 in script_arg is the idx+1 of the selectable script that is going to be run
when using scripts.scripts_*2img.run()
if
api_
selectable_scripts
:
if
selectable_scripts
:
script_args
[
api_selectable_scripts
.
args_from
:
api_selectable_scripts
.
args_to
]
=
txt2imgreq
.
script_args
script_args
[
selectable_scripts
.
args_from
:
selectable_scripts
.
args_to
]
=
request
.
script_args
script_args
[
0
]
=
api_selectable_script
_idx
+
1
script_args
[
0
]
=
selectable
_idx
+
1
else
:
else
:
# if 0 then none
# if 0 then none
script_args
[
0
]
=
0
script_args
[
0
]
=
0
# Now check for always on scripts
# Now check for always on scripts
if
len
(
txt2imgreq
.
alwayson_script_name
)
>
0
:
if
request
.
alwayson_script_name
and
(
len
(
request
.
alwayson_script_name
)
>
0
)
:
# always on script with no arg should always run, but if you include their name in the api request, send an empty list for there args
# always on script with no arg should always run, but if you include their name in the api request, send an empty list for there args
if
len
(
txt2imgreq
.
alwayson_script_name
)
!=
len
(
txt2imgreq
.
alwayson_script_args
):
if
not
request
.
alwayson_script_args
:
raise
HTTPException
(
status_code
=
422
,
detail
=
f
"Script {request.alwayson_script_name} has no arg list"
)
if
len
(
request
.
alwayson_script_name
)
!=
len
(
request
.
alwayson_script_args
):
raise
HTTPException
(
status_code
=
422
,
detail
=
f
"Number of script names and number of script arg lists doesn't match"
)
raise
HTTPException
(
status_code
=
422
,
detail
=
f
"Number of script names and number of script arg lists doesn't match"
)
for
alwayson_script_name
,
alwayson_script_args
in
zip
(
txt2imgreq
.
alwayson_script_name
,
txt2imgreq
.
alwayson_script_args
):
for
alwayson_script_name
,
alwayson_script_args
in
zip
(
request
.
alwayson_script_name
,
request
.
alwayson_script_args
):
alwayson_script
=
self
.
get_script
(
alwayson_script_name
,
script_runner
)
alwayson_script
=
self
.
get_script
(
alwayson_script_name
,
script_runner
)
if
alwayson_script
==
None
:
if
alwayson_script
==
None
:
raise
HTTPException
(
status_code
=
422
,
detail
=
f
"always on script {alwayson_script_name} not found"
)
raise
HTTPException
(
status_code
=
422
,
detail
=
f
"always on script {alwayson_script_name} not found"
)
...
@@ -230,19 +211,45 @@ class Api:
...
@@ -230,19 +211,45 @@ class Api:
raise
HTTPException
(
status_code
=
422
,
detail
=
f
"Cannot have a selectable script in the always on scripts params"
)
raise
HTTPException
(
status_code
=
422
,
detail
=
f
"Cannot have a selectable script in the always on scripts params"
)
if
alwayson_script_args
!=
[]:
if
alwayson_script_args
!=
[]:
script_args
[
alwayson_script
.
args_from
:
alwayson_script
.
args_to
]
=
alwayson_script_args
script_args
[
alwayson_script
.
args_from
:
alwayson_script
.
args_to
]
=
alwayson_script_args
return
script_args
def
text2imgapi
(
self
,
txt2imgreq
:
StableDiffusionTxt2ImgProcessingAPI
):
script_runner
=
scripts
.
scripts_txt2img
if
not
script_runner
.
scripts
:
script_runner
.
initialize_scripts
(
False
)
ui
.
create_ui
()
selectable_scripts
,
selectable_script_idx
=
self
.
get_selectable_script
(
txt2imgreq
.
script_name
,
script_runner
)
populate
=
txt2imgreq
.
copy
(
update
=
{
# Override __init__ params
"sampler_name"
:
validate_sampler_name
(
txt2imgreq
.
sampler_name
or
txt2imgreq
.
sampler_index
),
"do_not_save_samples"
:
True
,
"do_not_save_grid"
:
True
}
)
if
populate
.
sampler_name
:
populate
.
sampler_index
=
None
# prevent a warning later on
args
=
vars
(
populate
)
args
.
pop
(
'script_name'
,
None
)
args
.
pop
(
'script_args'
,
None
)
# will refeed them to the pipeline directly after initializing them
args
.
pop
(
'alwayson_script_name'
,
None
)
args
.
pop
(
'alwayson_script_args'
,
None
)
script_args
=
self
.
init_script_args
(
txt2imgreq
,
selectable_scripts
,
selectable_script_idx
,
script_runner
)
with
self
.
queue_lock
:
with
self
.
queue_lock
:
p
=
StableDiffusionProcessingTxt2Img
(
sd_model
=
shared
.
sd_model
,
**
args
)
p
=
StableDiffusionProcessingTxt2Img
(
sd_model
=
shared
.
sd_model
,
**
args
)
p
.
scripts
=
script_runner
p
.
scripts
=
script_runner
shared
.
state
.
begin
()
shared
.
state
.
begin
()
if
api_
selectable_scripts
!=
None
:
if
selectable_scripts
!=
None
:
p
.
script_args
=
script_args
p
.
script_args
=
script_args
p
.
outpath_grids
=
opts
.
outdir_txt2img_grids
p
.
outpath_grids
=
opts
.
outdir_txt2img_grids
p
.
outpath_samples
=
opts
.
outdir_txt2img_samples
p
.
outpath_samples
=
opts
.
outdir_txt2img_samples
processed
=
scripts
.
scripts_txt2img
.
run
(
p
,
*
p
.
script_args
)
processed
=
scripts
.
scripts_txt2img
.
run
(
p
,
*
p
.
script_args
)
# Need to pass args as list here
else
:
else
:
p
.
script_args
=
tuple
(
script_args
)
p
.
script_args
=
tuple
(
script_args
)
# Need to pass args as tuple here
processed
=
process_images
(
p
)
processed
=
process_images
(
p
)
shared
.
state
.
end
()
shared
.
state
.
end
()
...
@@ -263,7 +270,7 @@ class Api:
...
@@ -263,7 +270,7 @@ class Api:
if
not
script_runner
.
scripts
:
if
not
script_runner
.
scripts
:
script_runner
.
initialize_scripts
(
True
)
script_runner
.
initialize_scripts
(
True
)
ui
.
create_ui
()
ui
.
create_ui
()
api_selectable_scripts
,
api_
selectable_script_idx
=
self
.
get_selectable_script
(
img2imgreq
.
script_name
,
script_runner
)
selectable_scripts
,
selectable_script_idx
=
self
.
get_selectable_script
(
img2imgreq
.
script_name
,
script_runner
)
populate
=
img2imgreq
.
copy
(
update
=
{
# Override __init__ params
populate
=
img2imgreq
.
copy
(
update
=
{
# Override __init__ params
"sampler_name"
:
validate_sampler_name
(
img2imgreq
.
sampler_name
or
img2imgreq
.
sampler_index
),
"sampler_name"
:
validate_sampler_name
(
img2imgreq
.
sampler_name
or
img2imgreq
.
sampler_index
),
...
@@ -279,41 +286,11 @@ class Api:
...
@@ -279,41 +286,11 @@ class Api:
args
=
vars
(
populate
)
args
=
vars
(
populate
)
args
.
pop
(
'include_init_images'
,
None
)
# this is meant to be done by "exclude": True in model, but it's for a reason that I cannot determine.
args
.
pop
(
'include_init_images'
,
None
)
# this is meant to be done by "exclude": True in model, but it's for a reason that I cannot determine.
args
.
pop
(
'script_name'
,
None
)
args
.
pop
(
'script_name'
,
None
)
args
.
pop
(
'script_args'
,
None
)
# will refeed them later with script_args
args
.
pop
(
'script_args'
,
None
)
# will refeed them to the pipeline directly after initializing them
args
.
pop
(
'alwayson_script_name'
,
None
)
args
.
pop
(
'alwayson_script_name'
,
None
)
args
.
pop
(
'alwayson_script_args'
,
None
)
args
.
pop
(
'alwayson_script_args'
,
None
)
#find max idx from the scripts in runner and generate a none array to init script_args
script_args
=
self
.
init_script_args
(
img2imgreq
,
selectable_scripts
,
selectable_script_idx
,
script_runner
)
last_arg_index
=
1
for
script
in
script_runner
.
scripts
:
if
last_arg_index
<
script
.
args_to
:
last_arg_index
=
script
.
args_to
# None everywhere exepct position 0 to initialize script args
script_args
=
[
None
]
*
last_arg_index
# position 0 in script_arg is the idx+1 of the selectable script that is going to be run
if
api_selectable_scripts
:
script_args
[
api_selectable_scripts
.
args_from
:
api_selectable_scripts
.
args_to
]
=
img2imgreq
.
script_args
script_args
[
0
]
=
api_selectable_script_idx
+
1
else
:
# if 0 then none
script_args
[
0
]
=
0
# Now check for always on scripts
if
len
(
img2imgreq
.
alwayson_script_name
)
>
0
:
# always on script with no arg should always run, but if you include their name in the api request, send an empty list for there args
if
len
(
img2imgreq
.
alwayson_script_name
)
!=
len
(
img2imgreq
.
alwayson_script_args
):
raise
HTTPException
(
status_code
=
422
,
detail
=
f
"Number of script names and number of script arg lists doesn't match"
)
for
alwayson_script_name
,
alwayson_script_args
in
zip
(
img2imgreq
.
alwayson_script_name
,
img2imgreq
.
alwayson_script_args
):
alwayson_script
=
self
.
get_script
(
alwayson_script_name
,
script_runner
)
if
alwayson_script
==
None
:
raise
HTTPException
(
status_code
=
422
,
detail
=
f
"always on script {alwayson_script_name} not found"
)
# Selectable script in always on script param check
if
alwayson_script
.
alwayson
==
False
:
raise
HTTPException
(
status_code
=
422
,
detail
=
f
"Cannot have a selectable script in the always on scripts params"
)
if
alwayson_script_args
!=
[]:
script_args
[
alwayson_script
.
args_from
:
alwayson_script
.
args_to
]
=
alwayson_script_args
with
self
.
queue_lock
:
with
self
.
queue_lock
:
p
=
StableDiffusionProcessingImg2Img
(
sd_model
=
shared
.
sd_model
,
**
args
)
p
=
StableDiffusionProcessingImg2Img
(
sd_model
=
shared
.
sd_model
,
**
args
)
...
@@ -321,13 +298,13 @@ class Api:
...
@@ -321,13 +298,13 @@ class Api:
p
.
scripts
=
script_runner
p
.
scripts
=
script_runner
shared
.
state
.
begin
()
shared
.
state
.
begin
()
if
api_
selectable_scripts
!=
None
:
if
selectable_scripts
!=
None
:
p
.
script_args
=
script_args
p
.
script_args
=
script_args
p
.
outpath_grids
=
opts
.
outdir_img2img_grids
p
.
outpath_grids
=
opts
.
outdir_img2img_grids
p
.
outpath_samples
=
opts
.
outdir_img2img_samples
p
.
outpath_samples
=
opts
.
outdir_img2img_samples
processed
=
scripts
.
scripts_img2img
.
run
(
p
,
*
p
.
script_args
)
processed
=
scripts
.
scripts_img2img
.
run
(
p
,
*
p
.
script_args
)
# Need to pass args as list here
else
:
else
:
p
.
script_args
=
tuple
(
script_args
)
p
.
script_args
=
tuple
(
script_args
)
# Need to pass args as tuple here
processed
=
process_images
(
p
)
processed
=
process_images
(
p
)
shared
.
state
.
end
()
shared
.
state
.
end
()
...
...
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