Skip to content

Commit b6e57e6

Browse files
committed
Change the way of running pip when startaup
1 parent 623537e commit b6e57e6

File tree

4 files changed

+64
-23
lines changed

4 files changed

+64
-23
lines changed

README.md

+6-14
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,17 @@ The colab notebook uses the Fooocus's `colab` branch, which may lack some latest
1818
| --- | --- |
1919
[![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
2020

21-
### Install dependencies.
22-
Need python version >= 3.10
23-
```
24-
pip install -r requirements.txt
25-
pip install torch==2.0.1 torchvision==0.15.2 --extra-index-url https://download.pytorch.org/whl/cu118 xformers
26-
```
27-
You may change the part "cu118" of extra-index-url to your local installed cuda driver version.
21+
### Start app
22+
Need python version >= 3.10, or use conda to create a new env.
2823

29-
### Sync dependent and download models (Optional)
3024
```
31-
python main.py --sync-repo only
25+
conda env create -f environment.yaml
26+
conda activate fooocus-api
3227
```
33-
After run successful, you can see the terminal print where to put the model files for Fooocus.
3428

35-
Then you can put the model files to target directories manually, or let it auto downloads when start app.
29+
Set enviroment variable `TORCH_INDEX_URL` to the version corresponding to the local cuda driver.
30+
Default is "https://download.pytorch.org/whl/cu121", you may change the part "cu118".
3631

37-
It will also apply user_path_config.txt config file as Fooocus. See [Changing Model Path](https://github.com/lllyasviel/Fooocus#changing-model-path).
38-
39-
### Start app
4032
Run
4133
```
4234
python main.py

environment.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name: fooocus-api
2+
channels:
3+
- defaults
4+
dependencies:
5+
- python=3.10
6+
- pip=23.0
7+
- packaging

fooocus_api_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version = '0.1.16'
1+
version = '0.1.17'

main.py

+50-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import argparse
22
import os
3+
import re
34
import shutil
45
import subprocess
56
import sys
@@ -13,6 +14,7 @@
1314
python = sys.executable
1415
default_command_live = True
1516
index_url = os.environ.get('INDEX_URL', "")
17+
re_requirement = re.compile(r"\s*([-_a-zA-Z0-9]+)\s*(?:==\s*([-+_.a-zA-Z0-9]+))?\s*")
1618

1719
fooocus_name = 'Fooocus'
1820

@@ -115,6 +117,43 @@ def run_pip(command, desc=None, live=default_command_live):
115117
return None
116118

117119

120+
121+
# This function was copied from [Fooocus](https://github.com/lllyasviel/Fooocus) repository.
122+
def requirements_met(requirements_file):
123+
"""
124+
Does a simple parse of a requirements.txt file to determine if all rerqirements in it
125+
are already installed. Returns True if so, False if not installed or parsing fails.
126+
"""
127+
128+
import importlib.metadata
129+
import packaging.version
130+
131+
with open(requirements_file, "r", encoding="utf8") as file:
132+
for line in file:
133+
if line.strip() == "":
134+
continue
135+
136+
m = re.match(re_requirement, line)
137+
if m is None:
138+
return False
139+
140+
package = m.group(1).strip()
141+
version_required = (m.group(2) or "").strip()
142+
143+
if version_required == "":
144+
continue
145+
146+
try:
147+
version_installed = importlib.metadata.version(package)
148+
except Exception:
149+
return False
150+
151+
if packaging.version.parse(version_required) != packaging.version.parse(version_installed):
152+
return False
153+
154+
return True
155+
156+
118157
def download_repositories():
119158
import pygit2
120159

@@ -175,17 +214,20 @@ def download_models():
175214
)
176215

177216

178-
def run_pip_install():
179-
print("Run pip install")
180-
run_pip("install -r requirements.txt", "requirements")
181-
run_pip("install torch torchvision --extra-index-url https://download.pytorch.org/whl/cu118", "torch")
182-
run_pip("install xformers", "xformers")
183-
184-
185217
def prepare_environments(args) -> bool:
218+
torch_index_url = os.environ.get('TORCH_INDEX_URL', "https://download.pytorch.org/whl/cu121")
219+
186220
# Check if need pip install
221+
requirements_file = 'requirements.txt'
222+
if not requirements_met(requirements_file):
223+
run_pip(f"install -r \"{requirements_file}\"", "requirements")
224+
225+
if not is_installed("torch") or not is_installed("torchvision"):
226+
print(f"torch_index_url: {torch_index_url}")
227+
run_pip(f"install torch==2.0.1 torchvision==0.15.2 --extra-index-url {torch_index_url}", "torch")
228+
187229
if not is_installed('xformers'):
188-
run_pip_install()
230+
run_pip("install xformers==0.0.21", "xformers")
189231

190232
skip_sync_repo = False
191233
if args.sync_repo is not None:

0 commit comments

Comments
 (0)