Commit ·
ac57c76
1
Parent(s): 4c793dd
Add Docling document intelligence
Browse files- app.py +12 -5
- document_processor.py +16 -22
- requirements.txt +3 -2
app.py
CHANGED
|
@@ -12,13 +12,15 @@ def process_document(file):
|
|
| 12 |
|
| 13 |
if file is None:
|
| 14 |
return {
|
| 15 |
-
"error": "Please upload a
|
| 16 |
}
|
| 17 |
|
| 18 |
try:
|
|
|
|
| 19 |
return processor.process(file)
|
| 20 |
|
| 21 |
except Exception as e:
|
|
|
|
| 22 |
return {
|
| 23 |
"error": str(e)
|
| 24 |
}
|
|
@@ -29,8 +31,13 @@ demo = gr.Interface(
|
|
| 29 |
fn=process_document,
|
| 30 |
|
| 31 |
inputs=gr.File(
|
| 32 |
-
label="Upload
|
| 33 |
-
file_types=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
type="filepath"
|
| 35 |
),
|
| 36 |
|
|
@@ -41,8 +48,8 @@ demo = gr.Interface(
|
|
| 41 |
title="AI Tutor Document Processor",
|
| 42 |
|
| 43 |
description=(
|
| 44 |
-
"
|
| 45 |
-
"
|
| 46 |
)
|
| 47 |
)
|
| 48 |
|
|
|
|
| 12 |
|
| 13 |
if file is None:
|
| 14 |
return {
|
| 15 |
+
"error": "Please upload a document."
|
| 16 |
}
|
| 17 |
|
| 18 |
try:
|
| 19 |
+
|
| 20 |
return processor.process(file)
|
| 21 |
|
| 22 |
except Exception as e:
|
| 23 |
+
|
| 24 |
return {
|
| 25 |
"error": str(e)
|
| 26 |
}
|
|
|
|
| 31 |
fn=process_document,
|
| 32 |
|
| 33 |
inputs=gr.File(
|
| 34 |
+
label="Upload Document",
|
| 35 |
+
file_types=[
|
| 36 |
+
".pdf",
|
| 37 |
+
".docx",
|
| 38 |
+
".pptx",
|
| 39 |
+
".html"
|
| 40 |
+
],
|
| 41 |
type="filepath"
|
| 42 |
),
|
| 43 |
|
|
|
|
| 48 |
title="AI Tutor Document Processor",
|
| 49 |
|
| 50 |
description=(
|
| 51 |
+
"Convert documents into structured "
|
| 52 |
+
"content using Docling."
|
| 53 |
)
|
| 54 |
)
|
| 55 |
|
document_processor.py
CHANGED
|
@@ -1,40 +1,34 @@
|
|
| 1 |
from pathlib import Path
|
| 2 |
-
import
|
|
|
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
class DocumentProcessor:
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
def process(self, file_path):
|
| 8 |
|
| 9 |
path = Path(file_path)
|
| 10 |
|
| 11 |
-
if path.
|
| 12 |
-
raise
|
| 13 |
-
f"
|
| 14 |
)
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
def process_pdf(self, path):
|
| 19 |
-
|
| 20 |
-
document = pymupdf.open(path)
|
| 21 |
-
|
| 22 |
-
pages = []
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
pages.append({
|
| 27 |
-
"page_number": page_number + 1,
|
| 28 |
-
"text": page.get_text()
|
| 29 |
-
})
|
| 30 |
-
|
| 31 |
-
document.close()
|
| 32 |
|
| 33 |
return {
|
| 34 |
"document": {
|
| 35 |
"filename": path.name,
|
| 36 |
-
"format": "
|
| 37 |
-
"page_count": len(pages)
|
| 38 |
},
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
}
|
|
|
|
| 1 |
from pathlib import Path
|
| 2 |
+
import json
|
| 3 |
+
|
| 4 |
+
from docling.document_converter import DocumentConverter
|
| 5 |
|
| 6 |
|
| 7 |
class DocumentProcessor:
|
| 8 |
|
| 9 |
+
def __init__(self):
|
| 10 |
+
self.converter = DocumentConverter()
|
| 11 |
+
|
| 12 |
def process(self, file_path):
|
| 13 |
|
| 14 |
path = Path(file_path)
|
| 15 |
|
| 16 |
+
if not path.exists():
|
| 17 |
+
raise FileNotFoundError(
|
| 18 |
+
f"File not found: {path}"
|
| 19 |
)
|
| 20 |
|
| 21 |
+
result = self.converter.convert(str(path))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
document = result.document
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
return {
|
| 26 |
"document": {
|
| 27 |
"filename": path.name,
|
| 28 |
+
"format": path.suffix.lower().replace(".", ""),
|
|
|
|
| 29 |
},
|
| 30 |
+
|
| 31 |
+
"markdown": document.export_to_markdown(),
|
| 32 |
+
|
| 33 |
+
"json": document.export_to_dict()
|
| 34 |
}
|
requirements.txt
CHANGED
|
@@ -1,3 +1,4 @@
|
|
| 1 |
gradio
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
+
spaces
|
| 3 |
+
docling
|
| 4 |
+
pymupdf
|