IceClear commited on
Commit
e3b4db8
·
1 Parent(s): 6237836

update download

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py CHANGED
@@ -56,6 +56,40 @@ from common.partition import partition_by_groups, partition_by_size
56
 
57
  import gradio as gr
58
  from pathlib import Path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
 
61
  # os.system("pip freeze")
 
56
 
57
  import gradio as gr
58
  from pathlib import Path
59
+ from urllib.parse import urlparse
60
+ from torch.hub import download_url_to_file, get_dir
61
+
62
+
63
+ def load_file_from_url(url, model_dir=None, progress=True, file_name=None):
64
+ """Load file form http url, will download models if necessary.
65
+
66
+ Reference: https://github.com/1adrianb/face-alignment/blob/master/face_alignment/utils.py
67
+
68
+ Args:
69
+ url (str): URL to be downloaded.
70
+ model_dir (str): The path to save the downloaded model. Should be a full path. If None, use pytorch hub_dir.
71
+ Default: None.
72
+ progress (bool): Whether to show the download progress. Default: True.
73
+ file_name (str): The downloaded file name. If None, use the file name in the url. Default: None.
74
+
75
+ Returns:
76
+ str: The path to the downloaded file.
77
+ """
78
+ if model_dir is None: # use the pytorch hub_dir
79
+ hub_dir = get_dir()
80
+ model_dir = os.path.join(hub_dir, 'checkpoints')
81
+
82
+ os.makedirs(model_dir, exist_ok=True)
83
+
84
+ parts = urlparse(url)
85
+ filename = os.path.basename(parts.path)
86
+ if file_name is not None:
87
+ filename = file_name
88
+ cached_file = os.path.abspath(os.path.join(model_dir, filename))
89
+ if not os.path.exists(cached_file):
90
+ print(f'Downloading: "{url}" to {cached_file}\n')
91
+ download_url_to_file(url, cached_file, hash_prefix=None, progress=progress)
92
+ return cached_file
93
 
94
 
95
  # os.system("pip freeze")