Spaces:
Sleeping
Sleeping
GotThatData
commited on
update
Browse files
app.py
CHANGED
@@ -7,14 +7,15 @@ import pandas as pd
|
|
7 |
from PIL import Image
|
8 |
from tqdm import tqdm
|
9 |
import logging
|
10 |
-
import
|
11 |
|
12 |
# Set up logging
|
13 |
logging.basicConfig(level=logging.INFO)
|
14 |
logger = logging.getLogger(__name__)
|
15 |
|
16 |
-
#
|
17 |
-
|
|
|
18 |
|
19 |
class DatasetManager:
|
20 |
def __init__(self, local_images_dir="downloaded_cards"):
|
@@ -28,30 +29,25 @@ class DatasetManager:
|
|
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 |
-
#
|
38 |
-
gauth.settings['
|
39 |
-
|
|
|
|
|
40 |
|
41 |
if gauth.credentials is None:
|
42 |
-
#
|
43 |
-
|
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("
|
55 |
|
56 |
self.drive = GoogleDrive(gauth)
|
57 |
return True, "Successfully authenticated with Google Drive"
|
@@ -156,52 +152,25 @@ def process_pipeline(folder_id, naming_convention):
|
|
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 |
-
|
173 |
-
|
174 |
-
|
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 |
-
|
190 |
label="Naming Convention",
|
191 |
placeholder="e.g., sports_card",
|
192 |
value="sports_card"
|
193 |
)
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
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()
|
|
|
7 |
from PIL import Image
|
8 |
from tqdm import tqdm
|
9 |
import logging
|
10 |
+
import yaml
|
11 |
|
12 |
# Set up logging
|
13 |
logging.basicConfig(level=logging.INFO)
|
14 |
logger = logging.getLogger(__name__)
|
15 |
|
16 |
+
# Load settings
|
17 |
+
with open('settings.yaml', 'r') as file:
|
18 |
+
settings = yaml.safe_load(file)
|
19 |
|
20 |
class DatasetManager:
|
21 |
def __init__(self, local_images_dir="downloaded_cards"):
|
|
|
29 |
def authenticate_drive(self):
|
30 |
"""Authenticate with Google Drive"""
|
31 |
try:
|
|
|
|
|
|
|
|
|
|
|
32 |
gauth = GoogleAuth()
|
33 |
+
# Use the settings from yaml file
|
34 |
+
gauth.settings['client_config_file'] = settings['client_secrets_file']
|
35 |
+
|
36 |
+
# Try to load saved credentials
|
37 |
+
gauth.LoadCredentialsFile("credentials.txt")
|
38 |
|
39 |
if gauth.credentials is None:
|
40 |
+
# Authenticate if no credentials found
|
41 |
+
gauth.LocalWebserverAuth()
|
|
|
|
|
|
|
|
|
42 |
elif gauth.access_token_expired:
|
43 |
+
# Refresh them if expired
|
44 |
gauth.Refresh()
|
45 |
else:
|
46 |
+
# Initialize the saved credentials
|
47 |
gauth.Authorize()
|
48 |
+
|
49 |
# Save the credentials for future use
|
50 |
+
gauth.SaveCredentialsFile("credentials.txt")
|
51 |
|
52 |
self.drive = GoogleDrive(gauth)
|
53 |
return True, "Successfully authenticated with Google Drive"
|
|
|
152 |
success, hf_message = manager.update_huggingface_dataset(renamed_files)
|
153 |
return f"{message}\n{hf_message}"
|
154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
# Gradio interface
|
156 |
+
demo = gr.Interface(
|
157 |
+
fn=process_pipeline,
|
158 |
+
inputs=[
|
159 |
+
gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
label="Google Drive File/Folder ID",
|
161 |
placeholder="Enter the ID from your Google Drive URL",
|
162 |
value="151VOxPO91mg0C3ORiioGUd4hogzP1ujm"
|
163 |
+
),
|
164 |
+
gr.Textbox(
|
165 |
label="Naming Convention",
|
166 |
placeholder="e.g., sports_card",
|
167 |
value="sports_card"
|
168 |
)
|
169 |
+
],
|
170 |
+
outputs=gr.Textbox(label="Status"),
|
171 |
+
title="Sports Cards Dataset Processor",
|
172 |
+
description="Download card images from Google Drive and add them to the sports-cards dataset"
|
173 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
|
175 |
if __name__ == "__main__":
|
176 |
demo.launch()
|