diff --git a/README.md b/README.md index 429a8ca..b2e71bf 100644 --- a/README.md +++ b/README.md @@ -18,25 +18,17 @@ The colab notebook uses the Fooocus's `colab` branch, which may lack some latest | --- | --- | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/konieshadow/Fooocus-API/blob/colab/colab.ipynb) | Fooocus-API -### Install dependencies. -Need python version >= 3.10 -``` -pip install -r requirements.txt -pip install torch==2.0.1 torchvision==0.15.2 --extra-index-url https://download.pytorch.org/whl/cu118 xformers -``` -You may change the part "cu118" of extra-index-url to your local installed cuda driver version. +### Start app +Need python version >= 3.10, or use conda to create a new env. -### Sync dependent and download models (Optional) ``` -python main.py --sync-repo only +conda env create -f environment.yaml +conda activate fooocus-api ``` -After run successful, you can see the terminal print where to put the model files for Fooocus. -Then you can put the model files to target directories manually, or let it auto downloads when start app. +Set enviroment variable `TORCH_INDEX_URL` to the version corresponding to the local cuda driver. +Default is "https://download.pytorch.org/whl/cu121", you may change the part "cu118". -It will also apply user_path_config.txt config file as Fooocus. See [Changing Model Path](https://github.com/lllyasviel/Fooocus#changing-model-path). - -### Start app Run ``` python main.py diff --git a/environment.yaml b/environment.yaml new file mode 100644 index 0000000..ec116cf --- /dev/null +++ b/environment.yaml @@ -0,0 +1,7 @@ +name: fooocus-api +channels: + - defaults +dependencies: + - python=3.10 + - pip=23.0 + - packaging \ No newline at end of file diff --git a/fooocus_api_version.py b/fooocus_api_version.py index 3463d04..aea1cc4 100644 --- a/fooocus_api_version.py +++ b/fooocus_api_version.py @@ -1 +1 @@ -version = '0.1.16' \ No newline at end of file +version = '0.1.17' \ No newline at end of file diff --git a/main.py b/main.py index d422316..e5508dd 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,6 @@ import argparse import os +import re import shutil import subprocess import sys @@ -13,6 +14,7 @@ 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*") fooocus_name = 'Fooocus' @@ -115,6 +117,43 @@ def run_pip(command, desc=None, live=default_command_live): return None + +# This function was copied from [Fooocus](https://github.com/lllyasviel/Fooocus) repository. +def requirements_met(requirements_file): + """ + Does a simple parse of a requirements.txt file to determine if all rerqirements in it + are already installed. Returns True if so, False if not installed or parsing fails. + """ + + import importlib.metadata + import packaging.version + + with open(requirements_file, "r", encoding="utf8") as file: + for line in file: + if line.strip() == "": + continue + + m = re.match(re_requirement, line) + if m is None: + return False + + package = m.group(1).strip() + version_required = (m.group(2) or "").strip() + + if version_required == "": + continue + + try: + version_installed = importlib.metadata.version(package) + except Exception: + return False + + if packaging.version.parse(version_required) != packaging.version.parse(version_installed): + return False + + return True + + def download_repositories(): import pygit2 @@ -175,17 +214,20 @@ def download_models(): ) -def run_pip_install(): - print("Run pip install") - run_pip("install -r requirements.txt", "requirements") - run_pip("install torch torchvision --extra-index-url https://download.pytorch.org/whl/cu118", "torch") - run_pip("install xformers", "xformers") - - def prepare_environments(args) -> bool: + torch_index_url = os.environ.get('TORCH_INDEX_URL', "https://download.pytorch.org/whl/cu121") + # Check if need pip install + requirements_file = 'requirements.txt' + if not requirements_met(requirements_file): + run_pip(f"install -r \"{requirements_file}\"", "requirements") + + if not is_installed("torch") or not is_installed("torchvision"): + print(f"torch_index_url: {torch_index_url}") + run_pip(f"install torch==2.0.1 torchvision==0.15.2 --extra-index-url {torch_index_url}", "torch") + if not is_installed('xformers'): - run_pip_install() + run_pip("install xformers==0.0.21", "xformers") skip_sync_repo = False if args.sync_repo is not None: