[Nougat] Fix pipeline (#28242)
* Fix pipeline * Remove print statements * Address comments * Address issue * Remove unused imports
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 = (
|
||||
"<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(
|
||||
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"}],
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user