Skip to content

Commit eefa144

Browse files
committed
documents resposeDocument compatible - Adithya S K
1 parent f8ea629 commit eefa144

File tree

2 files changed

+48
-18
lines changed

2 files changed

+48
-18
lines changed

omniparse/documents/__init__.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
import subprocess
44
from omniparse.documents.parse import parse_single_pdf
55
from omniparse.utils import encode_images
6+
from omniparse.models import responseDocument
67
# Function to handle PDF parsing
7-
def parse_pdf(input_data , model_state) -> dict:
8+
def parse_pdf(input_data , model_state) -> responseDocument:
89
try:
910
if isinstance(input_data, bytes):
1011
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_pdf_file:
@@ -22,18 +23,23 @@ def parse_pdf(input_data , model_state) -> dict:
2223
raise ValueError("Invalid input data format. Expected bytes or PDF file path.")
2324

2425
full_text, images, out_meta = parse_single_pdf(input_path, model_state.model_list)
25-
images = encode_images(images)
26+
27+
parse_pdf_result = responseDocument(
28+
text=full_text,
29+
metadata=out_meta
30+
)
31+
encode_images(images,parse_pdf_result)
2632

2733
if cleanup_tempfile:
2834
os.remove(input_path)
2935

30-
return {"message": "PDF parsed successfully", "markdown": full_text, "metadata": out_meta, "images": images}
36+
return parse_pdf_result
3137

3238
except Exception as e:
3339
raise RuntimeError(f"Error parsing PPT: {str(e)}")
3440

3541
# Function to handle PPT and DOC parsing
36-
def parse_ppt(input_data ,model_state) -> dict:
42+
def parse_ppt(input_data ,model_state) -> responseDocument:
3743
try:
3844
if isinstance(input_data, bytes):
3945
print("Recieved ppt file")
@@ -58,15 +64,21 @@ def parse_ppt(input_data ,model_state) -> dict:
5864
full_text, images, out_meta = parse_single_pdf(input_path, model_state.model_list)
5965
images = encode_images(images)
6066

67+
parse_ppt_result = responseDocument(
68+
text=full_text,
69+
metadata=out_meta
70+
)
71+
encode_images(images,parse_ppt_result)
72+
6173
if input_data != input_path:
6274
os.remove(input_path)
6375

64-
return {"message": "Document parsed successfully", "markdown": full_text, "metadata": out_meta, "images": images}
76+
return parse_ppt_result
6577

6678
except Exception as e:
6779
raise RuntimeError(f"Error parsing PPT: {str(e)}")
6880

69-
def parse_doc(input_data ,model_state) -> dict:
81+
def parse_doc(input_data ,model_state) -> responseDocument:
7082
try:
7183
if isinstance(input_data, bytes):
7284
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
@@ -90,10 +102,16 @@ def parse_doc(input_data ,model_state) -> dict:
90102
full_text, images, out_meta = parse_single_pdf(input_path, model_state.model_list)
91103
images = encode_images(images)
92104

105+
parse_doc_result = responseDocument(
106+
text=full_text,
107+
metadata=out_meta
108+
)
109+
encode_images(images,parse_doc_result)
110+
93111
if input_data != input_path:
94112
os.remove(input_path)
95113

96-
return {"message": "Document parsed successfully", "markdown": full_text, "metadata": out_meta, "images": images}
114+
return parse_doc_result
97115

98116
except Exception as e:
99117
raise RuntimeError(f"Error parsing PPT: {str(e)}")

omniparse/documents/router.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
# from omniparse.documents import parse_pdf , parse_ppt , parse_doc
99
from omniparse.documents import parse_pdf
1010
from omniparse.utils import encode_images
11+
from omniparse.models import responseDocument
1112

1213
document_router = APIRouter()
1314
model_state = get_shared_state()
@@ -17,9 +18,9 @@
1718
async def parse_pdf_endpoint(file: UploadFile = File(...)):
1819
try:
1920
file_bytes = await file.read()
20-
result = parse_pdf(file_bytes , model_state)
21+
result : responseDocument = parse_pdf(file_bytes , model_state)
2122

22-
return JSONResponse(content=result)
23+
return JSONResponse(content=result.model_dump())
2324

2425
except Exception as e:
2526
raise HTTPException(status_code=500, detail=str(e))
@@ -43,13 +44,18 @@ async def parse_ppt_endpoint(file: UploadFile = File(...)):
4344
pdf_bytes = pdf_file.read()
4445

4546
full_text, images, out_meta = parse_single_pdf(pdf_bytes, model_state.model_list)
46-
images = encode_images(images)
4747

4848
os.remove(input_path)
4949
os.remove(output_pdf_path)
5050
os.rmdir(output_dir)
5151

52-
return JSONResponse(content={"message": "PPT parsed successfully", "markdown": full_text, "metadata": out_meta, "images": images})
52+
result = responseDocument(
53+
text=full_text,
54+
metadata=out_meta
55+
)
56+
encode_images(images,result)
57+
58+
return JSONResponse(content=result.model_dump())
5359

5460
@document_router.post("/docs")
5561
async def parse_doc_endpoint(file: UploadFile = File(...)):
@@ -68,13 +74,14 @@ async def parse_doc_endpoint(file: UploadFile = File(...)):
6874
pdf_bytes = pdf_file.read()
6975

7076
full_text, images, out_meta = parse_single_pdf(pdf_bytes, model_state.model_list)
71-
images = encode_images(images)
7277

73-
os.remove(input_path)
74-
os.remove(output_pdf_path)
75-
os.rmdir(output_dir)
78+
result = responseDocument(
79+
text=full_text,
80+
metadata=out_meta
81+
)
82+
encode_images(images,result)
7683

77-
return JSONResponse(content={"message": "PPT parsed successfully", "markdown": full_text, "metadata": out_meta, "images": images})
84+
return JSONResponse(content=result.model_dump())
7885

7986
@document_router.post("")
8087
async def parse_any_endpoint(file: UploadFile = File(...)):
@@ -98,11 +105,16 @@ async def parse_any_endpoint(file: UploadFile = File(...)):
98105

99106
# Common parsing logic
100107
full_text, images, out_meta = parse_single_pdf(input_path, model_state.model_list)
101-
images = encode_images(images)
102108

103109
os.remove(input_path)
104110

105-
return JSONResponse(content={"message": "Document parsed successfully", "markdown": full_text, "metadata": out_meta, "images": images})
111+
result = responseDocument(
112+
text=full_text,
113+
metadata=out_meta
114+
)
115+
encode_images(images,result)
116+
117+
return JSONResponse(content=result.model_dump())
106118

107119

108120
# @document_router.post("/docs")

0 commit comments

Comments
 (0)