-
Notifications
You must be signed in to change notification settings - Fork 165
feat(huggingFace): add image task family via ImageTaskCodegen #5320
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1e8e330
feat(huggingFace): add image task family via ImageTaskCodegen
PG1204 c19dc38
style: apply scalafmt and prettier to HF inference spec and image upl…
PG1204 7896de2
chore: add Apache license header to HF image upload template and styles
PG1204 665ca4c
test(frontend): cover HuggingFaceImageUploadComponent
PG1204 9392add
fix(huggingFace): zero-shot labels, polling progress logs, data-URL c…
PG1204 5e0df3e
fix(huggingFace): address feedback on image tasks, add new regression…
PG1204 3553a2c
chore: retrigger CI
PG1204 9ab3e60
chore: retrigger CI
PG1204 b56f3ac
chore: retrigger CI
PG1204 ac0497f
Merge branch 'apache:main' into hf/03-image-tasks
PG1204 ea0036b
fix(huggingFace): allow image+prompt tasks to run without a prompt co…
PG1204 35e8348
fix(huggingFace): reject worker-filesystem path reads for image input…
PG1204 de50344
fix(huggingFace): correct double-escaped regexes, pandas NA sentinels…
PG1204 dc15e09
fix(huggingFace): harden remote URL fetches - https-only, block priva…
PG1204 eec7356
Merge branch 'apache:main' into hf/03-image-tasks
PG1204 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
...rc/main/scala/org/apache/texera/amber/operator/huggingFace/codegen/ImageTaskCodegen.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.texera.amber.operator.huggingFace.codegen | ||
|
|
||
| /** | ||
| * Codegen for the Hugging Face image-pipeline task family. | ||
| * | ||
| * Splits into two sub-families: | ||
| * - "image-only" tasks send raw image bytes as the request body and don't | ||
| * consume the prompt column: image-classification, object-detection, | ||
| * image-segmentation, image-to-text. | ||
| * - "image + prompt" tasks bundle a base64 image and a text prompt in a | ||
| * JSON payload: visual-question-answering, document-question-answering, | ||
| * zero-shot-image-classification, image-text-to-text, image-to-image. | ||
| * | ||
| * Per-row `current_image_bytes` is resolved upstream in | ||
| * [[PythonCodegenBase]]'s `process_table` (either from the operator's | ||
| * uploaded image or from `INPUT_IMAGE_COLUMN`). The image helpers | ||
| * (`_read_image_input`, `_compress_image_bytes`, `_image_input_as_base64`, | ||
| * `_read_binary_value`, `_looks_like_html`, `_html_to_image_bytes`, | ||
| * `_extract_json_arg`) live in PythonCodegenBase alongside the per-task | ||
| * tuples (`image_only_tasks`, `image_prompt_tasks`, `image_tasks`). | ||
| */ | ||
| object ImageTaskCodegen extends TaskCodegen { | ||
|
|
||
| /** Primary key for registration; the dispatcher maps every task in | ||
| * [[tasks]] to this codegen. | ||
| */ | ||
| override val task: String = "image-classification" | ||
|
|
||
| /** All HF tasks routed through this codegen. */ | ||
| override val tasks: Set[String] = Set( | ||
| // image-only | ||
| "image-classification", | ||
| "object-detection", | ||
| "image-segmentation", | ||
| "image-to-text", | ||
| // image + prompt | ||
| "visual-question-answering", | ||
| "document-question-answering", | ||
| "zero-shot-image-classification", | ||
| "image-text-to-text", | ||
| "image-to-image" | ||
| ) | ||
|
|
||
| override def payloadPython(ctx: CodegenContext): String = | ||
| """ if task in image_only_tasks: | ||
| | payload = current_image_bytes | ||
| | use_raw_binary_body = True | ||
| | raw_binary_headers = image_headers | ||
| | elif task in ("visual-question-answering", "document-question-answering"): | ||
| | payload = { | ||
| | "inputs": { | ||
| | "image": self._image_input_as_base64(current_image_bytes), | ||
| | "question": prompt_value, | ||
| | } | ||
| | } | ||
| | elif task == "image-text-to-text": | ||
| | img_b64 = self._image_input_as_base64(current_image_bytes) | ||
| | payload = { | ||
| | "model": self.MODEL_ID, | ||
| | "messages": [{ | ||
| | "role": "user", | ||
| | "content": [ | ||
| | {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{img_b64}"}}, | ||
| | {"type": "text", "text": prompt_value if prompt_value else "Describe this image."}, | ||
| | ], | ||
| | }], | ||
| | "max_tokens": self.MAX_NEW_TOKENS, | ||
| | } | ||
| | elif task == "image-to-image": | ||
| | payload = current_image_bytes | ||
| | use_raw_binary_body = True | ||
| | raw_binary_headers = image_headers | ||
| | elif task == "zero-shot-image-classification": | ||
| | # Zero-shot requires the caller to supply candidate labels. | ||
| | # We reuse the prompt column as a comma-separated label list so | ||
| | # the task is shippable without a dedicated operator field. | ||
| | # TODO: replace with a first-class `candidateLabels` field once | ||
| | # the property panel supports task-specific inputs. | ||
| | # | ||
| | # Fail fast if usable labels can't be derived. Both modes lead to | ||
| | # a meaningless inference call: | ||
| | # 1. Empty prompt column -> labels = [] | ||
| | # The HF API rejects candidate_labels: [] with an opaque 400. | ||
| | # 2. Missing prompt column -> upstream sets prompt_value | ||
| | # to the fallback "What is shown in this image?", which has | ||
| | # no comma, so labels collapses to a single nonsense entry. | ||
| | # Zero-shot classification needs >= 2 candidate labels to be | ||
| | # meaningful — surface a configuration error in both cases. | ||
| | labels = [s.strip() for s in prompt_value.split(",") if s.strip()] | ||
| | if len(labels) < 2: | ||
| | raise ValueError( | ||
| | "zero-shot-image-classification requires at least 2 candidate " | ||
| | "labels: provide a comma-separated list in the prompt column." | ||
| | ) | ||
| | payload = { | ||
| | "inputs": self._image_input_as_base64(current_image_bytes), | ||
| | "parameters": {"candidate_labels": labels}, | ||
| | } | ||
| | else: | ||
| | payload = {"inputs": prompt_value}""".stripMargin | ||
|
|
||
| override def parsePython(ctx: CodegenContext): String = | ||
| """ if task == "image-to-text": | ||
| | if isinstance(body, dict): | ||
| | if "md_results" in body: | ||
| | return body["md_results"] | ||
| | if "choices" in body: | ||
| | return body["choices"][0]["message"]["content"] | ||
| | if isinstance(body, list) and body and isinstance(body[0], dict): | ||
| | return body[0].get("generated_text", json.dumps(body)) | ||
| | return json.dumps(body) | ||
| | elif task in ("visual-question-answering", "document-question-answering"): | ||
| | if isinstance(body, dict): | ||
| | return body.get("answer", json.dumps(body)) | ||
| | return json.dumps(body) | ||
| | elif task == "image-text-to-text": | ||
| | if isinstance(body, dict) and "choices" in body: | ||
| | return body["choices"][0]["message"]["content"] | ||
| | if isinstance(body, list) and body and isinstance(body[0], dict): | ||
| | return body[0].get("generated_text", json.dumps(body)) | ||
| | return json.dumps(body) | ||
| | elif task == "image-to-image": | ||
| | if isinstance(body, dict): | ||
| | if "output" in body: | ||
| | out = body["output"] | ||
| | url = out[0] if isinstance(out, list) else out | ||
| | if isinstance(url, str) and url.startswith("http"): | ||
| | return self._url_to_data_url(url) | ||
| | if "images" in body: | ||
| | images = body["images"] | ||
| | if images and isinstance(images[0], dict) and "url" in images[0]: | ||
| | return self._url_to_data_url(images[0]["url"]) | ||
| | if "data" in body: | ||
| | data = body["data"] | ||
| | if isinstance(data, dict) and "outputs" in data: | ||
| | outputs = data["outputs"] | ||
| | if outputs and isinstance(outputs[0], str) and outputs[0].startswith("http"): | ||
| | return self._url_to_data_url(outputs[0]) | ||
| | if isinstance(data, list) and data and isinstance(data[0], dict): | ||
| | if "b64_json" in data[0]: | ||
| | return f"data:image/png;base64,{data[0]['b64_json']}" | ||
| | if "url" in data[0]: | ||
| | return self._url_to_data_url(data[0]["url"]) | ||
| | return json.dumps(body) | ||
| | elif task in ("image-classification", "object-detection", "image-segmentation", "zero-shot-image-classification"): | ||
| | return json.dumps(body)""".stripMargin | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.