Commit ·
7826d20
1
Parent(s): 221e41f
Add application file
Browse files- document_processor.py +41 -0
- requirements.txt +6 -0
document_processor.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
+
import json
|
| 3 |
+
import fitz
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class DocumentProcessor:
|
| 7 |
+
|
| 8 |
+
def process(self, file_path):
|
| 9 |
+
|
| 10 |
+
path = Path(file_path)
|
| 11 |
+
|
| 12 |
+
if path.suffix.lower() == ".pdf":
|
| 13 |
+
return self.process_pdf(path)
|
| 14 |
+
|
| 15 |
+
raise ValueError(
|
| 16 |
+
f"Unsupported file type: {path.suffix}"
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
def process_pdf(self, path):
|
| 20 |
+
|
| 21 |
+
document = fitz.open(path)
|
| 22 |
+
|
| 23 |
+
pages = []
|
| 24 |
+
|
| 25 |
+
for page_number, page in enumerate(document):
|
| 26 |
+
|
| 27 |
+
text = page.get_text()
|
| 28 |
+
|
| 29 |
+
pages.append({
|
| 30 |
+
"page_number": page_number + 1,
|
| 31 |
+
"text": text
|
| 32 |
+
})
|
| 33 |
+
|
| 34 |
+
return {
|
| 35 |
+
"document": {
|
| 36 |
+
"filename": path.name,
|
| 37 |
+
"format": "pdf",
|
| 38 |
+
"page_count": len(document)
|
| 39 |
+
},
|
| 40 |
+
"pages": pages
|
| 41 |
+
}
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
docling
|
| 3 |
+
pymupdf
|
| 4 |
+
python-docx
|
| 5 |
+
ebooklib
|
| 6 |
+
beautifulsoup4
|