Add files using upload-large-folder tool
Browse files- CreateDataset.py +22 -29
CreateDataset.py
CHANGED
|
@@ -11,7 +11,7 @@ import argparse
|
|
| 11 |
from pathlib import Path
|
| 12 |
import pandas as pd
|
| 13 |
from pydantic import BaseModel, field_validator
|
| 14 |
-
from typing import
|
| 15 |
|
| 16 |
|
| 17 |
IMAGE_EXT = {".jpg", ".jpeg", ".png", ".webp"}
|
|
@@ -23,15 +23,15 @@ def clean_name(name: str) -> str:
|
|
| 23 |
|
| 24 |
|
| 25 |
class MemeRecord(BaseModel):
|
| 26 |
-
|
| 27 |
-
|
| 28 |
category: str
|
| 29 |
caption: str = ""
|
| 30 |
|
| 31 |
-
@field_validator("
|
| 32 |
@classmethod
|
| 33 |
-
def normalize_path(cls, v: str) -> str:
|
| 34 |
-
return v.replace("\\", "/")
|
| 35 |
|
| 36 |
@field_validator("category")
|
| 37 |
@classmethod
|
|
@@ -41,7 +41,6 @@ class MemeRecord(BaseModel):
|
|
| 41 |
|
| 42 |
def main(root: Path):
|
| 43 |
src = root / "meme"
|
| 44 |
-
|
| 45 |
if not src.exists():
|
| 46 |
raise SystemExit(f"❌ Missing folder: {src}")
|
| 47 |
|
|
@@ -58,42 +57,36 @@ def main(root: Path):
|
|
| 58 |
path = Path(root_dir) / file
|
| 59 |
ext = path.suffix.lower()
|
| 60 |
|
|
|
|
|
|
|
| 61 |
if ext in IMAGE_EXT:
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
elif ext in VIDEO_EXT:
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
MemeRecord(
|
| 70 |
-
file_name=str(path.relative_to(root)),
|
| 71 |
-
type=media_type,
|
| 72 |
-
category=category,
|
| 73 |
-
caption=""
|
| 74 |
)
|
| 75 |
-
)
|
| 76 |
|
| 77 |
if not records:
|
| 78 |
-
raise SystemExit("❌ No media
|
| 79 |
|
| 80 |
df = pd.DataFrame([r.model_dump() for r in records])
|
| 81 |
-
|
| 82 |
-
# ✅ BOTH formats – HF folder builder expects CSV
|
| 83 |
df.to_csv(root / "metadata.csv", index=False)
|
| 84 |
df.to_parquet(root / "metadata.parquet", index=False)
|
| 85 |
|
| 86 |
-
print(f"✔ Indexed {len(df)} files (
|
| 87 |
|
| 88 |
|
| 89 |
if __name__ == "__main__":
|
| 90 |
parser = argparse.ArgumentParser()
|
| 91 |
-
parser.add_argument(
|
| 92 |
-
"--root",
|
| 93 |
-
type=Path,
|
| 94 |
-
default=Path.cwd(),
|
| 95 |
-
help="Dataset root directory"
|
| 96 |
-
)
|
| 97 |
args = parser.parse_args()
|
| 98 |
main(args.root)
|
| 99 |
|
|
|
|
| 11 |
from pathlib import Path
|
| 12 |
import pandas as pd
|
| 13 |
from pydantic import BaseModel, field_validator
|
| 14 |
+
from typing import Optional
|
| 15 |
|
| 16 |
|
| 17 |
IMAGE_EXT = {".jpg", ".jpeg", ".png", ".webp"}
|
|
|
|
| 23 |
|
| 24 |
|
| 25 |
class MemeRecord(BaseModel):
|
| 26 |
+
image_file_name: Optional[str] = None
|
| 27 |
+
video_file_name: Optional[str] = None
|
| 28 |
category: str
|
| 29 |
caption: str = ""
|
| 30 |
|
| 31 |
+
@field_validator("image_file_name", "video_file_name")
|
| 32 |
@classmethod
|
| 33 |
+
def normalize_path(cls, v: Optional[str]) -> Optional[str]:
|
| 34 |
+
return v.replace("\\", "/") if v else v
|
| 35 |
|
| 36 |
@field_validator("category")
|
| 37 |
@classmethod
|
|
|
|
| 41 |
|
| 42 |
def main(root: Path):
|
| 43 |
src = root / "meme"
|
|
|
|
| 44 |
if not src.exists():
|
| 45 |
raise SystemExit(f"❌ Missing folder: {src}")
|
| 46 |
|
|
|
|
| 57 |
path = Path(root_dir) / file
|
| 58 |
ext = path.suffix.lower()
|
| 59 |
|
| 60 |
+
rel = str(path.relative_to(root))
|
| 61 |
+
|
| 62 |
if ext in IMAGE_EXT:
|
| 63 |
+
records.append(
|
| 64 |
+
MemeRecord(
|
| 65 |
+
image_file_name=rel,
|
| 66 |
+
category=category
|
| 67 |
+
)
|
| 68 |
+
)
|
| 69 |
elif ext in VIDEO_EXT:
|
| 70 |
+
records.append(
|
| 71 |
+
MemeRecord(
|
| 72 |
+
video_file_name=rel,
|
| 73 |
+
category=category
|
| 74 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
)
|
|
|
|
| 76 |
|
| 77 |
if not records:
|
| 78 |
+
raise SystemExit("❌ No media found")
|
| 79 |
|
| 80 |
df = pd.DataFrame([r.model_dump() for r in records])
|
|
|
|
|
|
|
| 81 |
df.to_csv(root / "metadata.csv", index=False)
|
| 82 |
df.to_parquet(root / "metadata.parquet", index=False)
|
| 83 |
|
| 84 |
+
print(f"✔ Indexed {len(df)} files (HF-safe)")
|
| 85 |
|
| 86 |
|
| 87 |
if __name__ == "__main__":
|
| 88 |
parser = argparse.ArgumentParser()
|
| 89 |
+
parser.add_argument("--root", type=Path, default=Path.cwd())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
args = parser.parse_args()
|
| 91 |
main(args.root)
|
| 92 |
|