Skip to content

Commit

Permalink
process text-to-image requested image count sequentially
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-astra-video committed Apr 21, 2024
1 parent 2f4bb57 commit 19129a2
Showing 1 changed file with 21 additions and 24 deletions.
45 changes: 21 additions & 24 deletions runner/app/routes/text_to_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from app.routes.util import image_to_data_url, ImageResponse, HTTPError, http_error
import logging
import random
import os
import os, json

router = APIRouter()

Expand Down Expand Up @@ -54,31 +54,28 @@ async def text_to_image(
f"{params.model_id}"
),
)

if params.seed is None:
init_seed = random.randint(0, 2**32 - 1)
if params.num_images_per_prompt > 1:
params.seed = [
i for i in range(init_seed, init_seed + params.num_images_per_prompt)
]
else:
params.seed = init_seed

try:
images = pipeline(**params.model_dump())
except Exception as e:
logger.error(f"TextToImagePipeline error: {e}")
logger.exception(e)
return JSONResponse(
status_code=500, content=http_error("TextToImagePipeline error")
)

seeds = params.seed
if not isinstance(seeds, list):
seeds = [seeds]

num_images_per_prompt = params.num_images_per_prompt
params.num_images_per_prompt = 1
seeds = []
images = []
init_seed = True if params.seed is None else False #create seeds if not specified
for i in range(num_images_per_prompt):
if init_seed:
params.seed = random.randint(0, 2**32 - 1)

try:
images.extend(pipeline(**params.model_dump()))
seeds.append(params.seed)
except Exception as e:
logger.error(f"TextToImagePipeline error: {e}")
logger.exception(e)
return JSONResponse(
status_code=500, content=http_error("TextToImagePipeline error")
)

output_images = []
for img, sd in zip(images, seeds):
output_images.append({"url": image_to_data_url(img), "seed": sd})

return {"images": output_images}

0 comments on commit 19129a2

Please sign in to comment.