-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1549 from vespa-engine/thomasht86/vespa-feed-to-h…
…f-dataset.py (colpalidemo) vespa feed to hf dataset.py
- Loading branch information
Showing
3 changed files
with
102 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import pandas as pd | ||
from dotenv import load_dotenv | ||
import os | ||
import base64 | ||
from PIL import Image | ||
import io | ||
from datasets import Dataset, Image as HFImage | ||
from pathlib import Path | ||
from tqdm import tqdm | ||
|
||
load_dotenv() | ||
|
||
df = pd.read_json("output/vespa_feed_full.jsonl", lines=True) | ||
df = pd.json_normalize(df["fields"].tolist()) | ||
|
||
dataset_dir = Path("hf_dataset") | ||
image_dir = dataset_dir / "images" | ||
os.makedirs(image_dir, exist_ok=True) | ||
|
||
|
||
def save_image(image_data, filename): | ||
img_data = base64.b64decode(image_data) | ||
img = Image.open(io.BytesIO(img_data)) | ||
img.save(filename) | ||
|
||
|
||
for idx, row in tqdm(df.iterrows()): | ||
blur_filename = os.path.join(image_dir, f"blur_{idx}.jpg") | ||
full_filename = os.path.join(image_dir, f"full_{idx}.jpg") | ||
save_image(row["blur_image"], blur_filename) | ||
save_image(row["full_image"], full_filename) | ||
df.at[idx, "blur_image"] = blur_filename | ||
df.at[idx, "full_image"] = full_filename | ||
|
||
|
||
# Step 3: Convert to Hugging Face Dataset | ||
dataset = ( | ||
Dataset.from_dict(df.to_dict(orient="list")) | ||
.cast_column("blur_image", HFImage()) | ||
.cast_column("full_image", HFImage()) | ||
) | ||
dataset.push_to_hub("vespa-engine/gpfg-QA", private=True) |