allowing image inputs too
Browse files
app.py
CHANGED
|
@@ -92,11 +92,18 @@ def render_page(pdf_path: str, page_num: int, dpi: int) -> Image.Image:
|
|
| 92 |
doc.close()
|
| 93 |
|
| 94 |
|
| 95 |
-
def
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
fourth = "<predict_text_in_pic>" if text_in_pic else "<predict_no_text_in_pic>"
|
| 102 |
task_prompt = f"</s><s><predict_bbox><predict_classes><output_markdown>{fourth}"
|
|
@@ -130,17 +137,23 @@ def parse(pdf_file, page_num, dpi, text_in_pic, table_format):
|
|
| 130 |
with gr.Blocks(title="Nemotron Parse — Repair Manuals") as demo:
|
| 131 |
gr.Markdown(
|
| 132 |
"# 🔧 Nemotron Parse v1.2 — Repair Manual Explorer\n"
|
| 133 |
-
"Upload a PDF
|
| 134 |
"[NVIDIA Nemotron Parse v1.2](https://huggingface.co/nvidia/NVIDIA-Nemotron-Parse-v1.2) "
|
| 135 |
"on ZeroGPU. Returns structured markdown, a JSON of elements, and an "
|
| 136 |
"annotated page image."
|
| 137 |
)
|
| 138 |
with gr.Row():
|
| 139 |
with gr.Column(scale=1):
|
| 140 |
-
pdf_in = gr.File(
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
dpi_in = gr.Slider(
|
| 143 |
-
label="Render DPI", minimum=72, maximum=300, value=150, step=10
|
| 144 |
)
|
| 145 |
text_in_pic_in = gr.Checkbox(
|
| 146 |
label="Extract text inside pictures/diagrams", value=False
|
|
|
|
| 92 |
doc.close()
|
| 93 |
|
| 94 |
|
| 95 |
+
def load_input(file_path: str, page_num: int, dpi: int) -> Image.Image:
|
| 96 |
+
"""Return an RGB image from either a PDF page or an image file."""
|
| 97 |
+
if file_path.lower().endswith(".pdf"):
|
| 98 |
+
return render_page(file_path, page_num, dpi)
|
| 99 |
+
return Image.open(file_path).convert("RGB")
|
| 100 |
|
| 101 |
+
|
| 102 |
+
def parse(input_file, page_num, dpi, text_in_pic, table_format):
|
| 103 |
+
if input_file is None:
|
| 104 |
+
raise gr.Error("Please upload a PDF or image first.")
|
| 105 |
+
|
| 106 |
+
image = load_input(input_file, int(page_num), int(dpi))
|
| 107 |
|
| 108 |
fourth = "<predict_text_in_pic>" if text_in_pic else "<predict_no_text_in_pic>"
|
| 109 |
task_prompt = f"</s><s><predict_bbox><predict_classes><output_markdown>{fourth}"
|
|
|
|
| 137 |
with gr.Blocks(title="Nemotron Parse — Repair Manuals") as demo:
|
| 138 |
gr.Markdown(
|
| 139 |
"# 🔧 Nemotron Parse v1.2 — Repair Manual Explorer\n"
|
| 140 |
+
"Upload a PDF (choose a page) or an image, and parse it with "
|
| 141 |
"[NVIDIA Nemotron Parse v1.2](https://huggingface.co/nvidia/NVIDIA-Nemotron-Parse-v1.2) "
|
| 142 |
"on ZeroGPU. Returns structured markdown, a JSON of elements, and an "
|
| 143 |
"annotated page image."
|
| 144 |
)
|
| 145 |
with gr.Row():
|
| 146 |
with gr.Column(scale=1):
|
| 147 |
+
pdf_in = gr.File(
|
| 148 |
+
label="PDF or image",
|
| 149 |
+
file_types=[".pdf", ".png", ".jpg", ".jpeg", ".webp"],
|
| 150 |
+
type="filepath",
|
| 151 |
+
)
|
| 152 |
+
page_in = gr.Number(
|
| 153 |
+
label="Page (PDF only)", value=1, precision=0, minimum=1
|
| 154 |
+
)
|
| 155 |
dpi_in = gr.Slider(
|
| 156 |
+
label="Render DPI (PDF only)", minimum=72, maximum=300, value=150, step=10
|
| 157 |
)
|
| 158 |
text_in_pic_in = gr.Checkbox(
|
| 159 |
label="Extract text inside pictures/diagrams", value=False
|