3

CODE I AM RUNNING:

from transformers import pipeline

classifier = pipeline('sentiment-analysis')

res = classifier("I Love Python.'")

print(res)

ERROR I AM GETTING:

No model was supplied, defaulted to distilbert-base-uncased-finetuned-sst-2-english and revision af0f99b (https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english).
    Using a pipeline without specifying a model name and revision in production is not recommended.

C:\Users\omran\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\serialization.py:871: UserWarning: Failed to initialize NumPy: module compiled against API version 0x10 but this version of numpy is 0xf (Triggered internally at  ..\torch\csrc\utils\tensor_numpy.cpp:68.)
obj = cast(Storage, torch._UntypedStorage(nbytes))
Traceback (most recent call last):
    File "f:\AIAR\yooo\xox.py", line 5, in <module>
    res = classifier("I've been waiting for a HuggingFace course my whole life.'")
    File "C:\Users\omran\AppData\Local\Programs\Python\Python310\lib\site-packages\transformers\pipelines\text_classification.py", line 138, in __call__
    result = super().__call__(*args, **kwargs)
    File "C:\Users\omran\AppData\Local\Programs\Python\Python310\lib\site-packages\transformers\pipelines\base.py", line 1067, in __call__    
    return self.run_single(inputs, preprocess_params, forward_params, 
postprocess_params)
    File "C:\Users\omran\AppData\Local\Programs\Python\Python310\lib\site-packages\transformers\pipelines\base.py", line 1075, in run_single  
    outputs = self.postprocess(model_outputs, **postprocess_params)   
    File "C:\Users\omran\AppData\Local\Programs\Python\Python310\lib\site-packages\transformers\pipelines\text_classification.py", line 183, in postprocess
    outputs = outputs.numpy()
RuntimeError: Numpy is not available

PIP FREEZE: DON'T MIND, I'VE BEEN DOING A LOT OF TRIAL AND ERROR.

UPDATED OUTPUT:

No model was supplied, defaulted to distilbert-base-uncased-finetuned-sst-2-english and revision af0f99b (https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english). Using a pipeline without specifying a model name and revision in production is not recommended. 2022-08-14 18:45:12.106975: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX AVX2 To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. 2022-08-14 18:45:12.667076: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1532] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 1339 MB memory: -> device: 0, name: NVIDIA GeForce MX230, pci bus id: 0000:01:00.0, compute capability: 6.1 All model checkpoint layers were used when initializing TFDistilBertForSequenceClassification.

All the layers of TFDistilBertForSequenceClassification were initialized from the model checkpoint at distilbert-base-uncased-finetuned-sst-2-english. 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.

GETTING THE OUTPUT I WANT: [{'label': 'POSITIVE', 'score': 0.9973993301391602}]

2 Answers2

3

The immediate fix for you is probably

pip install --upgrade numpy

Which should get you numpy==1.23.1 at the time of this answer.

I didn't take too much of a look at the host of other requirements you have, but if you had other things that required older versions of numpy, you should really look into running virtual environments and isolating the requirements you have from each other, rather than installing everything into your system-wide Python site-packages.

And if I run your code with this setup (in a virtual env)

#> python -m venv .venv
#> source .venv/bin/activate
#> pip install tensorflow==2.9.1 numpy==1.23.1 transformers==4.21.1
#> python hug-test.py

[{'label': 'POSITIVE', 'score': 0.9996470212936401}]

A longer explanation follows...

Your requirement dependencies are broken, and the manual installations of numpy are the issue because you most likely did something similar to these steps to get to this state:

pip install tensorflow
pip install transformers
# at some point
pip uninstall numpy
pip install --ignore-installed numpy==1.22.0

tensorflow and transformers both have pre-built wheels which were built against numpy==1.23.x, so by downgrading your numpy versions, they failed their dependency checks because they have code that checks to make sure that the version that they're built against is the same one that you have installed.

This is the bare minimum requirements.txt that reproduces your issue.

transformers==4.21.1
tensorflow==2.9.1
numpy==1.22.0

And here's a Dockerfile that I used to recreate your environment:

FROM python:3.10-slim-buster

RUN mkdir -p /hug-app
WORKDIR /hug-app

RUN printf "from transformers import pipeline\nclassifier=pipeline('sentiment-analysis')\nres=classifier('I love python.')\nprint(res)" >> hugme.py
RUN printf "transformers==4.21.1\ntensorflow==2.9.1\nnumpy==1.22.0" >> requirements.txt
RUN pip install -r requirements.txt

CMD ["python", "hugme.py"]

If you have Docker/Podman you can run this like I do...

docker build --tag wkl:tensor-test -f ./Dockerfile
docker run wkl:tensor-test

And this produces almost exactly the same output as yours:

No model was supplied, defaulted to distilbert-base-uncased-finetuned-sst-2-english and revision af0f99b (https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english).
Using a pipeline without specifying a model name and revision in production is not recommended.
Downloading config.json: 100%|██████████| 629/629 [00:00<00:00, 425kB/s]
Downloading tensorflow_model.bin: 100%|██████████| 255M/255M [00:09<00:00, 27.6MB/s]
/usr/local/lib/python3.10/site-packages/torch/serialization.py:871: UserWarning: Failed to initialize NumPy: module compiled against API version 0x10 but this version of numpy is 0xf (Triggered internally at  /root/pytorch/torch/csrc/utils/tensor_numpy.cpp:68.)
  obj = cast(Storage, torch._UntypedStorage(nbytes))
