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
a7c213d0
Commit
a7c213d0
authored
Oct 21, 2022
by
Stephen
Committed by
AUTOMATIC1111
Oct 23, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[API][Feature] - Add img2img API endpoint
parent
1fbfc052
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
63 additions
and
8 deletions
+63
-8
modules/api/api.py
modules/api/api.py
+53
-5
modules/api/processing.py
modules/api/processing.py
+9
-2
modules/processing.py
modules/processing.py
+1
-1
No files found.
modules/api/api.py
View file @
a7c213d0
from
modules.api.processing
import
StableDiffusionProcessingAPI
from
modules.processing
import
StableDiffusionProcessingTxt2Img
,
process_images
from
modules.api.processing
import
StableDiffusion
Txt2ImgProcessingAPI
,
StableDiffusionImg2Img
ProcessingAPI
from
modules.processing
import
StableDiffusionProcessingTxt2Img
,
StableDiffusionProcessingImg2Img
,
process_images
from
modules.sd_samplers
import
all_samplers
from
modules.extras
import
run_pnginfo
import
modules.shared
as
shared
...
...
@@ -10,6 +10,7 @@ from pydantic import BaseModel, Field, Json
import
json
import
io
import
base64
from
PIL
import
Image
sampler_to_index
=
lambda
name
:
next
(
filter
(
lambda
row
:
name
.
lower
()
==
row
[
1
]
.
name
.
lower
(),
enumerate
(
all_samplers
)),
None
)
...
...
@@ -18,6 +19,11 @@ class TextToImageResponse(BaseModel):
parameters
:
Json
info
:
Json
class
ImageToImageResponse
(
BaseModel
):
images
:
list
[
str
]
=
Field
(
default
=
None
,
title
=
"Image"
,
description
=
"The generated image in base64 format."
)
parameters
:
Json
info
:
Json
class
Api
:
def
__init__
(
self
,
app
,
queue_lock
):
...
...
@@ -25,8 +31,9 @@ class Api:
self
.
app
=
app
self
.
queue_lock
=
queue_lock
self
.
app
.
add_api_route
(
"/sdapi/v1/txt2img"
,
self
.
text2imgapi
,
methods
=
[
"POST"
])
self
.
app
.
add_api_route
(
"/sdapi/v1/img2img"
,
self
.
img2imgapi
,
methods
=
[
"POST"
])
def
text2imgapi
(
self
,
txt2imgreq
:
StableDiffusion
ProcessingAPI
):
def
text2imgapi
(
self
,
txt2imgreq
:
StableDiffusion
Txt2ImgProcessingAPI
):
sampler_index
=
sampler_to_index
(
txt2imgreq
.
sampler_index
)
if
sampler_index
is
None
:
...
...
@@ -54,8 +61,49 @@ class Api:
def
img2imgapi
(
self
):
raise
NotImplementedError
def
img2imgapi
(
self
,
img2imgreq
:
StableDiffusionImg2ImgProcessingAPI
):
sampler_index
=
sampler_to_index
(
img2imgreq
.
sampler_index
)
if
sampler_index
is
None
:
raise
HTTPException
(
status_code
=
404
,
detail
=
"Sampler not found"
)
init_images
=
img2imgreq
.
init_images
if
init_images
is
None
:
raise
HTTPException
(
status_code
=
404
,
detail
=
"Init image not found"
)
populate
=
img2imgreq
.
copy
(
update
=
{
# Override __init__ params
"sd_model"
:
shared
.
sd_model
,
"sampler_index"
:
sampler_index
[
0
],
"do_not_save_samples"
:
True
,
"do_not_save_grid"
:
True
}
)
p
=
StableDiffusionProcessingImg2Img
(
**
vars
(
populate
))
imgs
=
[]
for
img
in
init_images
:
# if has a comma, deal with prefix
if
","
in
img
:
img
=
img
.
split
(
","
)[
1
]
# convert base64 to PIL image
img
=
base64
.
b64decode
(
img
)
img
=
Image
.
open
(
io
.
BytesIO
(
img
))
imgs
=
[
img
]
*
p
.
batch_size
p
.
init_images
=
imgs
# Override object param
with
self
.
queue_lock
:
processed
=
process_images
(
p
)
b64images
=
[]
for
i
in
processed
.
images
:
buffer
=
io
.
BytesIO
()
i
.
save
(
buffer
,
format
=
"png"
)
b64images
.
append
(
base64
.
b64encode
(
buffer
.
getvalue
()))
return
ImageToImageResponse
(
images
=
b64images
,
parameters
=
json
.
dumps
(
vars
(
img2imgreq
)),
info
=
json
.
dumps
(
processed
.
info
))
def
extrasapi
(
self
):
raise
NotImplementedError
...
...
modules/api/processing.py
View file @
a7c213d0
from
array
import
array
from
inflection
import
underscore
from
typing
import
Any
,
Dict
,
Optional
from
pydantic
import
BaseModel
,
Field
,
create_model
from
modules.processing
import
StableDiffusionProcessingTxt2Img
from
modules.processing
import
StableDiffusionProcessingTxt2Img
,
StableDiffusionProcessingImg2Img
import
inspect
...
...
@@ -92,8 +93,14 @@ class PydanticModelGenerator:
DynamicModel
.
__config__
.
allow_mutation
=
True
return
DynamicModel
StableDiffusionProcessingAPI
=
PydanticModelGenerator
(
StableDiffusion
Txt2Img
ProcessingAPI
=
PydanticModelGenerator
(
"StableDiffusionProcessingTxt2Img"
,
StableDiffusionProcessingTxt2Img
,
[{
"key"
:
"sampler_index"
,
"type"
:
str
,
"default"
:
"Euler"
}]
)
.
generate_model
()
StableDiffusionImg2ImgProcessingAPI
=
PydanticModelGenerator
(
"StableDiffusionProcessingImg2Img"
,
StableDiffusionProcessingImg2Img
,
[{
"key"
:
"sampler_index"
,
"type"
:
str
,
"default"
:
"Euler"
},
{
"key"
:
"init_images"
,
"type"
:
list
,
"default"
:
None
},
{
"key"
:
"denoising_strength"
,
"type"
:
float
,
"default"
:
0.75
}]
)
.
generate_model
()
\ No newline at end of file
modules/processing.py
View file @
a7c213d0
...
...
@@ -623,7 +623,7 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing):
class
StableDiffusionProcessingImg2Img
(
StableDiffusionProcessing
):
sampler
=
None
def
__init__
(
self
,
init_images
=
None
,
resize_mode
=
0
,
denoising_strength
=
0.75
,
mask
=
None
,
mask_blur
=
4
,
inpainting_fill
=
0
,
inpaint_full_res
=
True
,
inpaint_full_res_padding
=
0
,
inpainting_mask_inver
t
=
0
,
**
kwargs
):
def
__init__
(
self
,
init_images
:
list
=
None
,
resize_mode
:
int
=
0
,
denoising_strength
:
float
=
0.75
,
mask
:
str
=
None
,
mask_blur
:
int
=
4
,
inpainting_fill
:
int
=
0
,
inpaint_full_res
:
bool
=
True
,
inpaint_full_res_padding
:
int
=
0
,
inpainting_mask_invert
:
in
t
=
0
,
**
kwargs
):
super
()
.
__init__
(
**
kwargs
)
self
.
init_images
=
init_images
...
...
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