File size: 9,985 Bytes
50e6701
 
 
 
 
 
 
 
 
5cceefd
 
cd0046c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5cceefd
198f51a
 
5cceefd
 
198f51a
 
 
fd88f27
19c9f55
 
50e6701
bcedd61
a5347f3
 
 
 
 
50e6701
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import os
import random
import sys
from typing import Sequence, Mapping, Any, Union
import torch
from PIL import Image
from huggingface_hub import hf_hub_download
import spaces

import subprocess, sys

# ---------------------------------------------------------------------------------
# 🛠️ Monkey-patch для gradio_client: игнорируем булевы схемы и не падаем на TypeError
# ---------------------------------------------------------------------------------
import gradio_client.utils as _gc_utils

# Сохраняем оригинальные функции
_orig_js2pt = _gc_utils._json_schema_to_python_type
_orig_get_type = _gc_utils.get_type

def _safe_json_schema_to_python_type(schema, defs=None):
    """
    Если schema — bool (True/False), возвращаем 'Any',
    иначе — вызываем оригинальный код.
    """
    if isinstance(schema, bool):
        return "Any"
    return _orig_js2pt(schema, defs)

def _safe_get_type(schema):
    """
    Если schema — bool, возвращаем 'Any',
    иначе — вызываем оригинальную функцию get_type.
    """
    if isinstance(schema, bool):
        return "Any"
    return _orig_get_type(schema)

# Заменяем в модуле
_gc_utils._json_schema_to_python_type = _safe_json_schema_to_python_type
_gc_utils.get_type               = _safe_get_type
# ---------------------------------------------------------------------------------

# Дальше уже можно безопасно импортировать Gradio

import gradio
import gradio_client
import gradio as gr

print("gradio version:", gradio.__version__)
print("gradio_client version:", gradio_client.__version__)

hf_hub_download(repo_id="ezioruan/inswapper_128.onnx", filename="inswapper_128.onnx", local_dir="models/insightface")
hf_hub_download(repo_id="martintomov/comfy", filename="facerestore_models/GPEN-BFR-512.onnx", local_dir="models/facerestore_models")
# hf_hub_download(repo_id="Gourieff/ReActor", filename="models/facerestore_models/GPEN-BFR-512.onnx", local_dir="models/facerestore_models")
hf_hub_download(repo_id="darkeril/collection", filename="detection_Resnet50_Final.pth", local_dir="models/facedetection")
hf_hub_download(repo_id="gmk123/GFPGAN", filename="parsing_parsenet.pth", local_dir="models/facedetection")
hf_hub_download(repo_id="MonsterMMORPG/tools", filename="1k3d68.onnx", local_dir="models/insightface/models/buffalo_l")
hf_hub_download(repo_id="MonsterMMORPG/tools", filename="2d106det.onnx", local_dir="models/insightface/models/buffalo_l")
hf_hub_download(repo_id="maze/faceX", filename="det_10g.onnx", local_dir="models/insightface/models/buffalo_l")
hf_hub_download(repo_id="typhoon01/aux_models", filename="genderage.onnx", local_dir="models/insightface/models/buffalo_l")
hf_hub_download(repo_id="maze/faceX", filename="w600k_r50.onnx", local_dir="models/insightface/models/buffalo_l")


def get_value_at_index(obj: Union[Sequence, Mapping], index: int) -> Any:
    """Returns the value at the given index of a sequence or mapping.

    If the object is a sequence (like list or string), returns the value at the given index.
    If the object is a mapping (like a dictionary), returns the value at the index-th key.

    Some return a dictionary, in these cases, we look for the "results" key

    Args:
        obj (Union[Sequence, Mapping]): The object to retrieve the value from.
        index (int): The index of the value to retrieve.

    Returns:
        Any: The value at the given index.

    Raises:
        IndexError: If the index is out of bounds for the object and the object is not a mapping.
    """
    try:
        return obj[index]
    except KeyError:
        return obj["result"][index]


def find_path(name: str, path: str = None) -> str:
    """
    Recursively looks at parent folders starting from the given path until it finds the given name.
    Returns the path as a Path object if found, or None otherwise.
    """
    # If no path is given, use the current working directory
    if path is None:
        path = os.getcwd()

    # Check if the current directory contains the name
    if name in os.listdir(path):
        path_name = os.path.join(path, name)
        print(f"{name} found: {path_name}")
        return path_name

    # Get the parent directory
    parent_directory = os.path.dirname(path)

    # If the parent directory is the same as the current directory, we've reached the root and stop the search
    if parent_directory == path:
        return None

    # Recursively call the function with the parent directory
    return find_path(name, parent_directory)


