import importlib
import os
import platform
import shutil
import subprocess as sp
import sys
import gradio as gr
import modules
import modules.scripts as scripts
from modules import (
script_callbacks,
shared,
call_queue,
sd_samplers,
ui_prompt_styles,
sd_models,
)
from modules.call_queue import wrap_gradio_gpu_call
from modules.images import image_data
from modules.shared import opts
from modules.ui import (
ordered_ui_categories,
create_sampler_and_steps_selection,
switch_values_symbol,
create_override_settings_dropdown,
detect_image_size_symbol,
plaintext_to_html,
paste_symbol,
clear_prompt_symbol,
restore_progress_symbol,
)
from modules.ui_common import (
folder_symbol,
update_generation_info,
create_refresh_button,
)
from modules.ui_components import (
ResizeHandleRow,
FormRow,
ToolButton,
FormGroup,
InputAccordion,
)
from scripts import m2m_hook as patches
from scripts import m2m_util
from scripts import mov2mov
from scripts.mov2mov import scripts_mov2mov
from scripts.m2m_config import mov2mov_outpath_samples, mov2mov_output_dir
from scripts.movie_editor import MovieEditor
id_part = "mov2mov"
def save_video(video):
path = "logs/movies"
if not os.path.exists(path):
os.makedirs(path, exist_ok=True)
index = len([path for path in os.listdir(path) if path.endswith(".mp4")]) + 1
video_path = os.path.join(path, str(index).zfill(5) + ".mp4")
shutil.copyfile(video, video_path)
filename = os.path.relpath(video_path, path)
return gr.File.update(value=video_path, visible=True), plaintext_to_html(
f"Saved: {filename}"
)
class Toprow:
"""Creates a top row UI with prompts, generate button, styles, extra little buttons for things, and enables some functionality related to their operation"""
def create_output_panel(tabname, outdir):
def open_folder(f):
if not os.path.exists(f):
print(
f'Folder "{f}" does not exist. After you create an image, the folder will be created.'
)
return
elif not os.path.isdir(f):
print(
f"""
WARNING
An open_folder request was made with an argument that is not a folder.
This could be an error or a malicious attempt to run code on your computer.
Requested path was: {f}
""",
file=sys.stderr,
)
return
if not shared.cmd_opts.hide_ui_dir_config:
path = os.path.normpath(f)
if platform.system() == "Windows":
os.startfile(path)
elif platform.system() == "Darwin":
sp.Popen(["open", path])
elif "microsoft-standard-WSL2" in platform.uname().release:
sp.Popen(["wsl-open", path])
else:
sp.Popen(["xdg-open", path])
with gr.Column(variant="panel", elem_id=f"{tabname}_results"):
with gr.Group(elem_id=f"{tabname}_gallery_container"):
result_gallery = gr.Gallery(
label="Output",
show_label=False,
elem_id=f"{tabname}_gallery",
columns=4,
preview=True,
height=shared.opts.gallery_height or None,
)
result_video = gr.PlayableVideo(
label="Output Video", show_label=False, elem_id=f"{tabname}_video"
)
generation_info = None
with gr.Column():
with gr.Row(
elem_id=f"image_buttons_{tabname}", elem_classes="image-buttons"
):
open_folder_button = ToolButton(
folder_symbol,
elem_id=f"{tabname}_open_folder",
visible=not shared.cmd_opts.hide_ui_dir_config,
tooltip="Open images output directory.",
)
if tabname != "extras":
save = ToolButton(
"💾",
elem_id=f"save_{tabname}",
tooltip=f"Save the image to a dedicated directory ({shared.opts.outdir_save}).",
)
open_folder_button.click(
fn=lambda: open_folder(shared.opts.outdir_samples or outdir),
inputs=[],
outputs=[],
)
download_files = gr.File(
None,
file_count="multiple",
interactive=False,
show_label=False,
visible=False,
elem_id=f"download_files_{tabname}",
)
with gr.Group():
html_info = gr.HTML(
elem_id=f"html_info_{tabname}", elem_classes="infotext"
)
html_log = gr.HTML(
elem_id=f"html_log_{tabname}", elem_classes="html-log"
)
generation_info = gr.Textbox(
visible=False, elem_id=f"generation_info_{tabname}"
)
if tabname == "txt2img" or tabname == "img2img" or tabname == "mov2mov":
generation_info_button = gr.Button(
visible=False, elem_id=f"{tabname}_generation_info_button"
)
generation_info_button.click(
fn=update_generation_info,
_js="function(x, y, z){ return [x, y, selected_gallery_index()] }",
inputs=[generation_info, html_info, html_info],
outputs=[html_info, html_info],
show_progress=False,
)
save.click(
fn=call_queue.wrap_gradio_call(save_video),
inputs=[result_video],
outputs=[
download_files,
html_log,
],
show_progress=False,
)
return result_gallery, result_video, generation_info, html_info, html_log
def create_refiner():
with InputAccordion(
False, label="Refiner", elem_id=f"{id_part}_enable"
) as enable_refiner:
with gr.Row():
refiner_checkpoint = gr.Dropdown(
label="Checkpoint",
elem_id=f"{id_part}_checkpoint",
choices=sd_models.checkpoint_tiles(),
value="",
tooltip="switch to another model in the middle of generation",
)
create_refresh_button(
refiner_checkpoint,
sd_models.list_models,
lambda: {"choices": sd_models.checkpoint_tiles()},
f"{id_part}_checkpoint_refresh",
)
refiner_switch_at = gr.Slider(
value=0.8,
label="Switch at",
minimum=0.01,
maximum=1.0,
step=0.01,
elem_id=f"{id_part}_switch_at",
tooltip="fraction of sampling steps when the switch to refiner model should happen; 1=never, 0.5=switch in the middle of generation",
)
return enable_refiner, refiner_checkpoint, refiner_switch_at
import importlib import os import platform import shutil import subprocess as sp import sys
import gradio as gr
import modules import modules.scripts as scripts from modules import ( script_callbacks, shared, call_queue, sd_samplers, ui_prompt_styles, sd_models, ) from modules.call_queue import wrap_gradio_gpu_call from modules.images import image_data from modules.shared import opts from modules.ui import ( ordered_ui_categories, create_sampler_and_steps_selection, switch_values_symbol, create_override_settings_dropdown, detect_image_size_symbol, plaintext_to_html, paste_symbol, clear_prompt_symbol, restore_progress_symbol, ) from modules.ui_common import ( folder_symbol, update_generation_info, create_refresh_button, ) from modules.ui_components import ( ResizeHandleRow, FormRow, ToolButton, FormGroup, InputAccordion, ) from scripts import m2m_hook as patches from scripts import m2m_util from scripts import mov2mov from scripts.mov2mov import scripts_mov2mov from scripts.m2m_config import mov2mov_outpath_samples, mov2mov_output_dir from scripts.movie_editor import MovieEditor
id_part = "mov2mov"
def save_video(video): path = "logs/movies" if not os.path.exists(path): os.makedirs(path, exist_ok=True) index = len([path for path in os.listdir(path) if path.endswith(".mp4")]) + 1 video_path = os.path.join(path, str(index).zfill(5) + ".mp4") shutil.copyfile(video, video_path) filename = os.path.relpath(video_path, path) return gr.File.update(value=video_path, visible=True), plaintext_to_html( f"Saved: {filename}" )
class Toprow: """Creates a top row UI with prompts, generate button, styles, extra little buttons for things, and enables some functionality related to their operation"""
def create_output_panel(tabname, outdir): def open_folder(f): if not os.path.exists(f): print( f'Folder "{f}" does not exist. After you create an image, the folder will be created.' ) return elif not os.path.isdir(f): print( f""" WARNING An open_folder request was made with an argument that is not a folder. This could be an error or a malicious attempt to run code on your computer. Requested path was: {f} """, file=sys.stderr, ) return
def create_refiner(): with InputAccordion( False, label="Refiner", elem_id=f"{id_part}_enable" ) as enable_refiner: with gr.Row(): refiner_checkpoint = gr.Dropdown( label="Checkpoint", elem_id=f"{id_part}_checkpoint", choices=sd_models.checkpoint_tiles(), value="", tooltip="switch to another model in the middle of generation", ) create_refresh_button( refiner_checkpoint, sd_models.list_models, lambda: {"choices": sd_models.checkpoint_tiles()}, f"{id_part}_checkpoint_refresh", )
def on_ui_tabs(): scripts_mov2mov.initialize_scripts(is_img2img=True)
def calc_video_w_h(video, width, height): if not video: return width, height
def on_ui_settings(): section = ("mov2mov", "Mov2Mov") shared.opts.add_option( "mov2mov_outpath_samples", shared.OptionInfo( mov2mov_outpath_samples, "Mov2Mov output path for image", section=section ), ) shared.opts.add_option( "mov2mov_output_dir", shared.OptionInfo( mov2mov_output_dir, "Mov2Mov output path for video", section=section ), )
img2img_toprow: gr.Row = None
def block_context_init(self, *args, *kwargs): origin_block_context_init(self, args, **kwargs)
def on_app_reload(): global origin_block_context_init if origin_block_context_init: patches.undo(name, obj=gr.blocks.BlockContext, field="init") origin_block_context_init = None
origin_block_context_init = patches.patch( name, obj=gr.blocks.BlockContext, field="init", replacement=block_context_init, ) script_callbacks.on_before_reload(on_app_reload) script_callbacks.on_ui_settings(on_ui_settings)
script_callbacks.on_ui_tabs(on_ui_tabs)