jhj0517 commited on
Commit
51dc898
1 Parent(s): 7962f8d

Update model downloading function

Browse files
modules/live_portrait/model_downloader.py CHANGED
@@ -1,3 +1,7 @@
 
 
 
 
1
  import requests
2
  from tqdm import tqdm
3
 
@@ -11,33 +15,22 @@ MODELS_URL = {
11
  }
12
 
13
 
14
- def download_model(file_path, model_url):
15
- print('Downloading model...')
16
- response = requests.get(model_url, stream=True)
 
 
 
17
  try:
18
- if response.status_code == 200:
19
- total_size = int(response.headers.get('content-length', 0))
20
- block_size = 1024 # 1 Kibibyte
21
-
22
- # tqdm will display a progress bar
23
- with open(file_path, 'wb') as file, tqdm(
24
- desc='Downloading',
25
- total=total_size,
26
- unit='iB',
27
- unit_scale=True,
28
- unit_divisor=1024,
29
- ) as bar:
30
- for data in response.iter_content(block_size):
31
- bar.update(len(data))
32
- file.write(data)
33
 
34
  except requests.exceptions.RequestException as e:
35
  print(
36
- f"Model Download has failed. Download manually from: {model_url}"
37
- f"And place in {file_path}"
38
  )
39
- raise
40
  except Exception as e:
41
- print(f'An unexpected error occurred during model download')
42
- raise
43
-
 
1
+ import os.path
2
+ from typing import Optional
3
+ from torch.hub import download_url_to_file
4
+ from urllib.parse import urlparse
5
  import requests
6
  from tqdm import tqdm
7
 
 
15
  }
16
 
17
 
18
+ def download_model(
19
+ file_path: str,
20
+ url: str,
21
+ ) -> Optional[str]:
22
+ if os.path.exists(file_path):
23
+ return None
24
  try:
25
+ print(f'{file_path} is not detected. Downloading model...')
26
+ download_url_to_file(url, file_path, progress=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  except requests.exceptions.RequestException as e:
29
  print(
30
+ f"Model download has failed. Please download manually from: {url}\n"
31
+ f"and place it in {file_path}"
32
  )
33
+ raise e
34
  except Exception as e:
35
+ print('An unexpected error occurred during model download.')
36
+ raise e