pcuenq HF staff commited on
Commit
d5cdfff
1 Parent(s): d90dedd

Add progress bar.

Browse files
Files changed (1) hide show
  1. convert.py +8 -3
convert.py CHANGED
@@ -20,9 +20,10 @@ from transformers import CONFIG_MAPPING
20
  COMMIT_MESSAGE = " This PR adds fp32 and fp16 weights in PyTorch and safetensors format to {}"
21
 
22
 
23
- def convert_single(model_id: str, filename: str, model_type: str, sample_size: int, scheduler_type: str, extract_ema: bool, folder: str):
24
  from_safetensors = filename.endswith(".safetensors")
25
 
 
26
  local_file = os.path.join(model_id, filename)
27
  ckpt_file = local_file if os.path.isfile(local_file) else hf_hub_download(repo_id=model_id, filename=filename)
28
 
@@ -40,9 +41,11 @@ def convert_single(model_id: str, filename: str, model_type: str, sample_size: i
40
  config_file = BytesIO(requests.get(config_url).content)
41
 
42
  if model_type == "ControlNet":
 
43
  pipeline = download_controlnet_from_original_ckpt(ckpt_file, config_file, image_size=sample_size, from_safetensors=from_safetensors, extract_ema=extract_ema)
44
  to_args = {"dtype": torch.float16}
45
  else:
 
46
  pipeline = download_from_original_stable_diffusion_ckpt(ckpt_file, config_file, image_size=sample_size, scheduler_type=scheduler_type, from_safetensors=from_safetensors, extract_ema=extract_ema)
47
  to_args = {"torch_dtype": torch.float16}
48
 
@@ -68,7 +71,7 @@ def previous_pr(api: "HfApi", model_id: str, pr_title: str) -> Optional["Discuss
68
  return discussion
69
 
70
 
71
- def convert(token: str, model_id: str, filename: str, model_type: str, sample_size: int = 512, scheduler_type: str = "pndm", extract_ema: bool = True):
72
  api = HfApi()
73
 
74
  pr_title = "Adding `diffusers` weights of this model"
@@ -78,10 +81,12 @@ def convert(token: str, model_id: str, filename: str, model_type: str, sample_si
78
  os.makedirs(folder)
79
  new_pr = None
80
  try:
81
- folder = convert_single(model_id, filename, model_type, sample_size, scheduler_type, extract_ema, folder)
 
82
  new_pr = api.upload_folder(folder_path=folder, path_in_repo="./", repo_id=model_id, repo_type="model", token=token, commit_message=pr_title, commit_description=COMMIT_MESSAGE.format(model_id), create_pr=True)
83
  pr_number = new_pr.split("%2F")[-1].split("/")[0]
84
  link = f"Pr created at: {'https://huggingface.co/' + os.path.join(model_id, 'discussions', pr_number)}"
 
85
  except Exception as e:
86
  raise gr.exceptions.Error(str(e))
87
  finally:
 
20
  COMMIT_MESSAGE = " This PR adds fp32 and fp16 weights in PyTorch and safetensors format to {}"
21
 
22
 
23
+ def convert_single(model_id: str, filename: str, model_type: str, sample_size: int, scheduler_type: str, extract_ema: bool, folder: str, progress):
24
  from_safetensors = filename.endswith(".safetensors")
25
 
26
+ progress(0, desc="Downloading model")
27
  local_file = os.path.join(model_id, filename)
28
  ckpt_file = local_file if os.path.isfile(local_file) else hf_hub_download(repo_id=model_id, filename=filename)
29
 
 
41
  config_file = BytesIO(requests.get(config_url).content)
42
 
43
  if model_type == "ControlNet":
44
+ progress(0.2, desc="Converting ControlNet Model")
45
  pipeline = download_controlnet_from_original_ckpt(ckpt_file, config_file, image_size=sample_size, from_safetensors=from_safetensors, extract_ema=extract_ema)
46
  to_args = {"dtype": torch.float16}
47
  else:
48
+ progress(0.1, desc="Converting Model")
49
  pipeline = download_from_original_stable_diffusion_ckpt(ckpt_file, config_file, image_size=sample_size, scheduler_type=scheduler_type, from_safetensors=from_safetensors, extract_ema=extract_ema)
50
  to_args = {"torch_dtype": torch.float16}
51
 
 
71
  return discussion
72
 
73
 
74
+ def convert(token: str, model_id: str, filename: str, model_type: str, sample_size: int = 512, scheduler_type: str = "pndm", extract_ema: bool = True, progress=gr.Progress()):
75
  api = HfApi()
76
 
77
  pr_title = "Adding `diffusers` weights of this model"
 
81
  os.makedirs(folder)
82
  new_pr = None
83
  try:
84
+ folder = convert_single(model_id, filename, model_type, sample_size, scheduler_type, extract_ema, folder, progress)
85
+ progress(0.7, desc="Uploading to Hub")
86
  new_pr = api.upload_folder(folder_path=folder, path_in_repo="./", repo_id=model_id, repo_type="model", token=token, commit_message=pr_title, commit_description=COMMIT_MESSAGE.format(model_id), create_pr=True)
87
  pr_number = new_pr.split("%2F")[-1].split("/")[0]
88
  link = f"Pr created at: {'https://huggingface.co/' + os.path.join(model_id, 'discussions', pr_number)}"
89
+ progress(1, desc="Done")
90
  except Exception as e:
91
  raise gr.exceptions.Error(str(e))
92
  finally: