Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/konieshadow/Fooocus-API int…
Browse files Browse the repository at this point in the history
…o main
  • Loading branch information
konieshadow committed Oct 16, 2023
2 parents f995430 + 1b3e3d0 commit 623537e
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 62 deletions.
2 changes: 1 addition & 1 deletion 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.44
Currently loaded Fooocus version: 2.1.679

### 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 Down
3 changes: 3 additions & 0 deletions fooocusapi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class PerfomanceSelection(str, Enum):
class FooocusStyle(str, Enum):
fooocus_expansion = 'Fooocus V2'
default = 'Default (Slightly Cinematic)'
fooocus_anime = 'Fooocus Anime'
fooocus_realistic = 'Fooocus Realistic'
fooocus_strong_negative = 'Fooocus Strong Negative'
sai_3d_model = 'SAI 3D Model'
sai_analog_film = 'SAI Analog Film'
sai_anime = 'SAI Anime'
Expand Down
3 changes: 3 additions & 0 deletions fooocusapi/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
fooocus_styles = [
'Fooocus V2',
'Default (Slightly Cinematic)',
'Fooocus Anime',
'Fooocus Realistic',
'Fooocus Strong Negative',
'SAI 3D Model',
'SAI Analog Film',
'SAI Anime',
Expand Down
6 changes: 2 additions & 4 deletions fooocusapi/repositories_versions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import os

fooocus_version = '2.1.44'
comfy_commit_hash = os.environ.get(
'COMFY_COMMIT_HASH', "d1a0abd40b86f3f079b0cc71e49f9f4604831457")
fooocus_version = '2.1.679'
fooocus_commit_hash = os.environ.get(
'FOOOCUS_COMMIT_HASH', "5e8f2f96e91817cc6691642e71edec8b8264b114")
'FOOOCUS_COMMIT_HASH', "589409e3f4832014b931884eac6963997de01739")
17 changes: 7 additions & 10 deletions fooocusapi/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def process_generate(params: ImageGenerationParams) -> List[ImageGenerationResul
import modules.core as core
import modules.inpaint_worker as inpaint_worker
import modules.path as path
import comfy.model_management as model_management
import fcbh.model_management as model_management
import modules.advanced_parameters as advanced_parameters
import fooocus_extras.preprocessors as preprocessors
import fooocus_extras.ip_adapter as ip_adapter
Expand Down Expand Up @@ -119,14 +119,15 @@ def build_advanced_parameters():
canny_low_threshold = 64
canny_high_threshold = 128
inpaint_engine = inpaint_model_version
refiner_swap_method = 'joint'
freeu_enabled = False
freeu_b1, freeu_b2, freeu_s1, freeu_s2 = [None] * 4
return [adm_scaler_positive, adm_scaler_negative, adm_scaler_end, adaptive_cfg, sampler_name,
scheduler_name, 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,
freeu_enabled, freeu_b1, freeu_b2, freeu_s1, freeu_s2]
refiner_swap_method, freeu_enabled, freeu_b1, freeu_b2, freeu_s1, freeu_s2]

advanced_parameters.set_all_advanced_parameters(*build_advanced_parameters())

Expand Down Expand Up @@ -165,6 +166,7 @@ def build_advanced_parameters():
inpaint_worker.current_task = None
width, height = aspect_ratios[aspect_ratios_selection]
skip_prompt_processing = False
refiner_swap_method = advanced_parameters.refiner_swap_method

raw_prompt = prompt
raw_negative_prompt = negative_prompt
Expand Down Expand Up @@ -228,7 +230,7 @@ def build_advanced_parameters():
loras += [(inpaint_patch_model_path, 1.0)]
print(f'[Inpaint] Current inpaint model is {inpaint_patch_model_path}')
goals.append('inpaint')
sampler_name = 'dpmpp_fooocus_2m_sde_inpaint_seamless'
sampler_name = 'dpmpp_2m_sde_gpu' # only support the patched dpmpp_2m_sde_gpu
if current_tab == 'ip' or \
advanced_parameters.mixing_image_prompt_and_inpaint or \
advanced_parameters.mixing_image_prompt_and_vary_upscale:
Expand Down Expand Up @@ -530,12 +532,6 @@ def build_advanced_parameters():
print(f'Preparation time: {preparation_time:.2f} seconds')

outputs.append(['preview', (13, 'Moving model to GPU ...', None)])
execution_start_time = time.perf_counter()
model_management.load_models_gpu([pipeline.final_unet])
moving_time = time.perf_counter() - execution_start_time
print(f'Moving model to GPU: {moving_time:.2f} seconds')

outputs.append(['preview', (13, 'Starting tasks ...', None)])

def callback(step, x0, x, total_steps, y):
done_steps = current_task_id * steps + step
Expand Down Expand Up @@ -574,7 +570,8 @@ def callback(step, x0, x, total_steps, y):
latent=initial_latent,
denoise=denoising_strength,
tiled=tiled,
cfg_scale=cfg_scale
cfg_scale=cfg_scale,
refiner_swap_method=advanced_parameters.refiner_swap_method
)

del task['c'], task['uc'], positive_cond, negative_cond # Save memory
Expand Down
73 changes: 26 additions & 47 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@
from importlib.util import find_spec

