Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: gradio's ValueError about fetching extensions files #8492

Merged
merged 6 commits into from
Mar 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
import platform
import argparse
import json
try:
from modules.paths import script_path, data_path
except ModuleNotFoundError:
script_path = os.path.dirname(__file__)
data_path = os.getcwd()

dir_repos = "repositories"
dir_extensions = "extensions"
Expand Down Expand Up @@ -122,7 +127,7 @@ def is_installed(package):


def repo_dir(name):
return os.path.join(dir_repos, name)
return os.path.join(script_path, dir_repos, name)


def run_python(code, desc=None, errdesc=None):
Expand Down Expand Up @@ -215,7 +220,7 @@ def list_extensions(settings_file):

disabled_extensions = set(settings.get('disabled_extensions', []))

return [x for x in os.listdir(dir_extensions) if x not in disabled_extensions]
return [x for x in os.listdir(os.path.join(data_path, dir_extensions)) if x not in disabled_extensions]


def run_extensions_installers(settings_file):
Expand Down Expand Up @@ -306,7 +311,7 @@ def prepare_environment():
if not is_installed("pyngrok") and ngrok:
run_pip("install pyngrok", "ngrok")

os.makedirs(dir_repos, exist_ok=True)
os.makedirs(os.path.join(script_path, dir_repos), exist_ok=True)

git_clone(stable_diffusion_repo, repo_dir('stable-diffusion-stability-ai'), "Stable Diffusion", stable_diffusion_commit_hash)
git_clone(taming_transformers_repo, repo_dir('taming-transformers'), "Taming Transformers", taming_transformers_commit_hash)
Expand All @@ -317,15 +322,15 @@ def prepare_environment():
if not is_installed("lpips"):
run_pip(f"install -r {os.path.join(repo_dir('CodeFormer'), 'requirements.txt')}", "requirements for CodeFormer")

run_pip(f"install -r {requirements_file}", "requirements for Web UI")
run_pip(f"install -r {os.path.join(script_path, requirements_file)}", "requirements for Web UI")

run_extensions_installers(settings_file=args.ui_settings_file)

if update_check:
version_check(commit)

if update_all_extensions:
git_pull_recursive(dir_extensions)
git_pull_recursive(os.path.join(data_path, dir_extensions))

if "--exit" in sys.argv:
print("Exiting because of --exit argument")
Expand All @@ -341,7 +346,7 @@ def tests(test_dir):
sys.argv.append("--api")
if "--ckpt" not in sys.argv:
sys.argv.append("--ckpt")
sys.argv.append("./test/test_files/empty.pt")
sys.argv.append(os.path.join(script_path, "test/test_files/empty.pt"))
if "--skip-torch-cuda-test" not in sys.argv:
sys.argv.append("--skip-torch-cuda-test")
if "--disable-nan-check" not in sys.argv:
Expand All @@ -350,7 +355,7 @@ def tests(test_dir):
print(f"Launching Web UI in another process for testing with arguments: {' '.join(sys.argv[1:])}")

os.environ['COMMANDLINE_ARGS'] = ""
with open('test/stdout.txt', "w", encoding="utf8") as stdout, open('test/stderr.txt', "w", encoding="utf8") as stderr:
with open(os.path.join(script_path, 'test/stdout.txt'), "w", encoding="utf8") as stdout, open(os.path.join(script_path, 'test/stderr.txt'), "w", encoding="utf8") as stderr:
proc = subprocess.Popen([sys.executable, *sys.argv], stdout=stdout, stderr=stderr)

import test.server_poll
Expand Down
5 changes: 4 additions & 1 deletion modules/sd_vae_approx.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ def model():
global sd_vae_approx_model

if sd_vae_approx_model is None:
model_path = os.path.join(paths.models_path, "VAE-approx", "model.pt")
sd_vae_approx_model = VAEApprox()
sd_vae_approx_model.load_state_dict(torch.load(os.path.join(paths.models_path, "VAE-approx", "model.pt"), map_location='cpu' if devices.device.type != 'cuda' else None))
if not os.path.exists(model_path):
model_path = os.path.join(paths.script_path, "models", "VAE-approx", "model.pt")
sd_vae_approx_model.load_state_dict(torch.load(model_path, map_location='cpu' if devices.device.type != 'cuda' else None))
sd_vae_approx_model.eval()
sd_vae_approx_model.to(devices.device, devices.dtype)

