Upload folder using huggingface_hub
Browse files- llm_ocr/document.py +60 -0
- llm_ocr/storage.py +8 -1
llm_ocr/document.py
CHANGED
|
@@ -357,6 +357,65 @@ def display_markdown(sample: Dict[str, Any]) -> None:
|
|
| 357 |
display(Markdown(rendered))
|
| 358 |
|
| 359 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 360 |
__all__ = [
|
| 361 |
"encode_image",
|
| 362 |
"build_document_markdown",
|
|
@@ -364,6 +423,7 @@ __all__ = [
|
|
| 364 |
"render_markdown_with_images",
|
| 365 |
"render_sample_markdown",
|
| 366 |
"display_markdown",
|
|
|
|
| 367 |
"write_text",
|
| 368 |
"write_json",
|
| 369 |
]
|
|
|
|
| 357 |
display(Markdown(rendered))
|
| 358 |
|
| 359 |
|
| 360 |
+
def display_samples(dataset, num_samples: int = 2) -> None:
|
| 361 |
+
"""
|
| 362 |
+
Display a few samples from a dataset with images and figure descriptions.
|
| 363 |
+
|
| 364 |
+
Shows source images, markdown previews, extracted figures with their
|
| 365 |
+
descriptions from metadata.
|
| 366 |
+
|
| 367 |
+
Args:
|
| 368 |
+
dataset: A Hugging Face dataset or list of samples
|
| 369 |
+
num_samples: Number of samples to display (default 2)
|
| 370 |
+
"""
|
| 371 |
+
from IPython.display import display
|
| 372 |
+
|
| 373 |
+
print(f"Dataset: {len(dataset)} samples")
|
| 374 |
+
print(f"Columns: {list(dataset.column_names)}")
|
| 375 |
+
print()
|
| 376 |
+
|
| 377 |
+
for i in range(min(num_samples, len(dataset))):
|
| 378 |
+
sample = dataset[i]
|
| 379 |
+
print(f"=== Sample {i}: {sample.get('sample_id', i)} ===")
|
| 380 |
+
|
| 381 |
+
# Show source image
|
| 382 |
+
if sample.get('source_image'):
|
| 383 |
+
print("Source image:")
|
| 384 |
+
img = sample['source_image']
|
| 385 |
+
img.thumbnail((500, 500)) # Resize to max 500px
|
| 386 |
+
display(img)
|
| 387 |
+
|
| 388 |
+
# Show markdown preview
|
| 389 |
+
md = sample.get('document_markdown') or sample.get('document_markdown_text', '')
|
| 390 |
+
if md:
|
| 391 |
+
print(f"\nMarkdown preview ({len(md)} chars):")
|
| 392 |
+
print(md[:500] + '...' if len(md) > 500 else md)
|
| 393 |
+
|
| 394 |
+
# Show final markdown if available
|
| 395 |
+
final_md = sample.get('document_final_markdown') or sample.get('document_final_markdown_text', '')
|
| 396 |
+
if final_md:
|
| 397 |
+
print(f"\nFinal markdown preview ({len(final_md)} chars):")
|
| 398 |
+
print(final_md[:500] + '...' if len(final_md) > 500 else final_md)
|
| 399 |
+
|
| 400 |
+
# Show figures and their descriptions
|
| 401 |
+
figures = sample.get('extracted_figures', [])
|
| 402 |
+
metadata = sample.get('extracted_figures_metadata', [])
|
| 403 |
+
if figures:
|
| 404 |
+
print(f"\nExtracted figures: {len(figures)}")
|
| 405 |
+
for j, fig in enumerate(figures[:2]): # Show max 2 figures
|
| 406 |
+
fig.thumbnail((500, 500))
|
| 407 |
+
display(fig)
|
| 408 |
+
# Show figure description if available
|
| 409 |
+
if j < len(metadata):
|
| 410 |
+
try:
|
| 411 |
+
meta = json.loads(metadata[j]) if isinstance(metadata[j], str) else metadata[j]
|
| 412 |
+
if meta.get('description'):
|
| 413 |
+
print(f" 📝 Description: {meta['description'][:200]}...")
|
| 414 |
+
except Exception:
|
| 415 |
+
pass
|
| 416 |
+
print()
|
| 417 |
+
|
| 418 |
+
|
| 419 |
__all__ = [
|
| 420 |
"encode_image",
|
| 421 |
"build_document_markdown",
|
|
|
|
| 423 |
"render_markdown_with_images",
|
| 424 |
"render_sample_markdown",
|
| 425 |
"display_markdown",
|
| 426 |
+
"display_samples",
|
| 427 |
"write_text",
|
| 428 |
"write_json",
|
| 429 |
]
|
llm_ocr/storage.py
CHANGED
|
@@ -82,6 +82,13 @@ class HFHubStorage(DatasetStorage):
|
|
| 82 |
LOGGER.debug("HF Hub not configured, skipping dataset save")
|
| 83 |
return False
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
try:
|
| 86 |
dataset.push_to_hub(
|
| 87 |
self.repo_id,
|
|
@@ -93,7 +100,7 @@ class HFHubStorage(DatasetStorage):
|
|
| 93 |
return True
|
| 94 |
except Exception as exc:
|
| 95 |
LOGGER.exception("HF Hub dataset push failed: %s", exc)
|
| 96 |
-
|
| 97 |
|
| 98 |
def load_dataset(self, split: str = "train") -> Optional["Dataset"]:
|
| 99 |
if not self.is_configured:
|
|
|
|
| 82 |
LOGGER.debug("HF Hub not configured, skipping dataset save")
|
| 83 |
return False
|
| 84 |
|
| 85 |
+
# Fail early if token is missing or empty
|
| 86 |
+
if not self._token:
|
| 87 |
+
raise ValueError(
|
| 88 |
+
"HF_TOKEN is required to push datasets to the Hub. "
|
| 89 |
+
"Set the HF_TOKEN environment variable with a token that has write permissions."
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
try:
|
| 93 |
dataset.push_to_hub(
|
| 94 |
self.repo_id,
|
|
|
|
| 100 |
return True
|
| 101 |
except Exception as exc:
|
| 102 |
LOGGER.exception("HF Hub dataset push failed: %s", exc)
|
| 103 |
+
raise
|
| 104 |
|
| 105 |
def load_dataset(self, split: str = "train") -> Optional["Dataset"]:
|
| 106 |
if not self.is_configured:
|