File size: 3,082 Bytes
e77daac
 
 
 
 
8f04209
e77daac
 
 
 
 
 
 
 
 
 
8f04209
 
e77daac
 
 
 
 
 
 
 
8f04209
 
e77daac
8f04209
e77daac
 
 
 
 
 
 
8f04209
 
 
e77daac
 
 
 
8f04209
 
 
e77daac
 
8f04209
e77daac
 
 
 
 
 
8f04209
e77daac
021739f
84fd283
161310e
f31ff19
8f04209
e77daac
 
 
8f04209
e77daac
 
 
8f04209
e77daac
 
 
 
 
 
 
 
 
8f04209
e77daac
 
 
 
8f04209
e77daac
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import os
import tempfile
from collections import namedtuple
from pathlib import Path

import gradio as gr

from PIL import PngImagePlugin

from modules import shared


Savedfile = namedtuple("Savedfile", ["name"])


def register_tmp_file(gradio, filename):
    print(f'第一处gradio:{gradio}')
    print(f'第一处filename:{filename}')
    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(gradio, 'temp_dirs'):  # gradio 3.9
        gradio.temp_dirs = gradio.temp_dirs | {os.path.abspath(os.path.dirname(filename))}


def check_tmp_file(gradio, filename):
    print(f'第二处gradio:{gradio}')
    print(f'第二处filename:{filename}')
    if hasattr(gradio, 'temp_file_sets'):
        return any([filename in fileset for fileset in gradio.temp_file_sets])

    if hasattr(gradio, 'temp_dirs'):
        return any(Path(temp_dir).resolve() in Path(filename).resolve().parents for temp_dir in gradio.temp_dirs)

    return False


def save_pil_to_file(pil_image, dir=None):
    print(f'pil_image:{pil_image}')
    #print(f'shared.demo:{shared.demo}')
    already_saved_as = getattr(pil_image, 'already_saved_as', None)
    if already_saved_as and os.path.isfile(already_saved_as):
        register_tmp_file(shared.demo, already_saved_as)

        file_obj = Savedfile(already_saved_as)
        return file_obj
    #print(f'第一处file_obj:{file_obj}')
    if shared.opts.temp_dir != "":
        dir = shared.opts.temp_dir
    print(f'shared.opts.temp_dir:{shared.opts.temp_dir}')
    use_metadata = False
    metadata = PngImagePlugin.PngInfo()
    for key, value in pil_image.info.items():
        if isinstance(key, str) and isinstance(value, str):
            metadata.add_text(key, value)
            use_metadata = True
    print(f'dir:{dir}')
    file_obj = tempfile.NamedTemporaryFile(delete=False, suffix=".png", dir=dir)
    print(f'file_obj.name:{file_obj.name}')
    print(f'file_obj:{file_obj}')
    #pil_image.save(file_obj, pnginfo=(metadata if use_metadata else None))
    file_obj.name = './https://huggingface.co/datasets/xiemoxiaoshaso/image/blob/main/00008-1547306.jpg'
    return file_obj


# override save to file function so that it also writes PNG info
gr.processing_utils.save_pil_to_file = save_pil_to_file


def on_tmpdir_changed():
    print(f'shared.opts.temp_dir:{shared.opts.temp_dir}')
    if shared.opts.temp_dir == "" or shared.demo is None:
        return

    os.makedirs(shared.opts.temp_dir, exist_ok=True)

    register_tmp_file(shared.demo, os.path.join(shared.opts.temp_dir, "x"))


def cleanup_tmpdr():
    print(f'temp_dir:{temp_dir}')
    temp_dir = shared.opts.temp_dir
    if temp_dir == "" or not os.path.isdir(temp_dir):
        return

    for root, dirs, files in os.walk(temp_dir, topdown=False):
        for name in files:
            _, extension = os.path.splitext(name)
            if extension != ".png":
                continue

            filename = os.path.join(root, name)
            os.remove(filename)