fix serving images that have already been saved without temp files function that broke after updating gradio

This commit is contained in:
AUTOMATIC 2023-05-27 19:06:49 +03:00
parent 339b531570
commit 633867ecc6

View File

@ -4,6 +4,7 @@ from collections import namedtuple
from pathlib import Path from pathlib import Path
import gradio as gr import gradio as gr
import gradio.components
from PIL import PngImagePlugin from PIL import PngImagePlugin
@ -31,13 +32,16 @@ def check_tmp_file(gradio, filename):
return False return False
def save_pil_to_file(pil_image, dir=None): def save_pil_to_file(self, pil_image, dir=None):
already_saved_as = getattr(pil_image, 'already_saved_as', None) already_saved_as = getattr(pil_image, 'already_saved_as', None)
if already_saved_as and os.path.isfile(already_saved_as): if already_saved_as and os.path.isfile(already_saved_as):
register_tmp_file(shared.demo, already_saved_as) register_tmp_file(shared.demo, already_saved_as)
filename = already_saved_as
file_obj = Savedfile(f'{already_saved_as}?{os.path.getmtime(already_saved_as)}') if not shared.opts.save_images_add_number:
return file_obj filename += f'?{os.path.getmtime(already_saved_as)}'
return filename
if shared.opts.temp_dir != "": if shared.opts.temp_dir != "":
dir = shared.opts.temp_dir dir = shared.opts.temp_dir
@ -51,11 +55,11 @@ def save_pil_to_file(pil_image, dir=None):
file_obj = tempfile.NamedTemporaryFile(delete=False, suffix=".png", dir=dir) file_obj = tempfile.NamedTemporaryFile(delete=False, suffix=".png", dir=dir)
pil_image.save(file_obj, pnginfo=(metadata if use_metadata else None)) pil_image.save(file_obj, pnginfo=(metadata if use_metadata else None))
return file_obj return file_obj.name
# override save to file function so that it also writes PNG info # override save to file function so that it also writes PNG info
gr.processing_utils.save_pil_to_file = save_pil_to_file gradio.components.IOComponent.pil_to_temp_file = save_pil_to_file
def on_tmpdir_changed(): def on_tmpdir_changed():