Downloading tokenizer_config.json: 100%|██████████| 48.0/48.0 [00:00<00:00, 51.0kB/s]
Downloading vocab.txt: 100%|██████████| 226k/226k [00:00<00:00, 2.85MB/s]
Traceback (most recent call last):
  File "/tensor-app/hugme.py", line 3, in <module>
    res=classifier('I love python.')
  File "/usr/local/lib/python3.10/site-packages/transformers/pipelines/text_classification.py", line 138, in __call__
    result = super().__call__(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/transformers/pipelines/base.py", line 1067, in __call__
    return self.run_single(inputs, preprocess_params, forward_params, postprocess_params)
  File "/usr/local/lib/python3.10/site-packages/transformers/pipelines/base.py", line 1075, in run_single
    outputs = self.postprocess(model_outputs, **postprocess_params)
  File "/usr/local/lib/python3.10/site-packages/transformers/pipelines/text_classification.py", line 183, in postprocess
    outputs = outputs.numpy()
RuntimeError: Numpy is not available
wkl
  • 77,184
  • 16
  • 165
  • 176
  • I reinstalled modules with compatible versions, and it seems to be giving me desired output, but with something that concerns me; I have putted it in the post above – Om. Managements Aug 14 '22 at 13:21
  • 1
    @Om.Managements If you're talking about `Using a pipeline without specifying a model name and revision in production...`, it's just a warning to tell you that you should probably be specifying `model` and `revision` argument when creating `transformers.pipeline` objects. See the documentation: https://huggingface.co/docs/transformers/main_classes/pipelines#transformers.pipeline. – wkl Aug 14 '22 at 15:03
  • Thanks I got that, but how can I fix the other following stuff. I looked a little into it and I think it means tensorflow can also utilize my GPU to work faster. But I can't find how exactly. I downloaded cuDNN and CUDA trying to fix it but it isn't working. tensorflow = v2.91 CUDA = v11.2 cuDNN = v8.1 – Om. Managements Aug 14 '22 at 15:18
  • @Om.Managements Your follow-up is beyond the scope of your question, but Tensorflow transparently uses a GPU if you have a compatible one (and if you're not forcing operations to, say, use a CPU exclusively). In fact, your log indicates Tensorflow created a GPU task for execution, and doesn't seem to have any errors. If you're concerned this is not correct, I suggest: https://www.tensorflow.org/guide/gpu_performance_analysis – wkl Aug 14 '22 at 15:24
  • what can i do to not get that? – Om. Managements Aug 14 '22 at 15:58
  • @Om.Managements set your environment variable `TF_CPP_MIN_LOG_LEVEL` to `2` or `3` to suppress `INFO/WARNING` or all messages if you're asking about silencing the various log output/warnings. – wkl Aug 14 '22 at 16:01
  • is there a way to fix it? – Om. Managements Aug 14 '22 at 16:07
  • @Om.Managements fix...what? Nothing in your logs indicates you have an issue. And if you think you have an issue, have you read the link I gave or other documentation from Tensorflow about GPU usage? – wkl Aug 14 '22 at 16:08
  • can you elaborate what it says, i don't quite get what should i do.. – Om. Managements Aug 14 '22 at 16:12
  • @Om.Managements Again, I have no idea what issue you think you have because your logs don't indicate you have a problem. What do you think is wrong? – wkl Aug 14 '22 at 16:13
  • I want to get a clean output of the sentence i provide, like this tutorial i saw. https://www.youtube.com/watch?v=QEaBAZQCtwE – Om. Managements Aug 14 '22 at 16:15
  • @Om.Managements Since you're in windows, `set TF_CPP_MIN_LOG_LEVEL=3; python3 your-code.py` in your shell. – wkl Aug 14 '22 at 16:17
  • what should i do exactly? – Om. Managements Aug 14 '22 at 16:29
  • @Om.Managements I already told you multiple times. You have to set an environment variable called `TF_CPP_MIN_LOG_LEVEL` and set its value to `3` if you don't want those messages. – wkl Aug 14 '22 at 16:31
  • I mean how to do that.. – Om. Managements Aug 14 '22 at 16:34
  • @Om.Managements https://stackoverflow.com/questions/5971312/how-to-set-environment-variables-in-python or https://stackoverflow.com/questions/55500857/how-to-set-the-environment-variable-in-windows - please search. – wkl Aug 14 '22 at 16:34
  • can you tell me a step-by-step process. I can't find how to do it – Om. Managements Aug 14 '22 at 16:53
2

I too am just starting to play with this, and I got the same error/warning message that you got. I looked at the documentation and tried a couple of models used in what I saw. What I tried gave significantly lower scores, so I thouht: what if I specify the default model?

`classifier = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")

The error/warning was gone and the score was the same.

Ilan Tal
  • 509
  • 2
  • 8
  • 22