跳到主要内容

使用 LORA 微调模型

功能描述

LoRA 指的是低秩适应(Low-Rank Adaptation),是一种在预训练模型基础上进行微调的方法。LoRA 可以在不改变预训练模型权重的情况下,通过引入低秩矩阵来适应新的任务。在文生图和图像编辑任务中,LoRA 可以用于微调预训练的图像模型,使其在特定的任务上表现更好。

你可以通过模力方舟提供的 模型微调 来生成 LORA 模型,然后在文生图接口中使用这些模型。

支持 LoRA 的模型

体验接口效果

功能说明

Serverless API 提供了接口快速体验的界面,可以快速体验接口效果。 打开 Serverless API 页面 ,找到图像生成与处理下的 Flux.1-dev 模型,点击进入接口详情页。

image

文生图接口中,LoRA 设置中 url 是需要加载的 LoRA 模型的 URL,weight 是用于不同 LoRA 模型权重之间的加权融合,lora_scale 是 LoRA 模型对基础模型的影响程度。

调用文生图模型

使用 OpenAI SDK

以下将说明如何通过 OpenAI SDK 使用 Flux.1-dev 文生图 LoRA 接口。以下是一个使用 Python 的示例:

python
from openai import OpenAI
import base64
import requests

client = OpenAI(
base_url="https://ai.gitee.com/v1",
api_key="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", # 替换为您的 API Key
)

response = client.images.generate(
prompt="A robot sitting on open grassland, painting on a canvas.",
model="FLUX.1-dev",
size="1024x1024",
extra_body={
"num_inference_steps": 20,
"guidance_scale": 7.5,
"seed": 42,
"lora_weights": [
{
"url": "https://example.com/lora-model.safetensors", # 替换为您的 LoRA 模型 URL
"weight": 1,
}
],
"lora_scale": 0.5
},
)

for i, image_data in enumerate(response.data):
if image_data.url:
# Download from URL
ext = image_data.url.split('.')[-1].split('?')[0] or "jpg"
filename = f"FLUX.1-dev-output-{i}.{ext}"
response = requests.get(image_data.url, timeout=30)
response.raise_for_status()
with open(filename, "wb") as f:
f.write(response.content)
print(f"Downloaded image to {filename}")
elif image_data.b64_json:
# Decode base64
image_bytes = base64.b64decode(image_data.b64_json)
filename = f"FLUX.1-dev-output-{i}.jpg"
with open(filename, "wb") as f:
f.write(image_bytes)
print(f"Saved image to {filename}")

使用 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": "FLUX.1-dev", # 替换为您选择的模型名称
"size": "1024x1024",
"seed": 42,
"response_format": "b64_json",
"num_inference_steps": 25,
"guidance_scale": 7.5,
"lora_weights": [
{
"url": "https://example.com/lora-model.safetensors", # 替换为您的 LoRA 模型 URL
"weight": 1,
}
],
"lora_scale": 0.5
}

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

if response.status_code == 200:
for i, image_data in enumerate(response.data):
if image_data.url:
# Download from URL
ext = image_data.url.split('.')[-1].split('?')[0] or "jpg"
filename = f"FLUX.1-dev-output-{i}.{ext}"
response = requests.get(image_data.url, timeout=30)
response.raise_for_status()
with open(filename, "wb") as f:
f.write(response.content)
print(f"Downloaded image to {filename}")
elif image_data.b64_json:
# Decode base64
image_bytes = base64.b64decode(image_data.b64_json)
filename = f"FLUX.1-dev-output-{i}.jpg"
with open(filename, "wb") as f:
f.write(image_bytes)
print(f"Saved image to {filename}")
else:
print(f"请求失败,状态码: {response.status_code}")
print(f"错误信息: {response.text}")

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