[Misc] Fix t2i online serving example (#928)

Signed-off-by: zjy0516 <riverclouds.zhu@qq.com>
This commit is contained in:
Jiangyun Zhu authored and GitHub committed 2026-01-23 19:32:06 +08:00
1 parent 21ee1ff6e6
commit 28a6597c4c
2 files changed
+33 -62

No files matched your search

@@ -1,6 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
""" """
Qwen-Image OpenAI-compatible chat client for image generation. Qwen-Image OpenAI-compatible image generation client.
Usage: Usage:
python openai_chat_client.py --prompt "A beautiful landscape" --output output.png python openai_chat_client.py --prompt "A beautiful landscape" --output output.png
@@ -25,14 +25,14 @@ def generate_image(
negative_prompt: str | None = None, negative_prompt: str | None = None,
num_outputs_per_prompt: int = 1, num_outputs_per_prompt: int = 1,
) -> bytes | None: ) -> bytes | None:
"""Generate an image using the chat completions API. """Generate an image using the images generation API.
Args: Args:
prompt: Text description of the image prompt: Text description of the image
server_url: Server URL server_url: Server URL
height: Image height in pixels height: Image height in pixels
width: Image width in pixels width: Image width in pixels
steps: Number of inference steps steps: Number of diffusion steps
true_cfg_scale: Qwen-Image CFG scale true_cfg_scale: Qwen-Image CFG scale
seed: Random seed seed: Random seed
negative_prompt: Negative prompt negative_prompt: Negative prompt
@@ -41,33 +41,31 @@ def generate_image(
Returns: Returns:
Image bytes or None if failed Image bytes or None if failed
""" """
messages = [{"role": "user", "content": prompt}] payload: dict[str, object] = {
"prompt": prompt,
"response_format": "b64_json",
"n": num_outputs_per_prompt,
}
if width is not None and height is not None:
payload["size"] = f"{width}x{height}"
elif width is not None:
payload["size"] = f"{width}x{width}"
elif height is not None:
payload["size"] = f"{height}x{height}"
# Build extra_body with generation parameters
extra_body = {}
if height is not None:
extra_body["height"] = height
if width is not None:
extra_body["width"] = width
if steps is not None: if steps is not None:
extra_body["num_inference_steps"] = steps payload["num_inference_steps"] = steps
if true_cfg_scale is not None: if true_cfg_scale is not None:
extra_body["true_cfg_scale"] = true_cfg_scale payload["true_cfg_scale"] = true_cfg_scale
if seed is not None:
extra_body["seed"] = seed
if negative_prompt: if negative_prompt:
extra_body["negative_prompt"] = negative_prompt payload["negative_prompt"] = negative_prompt
extra_body["num_outputs_per_prompt"] = num_outputs_per_prompt if seed is not None:
payload["seed"] = seed
# Build request payload
payload = {"messages": messages}
if extra_body:
payload["extra_body"] = extra_body
# Send request
try: try:
response = requests.post( response = requests.post(
f"{server_url}/v1/chat/completions", f"{server_url}/v1/images/generations",
headers={"Content-Type": "application/json"}, headers={"Content-Type": "application/json"},
json=payload, json=payload,
timeout=300, timeout=300,
@@ -75,15 +73,13 @@ def generate_image(
response.raise_for_status() response.raise_for_status()
data = response.json() data = response.json()
# Extract image from response items = data.get("data")
content = data["choices"][0]["message"]["content"] if isinstance(items, list) and items:
if isinstance(content, list) and len(content) > 0: first = items[0].get("b64_json") if isinstance(items[0], dict) else None
image_url = content[0].get("image_url", {}).get("url", "") if isinstance(first, str):
if image_url.startswith("data:image"): return base64.b64decode(first)
_, b64_data = image_url.split(",", 1)
return base64.b64decode(b64_data)
print(f"Unexpected response format: {content}") print(f"Unexpected response format: {data}")
return None return None
except Exception as e: except Exception as e:
@@ -1,35 +1,10 @@
#!/bin/bash #!/bin/bash
# Qwen-Image text-to-image curl example # Qwen-Image text-to-image curl example
SERVER="${SERVER:-http://localhost:8091}" curl -X POST http://localhost:8091/v1/images/generations \
PROMPT="${PROMPT:-a good boy in the ocean}"
CURRENT_TIME=$(date +%Y%m%d%H%M%S)
OUTPUT="${OUTPUT:-qwen_image_output_${CURRENT_TIME}.png}"
echo "Generating image..."
echo "Prompt: $PROMPT"
echo "Output: $OUTPUT"
curl -s "$SERVER/v1/chat/completions" \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-d "{ -d '{
\"messages\": [ "prompt": "a dragon laying over the spine of the Green Mountains of Vermont",
{\"role\": \"user\", \"content\": \"$PROMPT\"} "size": "1024x1024",
], "seed": 42
\"extra_body\": { }' | jq -r '.data[0].b64_json' | base64 -d > dragon.png
\"height\": 1024,
\"width\": 1024,
\"num_inference_steps\": 100,
\"true_cfg_scale\": 4.0,
\"seed\": 42,
\"num_outputs_per_prompt\": 1
}
}" | jq -r '.choices[0].message.content[0].image_url.url' | sed 's/^data:image[^,]*,\s*//' | base64 -d > "$OUTPUT"
if [ -f "$OUTPUT" ]; then
echo "Image saved to: $OUTPUT"
echo "Size: $(du -h "$OUTPUT" | cut -f1)"
else
echo "Failed to generate image"
exit 1
fi