initial commit

This commit is contained in:
2025-10-21 23:45:31 +07:00
commit 37ab75f8bf
30 changed files with 5207 additions and 0 deletions

5
reactor_ui/__init__.py Executable file
View File

@@ -0,0 +1,5 @@
import reactor_ui.reactor_upscale_ui as ui_upscale
import reactor_ui.reactor_tools_ui as ui_tools
import reactor_ui.reactor_settings_ui as ui_settings
import reactor_ui.reactor_main_ui as ui_main
import reactor_ui.reactor_detection_ui as ui_detection

View File

@@ -0,0 +1,54 @@
import gradio as gr
from scripts.reactor_swapper import (
clear_faces,
clear_faces_list,
clear_faces_target,
clear_faces_all
)
# TAB DETECTION
def show(show_br: bool = True):
with gr.Tab("Detection"):
with gr.Row():
det_thresh = gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.5,
step=0.01,
label="Threshold",
info="The higher the value, the more sensitive the detection is to what is considered a face (0.5 by default)",
scale=2
)
det_maxnum = gr.Slider(
minimum=0,
maximum=20,
value=0,
step=1,
label="Max Faces",
info="Maximum number of faces to detect (0 is unlimited)",
scale=1
)
# gr.Markdown("<br>", visible=show_br)
gr.Markdown("Hashed images get processed with previously set detection parameters (the face is hashed with all available parameters to bypass the analyzer and speed up the process). Please clear the hash if you want to apply new detection settings.", visible=show_br)
with gr.Row():
imgs_hash_clear_single = gr.Button(
value="Clear Source Images Hash (Single)",
scale=1
)
imgs_hash_clear_multiple = gr.Button(
value="Clear Source Images Hash (Multiple)",
scale=1
)
imgs_hash_clear_target = gr.Button(
value="Clear Target Image Hash",
scale=1
)
imgs_hash_clear_all = gr.Button(
value="Clear All Hash"
)
progressbar_area = gr.Markdown("")
imgs_hash_clear_single.click(clear_faces,None,[progressbar_area])
imgs_hash_clear_multiple.click(clear_faces_list,None,[progressbar_area])
imgs_hash_clear_target.click(clear_faces_target,None,[progressbar_area])
imgs_hash_clear_all.click(clear_faces_all,None,[progressbar_area])
return det_thresh, det_maxnum

229
reactor_ui/reactor_main_ui.py Executable file
View File

