From f278ef20edb29382c636b3cb7b5b218bdf0b8c71 Mon Sep 17 00:00:00 2001 From: NielsRogge <48327001+NielsRogge@users.noreply.github.com> Date: Mon, 12 Feb 2024 10:21:15 +0100 Subject: [PATCH] [Nougat] Fix pipeline (#28242) * Fix pipeline * Remove print statements * Address comments * Address issue * Remove unused imports --- src/transformers/pipelines/__init__.py | 15 +++++++------- .../pipelines/test_pipelines_image_to_text.py | 20 ++++++++++++++++--- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/transformers/pipelines/__init__.py b/src/transformers/pipelines/__init__.py index 1684229354..1bb6b1c5e9 100755 --- a/src/transformers/pipelines/__init__.py +++ b/src/transformers/pipelines/__init__.py @@ -12,7 +12,6 @@ # 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. -import io import json import os import warnings @@ -20,7 +19,6 @@ from pathlib import Path from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union from huggingface_hub import model_info -from numpy import isin from ..configuration_utils import PretrainedConfig from ..dynamic_module_utils import get_class_from_dynamic_module @@ -446,7 +444,8 @@ NO_TOKENIZER_TASKS = set() # any tokenizer/feature_extractor might be use for a given model so we cannot # use the statically defined TOKENIZER_MAPPING and FEATURE_EXTRACTOR_MAPPING to # see if the model defines such objects or not. -MULTI_MODEL_CONFIGS = {"SpeechEncoderDecoderConfig", "VisionEncoderDecoderConfig", "VisionTextDualEncoderConfig"} +MULTI_MODEL_AUDIO_CONFIGS = {"SpeechEncoderDecoderConfig"} +MULTI_MODEL_VISION_CONFIGS = {"VisionEncoderDecoderConfig", "VisionTextDualEncoderConfig"} for task, values in SUPPORTED_TASKS.items(): if values["type"] == "text": NO_FEATURE_EXTRACTOR_TASKS.add(task) @@ -930,7 +929,10 @@ def pipeline( and not load_tokenizer and normalized_task not in NO_TOKENIZER_TASKS # Using class name to avoid importing the real class. - and model_config.__class__.__name__ in MULTI_MODEL_CONFIGS + and ( + model_config.__class__.__name__ in MULTI_MODEL_AUDIO_CONFIGS + or model_config.__class__.__name__ in MULTI_MODEL_VISION_CONFIGS + ) ): # This is a special category of models, that are fusions of multiple models # so the model_config might not define a tokenizer, but it seems to be @@ -941,8 +943,7 @@ def pipeline( and not load_image_processor and normalized_task not in NO_IMAGE_PROCESSOR_TASKS # Using class name to avoid importing the real class. - and model_config.__class__.__name__ in MULTI_MODEL_CONFIGS - and normalized_task != "automatic-speech-recognition" + and model_config.__class__.__name__ in MULTI_MODEL_VISION_CONFIGS ): # This is a special category of models, that are fusions of multiple models # so the model_config might not define a tokenizer, but it seems to be @@ -953,7 +954,7 @@ def pipeline( and not load_feature_extractor and normalized_task not in NO_FEATURE_EXTRACTOR_TASKS # Using class name to avoid importing the real class. - and model_config.__class__.__name__ in MULTI_MODEL_CONFIGS + and model_config.__class__.__name__ in MULTI_MODEL_AUDIO_CONFIGS ): # This is a special category of models, that are fusions of multiple models # so the model_config might not define a tokenizer, but it seems to be diff --git a/tests/pipelines/test_pipelines_image_to_text.py b/tests/pipelines/test_pipelines_image_to_text.py index b63589735d..21b297b1e1 100644 --- a/tests/pipelines/test_pipelines_image_to_text.py +++ b/tests/pipelines/test_pipelines_image_to_text.py @@ -247,14 +247,16 @@ class ImageToTextPipelineTests(unittest.TestCase): @require_torch def test_conditional_generation_llava(self): pipe = pipeline("image-to-text", model="llava-hf/bakLlava-v1-hf") - url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg" - image = Image.open(requests.get(url, stream=True).raw) prompt = ( "\nUSER: What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud?\nASSISTANT:" ) - outputs = pipe(image, prompt=prompt, generate_kwargs={"max_new_tokens": 200}) + outputs = pipe( + "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg", + prompt=prompt, + generate_kwargs={"max_new_tokens": 200}, + ) self.assertEqual( outputs, [ @@ -263,3 +265,15 @@ class ImageToTextPipelineTests(unittest.TestCase): } ], ) + + @slow + @require_torch + def test_nougat(self): + pipe = pipeline("image-to-text", "facebook/nougat-base") + + outputs = pipe("https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/nougat_paper.png") + + self.assertEqual( + outputs, + [{"generated_text": "# Nougat: Neural Optical Understanding for Academic Documents\n\n Lukas Blec"}], + )