BHARGAV REDDY commited on
Upload fetch_data.py with huggingface_hub
Browse files- fetch_data.py +59 -9
fetch_data.py
CHANGED
|
@@ -45,8 +45,8 @@ def download_huggingface(repo_id: str, out_dir: Path, hf_token: str = None):
|
|
| 45 |
# Auto-extract any zip files found in the download
|
| 46 |
_extract_zips(out_dir)
|
| 47 |
|
| 48 |
-
# If index.json landed in a subdirectory, move
|
| 49 |
-
_flatten_to_root(out_dir)
|
| 50 |
|
| 51 |
_verify(out_dir)
|
| 52 |
|
|
@@ -79,6 +79,7 @@ def download_gdrive(gdrive_id: str, out_dir: Path):
|
|
| 79 |
z.extractall(out_dir)
|
| 80 |
dest.unlink()
|
| 81 |
print(f" Downloaded to: {out_dir}")
|
|
|
|
| 82 |
_verify(out_dir)
|
| 83 |
|
| 84 |
|
|
@@ -90,6 +91,7 @@ def copy_local(local_path: str, out_dir: Path):
|
|
| 90 |
raise FileNotFoundError(f"Local path not found: {src}")
|
| 91 |
if out_dir.resolve() == src.resolve():
|
| 92 |
print(f" Source == destination, no copy needed.")
|
|
|
|
| 93 |
_verify(out_dir)
|
| 94 |
return
|
| 95 |
print(f" Copying {src} → {out_dir}")
|
|
@@ -97,6 +99,7 @@ def copy_local(local_path: str, out_dir: Path):
|
|
| 97 |
shutil.rmtree(out_dir)
|
| 98 |
shutil.copytree(src, out_dir)
|
| 99 |
print(f" Copied to: {out_dir}")
|
|
|
|
| 100 |
_verify(out_dir)
|
| 101 |
|
| 102 |
|
|
@@ -116,15 +119,62 @@ def _extract_zips(data_dir: Path):
|
|
| 116 |
print(f" Removed {zf.name}")
|
| 117 |
|
| 118 |
|
| 119 |
-
def
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
if (data_dir / "index.json").exists():
|
| 123 |
-
return
|
|
|
|
| 124 |
candidates = list(data_dir.glob("**/index.json"))
|
| 125 |
-
if
|
| 126 |
-
return
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
print(f" Moving contents from {sub.relative_to(data_dir)}/ up to {data_dir.name}/ ...")
|
| 129 |
for item in sub.iterdir():
|
| 130 |
dest = data_dir / item.name
|
|
|
|
| 45 |
# Auto-extract any zip files found in the download
|
| 46 |
_extract_zips(out_dir)
|
| 47 |
|
| 48 |
+
# If index.json landed in a subdirectory, move the intended dataset up
|
| 49 |
+
_flatten_to_root(out_dir, preferred_name=out_dir.name)
|
| 50 |
|
| 51 |
_verify(out_dir)
|
| 52 |
|
|
|
|
| 79 |
z.extractall(out_dir)
|
| 80 |
dest.unlink()
|
| 81 |
print(f" Downloaded to: {out_dir}")
|
| 82 |
+
_flatten_to_root(out_dir, preferred_name=out_dir.name)
|
| 83 |
_verify(out_dir)
|
| 84 |
|
| 85 |
|
|
|
|
| 91 |
raise FileNotFoundError(f"Local path not found: {src}")
|
| 92 |
if out_dir.resolve() == src.resolve():
|
| 93 |
print(f" Source == destination, no copy needed.")
|
| 94 |
+
_flatten_to_root(out_dir, preferred_name=out_dir.name)
|
| 95 |
_verify(out_dir)
|
| 96 |
return
|
| 97 |
print(f" Copying {src} → {out_dir}")
|
|
|
|
| 99 |
shutil.rmtree(out_dir)
|
| 100 |
shutil.copytree(src, out_dir)
|
| 101 |
print(f" Copied to: {out_dir}")
|
| 102 |
+
_flatten_to_root(out_dir, preferred_name=out_dir.name)
|
| 103 |
_verify(out_dir)
|
| 104 |
|
| 105 |
|
|
|
|
| 119 |
print(f" Removed {zf.name}")
|
| 120 |
|
| 121 |
|
| 122 |
+
def _read_index_summary(index_path: Path):
|
| 123 |
+
try:
|
| 124 |
+
with open(index_path, encoding="utf-8") as f:
|
| 125 |
+
idx = json.load(f)
|
| 126 |
+
except Exception:
|
| 127 |
+
return {"chunks": 0, "tokens": 0}
|
| 128 |
+
chunks = idx.get("chunks", [])
|
| 129 |
+
total_tokens = sum(c.get("dim", 0) for c in chunks)
|
| 130 |
+
return {"chunks": len(chunks), "tokens": total_tokens}
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
def _pick_dataset_subdir(candidates, preferred_name: str | None = None):
|
| 134 |
+
"""Pick the most likely dataset subdir from multiple nested index.json files.
|
| 135 |
+
|
| 136 |
+
Priority:
|
| 137 |
+
1. Parent folder name exactly matches preferred_name
|
| 138 |
+
2. Parent folder name contains preferred_name
|
| 139 |
+
3. Highest token count
|
| 140 |
+
"""
|
| 141 |
+
ranked = []
|
| 142 |
+
preferred_name = (preferred_name or "").lower()
|
| 143 |
+
|
| 144 |
+
for index_path in candidates:
|
| 145 |
+
parent = index_path.parent
|
| 146 |
+
summary = _read_index_summary(index_path)
|
| 147 |
+
name = parent.name.lower()
|
| 148 |
+
exact = int(bool(preferred_name and name == preferred_name))
|
| 149 |
+
contains = int(bool(preferred_name and preferred_name in name))
|
| 150 |
+
ranked.append((exact, contains, summary["tokens"], summary["chunks"], parent, summary))
|
| 151 |
+
|
| 152 |
+
ranked.sort(reverse=True, key=lambda item: (item[0], item[1], item[2], item[3]))
|
| 153 |
+
return ranked[0][4], ranked[0][5]
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
def _flatten_to_root(data_dir: Path, preferred_name: str | None = None):
|
| 157 |
+
"""If index.json is nested, move the intended dataset subfolder up to data_dir."""
|
| 158 |
if (data_dir / "index.json").exists():
|
| 159 |
+
return
|
| 160 |
+
|
| 161 |
candidates = list(data_dir.glob("**/index.json"))
|
| 162 |
+
if not candidates:
|
| 163 |
+
return
|
| 164 |
+
|
| 165 |
+
if len(candidates) == 1:
|
| 166 |
+
sub = candidates[0].parent
|
| 167 |
+
summary = _read_index_summary(candidates[0])
|
| 168 |
+
else:
|
| 169 |
+
sub, summary = _pick_dataset_subdir(candidates, preferred_name=preferred_name)
|
| 170 |
+
print(
|
| 171 |
+
f" Multiple nested datasets found; selected {sub.relative_to(data_dir)}/ "
|
| 172 |
+
f"({summary['chunks']} chunks, {summary['tokens']:,} tokens)"
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
if sub == data_dir:
|
| 176 |
+
return
|
| 177 |
+
|
| 178 |
print(f" Moving contents from {sub.relative_to(data_dir)}/ up to {data_dir.name}/ ...")
|
| 179 |
for item in sub.iterdir():
|
| 180 |
dest = data_dir / item.name
|