4

How to download hugging face sentiment-analysis pipeline to use it offline? I'm unable to use hugging face sentiment analysis pipeline without internet. How to download that pipeline?

The basic code for sentiment analysis using hugging face is

from transformers import pipeline
classifier = pipeline('sentiment-analysis') #This code will download the pipeline
classifier('We are very happy to show you the  Transformers library.')

And the output is

[{'label': 'POSITIVE', 'score': 0.9997795224189758}]
Nithin Reddy
  • 580
  • 2
  • 8
  • 18

1 Answers1

3

Use the save_pretrained() method to save the configs, model weights and vocabulary:

classifier.save_pretrained('/some/directory')  

and load it by specifying the tokenizer and model parameter:

from transformers import pipeline

c2 = pipeline(task = 'sentiment-analysis', model='/some/directory', tokenizer='/some/directory')
cronoik
  • 15,434
  • 3
  • 40
  • 78
  • 1
    Also found that using directly `pipeline(task='sentiment-analysis', model='/some/directory', tokenizer='/some/directory')` works. Though it is unclear that this is possible by reading the doc. – ygorg Apr 01 '21 at 15:33
  • Okay, If we consider the single pipeline code just from transformers import pipeline is enough right? – Nithin Reddy Apr 01 '21 at 17:22
  • It's saying cannot import 'AutoModelForSequenceClassification' from 'transformers'. I don't know why – Nithin Reddy Apr 01 '21 at 18:01
  • The code is not working, it's throwing an error like OSError: Can't load config for '\Huggingface-Sentiment-Pipeline'. Make sure that: - '\Huggingface-Sentiment-Pipeline' is a correct model identifier listed on 'https://huggingface.co/models' - or '\Huggingface-Sentiment-Pipeline' is the correct path to a directory containing a config.json file – Nithin Reddy Apr 02 '21 at 11:38
  • The code is working fine. You are probably want to use `Huggingface-Sentiment-Pipeline` (in case you have your python interpreter running in the same directory as `Huggingface-Sentiment-Pipeline`) without a backslash or even better the absolute path. @NithinReddy – cronoik Apr 02 '21 at 13:16
  • yea, it's working fine. We have to load the model with AutoModelForSequenceClassification and AutoTokenizer. – Nithin Reddy Apr 02 '21 at 17:55
  • You do not have to do that as shown above. The problem was your strange path string with a leading backslash @NithinReddy – cronoik Apr 02 '21 at 18:07
  • Yea, backslash is the issue. – Nithin Reddy Apr 03 '21 at 06:42