def add_comfyui_directory_to_sys_path() -> None:
    """
    Add 'ComfyUI' to the sys.path
    """
    comfyui_path = find_path("ComfyUI")
    if comfyui_path is not None and os.path.isdir(comfyui_path):
        sys.path.append(comfyui_path)
        print(f"'{comfyui_path}' added to sys.path")


def add_extra_model_paths() -> None:
    """
    Parse the optional extra_model_paths.yaml file and add the parsed paths to the sys.path.
    """
    try:
        from main import load_extra_path_config
    except ImportError:
        print(
            "Could not import load_extra_path_config from main.py. Looking in utils.extra_config instead."
        )
        from utils.extra_config import load_extra_path_config

    extra_model_paths = find_path("extra_model_paths.yaml")

    if extra_model_paths is not None:
        load_extra_path_config(extra_model_paths)
    else:
        print("Could not find the extra_model_paths config file.")


add_comfyui_directory_to_sys_path()
add_extra_model_paths()


def import_custom_nodes() -> None:
    """Find all custom nodes in the custom_nodes folder and add those node objects to NODE_CLASS_MAPPINGS

    This function sets up a new asyncio event loop, initializes the PromptServer,
    creates a PromptQueue, and initializes the custom nodes.
    """
    import asyncio
    import execution
    from nodes import init_extra_nodes
    import server

    # Creating a new event loop and setting it as the default loop
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)

    # Creating an instance of PromptServer with the loop
    server_instance = server.PromptServer(loop)
    execution.PromptQueue(server_instance)

    # Initializing custom nodes
    init_extra_nodes()

import_custom_nodes()
from nodes import NODE_CLASS_MAPPINGS

@spaces.GPU(duration=20)
def generate_image(source_image, target_image, restore_strength, target_index):
    with torch.inference_mode():
        loadimage = NODE_CLASS_MAPPINGS["LoadImage"]()
        loadimage_1 = loadimage.load_image(image=target_image)

        loadimage_3 = loadimage.load_image(image=source_image)

        reactorfaceswap = NODE_CLASS_MAPPINGS["ReActorFaceSwap"]()
        saveimage = NODE_CLASS_MAPPINGS["SaveImage"]()

        reactorfaceswap_2 = reactorfaceswap.execute(
            enabled=True,
            swap_model="inswapper_128.onnx",
            facedetection="retinaface_resnet50",
            face_restore_model="GPEN-BFR-512.onnx",
            face_restore_visibility=restore_strength,
            codeformer_weight=0.5,
            detect_gender_input="no",
            detect_gender_source="no",
            input_faces_index=str(target_index),  # Преобразуем в строку
            source_faces_index="0",
            console_log_level=1,
            input_image=get_value_at_index(loadimage_1, 0),
            source_image=get_value_at_index(loadimage_3, 0),
        )

        saveimage_4 = saveimage.save_images(
            filename_prefix="ComfyUI",
            images=get_value_at_index(reactorfaceswap_2, 0),
        )

        saved_path = f"output/{saveimage_4['ui']['images'][0]['filename']}"
        return saved_path

if __name__ == "__main__":
     with gr.Blocks() as app:
        # Add a title
        gr.Markdown("# ComfyUI Reactor Fast Face Swap")
        gr.Markdown("ComfyUI Reactor Fast Face Swap running directly on Gradio. - [How to convert your any ComfyUI workflow to Gradio](https://huggingface.co/blog/run-comfyui-workflows-on-spaces)")
        with gr.Row():
            with gr.Column():
                # Add an input
                # prompt_input = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...")
                # Add a `Row` to include the groups side by side
                with gr.Row():
                    # First group includes structure image and depth strength
                    with gr.Group():
                        source_image = gr.Image(label="Source Image", type="filepath")
                     #   depth_strength = gr.Slider(minimum=0, maximum=50, value=15, label="Depth Strength")
                    # Second group includes style image and style strength
                    with gr.Group():
                        target_image = gr.Image(label="Target Image", type="filepath")
                        restore_strength = gr.Slider(minimum=0, maximum=1, step=0.05, value=0.7, label="Face Restore Strength")
                        target_index = gr.Dropdown(choices=[0, 1, 2, 3, 4], value=0, label="Target Face Index")
                        gr.Markdown("Index_0 = Largest Face. To switch for another target face - switch to Index_1, e.t.c")

                # The generate button
                generate_btn = gr.Button("Generate")

            with gr.Column():
                # The output image
                output_image = gr.Image(label="Generated Image")

            # When clicking the button, it will trigger the `generate_image` function, with the respective inputs
            # and the output an image
            generate_btn.click(
                fn=generate_image,
                inputs=[source_image, target_image, restore_strength, target_index],
                outputs=[output_image]
            )
        app.launch(share=True)