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