Update app.py
Browse files
app.py
CHANGED
|
@@ -2,30 +2,54 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
from docling.document_converter import DocumentConverter, PdfFormatOption
|
| 4 |
from docling.datamodel.base_models import InputFormat
|
|
|
|
| 5 |
import tempfile
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def pdf_to_markdown(file):
|
| 8 |
# Save uploaded file temporarily
|
| 9 |
tmp_path = file.name
|
|
|
|
| 10 |
# Convert PDF using Docling/VLM (Granite Docling)
|
| 11 |
converter = DocumentConverter(
|
| 12 |
format_options={
|
| 13 |
InputFormat.PDF: PdfFormatOption()
|
| 14 |
}
|
| 15 |
)
|
|
|
|
|
|
|
| 16 |
result = converter.convert(tmp_path)
|
| 17 |
doc = result.document
|
|
|
|
| 18 |
# Export to Markdown (or you can export to JSON via doc.model_dump())
|
| 19 |
md = doc.export_to_markdown()
|
|
|
|
| 20 |
return md
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
interface = gr.Interface(
|
| 23 |
fn=pdf_to_markdown,
|
| 24 |
inputs=gr.File(file_types=[".pdf"]),
|
| 25 |
-
outputs=
|
| 26 |
-
title="PDF β Markdown/JSON with Granite Docling",
|
| 27 |
-
description="Upload a PDF and get parsed Markdown (or JSON) using Granite Docling via Docling."
|
| 28 |
)
|
| 29 |
|
|
|
|
| 30 |
if __name__ == "__main__":
|
| 31 |
interface.launch()
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from docling.document_converter import DocumentConverter, PdfFormatOption
|
| 4 |
from docling.datamodel.base_models import InputFormat
|
| 5 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 6 |
import tempfile
|
| 7 |
|
| 8 |
+
# Path to the model
|
| 9 |
+
model_name = "ibm-granite/granite-docling-258M"
|
| 10 |
+
|
| 11 |
+
# Load the OCR model from Hugging Face (assuming you have access to it)
|
| 12 |
+
# In this case, let's load the model and tokenizer if needed
|
| 13 |
+
ocr_model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 14 |
+
ocr_tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 15 |
+
|
| 16 |
def pdf_to_markdown(file):
|
| 17 |
# Save uploaded file temporarily
|
| 18 |
tmp_path = file.name
|
| 19 |
+
|
| 20 |
# Convert PDF using Docling/VLM (Granite Docling)
|
| 21 |
converter = DocumentConverter(
|
| 22 |
format_options={
|
| 23 |
InputFormat.PDF: PdfFormatOption()
|
| 24 |
}
|
| 25 |
)
|
| 26 |
+
|
| 27 |
+
# Perform OCR using granite-docling model if the file contains scanned text
|
| 28 |
result = converter.convert(tmp_path)
|
| 29 |
doc = result.document
|
| 30 |
+
|
| 31 |
# Export to Markdown (or you can export to JSON via doc.model_dump())
|
| 32 |
md = doc.export_to_markdown()
|
| 33 |
+
|
| 34 |
return md
|
| 35 |
|
| 36 |
+
# Define the output box size
|
| 37 |
+
output_box = gr.Textbox(
|
| 38 |
+
label="Markdown Output",
|
| 39 |
+
lines=20, # initial visible lines
|
| 40 |
+
max_lines=50, # maximum scrollable lines
|
| 41 |
+
placeholder="Converted Markdown will appear here..."
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
# Create the Gradio Interface
|
| 45 |
interface = gr.Interface(
|
| 46 |
fn=pdf_to_markdown,
|
| 47 |
inputs=gr.File(file_types=[".pdf"]),
|
| 48 |
+
outputs=output_box,
|
| 49 |
+
title="PDF β Markdown/JSON with Granite Docling (OCR)",
|
| 50 |
+
description="Upload a PDF (including scanned PDFs) and get parsed Markdown (or JSON) using Granite Docling via Docling, with OCR support."
|
| 51 |
)
|
| 52 |
|
| 53 |
+
# Launch the interface
|
| 54 |
if __name__ == "__main__":
|
| 55 |
interface.launch()
|