Spaces:
Sleeping
Sleeping
Commit ·
dccc3f5
1
Parent(s): ba8e81a
Fix image preview: build Parquet directly with PyArrow + HF schema metadata
Browse filesdatasets.Dataset was not writing the Arrow Image extension type metadata
correctly. Build the table directly with PyArrow using the exact struct
schema {bytes: binary, path: utf8} and a 'huggingface' schema metadata
key — the format the HF dataset viewer requires to render image columns.
Use replace_schema_metadata after concat to preserve the metadata.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- app.py +68 -49
- requirements.txt +1 -1
app.py
CHANGED
|
@@ -192,24 +192,43 @@ def log_inference(pil_inputs, output_pil, prompt, seed, steps, guidance_scale,
|
|
| 192 |
if not HF_TOKEN or not DATASET_REPO:
|
| 193 |
return
|
| 194 |
try:
|
| 195 |
-
import tempfile
|
| 196 |
-
import
|
|
|
|
| 197 |
from huggingface_hub import HfApi, hf_hub_download
|
| 198 |
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
"
|
| 204 |
-
"
|
| 205 |
-
"
|
| 206 |
-
"
|
| 207 |
-
"
|
| 208 |
-
"
|
| 209 |
-
"
|
| 210 |
-
"
|
| 211 |
-
"
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
def _to_jpeg(img, max_dim=1536, quality=85):
|
| 214 |
if img is None:
|
| 215 |
return None
|
|
@@ -221,58 +240,58 @@ def log_inference(pil_inputs, output_pil, prompt, seed, steps, guidance_scale,
|
|
| 221 |
img.convert("RGB").save(buf, format="JPEG", quality=quality)
|
| 222 |
return buf.getvalue()
|
| 223 |
|
| 224 |
-
|
| 225 |
-
"
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
"
|
| 232 |
-
"
|
| 233 |
-
"
|
| 234 |
-
"
|
| 235 |
-
"
|
| 236 |
-
"
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
|
|
|
|
|
|
| 243 |
|
| 244 |
today = datetime.now(timezone.utc).strftime("%Y-%m-%d")
|
| 245 |
path_in_repo = f"data/{today}.parquet"
|
| 246 |
api = HfApi(token=HF_TOKEN)
|
| 247 |
-
|
| 248 |
-
print(f"[log] ensuring repo {DATASET_REPO} exists")
|
| 249 |
api.create_repo(repo_id=DATASET_REPO, repo_type="dataset", private=True, exist_ok=True)
|
| 250 |
-
print(f"[log] repo ready")
|
| 251 |
|
| 252 |
try:
|
| 253 |
-
print(f"[log] downloading existing {path_in_repo}")
|
| 254 |
local_path = hf_hub_download(
|
| 255 |
repo_id=DATASET_REPO, filename=path_in_repo,
|
| 256 |
repo_type="dataset", token=HF_TOKEN,
|
| 257 |
)
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
|
|
|
| 261 |
except Exception as dl_err:
|
| 262 |
print(f"[log] no existing file ({dl_err}), starting fresh")
|
| 263 |
-
|
| 264 |
|
| 265 |
-
with tempfile.NamedTemporaryFile(suffix=".parquet", delete=False) as
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
print(f"[log] uploading {path_in_repo} ({
|
| 269 |
api.upload_file(
|
| 270 |
-
path_or_fileobj=
|
| 271 |
repo_id=DATASET_REPO, repo_type="dataset",
|
| 272 |
)
|
| 273 |
print(f"[log] upload done — {DATASET_REPO}/{path_in_repo}")
|
| 274 |
except Exception as log_err:
|
| 275 |
-
|
|
|
|
| 276 |
|
| 277 |
|
| 278 |
@spaces.GPU
|
|
|
|
| 192 |
if not HF_TOKEN or not DATASET_REPO:
|
| 193 |
return
|
| 194 |
try:
|
| 195 |
+
import tempfile, json as _json
|
| 196 |
+
import pyarrow as pa
|
| 197 |
+
import pyarrow.parquet as pq
|
| 198 |
from huggingface_hub import HfApi, hf_hub_download
|
| 199 |
|
| 200 |
+
# Image columns need Arrow struct {bytes: binary, path: utf8} plus
|
| 201 |
+
# a 'huggingface' schema metadata key for the HF viewer to render them.
|
| 202 |
+
img_struct = pa.struct([("bytes", pa.binary()), ("path", pa.string())])
|
| 203 |
+
hf_meta = _json.dumps({"info": {"features": {
|
| 204 |
+
"timestamp": {"dtype": "string", "_type": "Value"},
|
| 205 |
+
"prompt": {"dtype": "string", "_type": "Value"},
|
| 206 |
+
"seed": {"dtype": "int32", "_type": "Value"},
|
| 207 |
+
"steps": {"dtype": "int32", "_type": "Value"},
|
| 208 |
+
"guidance_scale": {"dtype": "float32", "_type": "Value"},
|
| 209 |
+
"input_images": {"feature": {"_type": "Image"}, "_type": "Sequence"},
|
| 210 |
+
"output_image": {"_type": "Image"},
|
| 211 |
+
"duration_seconds": {"dtype": "float32", "_type": "Value"},
|
| 212 |
+
"input_width": {"dtype": "int32", "_type": "Value"},
|
| 213 |
+
"input_height": {"dtype": "int32", "_type": "Value"},
|
| 214 |
+
"success": {"dtype": "bool", "_type": "Value"},
|
| 215 |
+
"error_message": {"dtype": "string", "_type": "Value"},
|
| 216 |
+
}}}).encode()
|
| 217 |
+
schema = pa.schema([
|
| 218 |
+
("timestamp", pa.string()),
|
| 219 |
+
("prompt", pa.string()),
|
| 220 |
+
("seed", pa.int32()),
|
| 221 |
+
("steps", pa.int32()),
|
| 222 |
+
("guidance_scale", pa.float32()),
|
| 223 |
+
("input_images", pa.list_(img_struct)),
|
| 224 |
+
("output_image", img_struct),
|
| 225 |
+
("duration_seconds", pa.float32()),
|
| 226 |
+
("input_width", pa.int32()),
|
| 227 |
+
("input_height", pa.int32()),
|
| 228 |
+
("success", pa.bool_()),
|
| 229 |
+
("error_message", pa.string()),
|
| 230 |
+
], metadata={b"huggingface": hf_meta})
|
| 231 |
+
|
| 232 |
def _to_jpeg(img, max_dim=1536, quality=85):
|
| 233 |
if img is None:
|
| 234 |
return None
|
|
|
|
| 240 |
img.convert("RGB").save(buf, format="JPEG", quality=quality)
|
| 241 |
return buf.getvalue()
|
| 242 |
|
| 243 |
+
def _img(b):
|
| 244 |
+
return {"bytes": b, "path": None}
|
| 245 |
+
|
| 246 |
+
input_jpegs = [_to_jpeg(img) for img in pil_inputs]
|
| 247 |
+
output_jpeg = _to_jpeg(output_pil)
|
| 248 |
+
|
| 249 |
+
new_table = pa.table({
|
| 250 |
+
"timestamp": pa.array([datetime.now(timezone.utc).isoformat()], type=pa.string()),
|
| 251 |
+
"prompt": pa.array([prompt], type=pa.string()),
|
| 252 |
+
"seed": pa.array([int(seed)], type=pa.int32()),
|
| 253 |
+
"steps": pa.array([int(steps)], type=pa.int32()),
|
| 254 |
+
"guidance_scale": pa.array([float(guidance_scale)], type=pa.float32()),
|
| 255 |
+
"input_images": pa.array([[_img(b) for b in input_jpegs]], type=pa.list_(img_struct)),
|
| 256 |
+
"output_image": pa.array([_img(output_jpeg) if output_jpeg else None], type=img_struct),
|
| 257 |
+
"duration_seconds": pa.array([float(duration_seconds)], type=pa.float32()),
|
| 258 |
+
"input_width": pa.array([int(input_width)], type=pa.int32()),
|
| 259 |
+
"input_height": pa.array([int(input_height)], type=pa.int32()),
|
| 260 |
+
"success": pa.array([bool(success)], type=pa.bool_()),
|
| 261 |
+
"error_message": pa.array([str(error_message)], type=pa.string()),
|
| 262 |
+
}, schema=schema)
|
| 263 |
+
print(f"[log] built row — success={success}, inputs={len(input_jpegs)}")
|
| 264 |
|
| 265 |
today = datetime.now(timezone.utc).strftime("%Y-%m-%d")
|
| 266 |
path_in_repo = f"data/{today}.parquet"
|
| 267 |
api = HfApi(token=HF_TOKEN)
|
|
|
|
|
|
|
| 268 |
api.create_repo(repo_id=DATASET_REPO, repo_type="dataset", private=True, exist_ok=True)
|
|
|
|
| 269 |
|
| 270 |
try:
|
|
|
|
| 271 |
local_path = hf_hub_download(
|
| 272 |
repo_id=DATASET_REPO, filename=path_in_repo,
|
| 273 |
repo_type="dataset", token=HF_TOKEN,
|
| 274 |
)
|
| 275 |
+
existing = pq.read_table(local_path)
|
| 276 |
+
combined = pa.concat_tables([existing, new_table])
|
| 277 |
+
combined = combined.replace_schema_metadata(schema.metadata)
|
| 278 |
+
print(f"[log] appending to existing {existing.num_rows} row(s)")
|
| 279 |
except Exception as dl_err:
|
| 280 |
print(f"[log] no existing file ({dl_err}), starting fresh")
|
| 281 |
+
combined = new_table
|
| 282 |
|
| 283 |
+
with tempfile.NamedTemporaryFile(suffix=".parquet", delete=False) as tmp:
|
| 284 |
+
tmp_path = tmp.name
|
| 285 |
+
pq.write_table(combined, tmp_path)
|
| 286 |
+
print(f"[log] uploading {path_in_repo} ({combined.num_rows} row(s), {os.path.getsize(tmp_path)//1024}KB)")
|
| 287 |
api.upload_file(
|
| 288 |
+
path_or_fileobj=tmp_path, path_in_repo=path_in_repo,
|
| 289 |
repo_id=DATASET_REPO, repo_type="dataset",
|
| 290 |
)
|
| 291 |
print(f"[log] upload done — {DATASET_REPO}/{path_in_repo}")
|
| 292 |
except Exception as log_err:
|
| 293 |
+
import traceback as _tb
|
| 294 |
+
print(f"[log] WARNING: {log_err}\n{_tb.format_exc()}")
|
| 295 |
|
| 296 |
|
| 297 |
@spaces.GPU
|
requirements.txt
CHANGED
|
@@ -3,7 +3,7 @@ git+https://github.com/huggingface/diffusers.git
|
|
| 3 |
git+https://github.com/huggingface/peft.git
|
| 4 |
transformers==4.57.1
|
| 5 |
huggingface_hub
|
| 6 |
-
|
| 7 |
sentencepiece
|
| 8 |
torchvision
|
| 9 |
kernels
|
|
|
|
| 3 |
git+https://github.com/huggingface/peft.git
|
| 4 |
transformers==4.57.1
|
| 5 |
huggingface_hub
|
| 6 |
+
pyarrow
|
| 7 |
sentencepiece
|
| 8 |
torchvision
|
| 9 |
kernels
|