Flux1.1 [pro] Ultra : Endpoint by BFL ⛵
Community Article
Published
November 9, 2024
Creating request with FLUX 1.1 [pro] Ultra & Poll for result
To successfully run the provided code using the FLUX 1.1 [pro] API, here is how you can structure the setup. This will include setting up the Python script and the environment to ensure you don’t encounter errors.
Step 1: Install Required Libraries
Ensure you have the requests
library installed:
pip install requests
Step 2: Set Up Your Project Structure
Create a project folder and file structure as follows:
flux_image_generation/
├── .env # For storing your API key
├── generate_image.py # The main Python script to run
└── requirements.txt # List of required libraries
Step 3: Add Your API Key to an Environment File
Create a .env
file in the project folder and add your API key:
BFL_API_KEY=your_actual_api_key_here
Note: Replace
your_actual_api_key_here
with the actual API key provided to you.
Step 4: Write the Python Script
In generate_image.py
, write the following code:
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)
Step 5: Install Required Python Packages
Add requests
and python-dotenv
to requirements.txt
:
requests
python-dotenv
Install the requirements:
pip install -r requirements.txt
Step 6: Run the Script
From within the flux_image_generation
directory, run:
python generate_image.py
Result Window
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
Key Steps
- Environment Variables: We use the
.env
file to securely store the API key andpython-dotenv
to load it in the script. - Error Handling: The script checks for a successful response after submitting the request.
- Polling: The script polls the API every 0.5 seconds until the image generation result is ready.
This setup should work without errors as long as the API key is valid, the API endpoint is reachable, and the request is formatted correctly. Let me know if you encounter any issues while running it!
- End of Article, Thanks for Reading 🤗!.
Try It Out!
| GitHub | Flux API | | Hugging Face | prithivMLmods |