Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from docling.datamodel.base_models import InputFormat
|
| 3 |
+
from docling.datamodel.pipeline_options import PdfPipelineOptions
|
| 4 |
+
from docling.document_converter import DocumentConverter, PdfFormatOption
|
| 5 |
+
from docling_core.types.doc import ImageRefMode
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# Define the document converter
|
| 10 |
+
pipeline_options = PdfPipelineOptions()
|
| 11 |
+
pipeline_options.do_ocr = True
|
| 12 |
+
pipeline_options.do_table_structure = True
|
| 13 |
+
pipeline_options.table_structure_options.do_cell_matching = True
|
| 14 |
+
pipeline_options.generate_picture_images = True
|
| 15 |
+
|
| 16 |
+
doc_converter = DocumentConverter(
|
| 17 |
+
format_options={
|
| 18 |
+
InputFormat.PDF: PdfFormatOption(pipeline_options=pipeline_options)
|
| 19 |
+
}
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
def convert_to_markdown(file):
|
| 23 |
+
# Save the uploaded file to a temporary path
|
| 24 |
+
input_path = Path(file.name)
|
| 25 |
+
file.save(str(input_path))
|
| 26 |
+
|
| 27 |
+
# Convert the document
|
| 28 |
+
result = doc_converter.convert(str(input_path))
|
| 29 |
+
|
| 30 |
+
# Prepare output directory
|
| 31 |
+
output_dir = Path("output")
|
| 32 |
+
output_dir.mkdir(exist_ok=True)
|
| 33 |
+
|
| 34 |
+
# Save result as markdown
|
| 35 |
+
doc_filename = result.input.file.stem
|
| 36 |
+
md_filename = output_dir / f"{doc_filename}-with-images.md"
|
| 37 |
+
result.document.save_as_markdown(md_filename, image_mode=ImageRefMode.REFERENCED)
|
| 38 |
+
|
| 39 |
+
# Load the markdown content
|
| 40 |
+
with open(md_filename, 'r', encoding='utf-8') as f:
|
| 41 |
+
markdown_content = f.read()
|
| 42 |
+
|
| 43 |
+
return markdown_content
|
| 44 |
+
|
| 45 |
+
# Create Gradio interface
|
| 46 |
+
iface = gr.Interface(
|
| 47 |
+
fn=convert_to_markdown,
|
| 48 |
+
inputs=gr.File(label="Upload your document"),
|
| 49 |
+
outputs="markdown",
|
| 50 |
+
title="Document to Markdown Converter",
|
| 51 |
+
description="Upload a document (e.g., PDF, DOCX, PPTX) and get its Markdown version."
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
iface.launch(share=True)
|