Update app.py
Browse files
app.py
CHANGED
|
@@ -2,13 +2,15 @@ import json
|
|
| 2 |
from docling.document_converter import DocumentConverter
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
-
def pdf_to_json(
|
| 6 |
converter = DocumentConverter()
|
| 7 |
-
result = converter.convert(
|
| 8 |
|
| 9 |
try:
|
|
|
|
| 10 |
json_output = result.document.export_to_json()
|
| 11 |
except AttributeError:
|
|
|
|
| 12 |
content = {
|
| 13 |
"title": result.document.title if hasattr(result.document, 'title') else "Untitled",
|
| 14 |
"sections": [section.text for section in result.document.sections]
|
|
@@ -20,9 +22,10 @@ def pdf_to_json(source):
|
|
| 20 |
# Gradio interface
|
| 21 |
iface = gr.Interface(
|
| 22 |
fn=pdf_to_json,
|
| 23 |
-
inputs=gr.Textbox(label="PDF URL"),
|
| 24 |
outputs="text",
|
| 25 |
-
title="PDF to JSON Converter"
|
|
|
|
| 26 |
)
|
| 27 |
|
| 28 |
iface.launch()
|
|
|
|
| 2 |
from docling.document_converter import DocumentConverter
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
+
def pdf_to_json(url):
|
| 6 |
converter = DocumentConverter()
|
| 7 |
+
result = converter.convert(url)
|
| 8 |
|
| 9 |
try:
|
| 10 |
+
# Attempt direct JSON export
|
| 11 |
json_output = result.document.export_to_json()
|
| 12 |
except AttributeError:
|
| 13 |
+
# Construct JSON if direct export is unavailable
|
| 14 |
content = {
|
| 15 |
"title": result.document.title if hasattr(result.document, 'title') else "Untitled",
|
| 16 |
"sections": [section.text for section in result.document.sections]
|
|
|
|
| 22 |
# Gradio interface
|
| 23 |
iface = gr.Interface(
|
| 24 |
fn=pdf_to_json,
|
| 25 |
+
inputs=gr.Textbox(label="Enter PDF URL"),
|
| 26 |
outputs="text",
|
| 27 |
+
title="PDF to JSON Converter",
|
| 28 |
+
description="Convert a PDF from a URL to JSON format."
|
| 29 |
)
|
| 30 |
|
| 31 |
iface.launch()
|