next-gpt-lite / tools /extract_samples.py
setariaa's picture
Code + report (attempt 1)
0b0e1c1 verified
Raw History Blame Contribute Delete
1.72 kB
"""Extract a handful of raw test images (and a few audio clips) for demos and figures."""
from __future__ import annotations
import io
import pathlib
import sys
import pyarrow.parquet as pq
from PIL import Image
ROOT = pathlib.Path(__file__).resolve().parents[1]
SAMPLES = ROOT / "data" / "samples"
CACHE = ROOT / ".cache"
def main(n_images: int = 60, n_audio: int = 6) -> None:
SAMPLES.mkdir(parents=True, exist_ok=True)
p = CACHE / "jxie--flickr8k" / "data" / "test-00000-of-00001-42a2661d12c73e48.parquet"
rows = pq.ParquetFile(p).iter_batches(batch_size=8)
count = 0
for batch in rows:
for row in batch.to_pylist():
if count >= n_images:
break
img = Image.open(io.BytesIO(row["image"]["bytes"])).convert("RGB")
img.save(SAMPLES / f"test_image_{count}.jpg")
count += 1
if count >= n_images:
break
print("images", count, flush=True)
import soundfile as sf
p = CACHE / "ashraq--esc50" / "data" / "train-00000-of-00002-2f1ab7b824ec751f.parquet"
rows = pq.ParquetFile(p).iter_batches(batch_size=8)
count = 0
for batch in rows:
for row in batch.to_pylist():
if count >= n_audio:
break
data, sr = sf.read(io.BytesIO(row["audio"]["bytes"]), dtype="float32")
if data.ndim > 1:
data = data.mean(1)
sf.write(SAMPLES / f"test_audio_{count}.wav", data, sr)
(SAMPLES / f"test_audio_{count}.txt").write_text(row.get("category", ""), encoding="utf-8")
count += 1
if count >= n_audio:
break
print("audio", count)
if __name__ == "__main__":
main()