r3gm commited on
Commit
c24dce8
1 Parent(s): 4a948d7

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +43 -2
utils.py CHANGED
@@ -7,7 +7,10 @@ from constants import (
7
  HF_TOKEN,
8
  MODEL_TYPE_CLASS,
9
  DIRECTORY_LORAS,
 
10
  DIFFUSECRAFT_CHECKPOINT_NAME,
 
 
11
  )
12
  from huggingface_hub import HfApi
13
  from huggingface_hub import snapshot_download
@@ -22,6 +25,8 @@ import copy
22
  import requests
23
  from requests.adapters import HTTPAdapter
24
  from urllib3.util import Retry
 
 
25
 
26
  USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:127.0) Gecko/20100101 Firefox/127.0'
27
 
@@ -110,7 +115,11 @@ def download_things(directory, url, hf_token="", civitai_api_key="", romanize=Fa
110
  print("\033[91mYou need an API key to download Civitai models.\033[0m")
111
 
112
  model_profile = retrieve_model_info(url)
113
- if model_profile.download_url and model_profile.filename_url:
 
 
 
 
114
  url = model_profile.download_url
115
  filename = unidecode(model_profile.filename_url) if romanize else model_profile.filename_url
116
  else:
@@ -314,7 +323,8 @@ def restart_space(repo_id: str, factory_reboot: bool):
314
 
315
 
316
  def extract_exif_data(image):
317
- if image is None: return ""
 
318
 
319
  try:
320
  metadata_keys = ['parameters', 'metadata', 'prompt', 'Comment']
@@ -410,6 +420,37 @@ def download_diffuser_repo(repo_name: str, model_type: str, revision: str = "mai
410
  return cached_folder
411
 
412
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
413
  def progress_step_bar(step, total):
414
  # Calculate the percentage for the progress bar width
415
  percentage = min(100, ((step / total) * 100))
 
7
  HF_TOKEN,
8
  MODEL_TYPE_CLASS,
9
  DIRECTORY_LORAS,
10
+ DIRECTORY_MODELS,
11
  DIFFUSECRAFT_CHECKPOINT_NAME,
12
+ CACHE_HF,
13
+ STORAGE_ROOT,
14
  )
15
  from huggingface_hub import HfApi
16
  from huggingface_hub import snapshot_download
 
25
  import requests
26
  from requests.adapters import HTTPAdapter
27
  from urllib3.util import Retry
28
+ import shutil
29
+ import subprocess
30
 
31
  USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:127.0) Gecko/20100101 Firefox/127.0'
32
 
 
115
  print("\033[91mYou need an API key to download Civitai models.\033[0m")
116
 
117
  model_profile = retrieve_model_info(url)
118
+ if (
119
+ model_profile is not None
120
+ and model_profile.download_url
121
+ and model_profile.filename_url
122
+ ):
123
  url = model_profile.download_url
124
  filename = unidecode(model_profile.filename_url) if romanize else model_profile.filename_url
125
  else:
 
323
 
324
 
325
  def extract_exif_data(image):
326
+ if image is None:
327
+ return ""
328
 
329
  try:
330
  metadata_keys = ['parameters', 'metadata', 'prompt', 'Comment']
 
420
  return cached_folder
421
 
422
 
423
+ def get_folder_size_gb(folder_path):
424
+ result = subprocess.run(["du", "-s", folder_path], capture_output=True, text=True)
425
+
426
+ total_size_kb = int(result.stdout.split()[0])
427
+ total_size_gb = total_size_kb / (1024 ** 2)
428
+
429
+ return total_size_gb
430
+
431
+
432
+ def get_used_storage_gb():
433
+ try:
434
+ used_gb = get_folder_size_gb(STORAGE_ROOT)
435
+ print(f"Used Storage: {used_gb:.2f} GB")
436
+ except Exception as e:
437
+ used_gb = 999
438
+ print(f"Error while retrieving the used storage: {e}.")
439
+
440
+ return used_gb
441
+
442
+
443
+ def delete_model(removal_candidate):
444
+ print(f"Removing: {removal_candidate}")
445
+
446
+ if os.path.exists(removal_candidate):
447
+ os.remove(removal_candidate)
448
+ else:
449
+ diffusers_model = f"{CACHE_HF}{DIRECTORY_MODELS}--{removal_candidate.replace('/', '--')}"
450
+ if os.path.isdir(diffusers_model):
451
+ shutil.rmtree(diffusers_model)
452
+
453
+
454
  def progress_step_bar(step, total):
455
  # Calculate the percentage for the progress bar width
456
  percentage = min(100, ((step / total) * 100))