Ajay98 commited on
Commit
da4daf3
·
verified ·
1 Parent(s): cfad6fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -53
app.py CHANGED
@@ -1,54 +1,35 @@
1
- import requests
2
  import os
3
- from googleapiclient.discovery import build
4
- from google.oauth2.service_account import Credentials
5
-
6
- # Define the Google Drive API setup
7
- SCOPES = ['https://www.googleapis.com/auth/drive']
8
- SERVICE_ACCOUNT_FILE = 'service_account.json'
9
-
10
- # URL of the service account JSON file on Hugging Face
11
- HUGGINGFACE_JSON_URL = 'https://huggingface.co/username/repository-name/resolve/main/service_account.json'
12
-
13
- def download_service_account_file():
14
- """Download the service account JSON file from Hugging Face."""
15
- if not os.path.exists(SERVICE_ACCOUNT_FILE): # Check if file already exists
16
- print("Downloading service account file from Hugging Face...")
17
- response = requests.get(HUGGINGFACE_JSON_URL)
18
- if response.status_code == 200:
19
- with open(SERVICE_ACCOUNT_FILE, 'wb') as f:
20
- f.write(response.content)
21
- print(f"Service account file downloaded: {SERVICE_ACCOUNT_FILE}")
22
- else:
23
- raise Exception(f"Failed to download service account file: {response.status_code}, {response.text}")
24
- else:
25
- print(f"Service account file already exists: {SERVICE_ACCOUNT_FILE}")
26
-
27
- # Download the service account file
28
- download_service_account_file()
29
-
30
- # Authenticate with the Google API
31
- def authenticate_google_drive():
32
- """Authenticate and return the Google Drive service object."""
33
- credentials = Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
34
- drive_service = build('drive', 'v3', credentials=credentials)
35
- print("Google Drive API authenticated successfully!")
36
- return drive_service
37
-
38
- # Example usage of the authenticated service
39
- drive_service = authenticate_google_drive()
40
-
41
- # Example API call: List files in Google Drive
42
- def list_drive_files():
43
- """List files in Google Drive."""
44
- results = drive_service.files().list(pageSize=10).execute()
45
- items = results.get('files', [])
46
- if not items:
47
- print("No files found.")
48
- else:
49
- print("Files:")
50
- for item in items:
51
- print(f"{item['name']} ({item['id']})")
52
-
53
- # Call the function to list files
54
- list_drive_files()
 
 
1
  import os
2
+ from flask import Flask, jsonify
3
+ from google.cloud import storage
4
+
5
+ # Check if you're using Hugging Face Secrets or a local file
6
+ service_account_secret = os.getenv("SERVICE_ACCOUNT_JSON")
7
+ if service_account_secret:
8
+ # Save the service account from the secret to a file
9
+ with open("service-account.json", "w") as f:
10
+ f.write(service_account_secret)
11
+
12
+ # Set the environment variable to point to the service account file
13
+ os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "service-account.json"
14
+
15
+ # Function to list Google Cloud Storage buckets
16
+ def list_gcs_buckets():
17
+ try:
18
+ client = storage.Client()
19
+ buckets = list(client.list_buckets())
20
+ return [bucket.name for bucket in buckets]
21
+ except Exception as e:
22
+ return str(e)
23
+
24
+ # Flask app to display bucket names
25
+ app = Flask(__name__)
26
+
27
+ @app.route("/")
28
+ def home():
29
+ buckets = list_gcs_buckets()
30
+ if isinstance(buckets, str): # If an error occurred
31
+ return jsonify({"error": buckets}), 500
32
+ return jsonify({"buckets": buckets})
33
+
34
+ if __name__ == "__main__":
35
+ app.run(host="0.0.0.0", port=8080)