Flux1.1 [pro] Ultra:BFL的端点⛵
社区文章 发布于2024年11月9日
使用 FLUX 1.1 [pro] Ultra 创建请求并轮询结果
为了成功运行所提供的使用 FLUX 1.1 [pro] API 的代码,您可以按照以下方式构建设置。这将包括设置 Python 脚本和环境,以确保您不会遇到错误。
步骤 1:安装所需库
确保您已安装 requests
库
pip install requests
步骤 2:设置项目结构
创建如下项目文件夹和文件结构
flux_image_generation/
├── .env # For storing your API key
├── generate_image.py # The main Python script to run
└── requirements.txt # List of required libraries
步骤 3:将您的 API 密钥添加到环境文件
在项目文件夹中创建 .env
文件并添加您的 API 密钥
BFL_API_KEY=your_actual_api_key_here
注意:将
your_actual_api_key_here
替换为提供给您的实际 API 密钥。
步骤 4:编写 Python 脚本
在 generate_image.py
中,编写以下代码
import os
import time
import requests
from dotenv import load_dotenv
# Load environment variables from the .env file
load_dotenv()
def submit_request():
# Set up the request payload and headers
response = requests.post(
'https://api.bfl.ml/v1/flux-pro-1.1-ultra',
headers={
'accept': 'application/json',
'x-key': os.environ.get("BFL_API_KEY"),
'Content-Type': 'application/json',
},
json={
'prompt': 'A cat on its back legs running like a human is holding a big silver fish with its arms. The cat is running away from the shop owner and has a panicked look on his face. The scene is situated in a crowded market.',
'width': 1024,
'height': 768,
},
)
# Check if the request was successful
if response.status_code == 200:
request_data = response.json()
print("Request Submitted:", request_data)
return request_data.get("id")
else:
print("Error submitting request:", response.text)
return None
def poll_for_result(request_id):
# Poll the get_result endpoint until the result is ready
while True:
time.sleep(0.5)
result = requests.get(
'https://api.bfl.ml/v1/get_result',
headers={
'accept': 'application/json',
'x-key': os.environ.get("BFL_API_KEY"),
},
params={'id': request_id},
).json()
# Check result status
if result["status"] == "Ready":
print("Result:", result['result']['sample'])
break
else:
print("Status:", result["status"])
if __name__ == "__main__":
# Submit the request
request_id = submit_request()
if request_id:
# Poll for the result if the request was successfully submitted
poll_for_result(request_id)
步骤 5:安装所需的 Python 包
将 requests
和 python-dotenv
添加到 requirements.txt
requests
python-dotenv
安装要求
pip install -r requirements.txt
步骤 6:运行脚本
在 flux_image_generation
目录中,运行
python generate_image.py
结果窗口
D:\flux_image_generation>python generate_image.py
Request Submitted: {'id': '00a0bcc2-e937-4663-b183-7c696c56d452'}
Status: Pending
Status: Pending
Status: Pending
Status: Pending
Status: Pending
Status: Pending
Status: Pending
Result: https://bfldeliverysc.blob.core.windows.net/results/6e4eda6134f540009a23b5b32e45c1f0/sample.jpeg?se=2024-11-08T18%3A40%3A56Z&sp=r&sv=2024-11-04&sr=b&rsct=image/jpeg&sig=fnjwl6QBH/TcJPBal1vK9xQ0BZyI0kHL7lLB%2B3BY8xM%3D
关键步骤
- 环境变量:我们使用
.env
文件安全地存储 API 密钥,并使用python-dotenv
在脚本中加载它。 - 错误处理:脚本在提交请求后检查响应是否成功。
- 轮询:脚本每 0.5 秒轮询 API 一次,直到图像生成结果准备就绪。
只要 API 密钥有效,API 端点可访问,并且请求格式正确,此设置就应该能正常运行。如果您在运行过程中遇到任何问题,请告诉我!
- 文章结束,感谢阅读 🤗!
尝试一下!
| GitHub | Flux API | | Hugging Face | prithivMLmods |