@@ -0,0 +1,229 @@
import gradio as gr
from scripts.reactor_helpers import (
get_model_names,
get_facemodels
)
from scripts.reactor_swapper import (
clear_faces_list,
)
from modules import shared
# SAVE_ORIGINAL: bool = False
def update_fm_list(selected: str):
try: # GR3.x
return gr.Dropdown.update(
value=selected, choices=get_model_names(get_facemodels)
)
except: # GR4.x
return gr.Dropdown(
value=selected, choices=get_model_names(get_facemodels)
)
# TAB MAIN
def show(is_img2img: bool, show_br: bool = True, **msgs):
# def on_select_source(selected: bool, evt: gr.SelectData):
def on_select_source(evt: gr.SelectData):
# global SAVE_ORIGINAL
if evt.index == 2:
# if SAVE_ORIGINAL != selected:
# SAVE_ORIGINAL = selected
try: # GR3.x
return {
control_col_1: gr.Column.update(visible=False),
control_col_2: gr.Column.update(visible=False),
control_col_3: gr.Column.update(visible=True),
# save_original: gr.Checkbox.update(value=False,visible=False),
imgs_hash_clear: gr.Button.update(visible=True)
}
except: # GR4.x
return {
control_col_1: gr.Column(visible=False),
control_col_2: gr.Column(visible=False),
control_col_3: gr.Column(visible=True),
# save_original: gr.Checkbox.update(value=False,visible=False),
imgs_hash_clear: gr.Button(visible=True)
}
if evt.index == 0:
try: # GR3.x
return {
control_col_1: gr.Column.update(visible=True),
control_col_2: gr.Column.update(visible=False),
control_col_3: gr.Column.update(visible=False),
# save_original: gr.Checkbox.update(value=SAVE_ORIGINAL,visible=show_br),
imgs_hash_clear: gr.Button.update(visible=False)
}
except: # GR4.x
return {
control_col_1: gr.Column(visible=True),
control_col_2: gr.Column(visible=False),
control_col_3: gr.Column(visible=False),
# save_original: gr.Checkbox.update(value=SAVE_ORIGINAL,visible=show_br),
imgs_hash_clear: gr.Button(visible=False)
}
if evt.index == 1:
try: # GR3.x
return {
control_col_1: gr.Column.update(visible=False),
control_col_2: gr.Column.update(visible=True),
control_col_3: gr.Column.update(visible=False),
# save_original: gr.Checkbox.update(value=SAVE_ORIGINAL,visible=show_br),
imgs_hash_clear: gr.Button.update(visible=False)
}
except: # GR4.x
return {
control_col_1: gr.Column(visible=False),
control_col_2: gr.Column(visible=True),
control_col_3: gr.Column(visible=False),
# save_original: gr.Checkbox.update(value=SAVE_ORIGINAL,visible=show_br),
imgs_hash_clear: gr.Button(visible=False)
}
progressbar_area = gr.Markdown("")
with gr.Tab("Main"):
with gr.Column():
with gr.Row():
select_source = gr.Radio(
["Image(s)","Face Model","Folder"],
value="Image(s)",
label="Select Source",
type="index",
scale=1,
)
with gr.Column(visible=False) as control_col_2:
with gr.Row():
face_models = get_model_names(get_facemodels)
face_model = gr.Dropdown(
choices=face_models,
label="Choose Face Model",
value="None",
scale=1,
)
fm_update = gr.Button(
value="🔄",
variant="tool",
)
fm_update.click(
update_fm_list,
inputs=[face_model],
outputs=[face_model],
)
imgs_hash_clear = gr.Button(
value="Clear Source Images Hash",
scale=1,
visible=False,
)
imgs_hash_clear.click(clear_faces_list,None,[progressbar_area])
gr.Markdown("<br>", visible=show_br)
with gr.Column(visible=True) as control_col_1:
with gr.Row():
selected_tab = gr.Textbox('tab_single', visible=False)
with gr.Tabs() as tab_single:
with gr.Tab('Single'):
img = gr.Image(
type="pil",
label="Single Source Image",
)
with gr.Tab('Multiple') as tab_multiple:
imgs = gr.Files(
label=f"Multiple Source Images{msgs['extra_multiple_source']}",
file_types=["image"],
)
tab_single.select(fn=lambda: 'tab_single', inputs=[], outputs=[selected_tab])
tab_multiple.select(fn=lambda: 'tab_multiple', inputs=[], outputs=[selected_tab])
with gr.Column(visible=False) as control_col_3:
gr.Markdown("<span style='display:block;text-align:right;padding-right:3px;margin: -15px 0;font-size:1.1em'><sup>Clear Hash if you see the previous face was swapped instead of the new one</sup></span>")
with gr.Row():
source_folder = gr.Textbox(
value="",
placeholder="Paste here the path to the folder containing source faces images",
label=f"Source Folder{msgs['extra_multiple_source']}",
scale=2,
)
random_image = gr.Checkbox(
False,
label="Random Image",
info="Randomly select an image from the path",
scale=1,
)
setattr(face_model, "do_not_save_to_config", True)
if is_img2img:
save_original = gr.Checkbox(
False,
label="Save Original (Swap in generated only)",
info="Save the original image(s) made before swapping"
)
else:
save_original = gr.Checkbox(
False,
label="Save Original",
info="Save the original image(s) made before swapping",
visible=show_br
)
# imgs.upload(on_files_upload_uncheck_so,[save_original],[save_original],show_progress=False)
# imgs.clear(on_files_clear,None,[save_original],show_progress=False)
imgs.clear(clear_faces_list,None,None,show_progress=False)
mask_face = gr.Checkbox(
False,
label="Face Mask Correction",
info="Apply this option if you see some pixelation around face contours"
)
gr.Markdown("<br>", visible=show_br)
gr.Markdown("Source Image (above):")
with gr.Row():
source_faces_index = gr.Textbox(
value="0",
placeholder="Which face(s) to use as Source (comma separated)",
label="Comma separated face number(s); Example: 0,2,1",
)
gender_source = gr.Radio(
["No", "Female Only", "Male Only"],
value="No",
label="Gender Detection (Source)",
type="index",
)
gr.Markdown("<br>", visible=show_br)
gr.Markdown("Target Image (result):")
with gr.Row():
faces_index = gr.Textbox(
value="0",
placeholder="Which face(s) to Swap into Target (comma separated)",
label="Comma separated face number(s); Example: 1,0,2",
)
gender_target = gr.Radio(
["No", "Female Only", "Male Only"],
value="No",
label="Gender Detection (Target)",
type="index",
)
gr.Markdown("<br>", visible=show_br)
with gr.Row():
face_restorer_name = gr.Radio(
label="Restore Face",
choices=["None"] + [x.name() for x in shared.face_restorers],
value=shared.face_restorers[0].name(),
type="value",
)
with gr.Column():
face_restorer_visibility = gr.Slider(
0, 1, 1, step=0.1, label="Restore Face Visibility"
)
codeformer_weight = gr.Slider(
0, 1, 0.5, step=0.1, label="CodeFormer Weight (Fidelity)", info="0 = far from original (max restoration), 1 = close to original (min restoration)"
)
gr.Markdown("<br>", visible=show_br)
swap_in_source = gr.Checkbox(
False,
label="Swap in source image",
visible=is_img2img,
)
swap_in_generated = gr.Checkbox(
True,
label="Swap in generated image",
visible=is_img2img,
)
# select_source.select(on_select_source,[save_original],[control_col_1,control_col_2,control_col_3,save_original,imgs_hash_clear],show_progress=False)
select_source.select(on_select_source,None,[control_col_1,control_col_2,control_col_3,imgs_hash_clear],show_progress=False)
return img, imgs, selected_tab, select_source, face_model, source_folder, save_original, mask_face, source_faces_index, gender_source, faces_index, gender_target, face_restorer_name, face_restorer_visibility, codeformer_weight, swap_in_source, swap_in_generated, random_image

