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 optional language_hints input parameter to be passed as imageContext.languageHints #982

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ class BlockManifest(WorkflowBlockManifest):
},
},
)
language_hints: Optional[List[str]] = Field(
default=None,
description="Optional list of language codes to pass to the OCR API. If not provided, the API will attempt to detect the language automatically."
"If provided, language codes must be supported by the OCR API, visit https://cloud.google.com/vision/docs/languages for list of supported language codes.",
examples=[["en", "fr"], ["de"]],
)
api_key: Union[Selector(kind=[STRING_KIND, SECRET_KIND]), str] = Field(
description="Your Google Vision API key",
examples=["xxx-xxx", "$inputs.google_api_key"],
Expand Down Expand Up @@ -111,6 +117,7 @@ def run(
self,
image: WorkflowImageData,
ocr_type: Literal["text_detection", "ocr_text_detection"],
language_hints: Optional[List[str]],
api_key: str,
) -> BlockResult:
# Decide which type of OCR to use and make the request to Google Vision API
Expand All @@ -121,17 +128,22 @@ def run(
else:
raise ValueError(f"Invalid ocr_type: {ocr_type}")

request_json = {
"requests": [
{
"image": {"content": image.base64_image},
"features": [{"type": type}],
}
]
}

if language_hints is not None:
for r in request_json["requests"]:
r["imageContext"] = {"languageHints": language_hints}
response = requests.post(
"https://vision.googleapis.com/v1/images:annotate",
params={"key": api_key},
json={
"requests": [
{
"image": {"content": image.base64_image},
"features": [{"type": type}],
}
]
},
json=request_json,
)

if response.status_code != 200:
Expand Down
Loading