Skip to content

Commit

Permalink
Change the way of running pip when startaup
Browse files Browse the repository at this point in the history
  • Loading branch information
konieshadow committed Oct 16, 2023
1 parent 623537e commit b6e57e6
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 23 deletions.
20 changes: 6 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions environment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: fooocus-api
channels:
- defaults
dependencies:
- python=3.10
- pip=23.0
- packaging
2 changes: 1 addition & 1 deletion fooocus_api_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = '0.1.16'
version = '0.1.17'
58 changes: 50 additions & 8 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import argparse
import os
import re
import shutil
import subprocess
import sys
Expand All @@ -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'

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit b6e57e6

Please sign in to comment.