FastAPI is a modern, fast (high-performance) web framework for building APIs. This project builds the Rest interface of Fooocus based on Fastapi.
For a partial description of Fooocus, please refer to the Fooocus documentation, which mainly introduces the interface section.
Compared to the previous API project Fooocus-API, there are major changes:
You can get a guide here if you use the previous API project.
- Remove the task queue and no longer maintain a separate queue
- Full use of Fooocus' generated code
- Can start at the same time as WebUI
- Removed the form submission interface, leaving only the JSON submission interface
- Main functions merged into one interface
- Simplified parameter structure, consistent with Fooocus' WebUI
- Added streaming output function
- preset support
- More complete task history function
advantage:
- Reduce development load
- More complete Fooocus support
- Easier and faster tracking Fooocus version
- Full Fooocus support
- You can start both the API and WebUI at the same time, or choose not to start WebUI
- Use X-API-KEY for authentication
- all-in-one interface
- use URL provide INPUT image
- streaming output, binary image, asynchronous task and synchronous task support
- persistent task history
- enhanced task history management
- task query function
- Custom upscale rate, Limit by Fooocus, max is 2800px
- preset support
- WebHook support
Based on Fooocus, there are several dependencies, so you can install it in the same way as Fooocus.
Same as Fooocus, use --apikey
to specify the API authentication key.
Default API port is WebUI port plus 1, that is 7866, use --port
to modify WebUI port
Environment variable API_PORT is used to specify the API port. It takes precedence over the default setting.
example for WebUI and API:
python launch.py --listen 0.0.0.0 --port 7865
Only API example:
python launch.py --listen 0.0.0.0 --port 7865 --nowebui
- Use API key for authentication, the key is passed in the request header
X-API-KEY
.
Use --apikey
to specify the API authentication key on startup.
POST /v1/engine/generate/
- Abstract: Generate API route
- RequestBody: Required, JSON format, based on
CommonRequest
model. - Response:
- 200: Success response, return the generation result.
Explain:
Request parameter model CommonRequest
contains all parameters of WebUI, but some parameters you need to pay attention to when using them, including the following categories:
Invalid parameters in the API, this part of the parameter contains:
input_image_checkbox
, This parameter always set to Trueinpaint_mask_upload_checkbox
, This parameter always set to Trueinvert_mask_checkbox
, This parameter always set to Falsecurrent_tab
, This parameter checks the image information in the parameter and is automatically set. The check order is 'ip' -> 'uov' -> 'inpaint'
Parameters that are not recommended:
generate_image_grid
, It is not clear what this parameter does. It is recommended to leave it false.
The following parameters need to be set according to the usage scenario:
mixing_image_prompt_and_vary_upscale
mixing_image_prompt_and_inpaint
In addition, some API-specific parameters are also included:
preset
, You can use this parameter to specify a preset that takes precedence over the global default and below the passed parameter, but if the passed parameter is equal to the default value, the preset parameter is usedstream_output
, true for streaming output, default falserequire_base64
, not usedasync_process
, async task, default false, a synchronous task will be returned whenstream_output
is false at the same timewebhook_url
, Webhook addr, if set, the task will be sent to the address after the task is completed.
stream_output
has a higher priority thanasync_process
, that is, when both aretrue
, return streaming output. When all are false, task will be synchronously returned. when you setAccept: image/xxx
in the request header, the response will be a binary image
POST /v1/engine/control/
- Label: GenerateV1
- Abstract: Stop or Skip task
- Describe: stop or skip task, only valid for the current task, stop will stop the current task and continue to the next task. Skip will skip the current generation and continue the task.
- Params:
action
(string): action type, can be "stop" or "skip".
- Response:
{"message": "{message}"}
GET /tasks
- Label: Query
- Abstract: get all tasks
- Describe: filter tasks and support paging and time filtering.
- Params:
query
(string, default: "all"): task type, one of "all", "history", "current".page
(integer, default: 0): page number, used for history and pending tasks.page_size
(integer, default: 10): page size of each pagestart_at
(string): filter tasks start time, only valid for history. format by ISO8601 example: "2024-06-30T17:57:07.045812"end_at
(string): default to current time format by ISO8601, example: "2024-06-30T17:57:07.045812", filter tasks end time, only valid for history.action
(string): used for delete operation, only valid for history, will delete database record and generated images.
- Response:
- 200: {"history": List[RecordResponse], "current": List[RecordResponse], "pending": List[RecordResponse]}
Although all models are based on
RecordResponse
, thecurrent
one will have a preview field
GET /tasks/{task_id}
- Label: Query
- Abstract: get task by id
- Params:
task_id
(string): task id
- Response:
- 200: RecordResponse
GET /v1/engines/all-models
- Label: Query
- Abstract: get all models
- Response:
- 200: return all local checkpoint and lora models.
GET /v1/engines/styles
- Label: Query
- Abstract: get all styles
- Response:
- 200: return a list of styles.
GET /outputs/{date}/{file_name}
- Label: Query
- Abstract: used for get output image
- params:
date
(string): date, the generated image is created in the day folder for classification, the part is the generation date.file_name
(string): file name
- Response:
- 200: success response, return output content.
- 422: validation error.
if you set
Accept: image/xxx
in the request header, server will convert the output to the specified format and return it.image/png
image/jpeg
image/webp
image/jpg
are supported.
POST /v1/tools/describe-image
- Label: GenerateV1
- Abstract: get tags from image
- Describe: get tags from image, Photo or Anime
- params:
image_type
(string): default: "Photo", image type
- RequestBody: required
multipart/form-data
format, include image file. - Response:
- 200: success response, return
DescribeImageResponse
model. - 422: validation error.
- 200: success response, return
GET /
- Label: Query
- Abstract: root endpoint
- Response: Redirect to
/docs
- Attributes:
prompt
(string): prompt for generation image.negative_prompt
(string): negative prompt for filtering unwanted content.style_selections
(array): style selections.performance_selection
(Performance): performance, defaultSpeed
, one ofQuality
Speed
Extreme Speed
Lightning
Hyper-SD
aspect_ratios_selection
(string): aspect radios selection, default 1152*896image_number
(int): number of images to generate, default 1, range 1-32output_format
(string): out image format, defaultpng
, one ofjpg
webp
png
image_seed
(int): seed, -1 for randomread_wildcards_in_order
(bool): read wildcards in order, default falsesharpness
(float): sharpness, default 2.0, range 0.0-30.0guidance_scale
(float): guidance scale, default 4, range 1.0-30.0base_model_name
(string): base model name, defaultjuggernautXL_v8Rundiffusion.safetensors
for nowrefiner_model_name
(string): refiner model name, default Nonerefiner_switch
(float): refiner switch, default 0.5, range 0.1-1.0loras
(Lora): lora list to use, defaultsd_xl_offset_example-lora_1.0.safetensors
, format: Lorainput_image_checkbox
(bool): this will always to be truecurrent_tab
(string): current tab, defaultuov
one ofuov
inpaint
outpaint
, you don't need to pass this parameter.uov_method
(string): uov method, defaultdisable
, all choice UpscaleOrVaryMethoduov_input_image
(string): URL or Base64 image for Upscale or vary, default "None"outpaint_selections
(array): Outpaint selection, example ["Left", "Right", "Top", "Bottom"]inpaint_input_image
(string): URL or Base64 image for inpaintinpaint_additional_prompt
(string): Inpaint additional prompt, default "None"inpaint_mask_image_upload
(string): URL or Base64 image for inpaint maskinpaint_mask_upload_checkbox
(bool): this will always truedisable_preview
(bool): disable preview, default falsedisable_intermediate_results
(bool): disable intermediate, default falsedisable_seed_increment
(bool): disable seed increment, default falseblack_out_nsfw
(bool): black out nsfw result, default falseadm_scaler_positive
(float): The scaler multiplied to positive ADM (use 1.0 to disable). default 1.5, range 0.0-3.0adm_scaler_negative
(float): The scaler multiplied to negative ADM (use 1.0 to disable). default 1.5, range 0.0-3.0adm_scaler_end
(float): ADM Guidance End At Step, default 0.8, range 0.0-1.0adaptive_cfg
(float): Adaptive cfg, default 7, range 1.0-30.0clip_skip
(float): clip skip, default 2, range 1-12sampler_name
(string): sampler name, default dpmpp_2m_sde_gpuscheduler_name
(string): scheduler name, default karrasvae_name
(string): VAE name, default Default (model)overwrite_step
(int): overwrite steps in Performance, default -1overwrite_switch
(float): overwrite refiner_switch, default -1overwrite_width
(int): overwrite width in aspect_ratios_selection, default -1, range -1-2048overwrite_height
(int): overwrite height in aspect_ratios_selection, default -1, range -1-2048overwrite_vary_strength
(float): overwrite vary_strength, default -1, range 0.0-1.0overwrite_upscale_strength
(float): overwrite upscale_strength, default -1, range 0.0-1.0mixing_image_prompt_and_vary_upscale
(bool): mixing image prompt and vary_upscale, default falsemixing_image_prompt_and_inpaint
(bool): mixing image prompt and inpaint, default falsedebugging_cn_preprocessor
(bool): debugging cn preprocessor, default falseskipping_cn_preprocessor
(bool): skipping cn preprocessor, default falsecanny_low_threshold
(int): default 64, range 1-255canny_high_threshold
(int): default 128, range 1-255refiner_swap_method
(string): default jointcontrolnet_softness
(float): default 0.25, range 0.0-1.0freeu_enabled
(bool): enable freeu, default falsefreeu_b1
(float): default 1.01freeu_b2
(float): default 1.02freeu_s1
(float): default 0.99freeu_s2
(float): default 0.95debugging_inpaint_preprocessor
(bool): debugging inpaint preprocessor, default falseinpaint_disable_initial_latent
(bool): default falseinpaint_engine
(string): default v2.6inpaint_strength
(float): default 1.0, range 0.0-1.0inpaint_respective_field
(float): default0.618, range 0.0-1.0invert_mask_checkbox
(bool): default false, this always falseinpaint_erode_or_dilate
(int): default 0, range -64-64save_metadata_to_images
(bool): save metadata to images, default truemetadata_scheme
(string): default foocus, one of fooocus, a111controlnet_image
(ImagePrompt): ImagePromptgenerate_image_grid
(bool): default false, suggested to falseoutpaint_distance
(List[int]): outpaint distance, default [0, 0, 0, 0], left, top, right, bottom, this params must with outpaint_selections at the same timeupscale_multiple
(float): default 1.0, range 1.0-5.0, work only upscale method isUpscale (Custom)
preset
(string): preset, default initialstream_output
(bool): stream output, default falserequire_base64
(bool): not usedasync_process
(bool): async process, default falsewebhook_url
(string): Webhook URL, default ""
- Attributes:
enabled
(bool): enable Lora, default falsemodel_name
(string): Lora file name, default Noneweight
(float): Lora weight, default 0.5, range -2-2
- Attributes:
- "Disabled"
- "Vary (Subtle)"
- "Vary (Strong)"
- "Upscale (1.5x)"
- "Upscale (2x)"
- "Upscale (Fast 2x)"
- "Upscale (Custom)"
- Attributes:
cn_img
(str): ImageUrl or base64 imagecn_stop
(float): default 0.6, range 0-1cn_weight
(float): default 0.5, range 0-2cn_type
(string): default ImagePrompt, one of ImagePrompt FaceSwap, PyraCanny, CPDS
- Attributes:
describe
(string): Image prompt.
- Attributes::
id
(int): id in sqlite, no use for user.task_id
(str): task ID, generate byuuid.uuid4().hex
.req_params
(CommonRequest): required parameters, convert to url for input imagein_queue_mills
(int): in queue time in millis.start_mills
(int): start task time in millis.finish_mills
(int): finish task time in millis.task_status
(str): task status.progress
(float): task progress.preview
(str): previewwebhook_url
(str): Webhook URL.result
(List): result for generate
This project is based on Fooocus-API, thanks all the developers who participated in this project.