Skip to content

Commit 7f5212f

Browse files
committed
Merge branch 'master' into feat/progress-api
2 parents 6b719c4 + d699720 commit 7f5212f

File tree

6 files changed

+59
-10
lines changed

6 files changed

+59
-10
lines changed

CODEOWNERS

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
11
* @AUTOMATIC1111
2+
/localizations/ar_AR.json @xmodar @blackneoo
3+
/localizations/de_DE.json @LunixWasTaken
4+
/localizations/es_ES.json @innovaciones
5+
/localizations/fr_FR.json @tumbly
6+
/localizations/it_IT.json @EugenioBuffo
7+
/localizations/ja_JP.json @yuuki76
8+
/localizations/ko_KR.json @36DB
9+
/localizations/pt_BR.json @M-art-ucci
10+
/localizations/ru_RU.json @kabachuha
11+
/localizations/tr_TR.json @camenduru
12+
/localizations/zh_CN.json @dtlnor @bgluminous
13+
/localizations/zh_TW.json @benlisquare
File renamed without changes.

modules/api/api.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
from modules.api.models import *
3838
from modules.processing import StableDiffusionProcessingTxt2Img, StableDiffusionProcessingImg2Img, process_images
3939
from modules.sd_samplers import all_samplers
40-
from modules.extras import run_extras
40+
from modules.extras import run_extras, run_pnginfo
4141

4242
# copy from wrap_gradio_gpu_call of webui.py
4343
# because queue lock will be acquired in api handlers
@@ -90,6 +90,7 @@ def __init__(self, app, queue_lock):
9090
self.app.add_api_route("/sdapi/v1/img2img", self.img2imgapi, methods=["POST"], response_model=ImageToImageResponse)
9191
self.app.add_api_route("/sdapi/v1/extra-single-image", self.extras_single_image_api, methods=["POST"], response_model=ExtrasSingleImageResponse)
9292
self.app.add_api_route("/sdapi/v1/extra-batch-images", self.extras_batch_images_api, methods=["POST"], response_model=ExtrasBatchImagesResponse)
93+
self.app.add_api_route("/sdapi/v1/png-info", self.pnginfoapi, methods=["POST"], response_model=PNGInfoResponse)
9394
self.app.add_api_route("/sdapi/v1/progress", self.progressapi, methods=["GET"])
9495

9596
def text2imgapi(self, txt2imgreq: StableDiffusionTxt2ImgProcessingAPI):
@@ -188,6 +189,14 @@ def prepareFiles(file):
188189

189190
return ExtrasBatchImagesResponse(images=list(map(encode_pil_to_base64, result[0])), html_info=result[1])
190191

192+
def pnginfoapi(self, req: PNGInfoRequest):
193+
if(not req.image.strip()):
194+
return PNGInfoResponse(info="")
195+
196+
result = run_pnginfo(decode_base64_to_image(req.image.strip()))
197+
198+
return PNGInfoResponse(info=result[1])
199+
191200
def progressapi(self):
192201
# copy from check_progress_call of ui.py
193202

@@ -210,9 +219,6 @@ def progressapi(self):
210219

211220
return ProgressResponse(progress=progress, eta_relative=eta_relative, state=shared.state.js())
212221

213-
def pnginfoapi(self):
214-
raise NotImplementedError
215-
216222
def launch(self, server_name, port):
217223
self.app.include_router(self.router)
218224
uvicorn.run(self.app, host=server_name, port=port)

modules/api/models.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import inspect
2+
from click import prompt
23
from pydantic import BaseModel, Field, create_model
34
from typing import Any, Optional
45
from typing_extensions import Literal
@@ -150,6 +151,12 @@ class ExtrasBatchImagesRequest(ExtrasBaseRequest):
150151
class ExtrasBatchImagesResponse(ExtraBaseResponse):
151152
images: list[str] = Field(title="Images", description="The generated images in base64 format.")
152153

