33import subprocess
44from omniparse .documents .parse import parse_single_pdf
55from 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 )} " )
0 commit comments