Unverified Commit f278ef20 authored by NielsRogge's avatar NielsRogge Committed by GitHub
Browse files

[Nougat] Fix pipeline (#28242)

* Fix pipeline

* Remove print statements

* Address comments

* Address issue

* Remove unused imports
parent 58e3d23e
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import io
import json import json
import os import os
import warnings import warnings
...@@ -20,7 +19,6 @@ from pathlib import Path ...@@ -20,7 +19,6 @@ from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union
from huggingface_hub import model_info from huggingface_hub import model_info
from numpy import isin
from ..configuration_utils import PretrainedConfig from ..configuration_utils import PretrainedConfig
from ..dynamic_module_utils import get_class_from_dynamic_module from ..dynamic_module_utils import get_class_from_dynamic_module
...@@ -446,7 +444,8 @@ NO_TOKENIZER_TASKS = set() ...@@ -446,7 +444,8 @@ NO_TOKENIZER_TASKS = set()
# any tokenizer/feature_extractor might be use for a given model so we cannot # 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 # use the statically defined TOKENIZER_MAPPING and FEATURE_EXTRACTOR_MAPPING to
# see if the model defines such objects or not. # 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(): for task, values in SUPPORTED_TASKS.items():
if values["type"] == "text": if values["type"] == "text":
NO_FEATURE_EXTRACTOR_TASKS.add(task) NO_FEATURE_EXTRACTOR_TASKS.add(task)
...@@ -930,7 +929,10 @@ def pipeline( ...@@ -930,7 +929,10 @@ def pipeline(
and not load_tokenizer and not load_tokenizer
and normalized_task not in NO_TOKENIZER_TASKS and normalized_task not in NO_TOKENIZER_TASKS
# Using class name to avoid importing the real class. # 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 # 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 # so the model_config might not define a tokenizer, but it seems to be
...@@ -941,8 +943,7 @@ def pipeline( ...@@ -941,8 +943,7 @@ def pipeline(
and not load_image_processor and not load_image_processor
and normalized_task not in NO_IMAGE_PROCESSOR_TASKS and normalized_task not in NO_IMAGE_PROCESSOR_TASKS
# Using class name to avoid importing the real class. # 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_VISION_CONFIGS
and normalized_task != "automatic-speech-recognition"
): ):
# This is a special category of models, that are fusions of multiple models # 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 # so the model_config might not define a tokenizer, but it seems to be
...@@ -953,7 +954,7 @@ def pipeline( ...@@ -953,7 +954,7 @@ def pipeline(
and not load_feature_extractor and not load_feature_extractor
and normalized_task not in NO_FEATURE_EXTRACTOR_TASKS and normalized_task not in NO_FEATURE_EXTRACTOR_TASKS
# Using class name to avoid importing the real class. # 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 # 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 # so the model_config might not define a tokenizer, but it seems to be
......
...@@ -247,14 +247,16 @@ class ImageToTextPipelineTests(unittest.TestCase): ...@@ -247,14 +247,16 @@ class ImageToTextPipelineTests(unittest.TestCase):
@require_torch @require_torch
def test_conditional_generation_llava(self): def test_conditional_generation_llava(self):
pipe = pipeline("image-to-text", model="llava-hf/bakLlava-v1-hf") 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 = ( prompt = (
"<image>\nUSER: What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud?\nASSISTANT:" "<image>\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( self.assertEqual(
outputs, outputs,
[ [
...@@ -263,3 +265,15 @@ class ImageToTextPipelineTests(unittest.TestCase): ...@@ -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"}],
)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment