22import requests
33import os
44import time
5+ import json
56from typing import Dict , List , Union , Optional
67
78from .base_client import BaseClient , AsyncBaseClient
8- from . import ParseResponse
9- from .axtract .models import EquationExtractionResponse
10-
9+ from . import ParseResponse , EquationProcessingResponse , EquationExtractionResponse
1110
1211class Axiomatic (BaseClient ):
1312 def __init__ (self , * args , ** kwargs ):
@@ -17,6 +16,7 @@ def __init__(self, *args, **kwargs):
1716
1817 self .document_helper = DocumentHelper (self )
1918 self .tools_helper = ToolsHelper (self )
19+ self .axtract_helper = AxtractHelper (self )
2020
2121
2222class AxtractHelper :
@@ -38,6 +38,7 @@ def analyze_equations(
3838 url_path : Optional [str ] = None ,
3939 parsed_paper : Optional [ParseResponse ] = None ,
4040 ) -> Optional [EquationExtractionResponse ]:
41+ response : EquationExtractionResponse | EquationProcessingResponse
4142 if file_path :
4243 with open (file_path , "rb" ) as file :
4344 response = self ._ax_client .document .equation .from_pdf (document = file )
@@ -47,7 +48,7 @@ def analyze_equations(
4748
4849 response = self ._ax_client .document .equation .from_pdf (document = url_path )
4950 elif parsed_paper :
50- response = self ._ax_client .document .equation .process (** parsed_paper )
51+ response = self ._ax_client .document .equation .process (** parsed_paper . model_dump () )
5152 else :
5253 print ("Please provide either a file path or a URL to analyze." )
5354 return None
@@ -135,13 +136,19 @@ def navigate_image(change):
135136 def save_parsed_pdf (self , response : ParseResponse , path : str ):
136137 """Save a parsed PDF response to a file."""
137138 os .makedirs (path , exist_ok = True )
139+ with open (os .path .join (path , "text.md" ), "w" ) as f :
140+ f .write (response .markdown )
141+
138142 if response .images :
139143 for img_name , img in response .images .items ():
140144 with open (os .path .join (path , f"{ img_name } .png" ), "wb" ) as f :
141145 f .write (base64 .b64decode (img ))
142146
143- with open (os .path .join (path , "text.md" ), "w" ) as f :
144- f .write (response .markdown )
147+ with open (os .path .join (path , "interline_equations.json" ), "w" ) as f :
148+ json .dump (response .interline_equations , f )
149+
150+ with open (os .path .join (path , "inline_equations.json" ), "w" ) as f :
151+ json .dump (response .inline_equations , f )
145152
146153 def load_parsed_pdf (self , path : str ) -> ParseResponse :
147154 """Load a parsed PDF response from a file."""
@@ -154,7 +161,18 @@ def load_parsed_pdf(self, path: str) -> ParseResponse:
154161 with open (os .path .join (path , img_name ), "rb" ) as img_file :
155162 images [img_name ] = base64 .b64encode (img_file .read ()).decode ("utf-8" )
156163
157- return ParseResponse (markdown = markdown , images = images )
164+ with open (os .path .join (path , "interline_equations.json" ), "r" ) as f :
165+ interline_equations = json .load (f )
166+
167+ with open (os .path .join (path , "inline_equations.json" ), "r" ) as f :
168+ inline_equations = json .load (f )
169+
170+ return ParseResponse (
171+ markdown = markdown ,
172+ images = images ,
173+ interline_equations = interline_equations ,
174+ inline_equations = inline_equations ,
175+ )
158176
159177
160178class ToolsHelper :
0 commit comments