Chapter 1 questions

How can we do part of speech tagging using pipeline?

In the try-it-out section before Question Answering module in Transformers What can they do submodule, we are asked to try POS on this sentence “My name is Sylvain and I work at Hugging Face in Brooklyn.”

However in pipeline, I could only see the following tasks

[‘audio-classification’, ‘automatic-speech-recognition’, ‘conversational’, ‘depth-estimation’, ‘document-question-answering’, ‘feature-extraction’, ‘fill-mask’, ‘image-classification’, ‘image-segmentation’, ‘image-to-text’, ‘ner’, ‘object-detection’, ‘question-answering’, ‘sentiment-analysis’, ‘summarization’, ‘table-question-answering’, ‘text-classification’, ‘text-generation’, ‘text2text-generation’, ‘token-classification’, ‘translation’, ‘video-classification’, ‘visual-question-answering’, ‘vqa’, ‘zero-shot-audio-classification’, ‘zero-shot-classification’, ‘zero-shot-image-classification’, ‘zero-shot-object-detection’, ‘translation_XX_to_YY’]

There is no support for the POS task. Should we use text-classification and ask it to predict B-Noun, I-Noun, O, B-Verb, O, I-Verb or something like this to get it done?

The pipeline function does not support translation task out of the box. I get a KeyError as follows

KeyError                                  Traceback (most recent call last)
Cell In[34], line 1
----> 1 translator = pipeline("translation", model = "HariSekhar/Eng_Marathi_translation")

File ~/huggingface/lib/python3.10/site-packages/transformers/pipelines/__init__.py:692, in pipeline(task, model, config, tokenizer, feature_extractor, image_processor, framework, revision, use_fast, use_auth_token, device, device_map, torch_dtype, trust_remote_code, model_kwargs, pipeline_class, **kwargs)
    690     hub_kwargs["_commit_hash"] = config._commit_hash
    691 elif config is None and isinstance(model, str):
--> 692     config = AutoConfig.from_pretrained(model, _from_pipeline=task, **hub_kwargs, **model_kwargs)
    693     hub_kwargs["_commit_hash"] = config._commit_hash
    695 custom_tasks = {}

File ~/huggingface/lib/python3.10/site-packages/transformers/models/auto/configuration_auto.py:917, in AutoConfig.from_pretrained(cls, pretrained_model_name_or_path, **kwargs)
    915     return config_class.from_pretrained(pretrained_model_name_or_path, **kwargs)
    916 elif "model_type" in config_dict:
--> 917     config_class = CONFIG_MAPPING[config_dict["model_type"]]
    918     return config_class.from_dict(config_dict, **unused_kwargs)
    919 else:
    920     # Fallback: use pattern matching on the string.
    921     # We go from longer names to shorter names to catch roberta before bert (for instance)

File ~/huggingface/lib/python3.10/site-packages/transformers/models/auto/configuration_auto.py:623, in _LazyConfigMapping.__getitem__(self, key)
    621     return self._extra_content[key]
    622 if key not in self._mapping:
--> 623     raise KeyError(key)
    624 value = self._mapping[key]
    625 module_name = model_type_to_module_name(key)

KeyError: 'translation'

Does the pipeline now not support the translation task? I am using transformers 4.27.4 version.

i face some problem because of tensorflow and transforemer version so please anyone guide me that which versio is working in the given cource?

1 Like

Hi
I’m doing
:pencil2: Try it out! Search the Model Hub for a model able to do part-of-speech tagging (usually abbreviated as POS) in English. What does this model predict for the sentence in the example above?

But I’m confused about the pipeline to use (ner or token-classification), and why

pos = pipeline("ner", grouped_entities=True, model="QCRI/bert-base-multilingual-cased-pos-english", device=device)    
pos = pipeline("token-classification", grouped_entities=True, model="QCRI/bert-base-multilingual-cased-pos-english", device=device)

