Trabajo
UCloud ModelVerse 图片 API 用法
如何使用 UCloud 的 api
Usar este prompt
UCloud ModelVerse 图片 API 用法
接口:
POST https://api.umodelverse.ai/v1/images/generations
请求头:
Authorization: Bearer YOUR_API_KEY
Content-Type: application/json
推荐参数:
{
"model": "gpt-image-1.5",
"prompt": "英文图片提示词",
"n": 1,
"size": "1536x1024",
"quality": "high"
}
注意:这次实测 UCloud 不接受 response_format,加了会报:
Invalid param: Unknown parameter: 'response_format'
所以不要传 response_format。返回里可能是 b64_json,也可能是 url,脚本要两种都兼容。
标准 Python 调用模板
import base64
import json
import os
import urllib.request
import urllib.error
from pathlib import Path
API_URL = "https://api.umodelverse.ai/v1/images/generations"
MODEL = "gpt-image-1.5"
api_key = os.environ["MODELVERSE_API_KEY"]
prompt = """
Create a realistic B2B RF antenna industry blog image.
No readable text, no labels, no numbers, no Chinese characters,
no watermark, no logo, no brand names.
Professional engineering photography style.
"""
payload = {
"model": MODEL,
"prompt": prompt,
"n": 1,
"size": "1536x1024",
"quality": "high"
}
req = urllib.request.Request(
API_URL,
data=json.dumps(payload).encode("utf-8"),
headers={
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
},
method="POST",
)
try:
with urllib.request.urlopen(req, timeout=300) as resp:
body = resp.read().decode("utf-8")
except urllib.error.HTTPError as e:
err = e.read().decode("utf-8", errors="replace")
raise RuntimeError(f"UCloud API {e.code}: {err}") from e
data = json.loads(body)
image = data["data"][0]
if image.get("b64_json"):
image_bytes = base64.b64decode(image["b64_json"])
elif image.get("url"):
with urllib.request.urlopen(image["url"], timeout=300) as img_resp:
image_bytes = img_resp.read()
else:
raise RuntimeError(f"Unsupported response: {body[:1000]}")
Path("output.png").write_bytes(image_bytes)
终端使用方式
不要把 key 写进脚本或日志,只临时放环境变量:
export MODELVERSE_API_KEY='你的UCloud API Key'
python3 generate_images.py
Todavía no hay comentarios. Añade la primera nota útil.