| # /// script | |
| # requires-python = ">=3.10" | |
| # dependencies = [ | |
| # "pandas", | |
| # "pyarrow", | |
| # "pydantic", | |
| # ] | |
| # /// | |
| import os | |
| import argparse | |
| from pathlib import Path | |
| import pandas as pd | |
| from pydantic import BaseModel, field_validator | |
| from typing import Optional | |
| IMAGE_EXT = {".jpg", ".jpeg", ".png", ".webp"} | |
| VIDEO_EXT = {".mp4", ".webm", ".avi", ".mov"} | |
| def clean_name(name: str) -> str: | |
| return name.strip().replace(" ", "_") | |
| class MemeRecord(BaseModel): | |
| image_file_name: Optional[str] = None | |
| video_file_name: Optional[str] = None | |
| category: str | |
| caption: str = "" | |
| def normalize_path(cls, v: Optional[str]) -> Optional[str]: | |
| return v.replace("\\", "/") if v else v | |
| def normalize_category(cls, v: str) -> str: | |
| return clean_name(v) | |
| def main(root: Path): | |
| src = root / "meme" | |
| if not src.exists(): | |
| raise SystemExit(f"❌ Missing folder: {src}") | |
| records: list[MemeRecord] = [] | |
| for category_dir in src.iterdir(): | |
| if not category_dir.is_dir(): | |
| continue | |
| category = clean_name(category_dir.name) | |
| for root_dir, _, files in os.walk(category_dir): | |
| for file in files: | |
| path = Path(root_dir) / file | |
| ext = path.suffix.lower() | |
| rel = str(path.relative_to(root)) | |
| if ext in IMAGE_EXT: | |
| records.append( | |
| MemeRecord( | |
| image_file_name=rel, | |
| category=category | |
| ) | |
| ) | |
| elif ext in VIDEO_EXT: | |
| records.append( | |
| MemeRecord( | |
| video_file_name=rel, | |
| category=category | |
| ) | |
| ) | |
| if not records: | |
| raise SystemExit("❌ No media found") | |
| df = pd.DataFrame([r.model_dump() for r in records]) | |
| df.to_csv(root / "metadata.csv", index=False) | |
| df.to_parquet(root / "metadata.parquet", index=False) | |
| print(f"✔ Indexed {len(df)} files (HF-safe)") | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--root", type=Path, default=Path.cwd()) | |
| args = parser.parse_args() | |
| main(args.root) | |