View File

@@ -0,0 +1,77 @@
import gradio as gr
from scripts.reactor_logger import logger
from scripts.reactor_helpers import get_models, set_Device
from scripts.reactor_globals import DEVICE, DEVICE_LIST
try:
import torch.cuda as cuda
EP_is_visible = True if cuda.is_available() else False
except:
EP_is_visible = False
def update_models_list(selected: str):
return gr.Dropdown.update(
value=selected, choices=get_models()
)
def show(hash_check_block: bool = True):
# TAB SETTINGS
with gr.Tab("Settings"):
models = get_models()
with gr.Row(visible=EP_is_visible):
device = gr.Radio(
label="Execution Provider",
choices=DEVICE_LIST,
value=DEVICE,
type="value",
info="Click 'Save' to apply. If you already run 'Generate' - RESTART is required: (A1111) Extensions Tab -> 'Apply and restart UI' or (SD.Next) close the Server and start it again",
scale=2,
)
save_device_btn = gr.Button("Save", scale=0)
save = gr.Markdown("", visible=EP_is_visible)
setattr(device, "do_not_save_to_config", True)
save_device_btn.click(
set_Device,
inputs=[device],
outputs=[save],
)
with gr.Row():
if len(models) == 0:
logger.warning(
"You should at least have one model in models directory, please read the doc here: https://github.com/Gourieff/sd-webui-reactor/"
)
model = gr.Dropdown(
choices=models,
label="Model not found, please download one and refresh the list"
)
else:
model = gr.Dropdown(
choices=models, label="Model", value=models[0]
)
models_update = gr.Button(
value="🔄",
variant="tool",
)
models_update.click(
update_models_list,
inputs=[model],
outputs=[model],
)
console_logging_level = gr.Radio(
["No log", "Minimum", "Default"],
value="Minimum",
label="Console Log Level",
type="index"
)
gr.Markdown("<br>", visible=hash_check_block)
with gr.Row(visible=hash_check_block):
source_hash_check = gr.Checkbox(
True,
label="Source Image Hash Check",
info="Recommended to keep it ON. Processing is faster when Source Image is the same."
)
target_hash_check = gr.Checkbox(
False,
label="Target Image Hash Check",
info="Affects if you use Extras tab or img2img with only 'Swap in source image' on."
)
return model, device, console_logging_level, source_hash_check, target_hash_check

