forked from mrhan1993/Fooocus-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
213 lines (162 loc) · 5.86 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# -*- coding: utf-8 -*-
""" Entry for Fooocus API.
Use for starting Fooocus API.
python main.py --help for more usage
@file: main.py
@author: Konie
@update: 2024-03-22
"""
import argparse
import os
import re
import shutil
import sys
from threading import Thread
from fooocusapi.utils.logger import logger
from fooocusapi.utils.tools import run_pip, check_torch_cuda, requirements_check
from fooocus_api_version import version
script_path = os.path.dirname(os.path.realpath(__file__))
module_path = os.path.join(script_path, "repositories/Fooocus")
sys.path.append(script_path)
sys.path.append(module_path)
logger.std_info("[System ARGV] " + str(sys.argv))
try:
index = sys.argv.index('--gpu-device-id')
os.environ["CUDA_VISIBLE_DEVICES"] = str(sys.argv[index+1])
logger.std_info(f"[Fooocus] Set device to: {str(sys.argv[index+1])}")
except ValueError:
pass
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
os.environ["PYTORCH_MPS_HIGH_WATERMARK_RATIO"] = "0.0"
python = sys.executable
default_command_live = True
index_url = os.environ.get("INDEX_URL", "")
re_requirement = re.compile(r"\s*([-_a-zA-Z0-9]+)\s*(?:==\s*([-+_.a-zA-Z0-9]+))?\s*")
def install_dependents(skip: bool = False):
"""
Check and install dependencies
Args:
skip: skip pip install
"""
if skip:
return
torch_index_url = os.environ.get(
"TORCH_INDEX_URL", "https://download.pytorch.org/whl/cu121"
)
# Check if you need pip install
if not requirements_check():
run_pip("install -r requirements.txt", "requirements")
if not check_torch_cuda():
run_pip(
f"install torch==2.1.0 torchvision==0.16.0 --extra-index-url {torch_index_url}",
desc="torch",
)
def preload_pipeline():
"""Preload pipeline"""
logger.std_info("[Fooocus-API] Preloading pipeline ...")
import modules.default_pipeline as _
def prepare_environments(args) -> bool:
"""
Prepare environments
Args:
args: command line arguments
"""
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}"
sys.argv = [sys.argv[0]]
# Remove and copy preset folder
origin_preset_folder = os.path.abspath(os.path.join(module_path, "presets"))
preset_folder = os.path.abspath(os.path.join(script_path, "presets"))
if os.path.exists(preset_folder):
shutil.rmtree(preset_folder)
shutil.copytree(origin_preset_folder, preset_folder)
from modules import config
from fooocusapi.configs import default
from fooocusapi.utils.model_loader import download_models
default.default_inpaint_engine_version = config.default_inpaint_engine_version
default.default_styles = config.default_styles
default.default_base_model_name = config.default_base_model_name
default.default_refiner_model_name = config.default_refiner_model_name
default.default_refiner_switch = config.default_refiner_switch
default.default_loras = config.default_loras
default.default_cfg_scale = config.default_cfg_scale
default.default_prompt_negative = config.default_prompt_negative
default.default_aspect_ratio = default.get_aspect_ratio_value(
config.default_aspect_ratio
)
default.available_aspect_ratios = [
default.get_aspect_ratio_value(a) for a in config.available_aspect_ratios
]
download_models()
# Init task queue
from fooocusapi import worker
from fooocusapi.task_queue import TaskQueue
worker.worker_queue = TaskQueue(
queue_size=args.queue_size,
history_size=args.queue_history,
webhook_url=args.webhook_url,
persistent=args.persistent,
)
logger.std_info(f"[Fooocus-API] Task queue size: {args.queue_size}")
logger.std_info(f"[Fooocus-API] Queue history size: {args.queue_history}")
logger.std_info(f"[Fooocus-API] Webhook url: {args.webhook_url}")
return True
def pre_setup():
"""
Pre setup, for replicate
"""
class Args(object):
"""
Arguments object
"""
host = "127.0.0.1"
port = 8888
base_url = None
sync_repo = "skip"
disable_image_log = True
skip_pip = True
preload_pipeline = True
queue_size = 100
queue_history = 0
preset = "default"
webhook_url = None
persistent = False
always_gpu = False
all_in_fp16 = False
gpu_device_id = None
apikey = None
print("[Pre Setup] Prepare environments")
arguments = Args()
sys.argv = [sys.argv[0]]
sys.argv.append("--disable-image-log")
install_dependents(arguments.skip_pip)
prepare_environments(arguments)
# Start task schedule thread
from fooocusapi.worker import task_schedule_loop
task_thread = Thread(target=task_schedule_loop, daemon=True)
task_thread.start()
print("[Pre Setup] Finished")
if __name__ == "__main__":
logger.std_info(f"[Fooocus API] Python {sys.version}")
logger.std_info(f"[Fooocus API] Fooocus API version: {version}")
from fooocusapi.base_args import add_base_args
parser = argparse.ArgumentParser()
add_base_args(parser, True)
args, _ = parser.parse_known_args()
install_dependents(skip=args.skip_pip)
from fooocusapi.args import args
if prepare_environments(args):
sys.argv = [sys.argv[0]]
# Load pipeline in new thread
preload_pipeline_thread = Thread(target=preload_pipeline, daemon=True)
preload_pipeline_thread.start()
# Start task schedule thread
from fooocusapi.worker import task_schedule_loop
task_schedule_thread = Thread(target=task_schedule_loop, daemon=True)
task_schedule_thread.start()
# Start api server
from fooocusapi.api import start_app
start_app(args)