shohrukh1 commited on
Commit
45da4ed
·
verified ·
1 Parent(s): d9d3541

iniital commit

Browse files
Files changed (4) hide show
  1. credentials.json +1 -0
  2. main.py +76 -0
  3. requirements.txt +81 -0
  4. token.json +1 -0
credentials.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"installed":{"client_id":"885206778440-77f6gp3h0mb1mso22v48c0k0s2ouknh9.apps.googleusercontent.com","project_id":"high-electron-402810","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"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","redirect_uris":["http://localhost"]}}
main.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import os.path
3
+
4
+ import gradio as gr
5
+ from google.auth.transport import Request
6
+ from google.oauth2.credentials import Credentials
7
+ from google_auth_oauthlib.flow import InstalledAppFlow
8
+ from googleapiclient.discovery import build
9
+ from googleapiclient.errors import HttpError
10
+ from googleapiclient.http import MediaFileUpload
11
+
12
+ # Use the appropriate scope for full drive access
13
+ SCOPES = ["https://www.googleapis.com/auth/drive"]
14
+
15
+ # Delete the existing token.json file to force re-authentication
16
+ if os.path.exists("token.json"):
17
+ os.remove("token.json")
18
+
19
+ creds = None
20
+
21
+ if os.path.exists("token.json"):
22
+ creds = Credentials.from_authorized_user_file("token.json", scopes=SCOPES)
23
+
24
+ if not creds or not creds.valid:
25
+ if creds and creds.expired and creds.refresh_token:
26
+ creds.refresh(Request())
27
+ else:
28
+ flow = InstalledAppFlow.from_client_secrets_file("credentials.json", SCOPES)
29
+ creds = flow.run_local_server(port=0)
30
+ with open("token.json", "w") as token:
31
+ token.write(creds.to_json())
32
+
33
+ def upload_to_drive(file):
34
+ try:
35
+ service = build("drive", "v3", credentials=creds)
36
+ response = service.files().list(
37
+ q="name='MohirdevAutomation' and mimeType='application/vnd.google-apps.folder'",
38
+ spaces="drive"
39
+ ).execute()
40
+
41
+ if not response['files']:
42
+ file_metadata = {
43
+ "name": "MohirdevAutomation",
44
+ "mimeType": "application/vnd.google-apps.folder"
45
+ }
46
+
47
+ file = service.files().create(body=file_metadata, fields="id").execute()
48
+ folder_id = file.get("id")
49
+ else:
50
+ folder_id = response['files'][0]['id']
51
+
52
+ file_metadata = {
53
+ "name": os.path.basename(file.name),
54
+ "parents": [folder_id]
55
+ }
56
+ media = MediaFileUpload(file.name)
57
+ upload_file = service.files().create(
58
+ body=file_metadata,
59
+ media_body=media,
60
+ fields="id"
61
+ ).execute()
62
+
63
+ return f"File {os.path.basename(file.name)} uploaded successfully with ID: {upload_file.get('id')}"
64
+ except HttpError as e:
65
+ return f"Error: {e}"
66
+
67
+ iface = gr.Interface(
68
+ fn=upload_to_drive,
69
+ inputs=gr.File(),
70
+ outputs="text",
71
+ title="Upload to Google Drive",
72
+ description="Upload your files to a specific folder in Google Drive"
73
+ )
74
+
75
+ if __name__ == "__main__":
76
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiofiles==23.2.1
2
+ annotated-types==0.7.0
3
+ anyio==4.4.0
4
+ cachetools==5.4.0
5
+ certifi==2024.7.4
6
+ charset-normalizer==3.3.2
7
+ click==8.1.7
8
+ colorama==0.4.6
9
+ contourpy==1.2.1
10
+ cycler==0.12.1
11
+ dnspython==2.6.1
12
+ email_validator==2.2.0
13
+ exceptiongroup==1.2.2
14
+ fastapi==0.111.1
15
+ fastapi-cli==0.0.4
16
+ ffmpy==0.3.2
17
+ filelock==3.15.4
18
+ fonttools==4.53.1
19
+ fsspec==2024.6.1
20
+ google-api-core==2.19.1
21
+ google-api-python-client==2.137.0
22
+ google-auth==2.32.0
23
+ google-auth-httplib2==0.2.0
24
+ google-auth-oauthlib==1.2.1
25
+ googleapis-common-protos==1.63.2
26
+ gradio==4.39.0
27
+ gradio_client==1.1.1
28
+ h11==0.14.0
29
+ httpcore==1.0.5
30
+ httplib2==0.22.0
31
+ httptools==0.6.1
32
+ httpx==0.27.0
33
+ huggingface-hub==0.24.2
34
+ idna==3.7
35
+ importlib_resources==6.4.0
36
+ Jinja2==3.1.4
37
+ kiwisolver==1.4.5
38
+ markdown-it-py==3.0.0
39
+ MarkupSafe==2.1.5
40
+ matplotlib==3.9.1
41
+ mdurl==0.1.2
42
+ numpy==2.0.1
43
+ oauthlib==3.2.2
44
+ orjson==3.10.6
45
+ packaging==24.1
46
+ pandas==2.2.2
47
+ pillow==10.4.0
48
+ proto-plus==1.24.0
49
+ protobuf==5.27.2
50
+ pyasn1==0.6.0
51
+ pyasn1_modules==0.4.0
52
+ pydantic==2.8.2
53
+ pydantic_core==2.20.1
54
+ pydub==0.25.1
55
+ Pygments==2.18.0
56
+ pyparsing==3.1.2
57
+ python-dateutil==2.9.0.post0
58
+ python-dotenv==1.0.1
59
+ python-multipart==0.0.9
60
+ pytz==2024.1
61
+ PyYAML==6.0.1
62
+ requests==2.32.3
63
+ requests-oauthlib==2.0.0
64
+ rich==13.7.1
65
+ rsa==4.9
66
+ ruff==0.5.4
67
+ semantic-version==2.10.0
68
+ shellingham==1.5.4
69
+ six==1.16.0
70
+ sniffio==1.3.1
71
+ starlette==0.37.2
72
+ tomlkit==0.12.0
73
+ tqdm==4.66.4
74
+ typer==0.12.3
75
+ typing_extensions==4.12.2
76
+ tzdata==2024.1
77
+ uritemplate==4.1.1
78
+ urllib3==2.2.2
79
+ uvicorn==0.30.3
80
+ watchfiles==0.22.0
81
+ websockets==11.0.3
token.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"token": "ya29.a0AXooCgtV7lhzlEXD5fjktrU37f6lK3ZL5xBZmzJi008YSDG8GW-IyAsBnExErsgZAwI28feLu9HCJCy6ZHP5Svh4VXwQ_X0jZx4S-FWPnsN84FHpIWSWBxeJ-vtaWkPYHzlmxw-EINvWWCLoUouCG5Q-Fg0fcd-Xzl6ZaCgYKAYoSARESFQHGX2Mi7TDslGvIMh-alu0wG0T-qA0171", "refresh_token": "1//0covSudIU1evuCgYIARAAGAwSNwF-L9IrhIMZ2onxTwu09dvwZjJuKofnWr1FYfH0ANh9PLNVhu2ZRS3tdzr68bHoIUTmdKMn6iM", "token_uri": "https://oauth2.googleapis.com/token", "client_id": "885206778440-77f6gp3h0mb1mso22v48c0k0s2ouknh9.apps.googleusercontent.com", "client_secret": "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", "scopes": ["https://www.googleapis.com/auth/drive"], "universe_domain": "googleapis.com", "account": "", "expiry": "2024-07-25T13:34:55.268148Z"}