61
reactor_ui/reactor_tools_ui.py Executable file
View File

@@ -0,0 +1,61 @@
import gradio as gr
from scripts.reactor_swapper import build_face_model, blend_faces
# TAB TOOLS
def show():
with gr.Tab("Tools"):
with gr.Tab("Face Models"):
with gr.Tab("Single"):
gr.Markdown("Load an image containing one person, name it and click 'Build and Save'")
img_fm = gr.Image(
type="pil",
label="Load an Image to build -Face Model-",
)
with gr.Row(equal_height=True):
fm_name = gr.Textbox(
value="",
placeholder="Please type any name (e.g. Elena)",
label="Face Model Name",
)
save_fm_btn = gr.Button("Build and Save")
save_fm = gr.Markdown("You can find saved models in 'models/reactor/faces'")
save_fm_btn.click(
build_face_model,
inputs=[img_fm, fm_name],
outputs=[save_fm],
)
with gr.Tab("Blend"):
gr.Markdown("Load a set of images containing any person, name it and click 'Build and Save'")
with gr.Row():
imgs_fm = gr.Files(
label=f"Load Images to build -Blended Face Model-",
file_types=["image"]
)
with gr.Column():
compute_method = gr.Radio(
["Mean", "Median", "Mode"],
value="Mean",
label="Compute Method",
type="index",
info="Mean (recommended) - Average value (best result 👍); Median* - Mid-point value (may be funny 😅); Mode - Most common value (may be scary 😨); *Mean and Median will be similar if you load two images"
)
shape_check = gr.Checkbox(
False,
label="Check -Embedding Shape- on Similarity",
info="(Experimental) Turn it ON if you want to skip the faces which are too much different from the first one in the list to prevent some probable 'shape mismatches'"
)
with gr.Row(equal_height=True):
fm_name = gr.Textbox(
value="",
placeholder="Please type any name (e.g. Elena)",
label="Face Model Name",
)
save_fm_btn = gr.Button("Build and Save")
save_fm = gr.Markdown("You can find saved models in 'models/reactor/faces'")
save_fm_btn.click(
blend_faces,
inputs=[imgs_fm, fm_name, compute_method, shape_check],
outputs=[save_fm],
)

View File

@@ -0,0 +1,47 @@
import gradio as gr
from modules import shared
def update_upscalers_list(selected: str):
return gr.Dropdown.update(
value=selected, choices=[upscaler.name for upscaler in shared.sd_upscalers]
)
# TAB UPSCALE
def show(show_br: bool = True):
with gr.Tab("Upscale"):
with gr.Row():
restore_first = gr.Checkbox(
True,
label="1. Restore Face -> 2. Upscale (-Uncheck- if you want vice versa)",
info="Postprocessing Order",
scale=2
)
upscale_force = gr.Checkbox(
False,
label="Force Upscale",
info="Upscale anyway - even if no face found",
scale=1
)
with gr.Row():
upscaler_name = gr.Dropdown(
choices=[upscaler.name for upscaler in shared.sd_upscalers],
label="Upscaler",
value="None",
info="Won't scale if you choose -Swap in Source- via img2img, only 1x-postprocessing will affect (texturing, denoising, restyling etc.)"
)
upscalers_update = gr.Button(
value="🔄",
variant="tool",
)
upscalers_update.click(
update_upscalers_list,
inputs=[upscaler_name],
outputs=[upscaler_name],
)
gr.Markdown("<br>", visible=show_br)
with gr.Row():
upscaler_scale = gr.Slider(1, 8, 1, step=0.1, label="Scale by")
upscaler_visibility = gr.Slider(
0, 1, 1, step=0.1, label="Upscaler Visibility (if scale = 1)"
)
return restore_first, upscaler_name, upscaler_scale, upscaler_visibility, upscale_force