Expand Down
3 changes: 2 additions & 1 deletion modules/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -1745,7 +1745,8 @@ def check_dropdown(val):


def reload_javascript():
head = f'<script type="text/javascript" src="file={os.path.abspath("script.js")}?{os.path.getmtime("script.js")}"></script>\n'
script_js = os.path.join(script_path, "script.js")
head = f'<script type="text/javascript" src="file={os.path.abspath(script_js)}?{os.path.getmtime(script_js)}"></script>\n'

inline = f"{localization.localization_js(shared.opts.localization)};"
if cmd_opts.theme is not None:
Expand Down
8 changes: 5 additions & 3 deletions test/basic_features/extras_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os
import unittest
import requests
from gradio.processing_utils import encode_pil_to_base64
from PIL import Image
from modules.paths import script_path

class TestExtrasWorking(unittest.TestCase):
def setUp(self):
Expand All @@ -19,7 +21,7 @@ def setUp(self):
"upscaler_1": "None",
"upscaler_2": "None",
"extras_upscaler_2_visibility": 0,
"image": encode_pil_to_base64(Image.open(r"test/test_files/img2img_basic.png"))
"image": encode_pil_to_base64(Image.open(os.path.join(script_path, r"test/test_files/img2img_basic.png")))
}

def test_simple_upscaling_performed(self):
Expand All @@ -31,7 +33,7 @@ class TestPngInfoWorking(unittest.TestCase):
def setUp(self):
self.url_png_info = "http://localhost:7860/sdapi/v1/extra-single-image"
self.png_info = {
"image": encode_pil_to_base64(Image.open(r"test/test_files/img2img_basic.png"))
"image": encode_pil_to_base64(Image.open(os.path.join(script_path, r"test/test_files/img2img_basic.png")))
}

def test_png_info_performed(self):
Expand All @@ -42,7 +44,7 @@ class TestInterrogateWorking(unittest.TestCase):
def setUp(self):
self.url_interrogate = "http://localhost:7860/sdapi/v1/extra-single-image"
self.interrogate = {
"image": encode_pil_to_base64(Image.open(r"test/test_files/img2img_basic.png")),
"image": encode_pil_to_base64(Image.open(os.path.join(script_path, r"test/test_files/img2img_basic.png"))),
"model": "clip"
}

Expand Down
8 changes: 5 additions & 3 deletions test/basic_features/img2img_test.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import os
import unittest
import requests
from gradio.processing_utils import encode_pil_to_base64
from PIL import Image
from modules.paths import script_path


