patrickvonplaten commited on
Commit
ae3b68e
1 Parent(s): 0b5e97e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -2
app.py CHANGED
@@ -4,6 +4,8 @@ from huggingface_hub import HfApi, upload_folder
4
  import gradio as gr
5
  import hf_utils
6
  import utils
 
 
7
 
8
  subprocess.run(["git", "clone", "https://github.com/huggingface/diffusers", "diffs"])
9
 
@@ -40,7 +42,7 @@ def get_ckpt_names(token, radio_model_names, input_model):
40
  except Exception as e:
41
  return error_str(e), gr.update(choices=[]), None
42
 
43
- def convert_and_push(radio_model_names, input_model, ckpt_name, sd_version, token, path_in_repo, ema):
44
  extract_ema = ema == "ema"
45
 
46
  if sd_version == None:
@@ -54,6 +56,16 @@ def convert_and_push(radio_model_names, input_model, ckpt_name, sd_version, toke
54
  # 1. Download the checkpoint file
55
  ckpt_path, revision = hf_utils.download_file(repo_id=model_id, filename=ckpt_name, token=token)
56
 
 
 
 
 
 
 
 
 
 
 
57
  # 2. Run the conversion script
58
  os.makedirs(model_id, exist_ok=True)
59
  run_command = [
@@ -133,6 +145,7 @@ with gr.Blocks() as demo:
133
  radio_ckpts = gr.Radio(label="Choose the checkpoint to convert", visible=False)
134
  path_in_repo = gr.Textbox(label="Path where the weights will be saved", placeholder="Leave empty for root folder")
135
  ema = gr.Radio(label="Extract EMA or non-EMA?", choices=["ema", "non-ema"])
 
136
  radio_sd_version = gr.Radio(label="Choose the model version", choices=["v1", "v2", "v2.1"])
137
  gr.Markdown("Conversion may take a few minutes.")
138
  btn_convert = gr.Button("Convert & Push")
@@ -163,7 +176,7 @@ with gr.Blocks() as demo:
163
 
164
  btn_convert.click(
165
  fn=convert_and_push,
166
- inputs=[radio_model_names, input_model, radio_ckpts, radio_sd_version, input_token, path_in_repo, ema],
167
  outputs=error_output,
168
  scroll_to_output=True
169
  )
 
4
  import gradio as gr
5
  import hf_utils
6
  import utils
7
+ from safetensors import safe_open
8
+ from safetensors.torch import save_file
9
 
10
  subprocess.run(["git", "clone", "https://github.com/huggingface/diffusers", "diffs"])
11
 
 
42
  except Exception as e:
43
  return error_str(e), gr.update(choices=[]), None
44
 
45
+ def convert_and_push(radio_model_names, input_model, ckpt_name, sd_version, token, path_in_repo, ema, safetensors):
46
  extract_ema = ema == "ema"
47
 
48
  if sd_version == None:
 
56
  # 1. Download the checkpoint file
57
  ckpt_path, revision = hf_utils.download_file(repo_id=model_id, filename=ckpt_name, token=token)
58
 
59
+ if safetensors == "yes":
60
+ tensors = {}
61
+ with safe_open(ckpt_path, framework="pt", device="cpu") as f:
62
+ for key in f.keys():
63
+ tensors[key] = f.get_tensor(key)
64
+
65
+ new_checkpoint_path = "/".join(ckpt_path.split("/")[:-1] + ["model_safe.ckpt"])
66
+ torch.save(tensors, new_checkpoint_path)
67
+ ckpt_path = new_checkpoint_path
68
+
69
  # 2. Run the conversion script
70
  os.makedirs(model_id, exist_ok=True)
71
  run_command = [
 
145
  radio_ckpts = gr.Radio(label="Choose the checkpoint to convert", visible=False)
146
  path_in_repo = gr.Textbox(label="Path where the weights will be saved", placeholder="Leave empty for root folder")
147
  ema = gr.Radio(label="Extract EMA or non-EMA?", choices=["ema", "non-ema"])
148
+ safetensors = gr.Radio(label="Extract from safetensors", choices=["yes", "no"], value="no")
149
  radio_sd_version = gr.Radio(label="Choose the model version", choices=["v1", "v2", "v2.1"])
150
  gr.Markdown("Conversion may take a few minutes.")
151
  btn_convert = gr.Button("Convert & Push")
 
176
 
177
  btn_convert.click(
178
  fn=convert_and_push,
179
+ inputs=[radio_model_names, input_model, radio_ckpts, radio_sd_version, input_token, path_in_repo, ema, safetensors],
180
  outputs=error_output,
181
  scroll_to_output=True
182
  )