测试极速成片

This commit is contained in:
Azmat@qq.com
2026-08-26 15:18:18 +08:00
parent 245525ec53
commit 0ee498d807
30 changed files with 1963 additions and 367 deletions
@@ -5,6 +5,26 @@ import requests
from .volcano import VolcanoArkProvider
# gpt-image / New API 网关只认这三档。业务层为了「真 16:9」会传 1536x864
# 不在这里收成合法横图的话,立绘能成、三视图 image_edit 直接 400。
_OPENAI_IMAGE_SIZES = {"1024x1024", "1024x1536", "1536x1024"}
def openai_image_size(size: str, default: str = "1024x1536") -> str:
"""把任意宽高收成 gpt-image 接受的三档:方 / 竖 / 横。"""
raw = (size or "").strip()
if raw in _OPENAI_IMAGE_SIZES:
return raw
try:
width, height = (int(part) for part in raw.lower().replace("×", "x").split("x", 1))
except (TypeError, ValueError):
return default
if width <= 0 or height <= 0:
return default
if width == height:
return "1024x1024"
return "1536x1024" if width > height else "1024x1536"
def _downscale_ref_for_edit(data: bytes, content_type: str, max_edge: int = 2048) -> tuple[bytes, str]:
"""图生图参考图过大时按需降采样,避免中转站(yunqi/gpt-image edits)拒收大图报 400
@@ -91,7 +111,7 @@ class OpenAICompatibleProvider(VolcanoArkProvider):
"""文生图(可选单图参考 base64)。多图参考请用 image_edit。返回体含 url 或 b64_json。"""
if not self.api_key:
raise ValueError("中转站 api_key 未配置")
body: dict[str, Any] = {"model": model, "prompt": prompt, "size": size, "n": 1}
body: dict[str, Any] = {"model": model, "prompt": prompt, "size": openai_image_size(size), "n": 1}
if image:
body["image"] = image
# 实测中转站生图延迟可达 75s+,超时给到 300s
@@ -135,7 +155,7 @@ class OpenAICompatibleProvider(VolcanoArkProvider):
files.append(("image[]", (f"ref{idx + 1}.{ext}", img_bytes, content_type)))
if not files:
raise ValueError("image_edit 至少需要一张参考图")
data = {"model": model, "prompt": prompt, "size": size, "n": "1"}
data = {"model": model, "prompt": prompt, "size": openai_image_size(size), "n": "1"}
response = requests.post(
self._endpoint_url(endpoint),
headers={"Authorization": f"Bearer {self.api_key}"}, # multipart 不要手设 Content-Type
+2 -1
View File
@@ -3,6 +3,7 @@ from typing import Any
import requests
from django.conf import settings
from .openai_compatible import openai_image_size
from .volcano import VolcanoArkProvider
@@ -32,7 +33,7 @@ class YunqiProvider(VolcanoArkProvider):
raise ValueError("YUNQI_API_KEY is not configured")
if image:
raise ValueError("gpt-image-2 via YunQi only supports text-to-image; reference image is not supported")
body: dict[str, Any] = {"model": model, "prompt": prompt, "size": size, "n": 1}
body: dict[str, Any] = {"model": model, "prompt": prompt, "size": openai_image_size(size), "n": 1}
# 实测该网关生图平均延迟 75s+,超时须显著高于火山的 180s
response = requests.post(
f"{self.base_url.rstrip('/')}/{endpoint.lstrip('/')}",