Hugging face: New way to handle NLP problems
By: Ashish Solanki
Tags: NLP, Sentiment Analysis, Questions Answers, Hugging Face, Summarization

Hugging Face is a company which develops social AI-run chatbot applications. It was established in 2016 by Clement Delangue and Julien Chaumond. The company is based in Brooklyn, New York, United States. The company’s main products are their chatbot applications, which allows users to interact with the artificial intelligence that the company developed. To accomplish this, Hugging Face developed its own natural language processing (NLP) model called Hierarchical Multi-Task Learning (HMTL) and managed a library of pre-trained NPL models under PyTorch-Transformers. Their chatbot applications are, as of September 2019, only available on iOS. These applications are Chatty, Talking Dog, Talking Egg, and Boloss.

Hugging face transformers consist of wide varieties of pretrained model which helps makes it more easy to use. There is a library called transformer from which help you to hands-on practice on this beautiful library.

Table of Contents

1. What Is transformer

2. Installing transformer

3. Some NLP implementation

1. What are transformers?

Transformers is a python library which is used majorly for Natural Language Processing tasks such as classification, information extraction, question answering, summarization, translation, text generation, etc by using thousands of pretrained models. Transformers provides APIs to quickly download and use those pretrained models

2. Installing Transformers

Conda — -> conda install -c huggingface transformers

jupyter / colab — -> ! pip install transformers

With pip — -> pip install transformers

3. Some NLP tasks

a. Question answer

b. Fill-mask

c. Sentiment Analysis

d. Summarization

e. Translation

# importing the libraries

In[ ]:

from transformers import pipeline # contains all the pretrained models

import warnings # used for ignoring the warnings

warnings.filterwarnings(‘ignore’)

# Assuming the text

