跳到主要内容

文生图

功能描述

文生图模型可以根据用户输入的文本描述生成相应的图像。该模型支持多种风格和主题,能够生成高质量的图像,适用于创意设计、艺术创作等多个领域。

模型参数

注意事项

同是文生图模型,但不同模型的调用参数可能不一致,具体的参数以及参数的意思请参考对应模型的体验界面。

文生图模型列表

模力方舟提供非常丰富的文生图模型,您可以在 模型广场 中查看和体验这些模型。

加载 Serverless API 服务列表...

调用文生图模型

使用 OpenAI SDK

您可以使用 OpenAI 的 SDK 来调用文生图模型。以下是一个使用 Python 的示例:

python
from openai import OpenAI
import base64

client = OpenAI(
base_url="https://ai.gitee.com/v1",
api_key="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", # 替换为您的 API Key
default_headers={"X-Failover-Enabled":"true"},
)

response = client.images.generate(
prompt="a white siamese cat", # 替换为您的文本描述
model="Kolors", # 替换为您选择的模型名称
size="1024x1024",
extra_body={
"num_inference_steps": 25,
"guidance_scale": 7.5,
},
)

image_base64 = response.data[0].b64_json
image_bytes = base64.b64decode(image_base64)

# Save the image to a file
with open("Kolors-output.png", "wb") as f:
f.write(image_bytes)

使用 Requests 库

如果您不想使用 OpenAI 的 SDK,也可以直接使用 requests 库来调用文生图模型。以下是一个使用 Python 的示例:

python
import requests
import base64
import json

url = "https://ai.gitee.com/v1/images/generations"

headers = {
"Authorization": "Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", # 替换为您的 API Key
"Content-Type": "application/json",
"X-Failover-Enabled": "true"
}

data = {
"prompt": "a white siamese cat", # 替换为您的文本描述
"model": "Kolors", # 替换为您选择的模型名称
"size": "1024x1024",
"response_format": "b64_json",
"num_inference_steps": 25,
"guidance_scale": 7.5
}

response = requests.post(url, headers=headers, json=data)

if response.status_code == 200:
result = response.json()
image_base64 = result["data"][0]["b64_json"]
image_bytes = base64.b64decode(image_base64)

# 保存图片到文件
with open("Kolors-output.png", "wb") as f:
f.write(image_bytes)

print("图片生成成功并已保存为 Kolors-output.png")
else:
print(f"请求失败,状态码: {response.status_code}")
print(f"错误信息: {response.text}")

其他编程语言可以参考 接口文档 中的示例代码。