-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor any parser classes and update testcases #70
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,48 @@ | |
TIMEOUT = 60 | ||
|
||
|
||
class BasePostProcessor: | ||
def __init__(self, successor=None) -> None: | ||
self.successor = successor | ||
|
||
def process(self, json_response: Dict) -> str: | ||
if self.successor: | ||
return self.successor.process(json_response) | ||
return f"Error: Invalid JSON response: {json_response}" | ||
|
||
|
||
class ParsePostProcessor(BasePostProcessor): | ||
def process(self, json_response: Dict) -> str: | ||
if "markdown" in json_response: | ||
return json_response["markdown"] | ||
return super().process(json_response) | ||
|
||
|
||
class KeyValuePostProcessor(BasePostProcessor): | ||
def process(self, json_response: Dict) -> str: | ||
if "json" in json_response: | ||
return json_response["json"] | ||
return super().process(json_response) | ||
|
||
|
||
class ExtractPIIPostProcessor(BasePostProcessor): | ||
def process(self, json_response: Dict) -> str: | ||
if "pii_extraction" in json_response: | ||
return json_response["pii_extraction"] | ||
return super().process(json_response) | ||
|
||
|
||
class ExtractResumeKeyValuePostProcessor(BasePostProcessor): | ||
|
||
def process(self, json_response: Dict) -> str: | ||
if "resume_extraction" in json_response: | ||
return json_response["resume_extraction"] | ||
return super().process(json_response) | ||
|
||
|
||
class AsyncParser(BaseParser): | ||
def _setup_endpoints(self) -> None: | ||
def __init__(self, api_key: str, base_url: str) -> None: | ||
super().__init__(api_key, base_url) | ||
self._async_upload_url = f"{self._base_url}/async/upload" | ||
self._async_fetch_url = f"{self._base_url}/async/fetch" | ||
|
||
|
@@ -58,3 +98,20 @@ def send_async_request( | |
|
||
# If response successful, upload the file | ||
return upload_file_to_presigned_url(file_content, response) | ||
|
||
def handle_async_response(self, response) -> str: | ||
if response is None: | ||
return "Error: timeout, no response received" | ||
if response.status_code == 202: | ||
return "" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: we might want a better logging here for 202 to indicate that this is still in progress. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ACK |
||
if response.status_code == 200: | ||
extract_resume_processor = ExtractResumeKeyValuePostProcessor() | ||
key_value_processor = KeyValuePostProcessor(extract_resume_processor) | ||
extract_pii_processor = ExtractPIIPostProcessor(key_value_processor) | ||
handler = ParsePostProcessor(extract_pii_processor) | ||
Comment on lines
+108
to
+111
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is interesting to handle all possible cases in the json_response. |
||
try: | ||
return handler.process(response.json()) | ||
except json.JSONDecodeError: | ||
return f"Error: Invalid JSON response: {response.text}" | ||
|
||
return f"Error: {response.status_code} {response.text}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
qq: why we need extract_args for parse?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parse uses VQAProcessor which also has some extract args (VqaProcessorArgs).