this is unseen passage which comes in kids exam {you can find here — https://www.learninsta.com/unseen-passage-for-class-4/}

In[ ]: text = ‘’’ Amit asked his mom if he could ride his bike to the park. “As long as you wear your helmet when you are riding”, she said.When Amit got to the park it was filled with people holding garbage bags. A lot of his friends were there and so were some of his teachers. His friend Babita said “Everyone is pitching in and cleaning the park today.” She asked Amit if he would help. Amit said, “Sure,” and asked for a garbage bag. There was garbage everywhere. Amit picked up candy wrappers, juice pouches, newspapers and apple cores. It was really gross. After an hour he had filled three garbage bags. His friends filled a lot of garbage bags too. When everyone was finished, one of the teachers loaded the garbage bags into his pick -up truck to take them to the dump. The park looked great. They put signs around the park that read, “Please place garbage in garbage cans and help to keep the park clean.” The teachers bought pizza and lemonade for everyone who helped clean the park. After they ate, Amit and his friends made sure to put their garbage into the garbage cans. ‘’’ In[ ]: text = text[1:-1] In[ ]: text Out[ ]: ‘Amit asked his mom if he could ride his bike to the park. “As long as you wear your helmet when you are riding”, she said.When Amit got to the park it was filled with people holding garbage bags. A lot of his friends were there and so were some of his teachers. His friend Babita said “Everyone is pitching in and cleaning the park today.” She asked Amit if he would help. Amit said, “Sure,” and asked for a garbage bag. There was garbage everywhere. Amit picked up candy wrappers, juice pouches, newspapers and apple cores. It was really gross. After an hour he had filled three garbage bags. His friends filled a lot of garbage bags too. When everyone was finished, one of the teachers loaded the garbage bags into his pick -up truck to take them to the dump. The park looked great. They put signs around the park that read, “Please place garbage in garbage cans and help to keep the park clean.”

The teachers bought pizza and lemonade for everyone who helped clean the park. After they ate, Amit and his friends made sure to put their garbage into the garbage cans.’ while applying you will went through come terms which are common: score — 0.98414 which means it is 98.414% start — give the starting position of the string end — give the end position of the string

# question answer

In[17]: ques_ans = pipeline(‘question-answering’) In[ ]: print(‘What were people holding when Amit got to the park?’) ans1 = ques_ans(question=”What were people holding when Amit got to the park?”, context=text) ans1 Out[ ]: What were people holding when Amit got to the park? {‘score’: 0.984142541885376, ‘start’: 182, ‘end’: 194, ‘answer’: ‘garbage bags’} In[ ]: print(‘What were Amit’s friends and teachers doing at the park?’) ans2 = ques_ans(question=”What were Amit’s friends and teachers doing at the park?”, context=text) ans2 Out[ ]: What were Amit’s friends and teachers doing at the park? {‘score’: 0.2516492009162903, ‘start’: 688, ‘end’: 735, ‘answer’: ‘loaded the garbage bags into his pick -up truck’} In[ ]: print(‘Where was Amit’s teacher taking the garbage bags?’) ans3 = ques_ans(question=”Where was Amit’s teacher taking the garbage bags?”, context=text) ans3 Out[ ]: Where was Amit’s teacher taking the garbage bags? {‘score’: 0.48300957679748535, ‘start’: 717, ‘end’: 735, ‘answer’: ‘his pick -up truck’}

# fill-mask

In[ ]: fill_blank = pipeline(‘fill-mask’) In[ ]: print(‘ans = When Amit got to the park it was filled with _people__ holding garbage bags.’) fill_blank(f’When Amit got to the park it was filled with {fill_blank.tokenizer.mask_token} holding garbage bags’,top_k=2) Out[ ]: ans = When Amit got to the park it was filled with _people__ holding garbage bags. [{‘sequence’: ‘When Amit got to the park it was filled with people holding garbage bags’, ‘score’: 0.1107989028096199, ‘token’: 82, ‘token_str’: ‘ people’}, {‘sequence’: ‘When Amit got to the park it was filled with trucks holding garbage bags’, ‘score’: 0.05789736285805702, ‘token’: 5875, ‘token_str’: ‘ trucks’}] In[ ]: print(‘ans — Amit picked up __candy__ wrappers, juice pouches, newspapers and apple cores’) fill_blank(f’Amit picked up {fill_blank.tokenizer.mask_token} warppers, juice pouches, newspapers and apple core.’,top_k=4) Out[ ]: ans — Amit picked up __candy__ wrappers, juice pouches, newspapers and apple cores [{‘sequence’: ‘Amit picked up potato warppers, juice pouches, newspapers and apple core.’, ‘score’: 0.04569796472787857, ‘token’: 17284, ‘token_str’: ‘ potato’}, {‘sequence’: ‘Amit picked up apple warppers, juice pouches, newspapers and apple core.’, ‘score’: 0.03132599592208862, ‘token’: 15162, ‘token_str’: ‘ apple’}, {‘sequence’: ‘Amit picked up fruit warppers, juice pouches, newspapers and apple core.’, ‘score’: 0.031315091997385025, ‘token’: 6231, ‘token_str’: ‘ fruit’}, {‘sequence’: ‘Amit picked up candy warppers, juice pouches, newspapers and apple core.’, ‘score’: 0.029933838173747063, ‘token’: 12644, ‘token_str’: ‘ candy’}]

# sentiment analysis

In[ ]: sentiment_analysis = pipeline(“sentiment-analysis”) # a = text.split(‘.’) sentiments = {} for i in a: sentiments[i] = sentiment_analysis(i) sentiments Out[ ] {‘Amit asked his mom if he could ride his bike to the park’: [{‘label’: ‘NEGATIVE’, ‘score’: 0.9508203864097595}], ‘ “As long as you wear your helmet when you are riding”, she said’: [{‘label’: ‘POSITIVE’, ‘score’: 0.8451337218284607}], ‘When Amit got to the park it was filled with people holding garbage bags’: [{‘label’: ‘NEGATIVE’, ‘score’: 0.9968301653862}], ‘ A lot of his friends were there and so were some of his teachers’: [{‘label’: ‘POSITIVE’, ‘score’: 0.9984995722770691}], ‘ His friend Babita said “Everyone is pitching in and cleaning the park today’: [{‘label’: ‘POSITIVE’, ‘score’: 0.9943248629570007}], ‘” She asked Amit if he would help’: [{‘label’: ‘NEGATIVE’, ‘score’: 0.9845715165138245}], ‘ Amit said, “Sure,” and asked for a garbage bag’: [{‘label’: ‘NEGATIVE’, ‘score’: 0.9906355142593384}], ‘ There was garbage everywhere’: [{‘label’: ‘NEGATIVE’, ‘score’: 0.9995337128639221}], ‘ Amit picked up candy wrappers, juice pouches, newspapers and apple cores’: [{‘label’: ‘NEGATIVE’, ‘score’: 0.9003668427467346}], ‘ It was really gross’: [{‘label’: ‘NEGATIVE’, ‘score’: 0.9998053312301636}], ‘ After an hour he had filled three garbage bags’: [{‘label’: ‘NEGATIVE’, ‘score’: 0.9980144500732422}], ‘ His friends filled a lot of garbage bags too’: [{‘label’: ‘NEGATIVE’, ‘score’: 0.9977257251739502}], ‘ When everyone was finished, one of the teachers loaded the garbage bags into his pick -up truck to take them to the dump’: [{‘label’: ‘NEGATIVE’, ‘score’: 0.9992215633392334}], ‘ The park looked great’: [{‘label’: ‘POSITIVE’, ‘score’: 0.9998442530632019}], ‘ They put signs around the park that read, “Please place garbage in garbage cans and help to keep the park clean’: [{‘label’: ‘NEGATIVE’, ‘score’: 0.9948853254318237}], ‘”

The teachers bought pizza and lemonade for everyone who helped clean the park’: [{‘label’: ‘POSITIVE’, ‘score’: 0.6552407145500183}], ‘ After they ate, Amit and his friends made sure to put their garbage into the garbage cans’: [{‘label’: ‘NEGATIVE’, ‘score’: 0.9946525692939758}], ‘’: [{‘label’: ‘POSITIVE’, ‘score’: 0.7481210231781006}]}

# Summarization

In[ ]:

summarizer = pipeline(“summarization”)

print(summarizer(text))

Out[ ]:

[{‘summary_text’: ‘ Amit asked his mom if he could ride his bike to the park . Amit picked up candy wrappers, juice pouches, newspapers and apple cores . After an hour he had filled three garbage bags . When everyone was finished, one of the teachers loaded the garbage bags into his pick-up truck to take them to the dump .’}]

# translation

In[ ]:

translete_en_to_fr = pipeline(“translation_en_to_fr”)

Out[ ]:

[{‘translation_text’: “C’était un peu d’intro.”}]