Skip to content

Commit

Permalink
Merge pull request #106 from konieshadow/dev
Browse files Browse the repository at this point in the history
Add CMD flags support of Fooocus
  • Loading branch information
konieshadow authored Dec 22, 2023
2 parents 49639d6 + 5efa725 commit ddd4d40
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 41 deletions.
27 changes: 21 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,28 @@ python main.py
```
On default, server is listening on 'http://127.0.0.1:8888'

Using Fooocus preset, run:
```
python main.py --preset anime
```

For program arguments, see
### CMD Flags
* -h, --help show this help message and exit
* --port PORT Set the listen port, default: 8888
* --host HOST Set the listen host, default: 127.0.0.1
* --base-url BASE_URL Set base url for outside visit, default is http://host:port
* --log-level LOG_LEVEL Log info for Uvicorn, default: info
* --sync-repo SYNC_REPO Sync dependent git repositories to local, 'skip' for skip sync action, 'only' for only do the sync action and not launch app
* --skip-pip Skip automatic pip install when setup
* --preload-pipeline Preload pipeline before start http server
* --queue-size QUEUE_SIZE Working queue size, default: 3, generation requests exceeding working queue size will return failure
* --queue-history QUEUE_HISTORY Finished jobs reserve size, tasks exceeding the limit will be deleted, including output image files, default: 100

Since v0.3.25, added CMD flags support of Fooocus. You can pass any argument which Fooocus supported.

For example, to startup image generation (need more vRAM):
```
python main.py -h
python main.py --all-in-fp16 --always-gpu
```

For Fooocus CMD flags, see [here](https://github.com/lllyasviel/Fooocus?tab=readme-ov-file#all-cmd-flags).

### Start with docker
Before use docker with GPU, you should [install NVIDIA Container Toolkit](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html) first.

Expand Down Expand Up @@ -79,6 +91,9 @@ All the generation api support for response in PNG bytes directly when request's

All the generation api support async process by pass parameter `async_process` to true. And then use query job api to retrieve progress and generation results.

Break changes from v0.3.25
* Removed cli argument `disable-private-log`. You can use Fooocus's `--disable-image-log` for the same purpose.

Break changes from v0.3.24:
* This version merged Fooocus v2.1.839, which include a seed breaking change. Details for [2.1.839](https://github.com/lllyasviel/Fooocus/blob/main/update_log.md#21839).

Expand Down
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.24'
version = '0.3.25'
2 changes: 1 addition & 1 deletion fooocusapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from fooocusapi.models import AllModelNamesResponse, AsyncJobResponse, QueryJobRequest,StopResponse , GeneratedImageResult, ImgInpaintOrOutpaintRequest, ImgPromptRequest, ImgUpscaleOrVaryRequest, JobQueueInfo, Text2ImgRequest
from fooocusapi.api_utils import generation_output, req_to_params
from fooocusapi import file_utils
import fooocusapi.file_utils as file_utils
from fooocusapi.parameters import GenerationFinishReason, ImageGenerationResult
from fooocusapi.task_queue import TaskType
from fooocusapi.worker import process_generate, task_queue, process_top
Expand Down
17 changes: 17 additions & 0 deletions fooocusapi/args.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from fooocusapi.base_args import add_base_args
import ldm_patched.modules.args_parser as args_parser

# Add Fooocus-API args to parser
add_base_args(args_parser.parser, False)

# Apply Fooocus's args
from args_manager import args_parser

# Override the port default value
args_parser.parser.set_defaults(
port=8888
)

# Execute args parse again
args_parser.args = args_parser.parser.parse_args()
args = args_parser.args
15 changes: 15 additions & 0 deletions fooocusapi/base_args.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from argparse import ArgumentParser


def add_base_args(parser: ArgumentParser, before_prepared: bool):
if before_prepared:
parser.add_argument("--port", type=int, default=8888, help="Set the listen port, default: 8888")

parser.add_argument("--host", type=str, default='127.0.0.1', help="Set the listen host, default: 127.0.0.1")
parser.add_argument("--base-url", type=str, default=None, help="Set base url for outside visit, default is http://host:port")
parser.add_argument("--log-level", type=str, default='info', help="Log info for Uvicorn, default: info")
parser.add_argument("--sync-repo", default=None, help="Sync dependent git repositories to local, 'skip' for skip sync action, 'only' for only do the sync action and not launch app")
parser.add_argument("--skip-pip", default=False, action="store_true", help="Skip automatic pip install when setup")
parser.add_argument("--preload-pipeline", default=False, action="store_true", help="Preload pipeline before start http server")
parser.add_argument("--queue-size", type=int, default=3, help="Working queue size, default: 3, generation requests exceeding working queue size will return failure")
parser.add_argument("--queue-history", type=int, default=100, help="Finished jobs reserve size, tasks exceeding the limit will be deleted, including output image files, default: 100")
59 changes: 27 additions & 32 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from fooocus_api_version import version
from fooocusapi.repositories_versions import fooocus_commit_hash

print('[System ARGV] ' + str(sys.argv))

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

Expand Down Expand Up @@ -67,6 +69,7 @@ def git_clone(url, dir, name, hash=None):
print(f'{name} checkout finished for {hash}.')
except Exception as e:
print(f'Git clone failed for {name}: {str(e)}')
raise e


# This function was copied from [Fooocus](https://github.com/lllyasviel/Fooocus) repository.
Expand Down Expand Up @@ -216,7 +219,7 @@ def download_models():
)


def prepare_environments(args) -> bool:
def install_dependents(args):
if not args.skip_pip:
torch_index_url = os.environ.get('TORCH_INDEX_URL', "https://download.pytorch.org/whl/cu121")

Expand Down Expand Up @@ -249,28 +252,31 @@ def prepare_environments(args) -> bool:
if not skip_sync_repo:
download_repositories()

# 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)
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"


def prepare_environments(args) -> bool:
import fooocusapi.worker as worker
worker.task_queue.queue_size = args.queue_size
worker.task_queue.history_size = args.queue_history
print(f"[Fooocus-API] Task queue size: {args.queue_size}, queue history size: {args.queue_history}")

if args.gpu_device_id is not None:
os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu_device_id)
print("Set device to:", args.gpu_device_id)

if args.base_url is None or len(args.base_url.strip()) == 0:
host = args.host
if host == '0.0.0.0':
host = '127.0.0.1'
args.base_url = f"http://{host}:{args.port}"

# 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)
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"

sys.argv = [sys.argv[0]]

if args.disable_private_log:
sys.argv.append('--disable-image-log')

if args.preset is not None:
# Remove and copy preset folder
origin_preset_folder = os.path.abspath(os.path.join(script_path, dir_repos, fooocus_name, 'presets'))
Expand All @@ -282,7 +288,6 @@ def prepare_environments(args) -> bool:
sys.argv.append('--preset')
sys.argv.append(args.preset)
import modules.config as config
import modules.flags as flags
import fooocusapi.parameters as parameters
parameters.default_inpaint_engine_version = config.default_inpaint_engine_version
parameters.defualt_styles = config.default_styles
Expand All @@ -304,13 +309,13 @@ def prepare_environments(args) -> bool:

return True

def pre_setup(skip_sync_repo: bool=False, disable_private_log: bool=False, skip_pip=False, load_all_models: bool=False, preload_pipeline: bool=False, preset: str | None=None):
def pre_setup(skip_sync_repo: bool=False, disable_image_log: bool=False, skip_pip=False, load_all_models: bool=False, preload_pipeline: bool=False, preset: str | None=None):
class Args(object):
host = '127.0.0.1'
port = 8888
base_url = None
sync_repo = None
disable_private_log = False
disable_image_log = False
skip_pip = False
preload_pipeline = False
queue_size = 3
Expand All @@ -320,7 +325,7 @@ class Args(object):
print("[Pre Setup] Prepare environments")

args = Args()
args.disable_private_log = disable_private_log
args.disable_image_log = disable_image_log
args.preload_pipeline = preload_pipeline
args.preset = preset
if skip_sync_repo:
Expand Down Expand Up @@ -353,25 +358,15 @@ def preplaod_pipeline():
print(f"Python {sys.version}")
print(f"Fooocus-API version: {version}")

from fooocusapi.base_args import add_base_args

parser = argparse.ArgumentParser()
parser.add_argument("--port", type=int, default=8888,
help="Set the listen port, default: 8888")
parser.add_argument("--host", type=str,
default='127.0.0.1', help="Set the listen host, default: 127.0.0.1")
parser.add_argument("--base-url", type=str, default=None, help="Set base url for outside visit, default is http://host:port")
parser.add_argument("--log-level", type=str,
default='info', help="Log info for Uvicorn, default: info")
parser.add_argument("--sync-repo", default=None,
help="Sync dependent git repositories to local, 'skip' for skip sync action, 'only' for only do the sync action and not launch app")
parser.add_argument("--disable-private-log", default=False, action="store_true", help="Disable Fooocus image log, won't save output files (include generated image files)")
parser.add_argument("--skip-pip", default=False, action="store_true", help="Skip automatic pip install when setup")
parser.add_argument("--preload-pipeline", default=False, action="store_true", help="Preload pipeline before start http server")
parser.add_argument("--queue-size", type=int, default=3, help="Working queue size, default: 3, generation requests exceeding working queue size will return failure")
parser.add_argument("--queue-history", type=int, default=100, help="Finished jobs reserve size, tasks exceeding the limit will be deleted, including output image files, default: 100")
parser.add_argument("--preset", type=str, default=None, help="Apply specified UI preset.")


args = parser.parse_args()
add_base_args(parser, True)

args, _ = parser.parse_known_args()
install_dependents(args)

from fooocusapi.args import args

if prepare_environments(args):
sys.argv = [sys.argv[0]]
Expand Down
2 changes: 1 addition & 1 deletion predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Predictor(BasePredictor):
def setup(self) -> None:
"""Load the model into memory to make running multiple predictions efficient"""
from main import pre_setup
pre_setup(disable_private_log=True, skip_pip=True, preload_pipeline=True, preset=None)
pre_setup(disable_image_log=True, skip_pip=True, preload_pipeline=True, preset=None)

def predict(
self,
Expand Down

0 comments on commit ddd4d40

Please sign in to comment.