Skip to content
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

add parse method #4

Merged
merged 3 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions open_parser/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,41 @@
CAMBIO_EXTRACT_URL = (
"https://p1iz3c1c77.execute-api.us-west-2.amazonaws.com/v1/cambio_api/extract"
)
CAMBIO_PARSE_URL = (
"https://p1iz3c1c77.execute-api.us-west-2.amazonaws.com/v1/cambio_api/parse"
)


class OpenParser:
def __init__(self, apiKey="") -> None:
def __init__(self, apiKey) -> None:
self._uploadurl = CAMBIO_UPLOAD_URL
self._extracturl = CAMBIO_EXTRACT_URL
self._parseurl = CAMBIO_PARSE_URL
self._request_header = {"x-api-key": apiKey}

def setAPIKey(self, apiKey):
self._request_header = {"x-api-key": apiKey}

def extract_pdf_content(self, file_path):
def extract(self, file_path):
user_id, job_id, s3_key = self._request_and_upload_by_apiKey(file_path)
result = self._request_file_extraction(user_id, job_id, s3_key)
return result["file_content"]

def parse(self, file_path, prompt):
user_id, job_id, s3_key = self._request_and_upload_by_apiKey(file_path, prompt)
result = self._request_info_extraction(user_id, job_id, s3_key)
return result["results"]

def _error_handler(self, response):
if response.status_code == 403:
raise Exception("Invalid API Key")
elif response.status_code == 429:
return Exception("API Key limit exceeded")
raise Exception("API Key limit exceeded")
else:
return Exception(f"Error: {response.status_code} {response.text}")
raise Exception(f"Error: {response.status_code} {response.text}")

def _request_and_upload_by_apiKey(self, file_path):
params = {"fileName": file_path}
def _request_and_upload_by_apiKey(self, file_path, prompt=""):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we should force people to pass in prompt or to fail people early. Otherwise, user might force to put prompt and cause the backend to function improperly.

params = {"fileName": file_path, "prompt": prompt}
response = requests.get(
self._uploadurl, headers=self._request_header, params=params
)
Expand Down Expand Up @@ -67,3 +76,19 @@ def _request_file_extraction(self, user_id, job_id, s3_key):
return json.loads(response.json()["result"])

self._error_handler(response)

def _request_info_extraction(self, user_id, job_id, s3_key):
payload = {
"userId": user_id,
"jobId": job_id,
"fileKey": s3_key,
}
response = requests.post(
self._parseurl, headers=self._request_header, json=payload
)

if response.status_code == 200:
print("Extraction success.")
return json.loads(response.json()["result"])

self._error_handler(response)
13 changes: 10 additions & 3 deletions tests/test_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@
example_local_file = "./schedule.pdf"

op = OpenParser(example_apikey)
result = op.extract_pdf_content(example_local_file)

print(type(result))
print(result)
print("file content extraction test:")
content_result = op.extract(example_local_file)
print(type(content_result))
print(content_result)

print("file information QA test:")
example_prompt = "This is a test, simply return filename and 'test'"
qa_result = op.parse(example_local_file, example_prompt)
print(type(qa_result))
print(qa_result)
Loading