Datasets:
Download scripts/export_cards.py from bep40/grob-products-updated: direct link, hf CLI and curl.
- Browser
- Download file 1.43 kB
-
https://huggingface.co/datasets/bep40/grob-products-updated/resolve/main/scripts/export_cards.py
- Command line
-
hf download hf://datasets/bep40/grob-products-updated/scripts/export_cards.py
-
curl -L -o export_cards.py https://huggingface.co/datasets/bep40/grob-products-updated/resolve/main/scripts/export_cards.py
1.43 kB
| import requests, json, base64, os, io | |
| H = "https://huggingface.co" | |
| from PIL import Image | |
| from huggingface_hub import HfApi | |
| tok = os.environ["HF_TOKEN"] | |
| api = HfApi(token=tok, endpoint=H) | |
| def norm(s): | |
| return "".join(ch for ch in str(s) if ch.isdigit()) | |
| idx = requests.get("https://bep40-vai-avatar2.hf.space/api/catindex", params={"t": "vis2"}, timeout=120).json() | |
| prods = idx["products"] | |
| targets = ["7801700", "7804104", "7809104", "7801100", "7809109", "7804116", "7041200"] | |
| os.makedirs("/tmp/out", exist_ok=True) | |
| uploaded = [] | |
| for p in prods: | |
| if not isinstance(p, dict): | |
| continue | |
| sk = norm(p.get("sku", "")) | |
| if sk in targets: | |
| img = p.get("image") or "" | |
| if img.startswith("data:"): | |
| b = base64.b64decode(img.split(",", 1)[1]) | |
| fn = f"/tmp/out/card_{sk}.jpg" | |
| open(fn, "wb").write(b) | |
| im = Image.open(io.BytesIO(b)) | |
| print(f"{sk}: {im.size[0]}x{im.size[1]} name={p.get('name')!r} page={p.get('page')}") | |
| # upload to dataset repo for viewing | |
| info = api.upload_file(path_or_fileobj=b, path_in_repo=f"debug_cards/card_{sk}.jpg", | |
| repo_id="bep40/grob-products-updated", repo_type="dataset", | |
| commit_message=f"debug card {sk}") | |
| uploaded.append((sk, info.commit_url)) | |
| else: | |
| print(f"{sk}: image not data-uri ({img[:40]})") | |
| print("uploaded:", uploaded) | |