Spaces:
Runtime error
Runtime error
Create get_drive_model.py
Browse files- get_drive_model.py +33 -0
get_drive_model.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
def download_file_from_google_drive(file_id, destination):
|
| 5 |
+
URL = "https://drive.google.com/uc?export=download"
|
| 6 |
+
|
| 7 |
+
session = requests.Session()
|
| 8 |
+
response = session.get(URL, params={'id': file_id}, stream=True)
|
| 9 |
+
token = get_confirm_token(response)
|
| 10 |
+
|
| 11 |
+
if token:
|
| 12 |
+
params = {'id': file_id, 'confirm': token}
|
| 13 |
+
response = session.get(URL, params=params, stream=True)
|
| 14 |
+
|
| 15 |
+
save_response_content(response, destination)
|
| 16 |
+
|
| 17 |
+
def get_confirm_token(response):
|
| 18 |
+
for key, value in response.cookies.items():
|
| 19 |
+
if key.startswith('download_warning'):
|
| 20 |
+
return value
|
| 21 |
+
return None
|
| 22 |
+
|
| 23 |
+
def save_response_content(response, destination):
|
| 24 |
+
CHUNK_SIZE = 32768
|
| 25 |
+
|
| 26 |
+
with open(destination, "wb") as f:
|
| 27 |
+
for chunk in response.iter_content(CHUNK_SIZE):
|
| 28 |
+
if chunk: # filter out keep-alive new chunks
|
| 29 |
+
f.write(chunk)
|
| 30 |
+
|
| 31 |
+
def ensure_model_download(file_id, destination):
|
| 32 |
+
if not os.path.exists(destination):
|
| 33 |
+
download_file_from_google_drive(file_id, destination)
|