Spaces:
Running
Running
import gradio as gr | |
from docling.parsers.pdf_parser import PDFParser | |
import tempfile | |
import os | |
def pdf_to_markdown(pdf_file): | |
if pdf_file is None: | |
return "No file uploaded." | |
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp: | |
tmp.write(pdf_file.read()) | |
tmp_path = tmp.name | |
try: | |
parser = PDFParser(tmp_path) | |
doc = parser.parse() | |
markdown = doc.to_markdown() | |
return markdown | |
except Exception as e: | |
return f"Error parsing PDF: {str(e)}" | |
finally: | |
os.remove(tmp_path) | |
iface = gr.Interface( | |
fn=pdf_to_markdown, | |
inputs=gr.File(label="Upload PDF", file_types=[".pdf"]), | |
outputs=gr.Markdown(label="Markdown Output"), | |
title="PDF to Markdown with docling", | |
description="Upload a PDF file. This app parses it using `docling` and displays the Markdown version." | |
) | |
if __name__ == "__main__": | |
iface.launch() |