Skip to content

Commit 9f4e853

Browse files
authored
GitHub API rate limit fallback (#1661)
1 parent 2271a2e commit 9f4e853

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

utils/google_utils.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,32 @@ def gsutil_getsize(url=''):
1616
return eval(s.split(' ')[0]) if len(s) else 0 # bytes
1717

1818

19-
def attempt_download(file):
19+
def attempt_download(file, repo='ultralytics/yolov3'):
2020
# Attempt file download if does not exist
2121
file = Path(str(file).strip().replace("'", '').lower())
2222

2323
if not file.exists():
24-
response = requests.get('https://api.github.com/repos/ultralytics/yolov3/releases/latest').json() # github api
25-
assets = [x['name'] for x in response['assets']] # release assets ['yolov3.pt', 'yolov3-spp.pt', ...]
26-
name = file.name
24+
try:
25+
response = requests.get(f'https://api.github.com/repos/{repo}/releases/latest').json() # github api
26+
assets = [x['name'] for x in response['assets']] # release assets, i.e. ['yolov5s.pt', 'yolov5m.pt', ...]
27+
tag = response['tag_name'] # i.e. 'v1.0'
28+
except: # fallback plan
29+
assets = ['yolov3.pt', 'yolov3-spp.pt', 'yolov3-tiny.pt']
30+
tag = subprocess.check_output('git tag', shell=True).decode('utf-8').split('\n')[-2]
2731

32+
name = file.name
2833
if name in assets:
29-
msg = f'{file} missing, try downloading from https://github.com/ultralytics/yolov3/releases/'
34+
msg = f'{file} missing, try downloading from https://github.com/{repo}/releases/'
3035
redundant = False # second download option
3136
try: # GitHub
32-
tag = response['tag_name'] # i.e. 'v1.0'
33-
url = f'https://github.com/ultralytics/yolov3/releases/download/{tag}/{name}'
37+
url = f'https://github.com/{repo}/releases/download/{tag}/{name}'
3438
print(f'Downloading {url} to {file}...')
3539
torch.hub.download_url_to_file(url, file)
3640
assert file.exists() and file.stat().st_size > 1E6 # check
3741
except Exception as e: # GCP
3842
print(f'Download error: {e}')
3943
assert redundant, 'No secondary mirror'
40-
url = f'https://storage.googleapis.com/ultralytics/yolov3/ckpt/{name}'
44+
url = f'https://storage.googleapis.com/{repo}/ckpt/{name}'
4145
print(f'Downloading {url} to {file}...')
4246
os.system(f'curl -L {url} -o {file}') # torch.hub.download_url_to_file(url, weights)
4347
finally:

0 commit comments

Comments
 (0)