ELin2025 commented on code in PR #5568: URL: https://github.com/apache/texera/pull/5568#discussion_r3445099673
########## common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/huggingFace/codegen/ImageTaskCodegen.scala: ########## @@ -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." + | ) Review Comment: Done -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
