Skip to content

Commit

Permalink
Merge for Fooocus v2.1.806
Browse files Browse the repository at this point in the history
Change stop api to POST
Preload pipeline when start web app
  • Loading branch information
konieshadow committed Nov 14, 2023
1 parent c263ac4 commit 961c22c
Show file tree
Hide file tree
Showing 13 changed files with 374 additions and 116 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

FastAPI powered API for [Fooocus](https://github.com/lllyasviel/Fooocus)

Currently loaded Fooocus version: 2.1.781
Currently loaded Fooocus version: 2.1.806

### Run with Replicate
Now you can use Fooocus-API by Replicate, the model is in [konieshadow/fooocus-api](https://replicate.com/konieshadow/fooocus-api).
Expand All @@ -16,7 +16,7 @@ With preset:
I believe this is the easiest way to generate image with Fooocus's power.

### Reuse model files from Fooocus
You can simple copy `user_path_config.txt` file from your local Fooocus folder to Fooocus-API's root folder. See [Customization](https://github.com/lllyasviel/Fooocus#customization) for details.
You can simple copy `config.txt` file from your local Fooocus folder to Fooocus-API's root folder. See [Customization](https://github.com/lllyasviel/Fooocus#customization) for details.

### Start app
Need python version >= 3.10, or use conda to create a new env.
Expand Down Expand Up @@ -110,6 +110,11 @@ Query async generation request results, return job progress and generation resul
Query job queue info, include running job count, finished job count and last job id.

#### Stop Generation task
> POST /v1/generation/stop
Stop current generation task.

#### Get All Model Names
> GET /v1/engines/all-models
Expand Down
12 changes: 12 additions & 0 deletions config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"path_checkpoints": "/home/featurize/work/Fooocus/models/checkpoints",
"path_loras": "/home/featurize/work/Fooocus/models/loras",
"path_embeddings": "/home/featurize/work/Fooocus/models/embeddings",
"path_vae_approx": "/home/featurize/work/Fooocus/models/vae_approx",
"path_upscale_models": "/home/featurize/work/Fooocus/models/upscale_models",
"path_inpaint": "/home/featurize/work/Fooocus/models/inpaint",
"path_controlnet": "/home/featurize/work/Fooocus/models/controlnet",
"path_clip_vision": "/home/featurize/work/Fooocus/models/clip_vision",
"path_fooocus_expansion": "/home/featurize/work/Fooocus/models/prompt_expansion/fooocus_expansion",
"path_outputs": "/home/featurize/work/Fooocus/outputs"
}
72 changes: 72 additions & 0 deletions config_modification_tutorial.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
You can modify your "/home/featurize/work/Fooocus-API/config.txt" using the below keys, formats, and examples.
Do not modify this file. Modifications in this file will not take effect.
This file is a tutorial and example. Please edit "/home/featurize/work/Fooocus-API/config.txt" to really change any settings.
Remember to split the paths with "\\" rather than "\".


{
"path_checkpoints": "/home/featurize/work/Fooocus/models/checkpoints",
"path_loras": "/home/featurize/work/Fooocus/models/loras",
"path_embeddings": "/home/featurize/work/Fooocus/models/embeddings",
"path_vae_approx": "/home/featurize/work/Fooocus/models/vae_approx",
"path_upscale_models": "/home/featurize/work/Fooocus/models/upscale_models",
"path_inpaint": "/home/featurize/work/Fooocus/models/inpaint",
"path_controlnet": "/home/featurize/work/Fooocus/models/controlnet",
"path_clip_vision": "/home/featurize/work/Fooocus/models/clip_vision",
"path_fooocus_expansion": "/home/featurize/work/Fooocus/models/prompt_expansion/fooocus_expansion",
"path_outputs": "/home/featurize/work/Fooocus/outputs",
"default_model": "juggernautXL_version6Rundiffusion.safetensors",
"default_refiner": "None",
"default_refiner_switch": 0.5,
"default_lora": "sd_xl_offset_example-lora_1.0.safetensors",
"default_lora_weight": 0.1,
"default_cfg_scale": 4.0,
"default_sample_sharpness": 2,
"default_sampler": "dpmpp_2m_sde_gpu",
"default_scheduler": "karras",
"default_styles": [
"Fooocus V2",
"Fooocus Enhance",
"Fooocus Sharp"
],
"default_prompt_negative": "",
"default_prompt": "",
"default_advanced_checkbox": false,
"default_image_number": 2,
"checkpoint_downloads": {
"juggernautXL_version6Rundiffusion.safetensors": "https://huggingface.co/lllyasviel/fav_models/resolve/main/fav/juggernautXL_version6Rundiffusion.safetensors"
},
"lora_downloads": {
"sd_xl_offset_example-lora_1.0.safetensors": "https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/resolve/main/sd_xl_offset_example-lora_1.0.safetensors"
},
"embeddings_downloads": {},
"available_aspect_ratios": [
"704*1408",
"704*1344",
"768*1344",
"768*1280",
"832*1216",
"832*1152",
"896*1152",
"896*1088",
"960*1088",
"960*1024",
"1024*1024",
"1024*960",
"1088*960",
"1088*896",
"1152*896",
"1152*832",
"1216*832",
"1280*768",
"1344*768",
"1344*704",
"1408*704",
"1472*704",
"1536*640",
"1600*640",
"1664*576",
"1728*576"
],
"default_aspect_ratio": "1152*896"
}
2 changes: 1 addition & 1 deletion fooocus_api_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = '0.3.11'
version = '0.3.12'
20 changes: 11 additions & 9 deletions fooocusapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,28 +160,30 @@ def job_queue():
return JobQueueInfo(running_size=len(task_queue.queue), finished_size=len(task_queue.history), last_job_id=task_queue.last_seq)


@app.post("/v1/generation/stop", response_model=StopResponse, description="Job stoping")
def stop():
stop_worker()
return StopResponse(msg="success")


@app.get("/v1/engines/all-models", response_model=AllModelNamesResponse, description="Get all filenames of base model and lora")
def all_models():
import modules.path as path
return AllModelNamesResponse(model_filenames=path.model_filenames, lora_filenames=path.lora_filenames)
import modules.config as config
return AllModelNamesResponse(model_filenames=config.model_filenames, lora_filenames=config.lora_filenames)


@app.post("/v1/engines/refresh-models", response_model=AllModelNamesResponse, description="Refresh local files and get all filenames of base model and lora")
def refresh_models():
import modules.path as path
path.update_all_model_names()
return AllModelNamesResponse(model_filenames=path.model_filenames, lora_filenames=path.lora_filenames)
import modules.config as config
config.update_all_model_names()
return AllModelNamesResponse(model_filenames=config.model_filenames, lora_filenames=config.lora_filenames)


@app.get("/v1/engines/styles", response_model=List[str], description="Get all legal Fooocus styles")
def all_styles():
from modules.sdxl_styles import legal_style_names
return legal_style_names

@app.get("/v1/generation/stop", response_model=StopResponse, description="Job stoping")
def stop():
stop_worker()
return StopResponse(msg="success")

app.mount("/files", StaticFiles(directory=file_utils.output_dir), name="files")

Expand Down
26 changes: 17 additions & 9 deletions fooocusapi/api_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
from PIL import Image
from fooocusapi.file_utils import get_file_serve_url, output_file_to_base64img, output_file_to_bytesimg
from fooocusapi.models import AsyncJobResponse, AsyncJobStage, GeneratedImageResult, GenerationFinishReason, ImgInpaintOrOutpaintRequest, ImgPromptRequest, ImgUpscaleOrVaryRequest, Text2ImgRequest
from fooocusapi.parameters import ImageGenerationParams, ImageGenerationResult, available_aspect_ratios, default_aspect_ratio, inpaint_model_version, default_sampler, default_scheduler, default_base_model_name, default_refiner_model_name
from fooocusapi.parameters import ImageGenerationParams, ImageGenerationResult, available_aspect_ratios, default_aspect_ratio, default_inpaint_engine_version, default_sampler, default_scheduler, default_base_model_name, default_refiner_model_name
from fooocusapi.task_queue import QueueTask
import modules.flags as flags
import modules.config as path
import modules.config as config
from modules.sdxl_styles import legal_style_names


Expand Down Expand Up @@ -47,17 +47,17 @@ def read_input_image(input_image: UploadFile) -> np.ndarray:

def req_to_params(req: Text2ImgRequest) -> ImageGenerationParams:
if req.base_model_name is not None:
if req.base_model_name not in path.model_filenames:
if req.base_model_name not in config.model_filenames:
print(f"[Warning] Wrong base_model_name input: {req.base_model_name}, using default")
req.base_model_name = default_base_model_name

if req.refiner_model_name is not None and req.refiner_model_name != 'None':
if req.refiner_model_name not in path.model_filenames:
if req.refiner_model_name not in config.model_filenames:
print(f"[Warning] Wrong refiner_model_name input: {req.refiner_model_name}, using default")
req.refiner_model_name = default_refiner_model_name

for l in req.loras:
if l.model_name != 'None' and l.model_name not in path.lora_filenames:
if l.model_name != 'None' and l.model_name not in config.lora_filenames:
print(f"[Warning] Wrong lora model_name input: {l.model_name}, using 'None'")
l.model_name = 'None'

Expand Down Expand Up @@ -125,15 +125,15 @@ def req_to_params(req: Text2ImgRequest) -> ImageGenerationParams:
print(f"[Warning] Wrong scheduler_name input: {adp.scheduler_name}, using default")
adp.scheduler_name = default_scheduler

if adp.inpaint_engine not in ['v1', 'v2.5']:
if adp.inpaint_engine not in flags.inpaint_engine_versions:
print(f"[Warning] Wrong inpaint_engine input: {adp.inpaint_engine}, using default")
adp.inpaint_engine = inpaint_model_version
adp.inpaint_engine = flags.default_inpaint_engine_version

advanced_params = [adp.adm_scaler_positive, adp.adm_scaler_negative, adp.adm_scaler_end, adp.adaptive_cfg, adp.sampler_name,
adp.scheduler_name, False, adp.overwrite_step, adp.overwrite_switch, adp.overwrite_width, adp.overwrite_height,
adp.overwrite_vary_strength, adp.overwrite_upscale_strength,
adp.mixing_image_prompt_and_vary_upscale, adp.mixing_image_prompt_and_inpaint,
adp.debugging_cn_preprocessor, adp.controlnet_softness, adp.canny_low_threshold, adp.canny_high_threshold, adp.inpaint_engine,
adp.debugging_cn_preprocessor, adp.skipping_cn_preprocessor, adp.controlnet_softness, adp.canny_low_threshold, adp.canny_high_threshold, adp.inpaint_engine,
adp.refiner_swap_method, adp.freeu_enabled, adp.freeu_b1, adp.freeu_b2, adp.freeu_s1, adp.freeu_s2]

return ImageGenerationParams(prompt=prompt,
Expand Down Expand Up @@ -184,8 +184,16 @@ def generation_output(results: QueueTask | List[ImageGenerationResult], streamin
job_result=job_result)

if streaming_output:
if len(results) == 0 or results[0].finish_reason != GenerationFinishReason.success:
if len(results) == 0:
return Response(status_code=500)
result = results[0]
if result.finish_reason == GenerationFinishReason.queue_is_full:
return Response(status_code=409, content=result.finish_reason.value)
elif result.finish_reason == GenerationFinishReason.user_cancel:
return Response(status_code=400, content=result.finish_reason.value)
elif result.finish_reason == GenerationFinishReason.error:
return Response(status_code=500, content=result.finish_reason.value)

bytes = output_file_to_bytesimg(results[0].im)
return Response(bytes, media_type='image/png')
else:
Expand Down
9 changes: 6 additions & 3 deletions fooocusapi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Lora(BaseModel):
class PerfomanceSelection(str, Enum):
speed = 'Speed'
quality = 'Quality'
extreme_speed = 'Extreme Speed'


class UpscaleOrVaryMethod(str, Enum):
Expand All @@ -42,9 +43,10 @@ class OutpaintExpansion(str, Enum):


class ControlNetType(str, Enum):
cn_ip = 'Image Prompt'
cn_canny = 'PyraCanny'
cn_cpds = 'CPDS'
cn_ip = "ImagePrompt"
cn_ip_face = "FaceSwap"
cn_canny = "PyraCanny"
cn_cpds = "CPDS"


class ImagePrompt(BaseModel):
Expand Down Expand Up @@ -72,6 +74,7 @@ class AdvancedParams(BaseModel):
mixing_image_prompt_and_vary_upscale: bool = Field(False, description="Mixing Image Prompt and Vary/Upscale")
mixing_image_prompt_and_inpaint: bool = Field(False, description="Mixing Image Prompt and Inpaint")
debugging_cn_preprocessor: bool = Field(False, description="Debug Preprocessors")
skipping_cn_preprocessor: bool = Field(False, description="Skip Preprocessors")
controlnet_softness: float = Field(0.25, description="Softness of ControlNet", ge=0.0, le=1.0)
canny_low_threshold: int = Field(64, description="Canny Low Threshold", ge=1, le=255)
canny_high_threshold: int = Field(128, description="Canny High Threshold", ge=1, le=255)
Expand Down
9 changes: 5 additions & 4 deletions fooocusapi/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from fooocusapi.task_queue import TaskType


inpaint_model_version = 'v1'
default_inpaint_engine_version = 'v2.6'


defualt_styles = ['Fooocus V2', 'Fooocus Enhance', 'Fooocus Sharp']
Expand Down Expand Up @@ -78,7 +78,7 @@ class ImageGenerationParams(object):
def __init__(self, prompt: str,
negative_prompt: str,
style_selections: List[str],
performance_selection: List[str],
performance_selection: str,
aspect_ratios_selection: str,
image_number: int,
image_seed: int | None,
Expand Down Expand Up @@ -129,18 +129,19 @@ def __init__(self, prompt: str,
mixing_image_prompt_and_vary_upscale = False
mixing_image_prompt_and_inpaint = False
debugging_cn_preprocessor = False
skipping_cn_preprocessor = False
controlnet_softness = 0.25
canny_low_threshold = 64
canny_high_threshold = 128
inpaint_engine = inpaint_model_version
inpaint_engine = default_inpaint_engine_version
refiner_swap_method = 'joint'
freeu_enabled = False
freeu_b1, freeu_b2, freeu_s1, freeu_s2 = [None] * 4
self.advanced_params = [adm_scaler_positive, adm_scaler_negative, adm_scaler_end, adaptive_cfg, sampler_name,
scheduler_name, generate_image_grid, overwrite_step, overwrite_switch, overwrite_width, overwrite_height,
overwrite_vary_strength, overwrite_upscale_strength,
mixing_image_prompt_and_vary_upscale, mixing_image_prompt_and_inpaint,
debugging_cn_preprocessor, controlnet_softness, canny_low_threshold, canny_high_threshold, inpaint_engine,
debugging_cn_preprocessor, skipping_cn_preprocessor, controlnet_softness, canny_low_threshold, canny_high_threshold, inpaint_engine,
refiner_swap_method, freeu_enabled, freeu_b1, freeu_b2, freeu_s1, freeu_s2]
else:
self.advanced_params = advanced_params
4 changes: 2 additions & 2 deletions fooocusapi/repositories_versions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os

fooocus_version = '2.1.781'
fooocus_version = '2.1.806'
fooocus_commit_hash = os.environ.get(
'FOOOCUS_COMMIT_HASH', "a9bb1079cf44e892c8cabaf7d345db8b649ec5ac")
'FOOOCUS_COMMIT_HASH', "c9a5e729d91ba3772d421a5db7bf79497011e59f")
Loading

0 comments on commit 961c22c

Please sign in to comment.