HuggingFace Is Making NLP More Accessible



Original Source Here

Natural Language Processing

Here are a few practical examples of how 🤗 HuggingFace can be implemented within an existing chatbot development framework.

Sentiment Analysis

Classifying sequences according to positive or negative sentiments.

Input:

classifier = pipeline("sentiment-analysis")
classifier("I am not impressed with their slow and unfriendly service.")

Output:

[{'label': 'NEGATIVE', 'score': 0.9987296462059021}]

Question And Answer

Input:

q_a = pipeline("question-answering")context = "Mars is the fourth planet from the Sun and the second-smallest planet in the Solar System, being larger than only Mercury. In English, Mars carries the name of the Roman god of war and is often referred to as the Red Planet. The latter refers to the effect of the iron oxide prevalent on Mars's surface, which gives it a reddish appearance distinctive among the astronomical bodies visible to the naked eye.[18] Mars is a terrestrial planet with a thin atmosphere, with surface features reminiscent of the impact craters of the Moon and the valleys, deserts and polar ice caps of Earth."question = "Who is the Roman God of war?"q_a({"question": question, "context": context})

Output:

{'answer': 'Mars', 'end': 4, 'score': 0.4511910080909729, 'start': 0}

Text Generation

Input:

text = "Mars is the fourth planet from the Sun and the second-smallest planet in the Solar System."text_generator = pipeline("text-generation")text_generator(text)

Output:

Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.[{'generated_text': 'Mars is the fourth planet from the Sun and the second-smallest planet in the Solar System.\n\nThe Sun takes about 70 percent of the solar wind energy that travels to the Sun and about 100 percent is stored in the Sun.\n\n'}]

Named Entity Recognition

Input:

ner = pipeline("ner")text = "Johannesburg is located in South Africa in the contimnet of Africa"ner(text)

Output:

[{'end': 12,   'entity': 'I-LOC',   'index': 1,   'score': 0.9987455,   'start': 0,   'word': 'Johannesburg'},  {'end': 32,   'entity': 'I-LOC',   'index': 5,   'score': 0.99958795,   'start': 27,   'word': 'South'},  {'end': 39,   'entity': 'I-LOC',   'index': 6,   'score': 0.9996102,   'start': 33,   'word': 'Africa'},  {'end': 66,   'entity': 'I-LOC',   'index': 14,   'score': 0.6802336,   'start': 60,   'word': 'Africa'}]

Translation

English to German:

translator = pipeline("translation_en_to_de")text = "Mars is the fourth planet from the Sun and the second-smallest planet in the Solar System."translator(text)[{'translation_text': 'Mars ist der vierte Planet der Sonne und der zweitkleinste Planet im Sonnensystem.'}]

English to French:

translator = pipeline("translation_en_to_fr")text = "Mars is the fourth planet from the Sun and the second-smallest planet in the Solar System."translator(text)[{'translation_text': 'Mars est la quatrième planète du Soleil et la deuxième plus petite planète du Système solaire.'}]

AI/ML

Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot

%d bloggers like this: