Skip to content

Commit

Permalink
[Bugfix][API] - Fix API response for colab users
Browse files Browse the repository at this point in the history
  • Loading branch information
sphuff authored and AUTOMATIC1111 committed Oct 26, 2022
1 parent cbb857b commit db9ab1a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
17 changes: 13 additions & 4 deletions modules/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from fastapi import Body, APIRouter, HTTPException
from fastapi.responses import JSONResponse
from pydantic import BaseModel, Field, Json
from typing import List
import json
import io
import base64
Expand All @@ -15,12 +16,12 @@
sampler_to_index = lambda name: next(filter(lambda row: name.lower() == row[1].name.lower(), enumerate(all_samplers)), None)

class TextToImageResponse(BaseModel):
images: list[str] = Field(default=None, title="Image", description="The generated image in base64 format.")
images: List[str] = Field(default=None, title="Image", description="The generated image in base64 format.")
parameters: Json
info: Json

class ImageToImageResponse(BaseModel):
images: list[str] = Field(default=None, title="Image", description="The generated image in base64 format.")
images: List[str] = Field(default=None, title="Image", description="The generated image in base64 format.")
parameters: Json
info: Json

Expand All @@ -41,6 +42,9 @@ def __base64_to_image(self, base64_string):
# convert base64 to PIL image
return Image.open(io.BytesIO(imgdata))

def __processed_info_to_json(self, processed):
return json.dumps(processed.info)

def text2imgapi(self, txt2imgreq: StableDiffusionTxt2ImgProcessingAPI):
sampler_index = sampler_to_index(txt2imgreq.sampler_index)

Expand All @@ -65,7 +69,7 @@ def text2imgapi(self, txt2imgreq: StableDiffusionTxt2ImgProcessingAPI):
i.save(buffer, format="png")
b64images.append(base64.b64encode(buffer.getvalue()))

return TextToImageResponse(images=b64images, parameters=json.dumps(vars(txt2imgreq)), info=json.dumps(processed.info))
return TextToImageResponse(images=b64images, parameters=json.dumps(vars(txt2imgreq)), info=processed.js())



Expand Down Expand Up @@ -111,7 +115,12 @@ def img2imgapi(self, img2imgreq: StableDiffusionImg2ImgProcessingAPI):
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))
if (not img2imgreq.include_init_images):
# remove img2imgreq.init_images and img2imgreq.mask
img2imgreq.init_images = None
img2imgreq.mask = None

return ImageToImageResponse(images=b64images, parameters=json.dumps(vars(img2imgreq)), info=processed.js())

def extrasapi(self):
raise NotImplementedError
Expand Down
10 changes: 6 additions & 4 deletions modules/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ModelDef(BaseModel):
field_alias: str
field_type: Any
field_value: Any
field_exclude: bool = False


class PydanticModelGenerator:
Expand Down Expand Up @@ -68,7 +69,7 @@ def merge_class_params(class_):
field=underscore(k),
field_alias=k,
field_type=field_type_generator(k, v),
field_value=v.default
field_value=v.default,
)
for (k,v) in self._class_data.items() if k not in API_NOT_ALLOWED
]
Expand All @@ -78,15 +79,16 @@ def merge_class_params(class_):
field=underscore(fields["key"]),
field_alias=fields["key"],
field_type=fields["type"],
field_value=fields["default"]))
field_value=fields["default"],
field_exclude=fields["exclude"] if "exclude" in fields else False))

def generate_model(self):
"""
Creates a pydantic BaseModel
from the json and overrides provided at initialization
"""
fields = {
d.field: (d.field_type, Field(default=d.field_value, alias=d.field_alias)) for d in self._model_def
d.field: (d.field_type, Field(default=d.field_value, alias=d.field_alias, exclude=d.field_exclude)) for d in self._model_def
}
DynamicModel = create_model(self._model_name, **fields)
DynamicModel.__config__.allow_population_by_field_name = True
Expand All @@ -102,5 +104,5 @@ def generate_model(self):
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}, {"key": "mask", "type": str, "default": None}]
[{"key": "sampler_index", "type": str, "default": "Euler"}, {"key": "init_images", "type": list, "default": None}, {"key": "denoising_strength", "type": float, "default": 0.75}, {"key": "mask", "type": str, "default": None}, {"key": "include_init_images", "type": bool, "default": False, "exclude" : True}]
).generate_model()

0 comments on commit db9ab1a

Please sign in to comment.