Spaces:
Runtime error
Runtime error
Update
Browse files
app.py
CHANGED
|
@@ -7,11 +7,15 @@ import pandas as pd
|
|
| 7 |
from PIL import Image
|
| 8 |
from tqdm import tqdm
|
| 9 |
import logging
|
|
|
|
| 10 |
|
| 11 |
# Set up logging
|
| 12 |
logging.basicConfig(level=logging.INFO)
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
| 15 |
class DatasetManager:
|
| 16 |
def __init__(self, local_images_dir="downloaded_cards"):
|
| 17 |
self.local_images_dir = local_images_dir
|
|
@@ -24,10 +28,31 @@ class DatasetManager:
|
|
| 24 |
def authenticate_drive(self):
|
| 25 |
"""Authenticate with Google Drive"""
|
| 26 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
gauth = GoogleAuth()
|
| 28 |
-
#
|
| 29 |
-
gauth.settings['
|
| 30 |
-
gauth.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
self.drive = GoogleDrive(gauth)
|
| 32 |
return True, "Successfully authenticated with Google Drive"
|
| 33 |
except Exception as e:
|
|
@@ -131,25 +156,52 @@ def process_pipeline(folder_id, naming_convention):
|
|
| 131 |
success, hf_message = manager.update_huggingface_dataset(renamed_files)
|
| 132 |
return f"{message}\n{hf_message}"
|
| 133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
# Gradio interface
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
label="Google Drive File/Folder ID",
|
| 140 |
placeholder="Enter the ID from your Google Drive URL",
|
| 141 |
value="151VOxPO91mg0C3ORiioGUd4hogzP1ujm"
|
| 142 |
-
)
|
| 143 |
-
gr.Textbox(
|
| 144 |
label="Naming Convention",
|
| 145 |
placeholder="e.g., sports_card",
|
| 146 |
value="sports_card"
|
| 147 |
)
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
if __name__ == "__main__":
|
| 155 |
demo.launch()
|
|
|
|
| 7 |
from PIL import Image
|
| 8 |
from tqdm import tqdm
|
| 9 |
import logging
|
| 10 |
+
import json
|
| 11 |
|
| 12 |
# Set up logging
|
| 13 |
logging.basicConfig(level=logging.INFO)
|
| 14 |
logger = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
+
# Check for environment variables
|
| 17 |
+
GOOGLE_CREDENTIALS = os.getenv('GOOGLE_CREDENTIALS')
|
| 18 |
+
|
| 19 |
class DatasetManager:
|
| 20 |
def __init__(self, local_images_dir="downloaded_cards"):
|
| 21 |
self.local_images_dir = local_images_dir
|
|
|
|
| 28 |
def authenticate_drive(self):
|
| 29 |
"""Authenticate with Google Drive"""
|
| 30 |
try:
|
| 31 |
+
# Create client_secrets.json from environment variable if available
|
| 32 |
+
if GOOGLE_CREDENTIALS:
|
| 33 |
+
with open('client_secrets.json', 'w') as f:
|
| 34 |
+
f.write(GOOGLE_CREDENTIALS)
|
| 35 |
+
|
| 36 |
gauth = GoogleAuth()
|
| 37 |
+
# Configure for headless authentication
|
| 38 |
+
gauth.settings['get_refresh_token'] = True
|
| 39 |
+
gauth.LoadCredentialsFile("mycreds.txt")
|
| 40 |
+
|
| 41 |
+
if gauth.credentials is None:
|
| 42 |
+
# Create local webserver for authentication if running locally
|
| 43 |
+
if os.getenv('SPACE_ID') is None:
|
| 44 |
+
gauth.LocalWebserverAuth()
|
| 45 |
+
else:
|
| 46 |
+
# For Hugging Face Spaces, use service account or saved credentials
|
| 47 |
+
gauth.CommandLineAuth()
|
| 48 |
+
elif gauth.access_token_expired:
|
| 49 |
+
gauth.Refresh()
|
| 50 |
+
else:
|
| 51 |
+
gauth.Authorize()
|
| 52 |
+
|
| 53 |
+
# Save the credentials for future use
|
| 54 |
+
gauth.SaveCredentialsFile("mycreds.txt")
|
| 55 |
+
|
| 56 |
self.drive = GoogleDrive(gauth)
|
| 57 |
return True, "Successfully authenticated with Google Drive"
|
| 58 |
except Exception as e:
|
|
|
|
| 156 |
success, hf_message = manager.update_huggingface_dataset(renamed_files)
|
| 157 |
return f"{message}\n{hf_message}"
|
| 158 |
|
| 159 |
+
# Authentication status interface
|
| 160 |
+
def check_auth_status():
|
| 161 |
+
try:
|
| 162 |
+
gauth = GoogleAuth()
|
| 163 |
+
gauth.LoadCredentialsFile("mycreds.txt")
|
| 164 |
+
if gauth.credentials is not None and not gauth.access_token_expired:
|
| 165 |
+
return "🟢 Authenticated with Google Drive"
|
| 166 |
+
else:
|
| 167 |
+
return "🔴 Not authenticated with Google Drive"
|
| 168 |
+
except Exception as e:
|
| 169 |
+
return f"🔴 Error checking authentication: {str(e)}"
|
| 170 |
+
|
| 171 |
# Gradio interface
|
| 172 |
+
with gr.Blocks() as demo:
|
| 173 |
+
gr.Markdown("# Sports Cards Dataset Processor")
|
| 174 |
+
gr.Markdown("Download card images from Google Drive and add them to the sports-cards dataset")
|
| 175 |
+
|
| 176 |
+
# Authentication status
|
| 177 |
+
auth_status = gr.Textbox(
|
| 178 |
+
label="Authentication Status",
|
| 179 |
+
value=check_auth_status(),
|
| 180 |
+
interactive=False
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
with gr.Row():
|
| 184 |
+
folder_id = gr.Textbox(
|
| 185 |
label="Google Drive File/Folder ID",
|
| 186 |
placeholder="Enter the ID from your Google Drive URL",
|
| 187 |
value="151VOxPO91mg0C3ORiioGUd4hogzP1ujm"
|
| 188 |
+
)
|
| 189 |
+
naming = gr.Textbox(
|
| 190 |
label="Naming Convention",
|
| 191 |
placeholder="e.g., sports_card",
|
| 192 |
value="sports_card"
|
| 193 |
)
|
| 194 |
+
|
| 195 |
+
# Process button and output
|
| 196 |
+
process_btn = gr.Button("Process Images")
|
| 197 |
+
output = gr.Textbox(label="Status")
|
| 198 |
+
|
| 199 |
+
# Handle processing
|
| 200 |
+
process_btn.click(
|
| 201 |
+
fn=process_pipeline,
|
| 202 |
+
inputs=[folder_id, naming],
|
| 203 |
+
outputs=output
|
| 204 |
+
)
|
| 205 |
|
| 206 |
if __name__ == "__main__":
|
| 207 |
demo.launch()
|