John6666 commited on
Commit
6c61e31
·
verified ·
1 Parent(s): ab9eb81

Upload convert_repo_to_safetensors_gr.py

Browse files
Files changed (1) hide show
  1. convert_repo_to_safetensors_gr.py +10 -7
convert_repo_to_safetensors_gr.py CHANGED
@@ -22,9 +22,10 @@ def get_token():
22
  return token
23
 
24
  def is_repo_exists(repo_id: str, repo_type: str="model"):
25
- api = HfApi(token=get_token())
 
26
  try:
27
- if api.repo_exists(repo_id=repo_id, repo_type=repo_type): return True
28
  else: return False
29
  except Exception as e:
30
  print(f"Error: Failed to connect {repo_id} ({repo_type}). {e}")
@@ -353,8 +354,9 @@ def convert_diffusers_to_safetensors(model_path, checkpoint_path, dtype="fp16"):
353
 
354
 
355
  def download_repo(repo_id, dir_path):
 
356
  try:
357
- snapshot_download(repo_id=repo_id, local_dir=dir_path, token=get_token())
358
  except Exception as e:
359
  print(f"Error: Failed to download {repo_id}. {e}")
360
  gr.Warning(f"Error: Failed to download {repo_id}. {e}")
@@ -363,13 +365,14 @@ def download_repo(repo_id, dir_path):
363
 
364
  def upload_safetensors_to_repo(filename, repo_id, repo_type, is_private, progress=gr.Progress(track_tqdm=True)):
365
  output_filename = Path(filename).name
366
- api = HfApi(token=get_token())
 
367
  try:
368
- if not is_repo_exists(repo_id, repo_type): api.create_repo(repo_id, repo_type=repo_type, private=is_private)
369
  progress(0, desc="Start uploading...")
370
- api.upload_file(path_or_fileobj=filename, path_in_repo=output_filename, repo_type=repo_type, repo_id=repo_id)
371
  progress(1, desc="Uploaded.")
372
- url = hf_hub_url(repo_id=repo_id, repo_type=repo_type, filename=output_filename, token=get_token())
373
  except Exception as e:
374
  print(f"Error: Failed to upload to {repo_id}. {e}")
375
  gr.Warning(f"Error: Failed to upload to {repo_id}. {e}")
 
22
  return token
23
 
24
  def is_repo_exists(repo_id: str, repo_type: str="model"):
25
+ hf_token = get_token()
26
+ api = HfApi(token=hf_token)
27
  try:
28
+ if api.repo_exists(repo_id=repo_id, repo_type=repo_type, token=hf_token): return True
29
  else: return False
30
  except Exception as e:
31
  print(f"Error: Failed to connect {repo_id} ({repo_type}). {e}")
 
354
 
355
 
356
  def download_repo(repo_id, dir_path):
357
+ hf_token = get_token()
358
  try:
359
+ snapshot_download(repo_id=repo_id, local_dir=dir_path, token=hf_token)
360
  except Exception as e:
361
  print(f"Error: Failed to download {repo_id}. {e}")
362
  gr.Warning(f"Error: Failed to download {repo_id}. {e}")
 
365
 
366
  def upload_safetensors_to_repo(filename, repo_id, repo_type, is_private, progress=gr.Progress(track_tqdm=True)):
367
  output_filename = Path(filename).name
368
+ hf_token = get_token()
369
+ api = HfApi(token=hf_token)
370
  try:
371
+ if not is_repo_exists(repo_id, repo_type): api.create_repo(repo_id, repo_type=repo_type, token=hf_token, private=is_private)
372
  progress(0, desc="Start uploading...")
373
+ api.upload_file(path_or_fileobj=filename, path_in_repo=output_filename, repo_type=repo_type, token=hf_token, repo_id=repo_id)
374
  progress(1, desc="Uploaded.")
375
+ url = hf_hub_url(repo_id=repo_id, repo_type=repo_type, filename=output_filename, token=hf_token)
376
  except Exception as e:
377
  print(f"Error: Failed to upload to {repo_id}. {e}")
378
  gr.Warning(f"Error: Failed to upload to {repo_id}. {e}")