Skip to content

Commit fcd75bd

Browse files
authored
Fix other apis
1 parent 75b67ee commit fcd75bd

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

modules/api/api.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import uvicorn
55
from threading import Lock
66
from io import BytesIO
7-
from gradio.processing_utils import encode_pil_to_base64, decode_base64_to_file, decode_base64_to_image
7+
from gradio.processing_utils import decode_base64_to_file
88
from fastapi import APIRouter, Depends, FastAPI, HTTPException
99
from fastapi.security import HTTPBasic, HTTPBasicCredentials
1010
from secrets import compare_digest
@@ -41,6 +41,10 @@ def setUpscalers(req: dict):
4141
reqDict.pop('upscaler_2')
4242
return reqDict
4343

44+
def decode_base64_to_image(encoding):
45+
if encoding.startswith("data:image/"):
46+
encoding = encoding.split(";")[1].split(",")[1]
47+
return Image.open(BytesIO(base64.b64decode(encoding)))
4448

4549
def encode_pil_to_base64(image):
4650
with io.BytesIO() as output_bytes:
@@ -134,10 +138,7 @@ def img2imgapi(self, img2imgreq: StableDiffusionImg2ImgProcessingAPI):
134138

135139
mask = img2imgreq.mask
136140
if mask:
137-
if mask.startswith("data:image/"):
138-
mask = decode_base64_to_image(mask)
139-
else:
140-
mask = Image.open(BytesIO(base64.b64decode(mask)))
141+
mask = decode_base64_to_image(mask)
141142

142143
populate = img2imgreq.copy(update={ # Override __init__ params
143144
"sd_model": shared.sd_model,
@@ -151,10 +152,7 @@ def img2imgapi(self, img2imgreq: StableDiffusionImg2ImgProcessingAPI):
151152

152153
imgs = []
153154
for img in init_images:
154-
if img.startswith("data:image/"):
155-
img = decode_base64_to_image(img)
156-
else:
157-
img = Image.open(BytesIO(base64.b64decode(img)))
155+
img = decode_base64_to_image(img)
158156
imgs = [img] * p.batch_size
159157

160158
p.init_images = imgs

0 commit comments

Comments
 (0)