154+
class PNGInfoRequest(BaseModel):
155+
image: str = Field(title="Image", description="The base64 encoded PNG image")
156+
157+
class PNGInfoResponse(BaseModel):
158+
info: str = Field(title="Image info", description="A string with all the info the image had")
159+
153160
class ProgressResponse(BaseModel):
154161
progress: float
155162
eta_relative: float

modules/processing.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ def infotext(iteration=0, position_in_batch=0):
478478
model_hijack.embedding_db.load_textual_inversion_embeddings()
479479

480480
if p.scripts is not None:
481-
p.scripts.run_alwayson_scripts(p)
481+
p.scripts.process(p)
482482

483483
infotexts = []
484484
output_images = []
@@ -501,7 +501,7 @@ def infotext(iteration=0, position_in_batch=0):
501501
seeds = p.all_seeds[n * p.batch_size:(n + 1) * p.batch_size]
502502
subseeds = p.all_subseeds[n * p.batch_size:(n + 1) * p.batch_size]
503503

504-
if (len(prompts) == 0):
504+
if len(prompts) == 0:
505505
break
506506

507507
with devices.autocast():
@@ -590,7 +590,13 @@ def infotext(iteration=0, position_in_batch=0):
590590
images.save_image(grid, p.outpath_grids, "grid", p.all_seeds[0], p.all_prompts[0], opts.grid_format, info=infotext(), short_filename=not opts.grid_extended_filename, p=p, grid=True)
591591

592592
devices.torch_gc()
593-
return Processed(p, output_images, p.all_seeds[0], infotext() + "".join(["\n\n" + x for x in comments]), subseed=p.all_subseeds[0], all_prompts=p.all_prompts, all_seeds=p.all_seeds, all_subseeds=p.all_subseeds, index_of_first_image=index_of_first_image, infotexts=infotexts)
593+
594+
res = Processed(p, output_images, p.all_seeds[0], infotext() + "".join(["\n\n" + x for x in comments]), subseed=p.all_subseeds[0], all_prompts=p.all_prompts, all_seeds=p.all_seeds, all_subseeds=p.all_subseeds, index_of_first_image=index_of_first_image, infotexts=infotexts)
595+
596+
if p.scripts is not None:
597+
p.scripts.postprocess(p, res)
598+
599+
return res
594600

595601

596602
class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing):

modules/scripts.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,16 @@ def run(self, p, *args):
6464
def process(self, p, *args):
6565
"""
6666
This function is called before processing begins for AlwaysVisible scripts.
67-
scripts. You can modify the processing object (p) here, inject hooks, etc.
67+
You can modify the processing object (p) here, inject hooks, etc.
68+
args contains all values returned by components from ui()
69+
"""
70+
71+
pass
72+
73+
def postprocess(self, p, processed, *args):
74+
"""
75+
This function is called after processing ends for AlwaysVisible scripts.
76+
args contains all values returned by components from ui()
6877
"""
6978

7079
pass
@@ -289,13 +298,22 @@ def run(self, p: StableDiffusionProcessing, *args):
289298

290299
return processed
291300

292-
def run_alwayson_scripts(self, p):
301+
def process(self, p):
293302
for script in self.alwayson_scripts:
294303
try:
295304
script_args = p.script_args[script.args_from:script.args_to]
296305
script.process(p, *script_args)
297306
except Exception:
298-
print(f"Error running alwayson script: {script.filename}", file=sys.stderr)
307+
print(f"Error running process: {script.filename}", file=sys.stderr)
308+
print(traceback.format_exc(), file=sys.stderr)
309+
310+
def postprocess(self, p, processed):
311+
for script in self.alwayson_scripts:
312+
try:
313+
script_args = p.script_args[script.args_from:script.args_to]
314+
script.postprocess(p, processed, *script_args)
315+
except Exception:
316+
print(f"Error running postprocess: {script.filename}", file=sys.stderr)
299317
print(traceback.format_exc(), file=sys.stderr)
300318

301319
def reload_sources(self, cache):

0 commit comments

Comments
 (0)