class TestImg2ImgWorking(unittest.TestCase):
def setUp(self):
self.url_img2img = "http://localhost:7860/sdapi/v1/img2img"
self.simple_img2img = {
"init_images": [encode_pil_to_base64(Image.open(r"test/test_files/img2img_basic.png"))],
"init_images": [encode_pil_to_base64(Image.open(os.path.join(script_path, r"test/test_files/img2img_basic.png")))],
"resize_mode": 0,
"denoising_strength": 0.75,
"mask": None,
Expand Down Expand Up @@ -47,11 +49,11 @@ def test_img2img_simple_performed(self):
self.assertEqual(requests.post(self.url_img2img, json=self.simple_img2img).status_code, 200)

def test_inpainting_masked_performed(self):
self.simple_img2img["mask"] = encode_pil_to_base64(Image.open(r"test/test_files/mask_basic.png"))
self.simple_img2img["mask"] = encode_pil_to_base64(Image.open(os.path.join(script_path, r"test/test_files/img2img_basic.png")))
self.assertEqual(requests.post(self.url_img2img, json=self.simple_img2img).status_code, 200)

def test_inpainting_with_inverted_masked_performed(self):
self.simple_img2img["mask"] = encode_pil_to_base64(Image.open(r"test/test_files/mask_basic.png"))
self.simple_img2img["mask"] = encode_pil_to_base64(Image.open(os.path.join(script_path, r"test/test_files/img2img_basic.png")))
self.simple_img2img["inpainting_mask_invert"] = True
self.assertEqual(requests.post(self.url_img2img, json=self.simple_img2img).status_code, 200)

Expand Down
6 changes: 4 additions & 2 deletions test/server_poll.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import unittest
import requests
import time
import os
from modules.paths import script_path


def run_tests(proc, test_dir):
Expand All @@ -15,8 +17,8 @@ def run_tests(proc, test_dir):
break
if proc.poll() is None:
if test_dir is None:
test_dir = "test"
suite = unittest.TestLoader().discover(test_dir, pattern="*_test.py", top_level_dir="test")
test_dir = os.path.join(script_path, "test")
suite = unittest.TestLoader().discover(test_dir, pattern="*_test.py", top_level_dir=test_dir)
result = unittest.TextTestRunner(verbosity=2).run(suite)
return len(result.failures) + len(result.errors)
else:
Expand Down
28 changes: 14 additions & 14 deletions webui.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@

# If run from macOS, load defaults from webui-macos-env.sh
if [[ "$OSTYPE" == "darwin"* ]]; then
if [[ -f webui-macos-env.sh ]]
if [[ -f "$(dirname $0)/webui-macos-env.sh" ]]
then
source ./webui-macos-env.sh
source "$(dirname $0)/webui-macos-env.sh"
fi
fi

# Read variables from webui-user.sh
# shellcheck source=/dev/null
if [[ -f webui-user.sh ]]
if [[ -f "$(dirname $0)/webui-user.sh" ]]
then
source ./webui-user.sh
source "$(dirname $0)/webui-user.sh"
fi

# Set defaults
# Install directory without trailing slash
if [[ -z "${install_dir}" ]]
Expand Down Expand Up @@ -47,12 +46,12 @@ fi
# python3 venv without trailing slash (defaults to ${install_dir}/${clone_dir}/venv)
if [[ -z "${venv_dir}" ]]
then
venv_dir="venv"
venv_dir="${install_dir}/${clone_dir}/venv"
fi

if [[ -z "${LAUNCH_SCRIPT}" ]]
then
LAUNCH_SCRIPT="launch.py"
LAUNCH_SCRIPT="${install_dir}/${clone_dir}/launch.py"
fi

# this script cannot be run as root by default
Expand Down Expand Up @@ -140,22 +139,23 @@ then
exit 1
fi

cd "${install_dir}"/ || { printf "\e[1m\e[31mERROR: Can't cd to %s/, aborting...\e[0m" "${install_dir}"; exit 1; }
if [[ -d "${clone_dir}" ]]
if [[ ! -d "${install_dir}/${clone_dir}" ]]
then
cd "${clone_dir}"/ || { printf "\e[1m\e[31mERROR: Can't cd to %s/%s/, aborting...\e[0m" "${install_dir}" "${clone_dir}"; exit 1; }
else
printf "\n%s\n" "${delimiter}"
printf "Clone stable-diffusion-webui"
printf "\n%s\n" "${delimiter}"
"${GIT}" clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git "${clone_dir}"
cd "${clone_dir}"/ || { printf "\e[1m\e[31mERROR: Can't cd to %s/%s/, aborting...\e[0m" "${install_dir}" "${clone_dir}"; exit 1; }
mkdir -p "${install_dir}"
"${GIT}" clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git "${install_dir}/${clone_dir}"
fi

printf "\n%s\n" "${delimiter}"
printf "Create and activate python venv"
printf "\n%s\n" "${delimiter}"
cd "${install_dir}"/"${clone_dir}"/ || { printf "\e[1m\e[31mERROR: Can't cd to %s/%s/, aborting...\e[0m" "${install_dir}" "${clone_dir}"; exit 1; }
# Make venv_dir absolute
if [[ "${venv_dir}" != /* ]]
then
venv_dir="${install_dir}/${clone_dir}/${venv_dir}"
fi
if [[ ! -d "${venv_dir}" ]]
then
"${python_cmd}" -m venv "${venv_dir}"
Expand Down