from fooocus_api_version import version
from fooocusapi.repositories_versions import (comfy_commit_hash,
fooocus_commit_hash,
fooocus_version)
from fooocusapi.repositories_versions import fooocus_commit_hash

os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"

python = sys.executable
default_command_live = True
index_url = os.environ.get('INDEX_URL', "")

comfyui_name = 'ComfyUI-from-StabilityAI-Official'
fooocus_name = 'Fooocus'

modules_path = os.path.dirname(os.path.realpath(__file__))
Expand Down Expand Up @@ -134,12 +131,6 @@ def download_repositories():
print(f"Using https proxy for git clone: {https_proxy}")
os.environ['https_proxy'] = https_proxy

# Check and download ComfyUI
comfy_repo = os.environ.get(
'COMFY_REPO', "https://github.com/comfyanonymous/ComfyUI")
git_clone(comfy_repo, repo_dir(comfyui_name),
"Inference Engine", comfy_commit_hash)

# Check and download Fooocus
fooocus_repo = os.environ.get(
'FOOOCUS_REPO', 'https://github.com/lllyasviel/Fooocus')
Expand All @@ -157,37 +148,25 @@ def is_installed(package):


def download_models():
model_filenames = [
('sd_xl_base_1.0_0.9vae.safetensors',
'https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/resolve/main/sd_xl_base_1.0_0.9vae.safetensors'),
('sd_xl_refiner_1.0_0.9vae.safetensors',
'https://huggingface.co/stabilityai/stable-diffusion-xl-refiner-1.0/resolve/main/sd_xl_refiner_1.0_0.9vae.safetensors')
]

lora_filenames = [
('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')
]

vae_approx_filenames = [
('xlvaeapp.pth',
'https://huggingface.co/lllyasviel/misc/resolve/main/xlvaeapp.pth')
('xlvaeapp.pth', 'https://huggingface.co/lllyasviel/misc/resolve/main/xlvaeapp.pth'),
('vaeapp_sd15.pth', 'https://huggingface.co/lllyasviel/misc/resolve/main/vaeapp_sd15.pt'),
('xl-to-v1_interposer-v3.1.safetensors',
'https://huggingface.co/lllyasviel/misc/resolve/main/xl-to-v1_interposer-v3.1.safetensors')
]

from modules.model_loader import load_file_from_url
from modules.path import (fooocus_expansion_path, lorafile_path,
modelfile_path, upscale_models_path,
vae_approx_path)

for file_name, url in model_filenames:
load_file_from_url(url=url, model_dir=modelfile_path,
file_name=file_name)
for file_name, url in lora_filenames:
load_file_from_url(url=url, model_dir=lorafile_path,
file_name=file_name)
from modules.path import modelfile_path, lorafile_path, vae_approx_path, fooocus_expansion_path, \
checkpoint_downloads, embeddings_path, embeddings_downloads, lora_downloads

for file_name, url in checkpoint_downloads.items():
load_file_from_url(url=url, model_dir=modelfile_path, file_name=file_name)
for file_name, url in embeddings_downloads.items():
load_file_from_url(url=url, model_dir=embeddings_path, file_name=file_name)
for file_name, url in lora_downloads.items():
load_file_from_url(url=url, model_dir=lorafile_path, file_name=file_name)
for file_name, url in vae_approx_filenames:
load_file_from_url(
url=url, model_dir=vae_approx_path, file_name=file_name)
load_file_from_url(url=url, model_dir=vae_approx_path, file_name=file_name)

load_file_from_url(
url='https://huggingface.co/lllyasviel/misc/resolve/main/fooocus_expansion.bin',
Expand Down Expand Up @@ -228,14 +207,18 @@ def prepare_environments(args) -> bool:
if not skip_sync_repo:
download_repositories()

# Add indent repositories to import path
sys.path.append(os.path.join(script_path, dir_repos, comfyui_name))
sys.path.append(os.path.join(script_path, dir_repos, fooocus_name))

download_models()
# Add dependent repositories to import path
sys.path.append(script_path)
fooocus_path = os.path.join(script_path, dir_repos, fooocus_name)
sys.path.append(fooocus_path)
backend_path = os.path.join(fooocus_path, 'backend', 'headless')
if backend_path not in sys.path:
sys.path.append(backend_path)
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"

sys.argv = [sys.argv[0]]
ini_comfy_args()
download_models()
ini_cbh_args()

if args.preload_pipeline:
print("Preload pipeline")
Expand All @@ -256,9 +239,6 @@ class Args(object):
args.sync_repo = 'skip'
prepare_environments(args)

sys.argv = [sys.argv[0]]
ini_comfy_args()

if disable_private_log:
import fooocusapi.worker as worker
worker.save_log = False
Expand All @@ -275,7 +255,7 @@ class Args(object):


# This function was copied from [Fooocus](https://github.com/lllyasviel/Fooocus) repository.
def ini_comfy_args():
def ini_cbh_args():
from args_manager import args
return args

Expand All @@ -298,7 +278,6 @@ def ini_comfy_args():
args = parser.parse_args()

if prepare_environments(args):
argv = sys.argv
sys.argv = [sys.argv[0]]

# Start api server
Expand Down

0 comments on commit 623537e

Please sign in to comment.