update to latest gradio

This commit is contained in:
AUTOMATIC1111 2024-03-02 08:25:10 +03:00
parent 43850655d9
commit 3ee79332b1
5 changed files with 62 additions and 16 deletions

5
javascript/gradio.js Normal file
View File

@ -0,0 +1,5 @@
// added to fix a weird error in gradio 4.19 at page load
Object.defineProperty(Array.prototype, 'toLowerCase', {
value: function() { return this; }
});

View File

@ -109,7 +109,7 @@ def check_versions():
expected_torch_version = "2.1.2" expected_torch_version = "2.1.2"
expected_xformers_version = "0.0.23.post1" expected_xformers_version = "0.0.23.post1"
expected_gradio_version = "4.16.0" expected_gradio_version = "4.19.2"
if version.parse(torch.__version__) < version.parse(expected_torch_version): if version.parse(torch.__version__) < version.parse(expected_torch_version):
print_error_explanation(f""" print_error_explanation(f"""

View File

@ -4,6 +4,7 @@ from collections import namedtuple
from pathlib import Path from pathlib import Path
import gradio.components import gradio.components
import gradio as gr
from PIL import PngImagePlugin from PIL import PngImagePlugin
@ -15,7 +16,12 @@ Savedfile = namedtuple("Savedfile", ["name"])
def register_tmp_file(gradio, filename): def register_tmp_file(gradio, filename):
if hasattr(gradio, 'temp_file_sets'): # gradio 3.15 if hasattr(gradio, 'temp_file_sets'): # gradio 3.15
gradio.temp_file_sets[0] = gradio.temp_file_sets[0] | {os.path.abspath(filename)} if hasattr(gr.utils, 'abspath'): # gradio 4.19
filename = gr.utils.abspath(filename)
else:
filename = os.path.abspath(filename)
gradio.temp_file_sets[0] = gradio.temp_file_sets[0] | {filename}
if hasattr(gradio, 'temp_dirs'): # gradio 3.9 if hasattr(gradio, 'temp_dirs'): # gradio 3.9
gradio.temp_dirs = gradio.temp_dirs | {os.path.abspath(os.path.dirname(filename))} gradio.temp_dirs = gradio.temp_dirs | {os.path.abspath(os.path.dirname(filename))}
@ -23,6 +29,11 @@ def register_tmp_file(gradio, filename):
def check_tmp_file(gradio, filename): def check_tmp_file(gradio, filename):
if hasattr(gradio, 'temp_file_sets'): if hasattr(gradio, 'temp_file_sets'):
if hasattr(gr.utils, 'abspath'): # gradio 4.19
filename = gr.utils.abspath(filename)
else:
filename = os.path.abspath(filename)
return any(filename in fileset for fileset in gradio.temp_file_sets) return any(filename in fileset for fileset in gradio.temp_file_sets)
if hasattr(gradio, 'temp_dirs'): if hasattr(gradio, 'temp_dirs'):
@ -60,8 +71,9 @@ def save_pil_to_file(pil_image, cache_dir=None, format="png"):
return file_obj.name return file_obj.name
def move_files_to_cache(data, block, postprocess=False): def move_files_to_cache(data, block, postprocess=False, add_urls=False, check_in_upload_folder=False):
"""Move files to cache and replace the file path with the cache path. """Move any files in `data` to cache and (optionally), adds URL prefixes (/file=...) needed to access the cached file.
Also handles the case where the file is on an external Gradio app (/proxy=...).
Runs after postprocess and before preprocess. Runs after postprocess and before preprocess.
@ -69,35 +81,64 @@ def move_files_to_cache(data, block, postprocess=False):
Args: Args:
data: The input or output data for a component. Can be a dictionary or a dataclass data: The input or output data for a component. Can be a dictionary or a dataclass
block: The component block: The component whose data is being processed
postprocess: Whether its running from postprocessing postprocess: Whether its running from postprocessing
add_urls: Whether to add URLs to the payload
check_in_upload_folder: If True, instead of moving the file to cache, checks if the file is in already in cache (exception if not).
""" """
from gradio import FileData from gradio import FileData
from gradio.processing_utils import move_resource_to_block_cache
from gradio.data_classes import GradioRootModel from gradio.data_classes import GradioRootModel
from gradio.data_classes import GradioModel from gradio.data_classes import GradioModel
from gradio_client import utils as client_utils from gradio_client import utils as client_utils
from gradio.utils import get_upload_folder, is_in_or_equal
def _move_to_cache(d: dict): def _move_to_cache(d: dict):
payload = FileData(**d) payload = FileData(**d)
# EDITED
payload.path = payload.path.rsplit('?', 1)[0]
# If the gradio app developer is returning a URL from # If the gradio app developer is returning a URL from
# postprocess, it means the component can display a URL # postprocess, it means the component can display a URL
# without it being served from the gradio server # without it being served from the gradio server
# This makes it so that the URL is not downloaded and speeds up event processing # This makes it so that the URL is not downloaded and speeds up event processing
payload.path = payload.path.rsplit('?', 1)[0]
if payload.url and postprocess: if payload.url and postprocess:
temp_file_path = payload.url payload.path = payload.url
else: elif not block.proxy_url:
# If the file is on a remote server, do not move it to cache.
if check_in_upload_folder and not client_utils.is_http_url_like(
payload.path
):
path = os.path.abspath(payload.path)
if not is_in_or_equal(path, get_upload_folder()):
raise ValueError(
f"File {path} is not in the upload folder and cannot be accessed."
)
# EDITED
if check_tmp_file(shared.demo, payload.path): if check_tmp_file(shared.demo, payload.path):
temp_file_path = payload.path temp_file_path = payload.path
else: else:
temp_file_path = move_resource_to_block_cache(payload.path, block) temp_file_path = block.move_resource_to_block_cache(payload.path)
assert temp_file_path is not None
payload.path = temp_file_path if temp_file_path is None:
raise ValueError("Did not determine a file path for the resource.")
payload.path = temp_file_path
if add_urls:
url_prefix = "/stream/" if payload.is_stream else "/file="
if block.proxy_url:
proxy_url = block.proxy_url.rstrip("/")
url = f"/proxy={proxy_url}{url_prefix}{payload.path}"
elif client_utils.is_http_url_like(payload.path) or payload.path.startswith(
f"{url_prefix}"
):
url = payload.path
else:
url = f"{url_prefix}{payload.path}"
payload.url = url
return payload.model_dump() return payload.model_dump()
if isinstance(data, (GradioRootModel, GradioModel)): if isinstance(data, (GradioRootModel, GradioModel)):

View File

@ -7,7 +7,7 @@ clean-fid
einops einops
facexlib facexlib
fastapi>=0.90.1 fastapi>=0.90.1
gradio==4.16.0 gradio==4.19.2
inflection inflection
jsonmerge jsonmerge
kornia kornia

View File

@ -6,7 +6,7 @@ clean-fid==0.1.35
einops==0.4.1 einops==0.4.1
facexlib==0.3.0 facexlib==0.3.0
fastapi==0.104.1 fastapi==0.104.1
gradio==4.16.0 gradio==4.19.2
httpcore==0.15 httpcore==0.15
inflection==0.5.1 inflection==0.5.1
jsonmerge==1.8.0 jsonmerge==1.8.0