from pathlib import Path
from affinda import AffindaAPI, TokenCredential
from affinda_models.resume_parser import ResumeParser
API_KEY = "YOUR_API_KEY" # replace with your actual key
WORKSPACE_ID = "YOUR_WORKSPACE_IDENTIFIER" # e.g. "vBAdDBer"
FILE_PATH = Path("resume.pdf") # path to the résumé you want to parse
# Set up the client
credential = TokenCredential(token=API_KEY)
client = AffindaAPI(credential=credential)
# This will raise `pydantic_core.ValidationError` if the API response
# does not validate against ResumeParser
with FILE_PATH.open("rb") as f:
doc = client.create_document(
file=f,
workspace=WORKSPACE_ID,
data_model=ResumeParser,
)
# Access parsed data
print(doc.parsed) # This is a ResumeParser instance
print(doc.data) # This is the raw JSON response
# This will NOT raise `pydantic_core.ValidationError` if the API response
# does not validate against ResumeParser.
# Instead `parsed` will be None if the API response is not compatible.
with FILE_PATH.open("rb") as f:
doc = client.create_document(
file=f,
workspace=WORKSPACE_ID,
data_model=ResumeParser,
ignore_validation_errors=True,
)
if doc.parsed:
print("API response is valid.")
print(doc.parsed)
else:
print("API response is invalid.")
print(doc.data) # The raw JSON response is still available