Spaces:
Running
Running
Make Kaggle authentication lazy for HF Space
Browse files
src/mlpipeline/components/data_ingestion.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import shutil
|
| 3 |
from pathlib import Path
|
| 4 |
-
from kaggle.api.kaggle_api_extended import KaggleApi
|
| 5 |
|
| 6 |
from mlpipeline.entity import DataIngestionConfig, DataIngestionArtifact
|
| 7 |
from mlpipeline.logging.logger import get_logger
|
|
@@ -14,13 +13,29 @@ logger = get_logger(__name__)
|
|
| 14 |
class DataIngestion:
|
| 15 |
def __init__(self, config: DataIngestionConfig):
|
| 16 |
self.config = config
|
| 17 |
-
self.kaggle_api =
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
def download_data(self) -> DataIngestionArtifact:
|
| 21 |
try:
|
| 22 |
logger.info("Starting data ingestion")
|
| 23 |
|
|
|
|
|
|
|
|
|
|
| 24 |
os.makedirs(self.config.root_dir, exist_ok=True)
|
| 25 |
|
| 26 |
competition_name = "playground-series-s6e2"
|
|
|
|
| 1 |
import os
|
| 2 |
import shutil
|
| 3 |
from pathlib import Path
|
|
|
|
| 4 |
|
| 5 |
from mlpipeline.entity import DataIngestionConfig, DataIngestionArtifact
|
| 6 |
from mlpipeline.logging.logger import get_logger
|
|
|
|
| 13 |
class DataIngestion:
|
| 14 |
def __init__(self, config: DataIngestionConfig):
|
| 15 |
self.config = config
|
| 16 |
+
self.kaggle_api = None
|
| 17 |
+
|
| 18 |
+
def _authenticate_kaggle(self):
|
| 19 |
+
"""Lazy authentication - only when needed"""
|
| 20 |
+
if self.kaggle_api is None:
|
| 21 |
+
try:
|
| 22 |
+
from kaggle.api.kaggle_api_extended import KaggleApi
|
| 23 |
+
self.kaggle_api = KaggleApi()
|
| 24 |
+
self.kaggle_api.authenticate()
|
| 25 |
+
logger.info("Kaggle API authenticated successfully")
|
| 26 |
+
except Exception as e:
|
| 27 |
+
raise DataIngestionException(
|
| 28 |
+
f"Failed to authenticate with Kaggle API: {e}",
|
| 29 |
+
sys
|
| 30 |
+
)
|
| 31 |
|
| 32 |
def download_data(self) -> DataIngestionArtifact:
|
| 33 |
try:
|
| 34 |
logger.info("Starting data ingestion")
|
| 35 |
|
| 36 |
+
# Authenticate only when downloading
|
| 37 |
+
self._authenticate_kaggle()
|
| 38 |
+
|
| 39 |
os.makedirs(self.config.root_dir, exist_ok=True)
|
| 40 |
|
| 41 |
competition_name = "playground-series-s6e2"
|