Spaces:
Build error
Build error
Create google_drive.py
Browse files- google_drive.py +47 -0
google_drive.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# google_drive.py
|
| 2 |
+
from google.colab import drive
|
| 3 |
+
from google.colab import auth
|
| 4 |
+
from googleapiclient.discovery import build
|
| 5 |
+
from googleapiclient.errors import HttpError
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
def authenticate_and_get_path():
|
| 9 |
+
# authenticate user
|
| 10 |
+
auth.authenticate_user()
|
| 11 |
+
|
| 12 |
+
# mount Google Drive
|
| 13 |
+
drive.mount('/content/gdrive')
|
| 14 |
+
|
| 15 |
+
# get folder name from user input
|
| 16 |
+
folder_name = input("Enter the name of the folder containing the model: ")
|
| 17 |
+
|
| 18 |
+
# search for folder with given name
|
| 19 |
+
folder_id = search_folder(folder_name)
|
| 20 |
+
|
| 21 |
+
# check if folder was found
|
| 22 |
+
if folder_id is None:
|
| 23 |
+
print("Error: Folder not found.")
|
| 24 |
+
return None
|
| 25 |
+
|
| 26 |
+
# create path to model
|
| 27 |
+
path = os.path.join("/content/gdrive/MyDrive", folder_id)
|
| 28 |
+
|
| 29 |
+
return path
|
| 30 |
+
|
| 31 |
+
def search_folder(name):
|
| 32 |
+
# create Drive API client
|
| 33 |
+
service = build('drive', 'v3', developerKey='YOUR_API_KEY')
|
| 34 |
+
|
| 35 |
+
# search for folders with given name
|
| 36 |
+
query = "mimeType='application/vnd.google-apps.folder' and trashed=false and name='" + name + "'"
|
| 37 |
+
results = service.files().list(q=query, fields="nextPageToken, files(id, name)").execute()
|
| 38 |
+
|
| 39 |
+
# check if folder was found
|
| 40 |
+
items = results.get("files", [])
|
| 41 |
+
if len(items) == 0:
|
| 42 |
+
return None
|
| 43 |
+
elif len(items) > 1:
|
| 44 |
+
print("Warning: Multiple folders found with name '" + name + "'. Using first folder.")
|
| 45 |
+
folder_id = items[0]["id"]
|
| 46 |
+
|
| 47 |
+
return folder_id
|