|
| 1 | +import gradio as gr |
| 2 | +from easygui import msgbox |
| 3 | +import subprocess |
| 4 | +import os |
| 5 | +import shutil |
| 6 | +from .common_gui import get_folder_path, get_file_path |
| 7 | + |
| 8 | +folder_symbol = '\U0001f4c2' # 📂 |
| 9 | +refresh_symbol = '\U0001f504' # 🔄 |
| 10 | +save_style_symbol = '\U0001f4be' # 💾 |
| 11 | +document_symbol = '\U0001F4C4' # 📄 |
| 12 | + |
| 13 | +def convert_model(source_model_input, source_model_type, target_model_folder_input, target_model_name_input, target_model_type, target_save_precision_type): |
| 14 | + # Check for caption_text_input |
| 15 | + if source_model_type == "": |
| 16 | + msgbox("Invalid source model type") |
| 17 | + return |
| 18 | + |
| 19 | + # Check if source model exist |
| 20 | + if os.path.isfile(source_model_input): |
| 21 | + print('The provided source model is a file') |
| 22 | + elif os.path.isdir(source_model_input): |
| 23 | + print('The provided model is a folder') |
| 24 | + else: |
| 25 | + msgbox("The provided source model is neither a file nor a folder") |
| 26 | + return |
| 27 | + |
| 28 | + # Check if source model exist |
| 29 | + if os.path.isdir(target_model_folder_input): |
| 30 | + print('The provided model folder exist') |
| 31 | + else: |
| 32 | + msgbox("The provided target folder does not exist") |
| 33 | + return |
| 34 | + |
| 35 | + run_cmd = f'.\\venv\Scripts\python.exe "tools/convert_diffusers20_original_sd.py"' |
| 36 | + |
| 37 | + v1_models = [ |
| 38 | + 'runwayml/stable-diffusion-v1-5', |
| 39 | + 'CompVis/stable-diffusion-v1-4', |
| 40 | + ] |
| 41 | + |
| 42 | + # check if v1 models |
| 43 | + if str(source_model_type) in v1_models: |
| 44 | + print('SD v1 model specified. Setting --v1 parameter') |
| 45 | + run_cmd += ' --v1' |
| 46 | + else: |
| 47 | + print('SD v2 model specified. Setting --v2 parameter') |
| 48 | + run_cmd += ' --v2' |
| 49 | + |
| 50 | + if not target_save_precision_type == 'unspecified': |
| 51 | + run_cmd += f' --{target_save_precision_type}' |
| 52 | + |
| 53 | + if target_model_type == "diffuser": |
| 54 | + run_cmd += f' --reference_model="{source_model_type}"' |
| 55 | + |
| 56 | + run_cmd += f' "{source_model_input}"' |
| 57 | + |
| 58 | + if target_model_type == "diffuser": |
| 59 | + target_model_path = os.path.join(target_model_folder_input, target_model_name_input) |
| 60 | + run_cmd += f' "{target_model_path}"' |
| 61 | + else: |
| 62 | + target_model_path = os.path.join(target_model_folder_input, f'{target_model_name_input}.{target_model_type}') |
| 63 | + run_cmd += f' "{target_model_path}"' |
| 64 | + |
| 65 | + print(run_cmd) |
| 66 | + |
| 67 | + # Run the command |
| 68 | + subprocess.run(run_cmd) |
| 69 | + |
| 70 | + if not target_model_type == "diffuser": |
| 71 | + |
| 72 | + v2_models = ['stabilityai/stable-diffusion-2-1-base', |
| 73 | + 'stabilityai/stable-diffusion-2-base',] |
| 74 | + v_parameterization =[ |
| 75 | + 'stabilityai/stable-diffusion-2-1', |
| 76 | + 'stabilityai/stable-diffusion-2',] |
| 77 | + |
| 78 | + if str(source_model_type) in v2_models: |
| 79 | + inference_file = os.path.join(target_model_folder_input, f'{target_model_name_input}.yaml') |
| 80 | + print(f'Saving v2-inference.yaml as {inference_file}') |
| 81 | + shutil.copy( |
| 82 | + f'./v2_inference/v2-inference.yaml', |
| 83 | + f'{inference_file}', |
| 84 | + ) |
| 85 | + |
| 86 | + if str(source_model_type) in v_parameterization: |
| 87 | + inference_file = os.path.join(target_model_folder_input, f'{target_model_name_input}.yaml') |
| 88 | + print(f'Saving v2-inference-v.yaml as {inference_file}') |
| 89 | + shutil.copy( |
| 90 | + f'./v2_inference/v2-inference-v.yaml', |
| 91 | + f'{inference_file}', |
| 92 | + ) |
| 93 | + |
| 94 | +# parser = argparse.ArgumentParser() |
| 95 | +# parser.add_argument("--v1", action='store_true', |
| 96 | +# help='load v1.x model (v1 or v2 is required to load checkpoint) / 1.xのモデルを読み込む') |
| 97 | +# parser.add_argument("--v2", action='store_true', |
| 98 | +# help='load v2.0 model (v1 or v2 is required to load checkpoint) / 2.0のモデルを読み込む') |
| 99 | +# parser.add_argument("--fp16", action='store_true', |
| 100 | +# help='load as fp16 (Diffusers only) and save as fp16 (checkpoint only) / fp16形式で読み込み(Diffusers形式のみ対応)、保存する(checkpointのみ対応)') |
| 101 | +# parser.add_argument("--bf16", action='store_true', help='save as bf16 (checkpoint only) / bf16形式で保存する(checkpointのみ対応)') |
| 102 | +# parser.add_argument("--float", action='store_true', |
| 103 | +# help='save as float (checkpoint only) / float(float32)形式で保存する(checkpointのみ対応)') |
| 104 | +# parser.add_argument("--epoch", type=int, default=0, help='epoch to write to checkpoint / checkpointに記録するepoch数の値') |
| 105 | +# parser.add_argument("--global_step", type=int, default=0, |
| 106 | +# help='global_step to write to checkpoint / checkpointに記録するglobal_stepの値') |
| 107 | +# parser.add_argument("--reference_model", type=str, default=None, |
| 108 | +# help="reference model for schduler/tokenizer, required in saving Diffusers, copy schduler/tokenizer from this / scheduler/tokenizerのコピー元のDiffusersモデル、Diffusers形式で保存するときに必要") |
| 109 | + |
| 110 | +# parser.add_argument("model_to_load", type=str, default=None, |
| 111 | +# help="model to load: checkpoint file or Diffusers model's directory / 読み込むモデル、checkpointかDiffusers形式モデルのディレクトリ") |
| 112 | +# parser.add_argument("model_to_save", type=str, default=None, |
| 113 | +# help="model to save: checkpoint (with extension) or Diffusers model's directory (without extension) / 変換後のモデル、拡張子がある場合はcheckpoint、ない場合はDiffusesモデルとして保存") |
| 114 | + |
| 115 | + |
| 116 | +### |
| 117 | +# Gradio UI |
| 118 | +### |
| 119 | + |
| 120 | + |
| 121 | +def gradio_convert_model_tab(): |
| 122 | + with gr.Tab('Convert model'): |
| 123 | + gr.Markdown( |
| 124 | + 'This utility can be used to convert from one stable diffusion model format to another.' |
| 125 | + ) |
| 126 | + with gr.Row(): |
| 127 | + source_model_input = gr.Textbox( |
| 128 | + label='Source model', |
| 129 | + placeholder='path to source model folder of file to convert...', |
| 130 | + interactive=True, |
| 131 | + ) |
| 132 | + button_source_model_dir = gr.Button( |
| 133 | + folder_symbol, elem_id='open_folder_small' |
| 134 | + ) |
| 135 | + button_source_model_dir.click( |
| 136 | + get_folder_path, outputs=source_model_input |
| 137 | + ) |
| 138 | + |
| 139 | + button_source_model_file = gr.Button( |
| 140 | + document_symbol, elem_id='open_folder_small' |
| 141 | + ) |
| 142 | + button_source_model_file.click( |
| 143 | + get_file_path, inputs=[source_model_input], outputs=source_model_input |
| 144 | + ) |
| 145 | + |
| 146 | + source_model_type = gr.Dropdown(label="Source model type", choices=[ |
| 147 | + 'stabilityai/stable-diffusion-2-1-base', |
| 148 | + 'stabilityai/stable-diffusion-2-base', |
| 149 | + 'stabilityai/stable-diffusion-2-1', |
| 150 | + 'stabilityai/stable-diffusion-2', |
| 151 | + 'runwayml/stable-diffusion-v1-5', |
| 152 | + 'CompVis/stable-diffusion-v1-4', |
| 153 | + ],) |
| 154 | + with gr.Row(): |
| 155 | + target_model_folder_input = gr.Textbox( |
| 156 | + label='Target model folder', |
| 157 | + placeholder='path to target model folder of file name to create...', |
| 158 | + interactive=True, |
| 159 | + ) |
| 160 | + button_target_model_folder = gr.Button( |
| 161 | + folder_symbol, elem_id='open_folder_small' |
| 162 | + ) |
| 163 | + button_target_model_folder.click( |
| 164 | + get_folder_path, outputs=target_model_folder_input |
| 165 | + ) |
| 166 | + |
| 167 | + target_model_name_input = gr.Textbox( |
| 168 | + label='Target model name', |
| 169 | + placeholder='target model name...', |
| 170 | + interactive=True, |
| 171 | + ) |
| 172 | + target_model_type = gr.Dropdown(label="Target model type", choices=[ |
| 173 | + 'diffuser', |
| 174 | + 'ckpt', |
| 175 | + 'safetensors', |
| 176 | + ],) |
| 177 | + target_save_precision_type = gr.Dropdown(label="Target model precison", choices=[ |
| 178 | + 'unspecified', |
| 179 | + 'fp16', |
| 180 | + 'bf16', |
| 181 | + 'float' |
| 182 | + ], value='unspecified') |
| 183 | + |
| 184 | + |
| 185 | + convert_button = gr.Button('Convert model') |
| 186 | + |
| 187 | + convert_button.click( |
| 188 | + convert_model, |
| 189 | + inputs=[source_model_input, source_model_type, target_model_folder_input, target_model_name_input, target_model_type, target_save_precision_type |
| 190 | + ], |
| 191 | + ) |
0 commit comments