[Feat][HunyuanImage3] Stream AR text for IT2I image edits (#3723)
Signed-off-by: TaffyOfficial <2324465096@qq.com> Co-authored-by: TaffyOfficial <2324465096@qq.com>
This commit is contained in:
1 parent
283873411a
commit
3c48257f8d
7 files changed
+556
-62
No files matched your search
@@ -92,6 +92,7 @@ from vllm_omni.entrypoints.openai.errors import InvalidInputReferenceError
|
||||
from vllm_omni.entrypoints.openai.image_api_utils import (
|
||||
SUPPORTED_LAYERED_RESOLUTIONS,
|
||||
encode_image_base64,
|
||||
encode_image_base64_with_compression,
|
||||
parse_size,
|
||||
validate_layered_layers,
|
||||
)
|
||||
@@ -1644,7 +1645,7 @@ async def generate_images(request: ImageGenerationRequest, raw_request: Request)
|
||||
|
||||
# Encode images to base64 with the specified format
|
||||
image_data = [
|
||||
ImageData(b64_json=_encode_image_base64_with_compression(img, format=output_format), revised_prompt=None)
|
||||
ImageData(b64_json=encode_image_base64_with_compression(img, format=output_format), revised_prompt=None)
|
||||
for img in images
|
||||
]
|
||||
|
||||
@@ -1694,6 +1695,7 @@ async def edit_images(
|
||||
output_format: str | None = Form("png"),
|
||||
background: str | None = Form("auto"),
|
||||
output_compression: Annotated[int, Form(ge=0, le=100)] = 100,
|
||||
stream: bool = Form(False),
|
||||
user: str | None = Form(None), # unused now
|
||||
# vllm-omni extensions for image editing
|
||||
mask_image: str | UploadFile | None = None,
|
||||
@@ -1734,6 +1736,11 @@ async def edit_images(
|
||||
status_code=HTTPStatus.BAD_REQUEST.value,
|
||||
detail="Only response_format 'b64_json' is supported now.",
|
||||
)
|
||||
if stream and len(stage_configs) <= 1:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.BAD_REQUEST.value,
|
||||
detail="stream=true is only supported for multi-stage image editing pipelines.",
|
||||
)
|
||||
try:
|
||||
# 2. Build prompt & images params
|
||||
cot_output = None
|
||||
@@ -1941,7 +1948,17 @@ async def edit_images(
|
||||
extra_body=extra_body,
|
||||
reference_images=ref_b64_list,
|
||||
request_id=request_id,
|
||||
stream=stream,
|
||||
model=model_name,
|
||||
output_format=output_format,
|
||||
output_compression=output_compression,
|
||||
size=size_str,
|
||||
)
|
||||
if stream and not isinstance(generation_result, ErrorResponse):
|
||||
return StreamingResponse(
|
||||
content=generation_result,
|
||||
media_type="text/event-stream",
|
||||
)
|
||||
if isinstance(generation_result, ErrorResponse):
|
||||
raise HTTPException(
|
||||
status_code=generation_result.error.code if generation_result.error else 400,
|
||||
@@ -1964,7 +1981,7 @@ async def edit_images(
|
||||
# Encode images to base64
|
||||
image_data = [
|
||||
ImageData(
|
||||
b64_json=_encode_image_base64_with_compression(
|
||||
b64_json=encode_image_base64_with_compression(
|
||||
img, format=output_format, output_compression=output_compression
|
||||
),
|
||||
revised_prompt=None,
|
||||
@@ -2289,48 +2306,6 @@ def _choose_output_format(output_format: str | None, background: str | None) ->
|
||||
return "jpeg"
|
||||
|
||||
|
||||
def _prepare_image_for_output_format(image: Image.Image, format: str) -> Image.Image:
|
||||
fmt = format.lower()
|
||||
if fmt not in {"jpg", "jpeg"}:
|
||||
return image
|
||||
|
||||
if image.mode == "RGB":
|
||||
return image
|
||||
|
||||
if image.mode in {"RGBA", "LA"} or (image.mode == "P" and "transparency" in image.info):
|
||||
alpha_image = image.convert("RGBA")
|
||||
flattened = Image.new("RGB", alpha_image.size, (255, 255, 255))
|
||||
flattened.paste(alpha_image, mask=alpha_image.getchannel("A"))
|
||||
return flattened
|
||||
|
||||
return image.convert("RGB")
|
||||
|
||||
|
||||
def _encode_image_base64_with_compression(
|
||||
image: Image.Image, format: str = "png", output_compression: int = 100
|
||||
) -> str:
|
||||
"""Encode PIL Image to a base64 image string.
|
||||
|
||||
Args:
|
||||
image: PIL Image object
|
||||
format: Output image format (e.g., "PNG", "JPEG", "WEBP")
|
||||
output_compression: Compression level (0-100%), 100 for best quality
|
||||
Returns:
|
||||
Base64-encoded image as string
|
||||
"""
|
||||
buffer = io.BytesIO()
|
||||
image = _prepare_image_for_output_format(image, format)
|
||||
save_kwargs = {}
|
||||
if format in ("jpg", "jpeg", "webp"):
|
||||
save_kwargs["quality"] = output_compression
|
||||
elif format == "png":
|
||||
save_kwargs["compress_level"] = max(0, min(9, 9 - output_compression // 11)) # Map 0-100 to 9-0
|
||||
|
||||
image.save(buffer, format=format, **save_kwargs)
|
||||
buffer.seek(0)
|
||||
return base64.b64encode(buffer.read()).decode("utf-8")
|
||||
|
||||
|
||||
def apply_stage_default_sampling_params(
|
||||
default_params_json: str | None,
|
||||
sampling_params: Any,
|
||||
|
||||
Reference in new issue
Block a user