Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
@@ -1,124 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import platform
|
3 |
-
import subprocess
|
4 |
-
import time
|
5 |
-
from pathlib import Path
|
6 |
-
|
7 |
-
import requests
|
8 |
-
import torch
|
9 |
-
|
10 |
-
|
11 |
-
def gsutil_getsize(url=''):
|
12 |
-
# gs://bucket/file size https://cloud.google.com/storage/docs/gsutil/commands/du
|
13 |
-
s = subprocess.check_output(f'gsutil du {url}', shell=True).decode('utf-8')
|
14 |
-
return eval(s.split(' ')[0]) if len(s) else 0 # bytes
|
15 |
-
|
16 |
-
|
17 |
-
def attempt_download(file, repo='WongKinYiu/yolov7'):
|
18 |
-
# Attempt file download if does not exist
|
19 |
-
file = Path(str(file).strip().replace("'", '').lower())
|
20 |
-
|
21 |
-
if not file.exists():
|
22 |
-
try:
|
23 |
-
response = requests.get(f'https://api.github.com/repos/{repo}/releases/latest').json() # github api
|
24 |
-
assets = [x['name'] for x in response['assets']] # release assets
|
25 |
-
tag = response['tag_name'] # i.e. 'v1.0'
|
26 |
-
except: # fallback plan
|
27 |
-
assets = ['yolov7.pt', 'yolov7-tiny.pt', 'yolov7x.pt', 'yolov7-d6.pt', 'yolov7-e6.pt',
|
28 |
-
'yolov7-e6e.pt', 'yolov7-w6.pt']
|
29 |
-
try:
|
30 |
-
tag = subprocess.check_output('git tag', shell=True).decode().split()[-1]
|
31 |
-
except IndexError:
|
32 |
-
tag = 'default'
|
33 |
-
|
34 |
-
name = file.name
|
35 |
-
if name in assets:
|
36 |
-
msg = f'{file} missing, try downloading from https://github.com/{repo}/releases/'
|
37 |
-
redundant = False # second download option
|
38 |
-
try: # GitHub
|
39 |
-
url = f'https://github.com/{repo}/releases/download/{tag}/{name}'
|
40 |
-
print(f'Downloading {url} to {file}...')
|
41 |
-
torch.hub.download_url_to_file(url, file)
|
42 |
-
assert file.exists() and file.stat().st_size > 1E6 # check
|
43 |
-
except Exception as e: # GCP
|
44 |
-
print(f'Download error: {e}')
|
45 |
-
assert redundant, 'No secondary mirror'
|
46 |
-
url = f'https://storage.googleapis.com/{repo}/ckpt/{name}'
|
47 |
-
print(f'Downloading {url} to {file}...')
|
48 |
-
os.system(f'curl -L {url} -o {file}') # torch.hub.download_url_to_file(url, weights)
|
49 |
-
finally:
|
50 |
-
if not file.exists() or file.stat().st_size < 1E6: # check
|
51 |
-
file.unlink(missing_ok=True) # remove partial downloads
|
52 |
-
print(f'ERROR: Download failure: {msg}')
|
53 |
-
print('')
|
54 |
-
return
|
55 |
-
|
56 |
-
|
57 |
-
def gdrive_download(id='', file='tmp.zip'):
|
58 |
-
# Downloads a file from Google Drive. from yolov7.utils.google_utils import *; gdrive_download()
|
59 |
-
t = time.time()
|
60 |
-
file = Path(file)
|
61 |
-
cookie = Path('cookie') # gdrive cookie
|
62 |
-
print(f'Downloading https://drive.google.com/uc?export=download&id={id} as {file}... ', end='')
|
63 |
-
file.unlink(missing_ok=True) # remove existing file
|
64 |
-
cookie.unlink(missing_ok=True) # remove existing cookie
|
65 |
-
|
66 |
-
# Attempt file download
|
67 |
-
out = "NUL" if platform.system() == "Windows" else "/dev/null"
|
68 |
-
os.system(f'curl -c ./cookie -s -L "drive.google.com/uc?export=download&id={id}" > {out}')
|
69 |
-
if os.path.exists('cookie'): # large file
|
70 |
-
s = f'curl -Lb ./cookie "drive.google.com/uc?export=download&confirm={get_token()}&id={id}" -o {file}'
|
71 |
-
else: # small file
|
72 |
-
s = f'curl -s -L -o {file} "drive.google.com/uc?export=download&id={id}"'
|
73 |
-
r = os.system(s) # execute, capture return
|
74 |
-
cookie.unlink(missing_ok=True) # remove existing cookie
|
75 |
-
|
76 |
-
# Error check
|
77 |
-
if r != 0:
|
78 |
-
file.unlink(missing_ok=True) # remove partial
|
79 |
-
print('Download error ') # raise Exception('Download error')
|
80 |
-
return r
|
81 |
-
|
82 |
-
# Unzip if archive
|
83 |
-
if file.suffix == '.zip':
|
84 |
-
print('unzipping... ', end='')
|
85 |
-
os.system(f'unzip -q {file}') # unzip
|
86 |
-
file.unlink() # remove zip to free space
|
87 |
-
|
88 |
-
print(f'Done ({time.time() - t:.1f}s)')
|
89 |
-
return r
|
90 |
-
|
91 |
-
|
92 |
-
def get_token(cookie="./cookie"):
|
93 |
-
with open(cookie) as f:
|
94 |
-
for line in f:
|
95 |
-
if "download" in line:
|
96 |
-
return line.split()[-1]
|
97 |
-
return ""
|
98 |
-
|
99 |
-
# def upload_blob(bucket_name, source_file_name, destination_blob_name):
|
100 |
-
# # Uploads a file to a bucket
|
101 |
-
# # https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-python
|
102 |
-
#
|
103 |
-
# storage_client = storage.Client()
|
104 |
-
# bucket = storage_client.get_bucket(bucket_name)
|
105 |
-
# blob = bucket.blob(destination_blob_name)
|
106 |
-
#
|
107 |
-
# blob.upload_from_filename(source_file_name)
|
108 |
-
#
|
109 |
-
# print('File {} uploaded to {}.'.format(
|
110 |
-
# source_file_name,
|
111 |
-
# destination_blob_name))
|
112 |
-
#
|
113 |
-
#
|
114 |
-
# def download_blob(bucket_name, source_blob_name, destination_file_name):
|
115 |
-
# # Uploads a blob from a bucket
|
116 |
-
# storage_client = storage.Client()
|
117 |
-
# bucket = storage_client.get_bucket(bucket_name)
|
118 |
-
# blob = bucket.blob(source_blob_name)
|
119 |
-
#
|
120 |
-
# blob.download_to_filename(destination_file_name)
|
121 |
-
#
|
122 |
-
# print('Blob {} downloaded to {}.'.format(
|
123 |
-
# source_blob_name,
|
124 |
-
# destination_file_name))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|