FLUX文生圖模型
支持的領域 / 任務:aigc
FLUX模型是由 Black Forest Labs 開源的高質量文本到圖像生成模型,它在多個維度上展現了卓越性能,尤其在文本引導的圖像生成、多主體場景構建、以及精細的手部細節生成等方面,實現了顯著的提升,為文生圖領域設定了新的技術標桿。
上線的 FLUX.1 [schnell] 模型作為目前開源最先進的少步模型,不僅超越了同類競爭者,甚至還優于諸如 Midjourney v6.0 和 DALL·E 3 (HD) 等強大的非精餾模型。該模型經過專門微調,以保留預訓練階段的全部輸出多樣性,相較于當前市場上的最先進開源模型, FLUX.1 [schnell] 顯著提升了在視覺質量、指令遵從、尺寸/比例變化、字體處理及輸出多樣性等方面的可能,為用戶帶來更為豐富多樣的創意圖像生成體驗。
同時上線的 FLUX.1 [dev] 模型是一款面向非商業應用的開源權重、精煉模型。 FLUX.1 [dev] 在保持了與 FLUX 專業版相近的圖像質量和指令遵循能力的同時,具備更高的運行效率。相較于同尺寸的標準模型,它在資源利用上更為高效。
除了為 FLUX 系列的官方模型與基于 FLUX 架構的開源社區模型提供服務化支撐外,我們還針對 FLUX 文生圖模型實施了中文 Prompt 的適應性優化。此番調整確保了模型能夠深刻理解并精準響應中文指令,進而生成與英文 Prompt 同等質量的圖像成果。
快速開始
前提條件
已開通服務并獲得api-key:API-KEY的獲取與配置。
已安裝SDK:安裝DashScope SDK。
示例代碼
以下示例展示了調用 flux-schnell 模型API進行文生圖的快速示例代碼。以下示例展示了調用 flux-schnell 模型API進行文生圖的快速示例代碼。如果要調用 flux-dev 模型,只需要修改 model 為 "flux-dev" 即可。
需要使用您的api-key替換示例中的 your-dashscope-api-key ,代碼才能正常運行。
from http import HTTPStatus
from urllib.parse import urlparse, unquote
from pathlib import PurePosixPath
import requests
import dashscope
model = "flux-schnell"
prompt = "Eagle flying freely in the blue sky and white clouds"
prompt_cn = "一只飛翔在藍天白云的鷹" # Prompt支持中英文
def simple_call(input_prompt):
rsp = dashscope.ImageSynthesis.call(model=model,
prompt=input_prompt,
size='1024*1024')
if rsp.status_code == HTTPStatus.OK:
print(rsp.output)
print(rsp.usage)
# save file to current directory
for result in rsp.output.results:
file_name = PurePosixPath(unquote(urlparse(result.url).path)).parts[-1]
with open('./%s' % file_name, 'wb+') as f:
f.write(requests.get(result.url).content)
else:
print('Failed, status_code: %s, code: %s, message: %s' %
(rsp.status_code, rsp.code, rsp.message))
if __name__ == '__main__':
simple_call(prompt)
simple_call(prompt_cn)
// Copyright (c) Alibaba, Inc. and its affiliates.
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisParam;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class Main {
private static final OkHttpClient CLIENT = new OkHttpClient();
private static final String MODEL = "flux-schnell";
private static final String PROMPT = "Eagle flying freely in the blue sky and white clouds";
private static final String PROMPT_CN = "一只飛翔在藍天白云的鷹""
private static final String SIZE = "1024*1024";
public static void basicCall() throws ApiException, NoApiKeyException, IOException {
ImageSynthesis is = new ImageSynthesis();
ImageSynthesisParam param =
ImageSynthesisParam.builder()
.model(Main.MODEL)
.n(1)
.size(Main.SIZE)
.prompt(Main.PROMPT)
.negativePrompt("garfield")
.build();
ImageSynthesisResult result = is.call(param);
System.out.println(result);
// save image to local files.
for(Map<String, String> item :result.getOutput().getResults()){
String paths = new URL(item.get("url")).getPath();
String[] parts = paths.split("/");
String fileName = parts[parts.length-1];
Request request = new Request.Builder()
.url(item.get("url"))
.build();
try (Response response = CLIENT.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
}
Path file = Paths.get(fileName);
Files.write(file, response.body().bytes());
}
}
}
public void fetchTask() throws ApiException, NoApiKeyException {
String taskId = "your task id";
ImageSynthesis is = new ImageSynthesis();
// If set DASHSCOPE_API_KEY environment variable, apiKey can null.
ImageSynthesisResult result = is.fetch(taskId, null);
System.out.println(result.getOutput());
System.out.println(result.getUsage());
}
public static void main(String[] args){
try{
basicCall();
}catch(ApiException|NoApiKeyException | IOException e){
System.out.println(e.getMessage());
}
System.exit(0);
}
}
調用成功后,將會返回如下示例結果。
{
"status_code": 200,
"request_id": "ea8bfe77-2f35-9df3-ba47-7e05e917b3df",
"code": null,
"message": "",
"output": {
"task_id": "dea97660-9651-4e6b-a9c3-8afb325b28d0",
"task_status": "SUCCEEDED",
"results": [
{
"url": "url1"
}
],
"task_metrics": {
"TOTAL": 1,
"SUCCEEDED": 1,
"FAILED": 0
}
},
"usage": {
"image_count": 1
}
}
了解更多
有關 FLUX 文生圖模型API的詳細調用文檔可前往API詳情頁面進行了解。