Skip to content

Commit

Permalink
GitHub API rate limit fallback (ultralytics#1930)
Browse files Browse the repository at this point in the history
  • Loading branch information
glenn-jocher committed Jan 14, 2021
1 parent f5e223d commit cd2fabc
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions utils/google_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,32 @@ def gsutil_getsize(url=''):
return eval(s.split(' ')[0]) if len(s) else 0 # bytes


def attempt_download(file):
def attempt_download(file, repo='ultralytics/yolov5'):
# Attempt file download if does not exist
file = Path(str(file).strip().replace("'", '').lower())

if not file.exists():
response = requests.get('https://github.com/gitapi/repos/ultralytics/yolov5/releases/latest').json() # github api
assets = [x['name'] for x in response['assets']] # release assets, i.e. ['yolov5s.pt', 'yolov5m.pt', ...]
name = file.name
try:
response = requests.get(f'https://github.com/gitapi/repos/{repo}/releases/latest').json() # github api
assets = [x['name'] for x in response['assets']] # release assets, i.e. ['yolov5s.pt', 'yolov5m.pt', ...]
tag = response['tag_name'] # i.e. 'v1.0'
except: # fallback plan
assets = ['yolov5.pt', 'yolov5.pt', 'yolov5l.pt', 'yolov5x.pt']
tag = subprocess.check_output('git tag', shell=True).decode('utf-8').split('\n')[-2]

name = file.name
if name in assets:
msg = f'{file} missing, try downloading from https://github.com/ultralytics/yolov5/releases/'
msg = f'{file} missing, try downloading from https://github.com/{repo}/releases/'
redundant = False # second download option
try: # GitHub
tag = response['tag_name'] # i.e. 'v1.0'
url = f'https://github.com/ultralytics/yolov5/releases/download/{tag}/{name}'
url = f'https://github.com/{repo}/releases/download/{tag}/{name}'
print(f'Downloading {url} to {file}...')
torch.hub.download_url_to_file(url, file)
assert file.exists() and file.stat().st_size > 1E6 # check
except Exception as e: # GCP
print(f'Download error: {e}')
assert redundant, 'No secondary mirror'
url = f'https://storage.googleapis.com/ultralytics/yolov5/ckpt/{name}'
url = f'https://storage.googleapis.com/{repo}/ckpt/{name}'
print(f'Downloading {url} to {file}...')
os.system(f'curl -L {url} -o {file}') # torch.hub.download_url_to_file(url, weights)
finally:
Expand Down

0 comments on commit cd2fabc

Please sign in to comment.