Spaces:
Running
Running
Upload fso_kaggle_ingestor.py with huggingface_hub
Browse files- fso_kaggle_ingestor.py +58 -0
fso_kaggle_ingestor.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import time
|
| 4 |
+
|
| 5 |
+
class KaggleIngestor:
|
| 6 |
+
"""
|
| 7 |
+
Law XII Component: The Global Data Ingestor (Kaggle)
|
| 8 |
+
"""
|
| 9 |
+
def __init__(self, vault, m=256, k=4):
|
| 10 |
+
self.vault = vault
|
| 11 |
+
self.m = m
|
| 12 |
+
self.k = k
|
| 13 |
+
self.api = None
|
| 14 |
+
|
| 15 |
+
def authenticate(self):
|
| 16 |
+
# Delayed import to ensure environment variables are set
|
| 17 |
+
from kaggle.api.kaggle_api_extended import KaggleApi
|
| 18 |
+
try:
|
| 19 |
+
self.api = KaggleApi()
|
| 20 |
+
self.api.authenticate()
|
| 21 |
+
print("\n[✓] KAGGLE AUTHENTICATION SECURED.")
|
| 22 |
+
return True
|
| 23 |
+
except Exception as e:
|
| 24 |
+
print(f" [-] KAGGLE AUTH ERROR: {str(e)}")
|
| 25 |
+
return False
|
| 26 |
+
|
| 27 |
+
def download_and_shatter_dataset(self, dataset_id, orchestrator):
|
| 28 |
+
if not self.api:
|
| 29 |
+
if not self.authenticate(): return
|
| 30 |
+
|
| 31 |
+
print(f"\n[*] [KAGGLE]: Consuming Dataset '{dataset_id}'...")
|
| 32 |
+
try:
|
| 33 |
+
dataset_name = dataset_id.split('/')[-1]
|
| 34 |
+
download_path = f"./kaggle_data/{dataset_name}"
|
| 35 |
+
os.makedirs(download_path, exist_ok=True)
|
| 36 |
+
self.api.dataset_download_files(dataset_id, path=download_path, unzip=True)
|
| 37 |
+
|
| 38 |
+
total_atoms = 0
|
| 39 |
+
for root, dirs, files in os.walk(download_path):
|
| 40 |
+
for file in files:
|
| 41 |
+
file_path = os.path.join(root, file)
|
| 42 |
+
if os.path.getsize(file_path) > 1024 * 100: continue # Skip large files for demo
|
| 43 |
+
with open(file_path, "rb") as f:
|
| 44 |
+
content = f.read()
|
| 45 |
+
atoms = orchestrator.tgi.ingestor.shatterer.shatter(file, content)
|
| 46 |
+
for atom in atoms:
|
| 47 |
+
orchestrator.tgi.ingestor.topological_manifold[atom['coord']] = {
|
| 48 |
+
"filename": file, "data": atom['data'], "fiber": atom['fiber'], "type": "kaggle_data"
|
| 49 |
+
}
|
| 50 |
+
total_atoms += 1
|
| 51 |
+
print(f"\n[✓] KAGGLE INGESTION COMPLETE: {total_atoms} atoms added.")
|
| 52 |
+
return True
|
| 53 |
+
except Exception as e:
|
| 54 |
+
print(f" [-] KAGGLE INGESTION FAILED: {str(e)}")
|
| 55 |
+
return False
|
| 56 |
+
|
| 57 |
+
if __name__ == "__main__":
|
| 58 |
+
print("Kaggle Ingestor Ready.")
|