Someones help me ```
^^^^^^^^^^^^^^^^^^^^^^^
PS C:\Users\grknb> & C:/Users/grknb/AppData/Local/Microsoft/WindowsApps/python3.12.exe c:/Users/grknb/ChatBotProject/bot.py
Understood. How can I assist you today from this virtual cockpit, over?Traceback (most recent call last):
File “C:\Users\grknb\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\huggingface_hub\utils_errors.py”, line 304, in hf_raise_for_status
response.raise_for_status()
File “C:\Users\grknb\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\requests\models.py”, line 1024, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://huggingface.co/mistralai/Mistral-Nemo-Instruct-2407/resolve/main/config.json

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “C:\Users\grknb\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\transformers\utils\hub.py”, line 402, in cached_file
resolved_file = hf_hub_download(
^^^^^^^^^^^^^^^^
File “C:\Users\grknb\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\huggingface_hub\utils_deprecation.py”, line 101, in inner_f
return f(*args, **kwargs)
^^^^^^^^^^^^^^^^^^
File “C:\Users\grknb\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\huggingface_hub\utils_validators.py”, line 114, in _inner_fn
return fn(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^
File “C:\Users\grknb\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\huggingface_hub\file_download.py”, line 1240, in hf_hub_download
return _hf_hub_download_to_cache_dir(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\grknb\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\huggingface_hub\file_download.py”, line 1347, in _hf_hub_download_to_cache_dir
_raise_on_head_call_error(head_call_error, force_download, local_files_only)
File “C:\Users\grknb\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\huggingface_hub\file_download.py”, line 1854, in _raise_on_head_call_error
raise head_call_error
File “C:\Users\grknb\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\huggingface_hub\file_download.py”, line 1751, in _get_metadata_or_catch_error
metadata = get_hf_file_metadata(
^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\grknb\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\huggingface_hub\utils_validators.py”, line 114, in _inner_fn
return fn(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^
File “C:\Users\grknb\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\huggingface_hub\file_download.py”, line 1673, in get_hf_file_metadata
r = _request_wrapper(
^^^^^^^^^^^^^^^^^
File “C:\Users\grknb\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\huggingface_hub\file_download.py”, line 376, in _request_wrapper
response = _request_wrapper(
^^^^^^^^^^^^^^^^^
File “C:\Users\grknb\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\huggingface_hub\file_download.py”, line 400, in _request_wrapper
hf_raise_for_status(response)
File “C:\Users\grknb\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\huggingface_hub\utils_errors.py”, line 321, in hf_raise_for_status
raise GatedRepoError(message, response) from e
huggingface_hub.utils._errors.GatedRepoError: 401 Client Error. (Request ID: Root=1-66ab6fcd-47b492f27ea0681b1ac8ec4d;a116e880-29f9-442f-a9fe-367fe852eea2)

Cannot access gated repo for url https:/ /huggingface.co/mistralai/Mistral-Nemo-Instruct-2407/resolve/main/config.json.
Access to model mistralai/Mistral-Nemo-Instruct-2407 is restricted. You must be authenticated to access it.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “c:\Users\grknb\ChatBotProject\bot.py”, line 22, in
tokenizer = AutoTokenizer.from_pretrained(model_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\grknb\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\transformers\models\auto\tokenization_auto.py”, line 853, in from_pretrained
config = AutoConfig.from_pretrained(
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\grknb\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\transformers\models\auto\configuration_auto.py”, line 972, in from_pretrained
config_dict, unused_kwargs = PretrainedConfig.get_config_dict(pretrained_model_name_or_path, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\grknb\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\transformers\configuration_utils.py”, line 632, in get_config_dict
config_dict, kwargs = cls._get_config_dict(pretrained_model_name_or_path, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\grknb\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\transformers\configuration_utils.py”, line 689, in _get_config_dict
resolved_config_file = cached_file(
^^^^^^^^^^^^
File “C:\Users\grknb\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\transformers\utils\hub.py”, line 420, in cached_file
raise EnvironmentError(
OSError: You are trying to access a gated repo.
Make sure to have access to it at https:/ /huggingface.co/mistralai/Mistral-Nemo-Instruct-2407.
401 Client Error. (Request ID: Root=1-66ab6fcd-47b492f27ea0681b1ac8ec4d;a116e880-29f9-442f-a9fe-367fe852eea2)

Cannot access gated repo for url https: // huggingface .co/ mistralai/Mistral-Nemo-Instruct-2407/resolve/main/config.json.
Access to model mistralai/ Mistral-Nemo-Instruct-2407 is restricted. You must be authenticated to access it.

hi @ElisonSherton
I guess config.json · HariSekhar/Eng_Marathi_translation at main should be fixed.

“translation” is not a valid model_type:

1 Like

hi @jmalbarran
ner is an alias for token-classification. You can use either ner or token-classification.

Can someone help me? sentiment-analysis does not work. It crashes with this message:

.venv/lib/python3.12/site-packages/transformers/tokenization_utils_base.py:1602: FutureWarning: `clean_up_tokenization_spaces` was not set. It will be set to `True` by default. This behavior will be deprecated in transformers v4.45, and will be then set to `False` by default. For more details check this issue: https://github.com/huggingface/transformers/issues/31884
  warnings.warn(

hi @fromjon
I guess you run the snippet in Transformers, what can they do? - Hugging Face NLP Course. Actually there is no crash in the output, it’s just a warning. Did you run classifier("example sentence") part?

from transformers import pipeline

classifier = pipeline("sentiment-analysis")
classifier("I've been waiting for a HuggingFace course my whole life.")

Hello everyone :slight_smile:

I have been having this question in the back of my mind for several time now : while pretraining a decoder, when you do a forward pass on a single sequence (batch of one), you have outputs for each tokens passed. Thus is the loss multidimensional ? This is : one cross-entropy loss for each output ? And in that case, how do you do the backward pass, because it looks like you could do N backward passes if there are N outputs and N losses ?

Any help much appreciated :slight_smile:

In my case, none of those options work, despite having followed thoroughly the “set up” instructions (I’m running it locally from VS code).

In the case of “classifier = pipeline(“sentiment-analysis”, model=“distilbert-base-uncased-finetuned-sst-2-english”)”
the outcome I get in the terminal is:
2024-10-30 20:17:37.892992: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable TF_ENABLE_ONEDNN_OPTS=0.
2024-10-30 20:17:37.894943: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.
2024-10-30 20:17:37.899428: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.
2024-10-30 20:17:37.910112: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:477] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
E0000 00:00:1730315857.925005 3245 cuda_dnn.cc:8310] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
E0000 00:00:1730315857.929321 3245 cuda_blas.cc:1418] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
2024-10-30 20:17:37.945537: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
config.json: 100%|████████████████████████████████████████████████████████████████████████| 629/629 [00:00<00:00, 3.03MB/s]
model.safetensors: 100%|█████████████████████████████████████████████████████████████████| 268M/268M [00:01<00:00, 205MB/s]
2024-10-30 20:17:42.854809: E external/local_xla/xla/stream_executor/cuda/cuda_driver.cc:152] failed call to cuInit: INTERNAL: CUDA error: Failed call to cuInit: UNKNOWN ERROR (303)
All PyTorch model weights were used when initializing TFDistilBertForSequenceClassification.

All the weights of TFDistilBertForSequenceClassification were initialized from the PyTorch model.
If your task is similar to the task the model of the checkpoint was trained on, you can already use TFDistilBertForSequenceClassification for predictions without further training.
tokenizer_config.json: 100%|█████████████████████████████████████████████████████████████| 48.0/48.0 [00:00<00:00, 328kB/s]
vocab.txt: 100%|████████████████████████████████████████████████████████████████████████| 232k/232k [00:00<00:00, 1.43MB/s]

in the case of “classifier = pipeline(“sentiment-analysis”)” , this is what i get:
2024-10-30 20:21:00.260212: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable TF_ENABLE_ONEDNN_OPTS=0.
2024-10-30 20:21:00.261552: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.
2024-10-30 20:21:00.264552: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.
2024-10-30 20:21:00.273810: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:477] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
E0000 00:00:1730316060.288831 4773 cuda_dnn.cc:8310] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
E0000 00:00:1730316060.293224 4773 cuda_blas.cc:1418] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
2024-10-30 20:21:00.308215: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
No model was supplied, defaulted to distilbert/distilbert-base-uncased-finetuned-sst-2-english and revision 714eb0f (distilbert/distilbert-base-uncased-finetuned-sst-2-english · Hugging Face).
Using a pipeline without specifying a model name and revision in production is not recommended.
2024-10-30 20:21:02.557399: E external/local_xla/xla/stream_executor/cuda/cuda_driver.cc:152] failed call to cuInit: INTERNAL: CUDA error: Failed call to cuInit: UNKNOWN ERROR (303)
All PyTorch model weights were used when initializing TFDistilBertForSequenceClassification.

All the weights of TFDistilBertForSequenceClassification were initialized from the PyTorch model.
If your task is similar to the task the model of the checkpoint was trained on, you can already use TFDistilBertForSequenceClassification for predictions without further training.

Thanks in advance for your help!

I am just learning all the AI/ML . For the context in the question-answer pipeline , can we give a textfile, instead of a single line context ? If yes, is there any format for the textfile ?

I am runing codes in vscode or terminal or jupyter notebook of VMware-ubuntu,when i implementation code just like:“unmasker = pipeline(“fill-mask”, model = “bert-base-uncased”)” or " classifier = pipeline(“sentiment-analysis”)" ,always have network connection error(rarely it will be work)whatever i use VPN or not.
return like this:
OSError: We couldn’t connect to ‘https://huggingface.co’ to load this file, couldn’t find it in the cached files and it looks like bert-base-uncased is not the path to a directory containing a file named config.json.
Checkout your internet connection or see how to run the library in offline mode at ‘Installation’.

1 Like

Great it helped. I hope HF team can update the code in the course itself!

1 Like

Which IDE are you using?

1 Like

Add a print in the last line - - Other errors can be ignored—

from transformers import pipeline
classifier = pipeline(“sentiment-analysis”)
result = classifier(“I’ve been waiting for a HuggingFace course my whole life”)

print (result)

1 Like

Hi NLP Course Team,

I’ve been exploring the transformer content in your NLP course and creating educational materials in this space, including a tutorial on implementing transformer architecture from scratch. While working through the material, I noticed there might be opportunities to deepen the coverage of certain topics, such as:

  • Detailed exploration of each component within the transformer architecture
  • Advanced attention mechanisms and their applications
  • Practical implementation considerations and common pitfalls

I’ve also been writing about these topics on LinkedIn here and here. Would adding more educational content in these areas be beneficial? If so, I’d love to explore ways I could contribute.

I strongly believe in the power of knowledge sharing and collaboration, and my goal is to be part of something greater than myself—building tools and fostering communities that inspire others to learn, innovate, and create.

Looking forward to your thoughts!

1 Like