Commit ·
4c793dd
1
Parent(s): cd01d80
Make document processor ZeroGPU compatible
Browse files- app.py +5 -7
- how HEADdocument_processor.py +38 -0
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
|
| 4 |
from document_processor import DocumentProcessor
|
| 5 |
|
|
@@ -7,6 +7,7 @@ from document_processor import DocumentProcessor
|
|
| 7 |
processor = DocumentProcessor()
|
| 8 |
|
| 9 |
|
|
|
|
| 10 |
def process_document(file):
|
| 11 |
|
| 12 |
if file is None:
|
|
@@ -15,19 +16,16 @@ def process_document(file):
|
|
| 15 |
}
|
| 16 |
|
| 17 |
try:
|
| 18 |
-
|
| 19 |
-
result = processor.process(file)
|
| 20 |
-
|
| 21 |
-
return result
|
| 22 |
|
| 23 |
except Exception as e:
|
| 24 |
-
|
| 25 |
return {
|
| 26 |
"error": str(e)
|
| 27 |
}
|
| 28 |
|
| 29 |
|
| 30 |
demo = gr.Interface(
|
|
|
|
| 31 |
fn=process_document,
|
| 32 |
|
| 33 |
inputs=gr.File(
|
|
@@ -43,7 +41,7 @@ demo = gr.Interface(
|
|
| 43 |
title="AI Tutor Document Processor",
|
| 44 |
|
| 45 |
description=(
|
| 46 |
-
"Upload a PDF and extract
|
| 47 |
"text and page structure."
|
| 48 |
)
|
| 49 |
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import spaces
|
| 3 |
|
| 4 |
from document_processor import DocumentProcessor
|
| 5 |
|
|
|
|
| 7 |
processor = DocumentProcessor()
|
| 8 |
|
| 9 |
|
| 10 |
+
@spaces.GPU
|
| 11 |
def process_document(file):
|
| 12 |
|
| 13 |
if file is None:
|
|
|
|
| 16 |
}
|
| 17 |
|
| 18 |
try:
|
| 19 |
+
return processor.process(file)
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
except Exception as e:
|
|
|
|
| 22 |
return {
|
| 23 |
"error": str(e)
|
| 24 |
}
|
| 25 |
|
| 26 |
|
| 27 |
demo = gr.Interface(
|
| 28 |
+
|
| 29 |
fn=process_document,
|
| 30 |
|
| 31 |
inputs=gr.File(
|
|
|
|
| 41 |
title="AI Tutor Document Processor",
|
| 42 |
|
| 43 |
description=(
|
| 44 |
+
"Upload a PDF and extract "
|
| 45 |
"text and page structure."
|
| 46 |
)
|
| 47 |
)
|
how HEADdocument_processor.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
+
import fitz
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class DocumentProcessor:
|
| 6 |
+
|
| 7 |
+
def process(self, file_path):
|
| 8 |
+
|
| 9 |
+
path = Path(file_path)
|
| 10 |
+
|
| 11 |
+
if path.suffix.lower() != ".pdf":
|
| 12 |
+
raise ValueError(
|
| 13 |
+
f"Unsupported file type: {path.suffix}"
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
return self.process_pdf(path)
|
| 17 |
+
|
| 18 |
+
def process_pdf(self, path):
|
| 19 |
+
|
| 20 |
+
document = fitz.open(path)
|
| 21 |
+
|
| 22 |
+
pages = []
|
| 23 |
+
|
| 24 |
+
for page_number, page in enumerate(document):
|
| 25 |
+
|
| 26 |
+
pages.append({
|
| 27 |
+
"page_number": page_number + 1,
|
| 28 |
+
"text": page.get_text()
|
| 29 |
+
})
|
| 30 |
+
|
| 31 |
+
return {
|
| 32 |
+
"document": {
|
| 33 |
+
"filename": path.name,
|
| 34 |
+
"format": "pdf",
|
| 35 |
+
"page_count": len(document)
|
| 36 |
+
},
|
| 37 |
+
"pages": pages
|
| 38 |
+
}
|
requirements.txt
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
gradio
|
| 2 |
pymupdf
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
pymupdf
|
| 3 |
+
spaces
|