Skip to content

Commit

Permalink
feat: add config for temp path and temp path cleanup on launch (#1992)
Browse files Browse the repository at this point in the history
* Added options to set the Gradio cache path and  clear cache on launch.

* Renamed cache to temp

* clear temp

* feat: do not delete temp folder but only clean content

also use fallback to system temp dir
see https://github.com/gradio-app/gradio/blob/6683ab2589f9d8658e1f51acc1b7526edce988d3/gradio/utils.py#L1151

* refactor: code cleanup

* feat: unify arg --temp-path and new temp_path config value

* feat: change default temp dir from gradio to fooocus

* refactor: move temp path method definition and configs

* feat: rename get_temp_path to init_temp_path

---------

Co-authored-by: steveyourcreativepeople <steve@yourcreativepeople.com>
Co-authored-by: Manuel Schmid <manuel.schmid@odt.net>
  • Loading branch information
3 people authored Mar 10, 2024
1 parent 831c6b9 commit 85e8aa8
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 15 deletions.
3 changes: 0 additions & 3 deletions args_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,4 @@
if args_parser.args.disable_in_browser:
args_parser.args.in_browser = False

if args_parser.args.temp_path is None:
args_parser.args.temp_path = os.path.join(gettempdir(), 'Fooocus')

args = args_parser.args
21 changes: 14 additions & 7 deletions launch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import sys
import ssl
import sys

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

Expand All @@ -15,15 +15,13 @@

ssl._create_default_https_context = ssl._create_unverified_context


import platform
import fooocus_version

from build_launcher import build_launcher
from modules.launch_util import is_installed, run, python, run_pip, requirements_met
from modules.launch_util import is_installed, run, python, run_pip, requirements_met, delete_folder_content
from modules.model_loader import load_file_from_url


REINSTALL_ALL = False
TRY_INSTALL_XFORMERS = False

Expand Down Expand Up @@ -68,6 +66,7 @@ def prepare_environment():
'https://huggingface.co/lllyasviel/misc/resolve/main/xl-to-v1_interposer-v3.1.safetensors')
]


def ini_args():
from args_manager import args
return args
Expand All @@ -77,14 +76,23 @@ def ini_args():
build_launcher()
args = ini_args()


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)


from modules import config

os.environ['GRADIO_TEMP_DIR'] = config.temp_path

if config.temp_path_cleanup_on_launch:
print(f'[Cleanup] Attempting to delete content of temp dir {config.temp_path}')
result = delete_folder_content(config.temp_path, '[Cleanup] ')
if result:
print("[Cleanup] Cleanup successful")
else:
print(f"[Cleanup] Failed to delete content of temp dir.")


def download_models():
for file_name, url in vae_approx_filenames:
load_file_from_url(url=url, model_dir=config.path_vae_approx, file_name=file_name)
Expand Down Expand Up @@ -123,5 +131,4 @@ def download_models():

download_models()


from webui import *
36 changes: 35 additions & 1 deletion modules/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import math
import numbers
import args_manager
import tempfile
import modules.flags
import modules.sdxl_styles

from modules.model_loader import load_file_from_url
from modules.util import get_files_from_folder, makedirs_with_log
from modules.flags import Performance, MetadataScheme


def get_config_path(key, default_value):
env = os.getenv(key)
if env is not None and isinstance(env, str):
Expand All @@ -18,6 +20,7 @@ def get_config_path(key, default_value):
else:
return os.path.abspath(default_value)


config_path = get_config_path('config_path', "./config.txt")
config_example_path = get_config_path('config_example_path', "config_modification_tutorial.txt")
config_dict = {}
Expand Down Expand Up @@ -117,7 +120,7 @@ def get_path_output() -> str:
global config_dict
path_output = get_dir_or_set_default('path_outputs', '../outputs/', make_directory=True)
if args_manager.args.output_path:
print(f'[CONFIG] Overriding config value path_outputs with {args_manager.args.output_path}')
print(f'Overriding config value path_outputs with {args_manager.args.output_path}')
config_dict['path_outputs'] = path_output = args_manager.args.output_path
return path_output

Expand Down Expand Up @@ -178,6 +181,7 @@ def get_dir_or_set_default(key, default_value, as_array=False, make_directory=Fa
path_fooocus_expansion = get_dir_or_set_default('path_fooocus_expansion', '../models/prompt_expansion/fooocus_expansion')
path_outputs = get_path_output()


def get_config_item_or_set_default(key, default_value, validator, disable_empty_as_none=False):
global config_dict, visited_keys

Expand Down Expand Up @@ -206,6 +210,36 @@ def get_config_item_or_set_default(key, default_value, validator, disable_empty_
return default_value


def init_temp_path(path: str | None, default_path: str) -> str:
if args_manager.args.temp_path:
path = args_manager.args.temp_path

if path != '' and path != default_path:
try:
if not os.path.isabs(path):
path = os.path.abspath(path)
os.makedirs(path, exist_ok=True)
print(f'Using temp path {path}')
return path
except Exception as e:
print(f'Could not create temp path {path}. Reason: {e}')
print(f'Using default temp path {default_path} instead.')

os.makedirs(default_path, exist_ok=True)
return default_path


default_temp_path = os.path.join(tempfile.gettempdir(), 'fooocus')
temp_path = init_temp_path(get_config_item_or_set_default(
key='temp_path',
default_value=default_temp_path,
validator=lambda x: isinstance(x, str),
), default_temp_path)
temp_path_cleanup_on_launch = get_config_item_or_set_default(
key='temp_path_cleanup_on_launch',
default_value=True,
validator=lambda x: isinstance(x, bool)
)
default_base_model_name = get_config_item_or_set_default(
key='default_model',
default_value='model.safetensors',
Expand Down
20 changes: 17 additions & 3 deletions modules/launch_util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import importlib
import importlib.util
import shutil
import subprocess
import sys
import re
Expand All @@ -9,9 +10,6 @@
import packaging.version
from packaging.requirements import Requirement




logging.getLogger("torch.distributed.nn").setLevel(logging.ERROR) # sshh...
logging.getLogger("xformers").addFilter(lambda record: 'A matching Triton is not available' not in record.getMessage())

Expand Down Expand Up @@ -101,3 +99,19 @@ def requirements_met(requirements_file):

return True


def delete_folder_content(folder, prefix=None):
result = True

for filename in os.listdir(folder):
file_path = os.path.join(folder, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print(f'{prefix}Failed to delete {file_path}. Reason: {e}')
result = False

return result
2 changes: 1 addition & 1 deletion modules/private_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def get_current_html_path(output_format=None):


def log(img, metadata, metadata_parser: MetadataParser | None = None, output_format=None) -> str:
path_outputs = args_manager.args.temp_path if args_manager.args.disable_image_log else modules.config.path_outputs
path_outputs = modules.config.temp_path if args_manager.args.disable_image_log else modules.config.path_outputs
output_format = output_format if output_format else modules.config.default_output_format
date_string, local_temp_filename, only_name = generate_temp_filename(folder=path_outputs, extension=output_format)
os.makedirs(os.path.dirname(local_temp_filename), exist_ok=True)
Expand Down

0 comments on commit 85e8aa8

Please sign in to comment.