General Introduction
CloudFlare-AI-Image is an open source project hosted on GitHub that provides free AI image generation services through the Cloudflare Workers platform. It supports text-to-map and map-to-map functions , based on five models , including Flux.1 [schnell], Stable Diffusion XL , etc., the default use of SD-XL-Base-CF. users can enter a text description or upload a reference image , you can generate high-quality images . The project interface is compatible with OpenAI formats, easy to integrate and simple to deploy for developers, designers and creative enthusiasts. With Cloudflare's global network, the generation speed is fast and stable, and the code is open source and can be freely modified.
Function List
- Support text-to-generate images, input text description to generate high quality images.
- Supports graph generation to generate new images based on uploaded reference images.
- Five optional models are provided: DS-8-CF, SD-XL-Bash-CF, SD-XL-Lightning-CF, FLUX.1-Schnell-CF, SF-Kolors.
- REST API compatible with OpenAI format for easy integration into third-party applications.
- Customizable generation parameters such as image style, number of steps and resolution.
- Deployed on Cloudflare Workers, no management server is required.
- Supports KV storage for caching generated images.
- Open source code, user modifiable and extensible.
Using Help
Installation process
To use CloudFlare-AI-Image, you need to complete the following deployment steps. The process is simple, but requires careful configuration of your environment.
- Sign up for a Cloudflare account
Visit the official Cloudflare website to register or sign in for an account. The free plan is sufficient to support this program. Make sure the account has Workers AI and KV namespace permissions. - Cloning Project Code
Run the following command in the terminal to clone the project locally:git clone https://github.com/justlovemaki/CloudFlare-AI-Image.git
Go to the project catalog:
cd CloudFlare-AI-Image
- Installing Wrangler
Wrangler is a tool for deploying Cloudflare Workers. Installation command:npm install -g @cloudflare/wrangler
Log in to Wrangler:
wrangler login
Follow the prompts to authorize in your browser.
- Configuring Environment Variables
The project needs to bind the Workers AI and KV namespaces and configure account information.- Log in to the Cloudflare dashboard and find the Account ID.
- Create API tokens with permissions to include Workers AI, Workers script editing, and KV storage.
- compiler
wrangler.toml
file with the account ID and KV namespace:account_id = "你的账户ID" name = "cloudflare-ai-image" compatibility_date = "2023-09-01" [[kv_namespaces]] binding = "IMAGE_KV" id = "你的KV命名空间ID"
- Create a KV namespace:
wrangler kv:namespace create IMAGE_KV
Copy the returned ID and fill in the
wrangler.toml
The - Set the API token:
wrangler secret put CLOUDFLARE_API_TOKEN
Enter your API token.
- (Optional) Set SF_TOKEN if modeling with a silicon-based flow stage:
wrangler secret put SF_TOKEN
Enter the API token requested from the Silicon Mobility Platform.
- Deployment projects
Run in the project directory:wrangler publish
After a successful deployment, get the Workers URL, for example
https://cloudflare-ai-image.your-account.workers.dev
The - local test
If local debugging is required, run:wrangler dev
This will start the local server and access the
http://localhost:8787
Test function.
Usage
Once deployed, you can use Image Generation via the API, the client, or the Workers URL. The following are detailed instructions for doing so.
Select Model
The project supports five models, FLUX.1-Schnell-CF is recommended for best results, but there is a daily usage limit. Other models are listed below:
- DS-8-CF: Based on Dreamshaper-8-LCM, it is suitable for quickly generating art-style images.
- SD-XL-Bash-CF: Default model, based on Stable Diffusion XL, balancing speed and quality.
- SD-XL-Lightning-CF: Optimize speed for simple scenarios.
- FLUX.1-Schnell-CF: High-quality model with rich details, recommended for complex scenes.
- SF-Kolors: Based on Kwai-Kolors, suitable for Asian style images, SF_TOKEN needs to be configured.
When selecting a model, specify the model name in the API request or configuration file, for example "model": "@cf/black-forest-labs/flux-1-schnell"
The
Vincennes diagram
Venn diagrams generate images from textual descriptions.
- via API::
Call the API endpoint using a POST request:curl -X POST https://your-project.workers.dev/api/generate \ -H "Content-Type: application/json" \ -d '{"prompt": "一片星空下的雪山,月光照耀", "model": "@cf/black-forest-labs/flux-1-schnell", "steps": 8}'
Returns JSON containing the image URL or Base64 data.
- via the client::
The project interface is compatible with the OpenAI format and can be used with an OpenAI-enabled client (such as the ChatGPT client). Configure the client's API address to be your Workers URL and enter the prompt to generate the image. - via Workers URL::
interviewshttps://your-project.workers.dev
, enter the prompt word, click Generate, and the browser displays the image.
Optimize Cue Words::
Clear cue words enhance image quality. Example:
- Fuzzy:
"一朵花"
- Optimization:
"一朵盛开的红玫瑰,背景是绿色草地,阳光明媚"
English cues may work better, such as"A red rose in bloom on a green meadow under bright sunlight"
The
Tucson (city)
The graph generated image generates a new image based on the reference image.
- via API::
Upload an image and provide a cue word:curl -X POST https://your-project.workers.dev/api/generate \ -H "Content-Type: application/json" \ -d '{"prompt": "将这张图片变成卡通风格", "image": "base64编码的图像数据", "model": "@cf/stabilityai/stable-diffusion-xl-base-1.0"}'
- via Workers URL::
Visit the page, upload a reference image, enter a description (e.g."卡通风格"
) to generate a new image.
Customized parameters
Adjustment of the following parameters is supported:
- Steps: Default 8, range 1-50; higher step counts result in more detail, but increase in time consumption.
- resolution (of a photo): Default 512x512, adjustable to 1024x1024 (depending on model support).
- hairstyle: Controlled by cue words such as
"油画风格"
,"赛博朋克"
The
Add parameters to the API request:
{
"prompt": " futuristic city at night",
"steps": 12,
"width": 1024,
"height": 1024
}
Forced Translation Options
- --ntl: Disable translation and retain original prompt words for multilingual users.
- --tl: Force translation of prompt words into English to improve the quality of generation.
In the API request, add the"translate": "tl"
maybe"translate": "ntl"
The
View Log
Log in to the Cloudflare dashboard and go to the Workers overview to see request logs, generation logs, and resource usage.The KV namespace caches images to reduce duplicate generation.
caveat
- free quota: The Cloudflare Workers free plan of 100,000 requests per day is sufficient to support personal use. there may be additional limits on model calls for Workers AI (e.g., daily limits for FLUX.1).
- Model Performance: FLUX.1-Schnell-CF works best but may be limited at high loads.SD-XL-Bash-CF is suitable for everyday use.
- safety: It is recommended to set the API key to prevent unauthorized access.
- Cue word restrictions: To avoid sensitive or inappropriate content, models are automatically filtered.
application scenario
- content creation
Bloggers need to illustrate their posts by entering"一片热带雨林,阳光穿透树叶"
Generate high-quality images in seconds to save on design costs. - application development
The developer generates a product display map for the e-commerce platform, which is dynamically invoked through the API, with input descriptions such as"现代风格的沙发,白色背景"
, enhance the user experience. - Art Exploration
The student or artist uploads a sketch, uses the diagram to generate a diagram, and enters the"赛博朋克风格"
, generating unique work for study or presentation. - Education and training
Teachers generate instructional materials such as input"古代中国的城市街道"
, used in history courses to visualize scenes.
QA
- How long does it take to generate an image?
2-15 seconds, depending on the model, the number of steps and the network.FLUX.1-Schnell-CF is the fastest, SD-XL-Bash-CF is a little slower. - What image types are supported?
Supports landscape, people, objects, art styles, etc. Complex scenes need to optimize the cue words, and sensitive content will be filtered. - Do I have to pay for deployment?
Not required. The project is open source and the Cloudflare free plan is sufficient.SF-Kolors models require a free SF_TOKEN application. - How do I switch models?
Specify the model name in the API request or modify the default model in the configuration file. It is recommended to test FLUX.1-Schnell-CF. - How does the Tupelo feature work?
Upload a reference image and enter a description (e.g."卡通风格"
), generating new images through the API or a page.