{"id":176407,"date":"2024-01-16T13:52:49","date_gmt":"2024-01-16T12:52:49","guid":{"rendered":"https:\/\/liora.io\/en\/?p=176407"},"modified":"2026-02-06T08:36:38","modified_gmt":"2026-02-06T07:36:38","slug":"empowering-natural-language-processing-with-hugging-face-transformers-api","status":"publish","type":"post","link":"https:\/\/liora.io\/en\/empowering-natural-language-processing-with-hugging-face-transformers-api","title":{"rendered":"Empowering Natural Language Processing with Hugging Face Transformers API"},"content":{"rendered":"<h2>For years, AI has been attracting growing interest with the performance achieved through machine learning (<a href=\"https:\/\/liora.io\/en\/boosting-business-with-3-essential-machine-learning-algorithms\">Machine Learning<\/a>) and, more particularly, deep learning (<a href=\"https:\/\/liora.io\/en\/unraveling-machine-learning-vs-deep-learning-key-differences-explained\">Deep Learning<\/a>).<\/h2>\t\t\n\t\t<p>These technologies have played a decisive role in the evolution of<a href=\"https:\/\/liora.io\/en\/nlp-training-become-an-nlp-pro-and-master-the-art-of-natural-language-processing\"> natural language processing (NLP).<\/a> They have helped automate the most common <a href=\"https:\/\/liora.io\/en\/natural-language-processing-definition-and-principles\">NLP tasks<\/a>, such as word and sentence classification, text generation, response extraction, and speech and vision recognition.<\/p><p>Today, there are <a href=\"https:\/\/liora.io\/en\/spacy-nlps-open-source-python-library\">several open source libraries<\/a> containing pre-trained models, <strong>enabling data<\/strong> scientists and enterprises to use existing models to save computational costs and time. Hugging Face is one of them.<\/p>\t\t\n\t\t\t<h3>About Hugging Face Transformers<\/h3>\t\t\n\t\t<p><strong>Hugging Face<\/strong> transformers is a platform for advancing AI by sharing model collections and datasets, enabling users to manage, share and <a href=\"https:\/\/liora.io\/en\/xai-or-explainable-artificial-intelligence-what-is-it-about\">develop their AI models.<\/a><\/p><p>It is an open-source Python-based deep learning library that provides a wide variety of pre-trained models for <a href=\"https:\/\/liora.io\/en\/natural-language-processing-definition-and-principles\">different types of NLP<\/a> tasks. It provides an <a href=\"https:\/\/liora.io\/en\/api-the-path-to-seamless-integration\">API<\/a> for use in many pre-trained transformer architectures, such as BERT, GPT-2, DistilBERT, BART or <a href=\"https:\/\/liora.io\/en\/chatgpt-how-does-this-nlp-algorithm-work\">GPT-3,<\/a> enabling very good results to be obtained on a variety of NLP tasks such as text classification, named entity detection, text generation, etc.<\/p><p><strong>Hugging Face<\/strong>&#8216;s pre-trained transformers can be grouped into three categories:<\/p><ul><li><strong>Autoregressive transformers (GPT-type architecture):<\/strong> focused on predicting the next word in the sentence, they are suitable for generative tasks such as text generation.<\/li><li><strong>Auto-encoding transformers (BERT-type architecture)<\/strong>: more suitable for tasks requiring complete understanding of the input. Examples include sentence classification, named entity recognition and, more generally, word classification.<\/li><li><strong>Sequence-to-sequence transformers (BART\/T5 architecture):<\/strong> best suited to tasks involving the generation of new sentences based on a given input, such as text summarization, translation or question-and-answer generation.<\/li><\/ul><p>In addition to the official pre-trained models, there are hundreds of sentence-transformer models on the <strong>Hugging Face Hub.<\/strong><\/p>\t\t\n\t\t\t<h3>What&#8217;s the point of these Hugging Face Transformers? <\/h3>\t\t\n\t\t<p>The aim of <strong>Transformers<\/strong> is to facilitate the development of machine learning projects by <a href=\"https:\/\/liora.io\/en\/fastapi-everything-you-need-to-know-about-the-most-widely-used-python-web-framework-for-machine-learning\">providing APIs<\/a> and tools for downloading and training pre-trained models. Using these models can reduce computational costs, and save the time and resources needed to train a model from scratch.<\/p><p>They have rapidly become the dominant architecture for obtaining reliable results on a <a href=\"https:\/\/liora.io\/en\/natural-language-processing-definition-and-principles\">variety of NLP tasks.<\/a><\/p><p>These models support common tasks in different modalities:<\/p><ul><li><strong>Text:<\/strong> For everything from classification, information extraction and question answering, to the generation, creation and translation of text in over 100 different languages.<\/li><li><strong>Speech:<\/strong> For tasks such as audio classification of objects and speech recognition.<\/li><li><strong>Vision:<\/strong> For object detection, image classification and segmentation.<\/li><li><strong>Tabular data:<\/strong> For regression and <a href=\"https:\/\/liora.io\/en\/management-of-unbalanced-classification-problems-i\">classification problems<\/a>.<\/li><\/ul><p>Transformers also support framework <a href=\"https:\/\/liora.io\/en\/pytorch-all-about-this-framework\">interoperability between PyTorch<\/a>, TensorFlow and JAX.<\/p><p>This makes it possible to use a different framework at each stage of a model&#8217;s life: form a model in three lines of code in one <a href=\"https:\/\/liora.io\/en\/scaled-agile-framework-safe-the-complete-guide\">framework, and load it for inference in another.<\/a><\/p>\t\t\n\t\t\t\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\"><div class=\"wp-block-button \"><a class=\"wp-block-button__link wp-element-button \" href=\"https:\/\/liora.io\/en\/all-about-deep-learning\">Learn how to use Hugging Face Transformers<\/a><\/div><\/div>\n\n\t\t\t<h3>Setting up the Transformers<\/h3>\t\t\n\t\t<p>In this section, we&#8217;ll put Transformers into practice by learning about the pipeline tool, and then show how this tool facilitates the implementation of NLP and <a href=\"https:\/\/liora.io\/en\/opencv-everything-you-need-to-know-about-computer-visions-leading-tool\">computer vision tasks.<\/a><\/p>\t\t\n\t\t\t<h4>Pipelines<\/h4>\t\t\n\t\t<p>Pipelines are a simple and effective way of extracting much of the complex code from the library providing an API for performing inference on a variety of tasks, including named entity recognition, sentiment analysis, question answering and so on.<\/p><p>They are used to encapsulate the overall process of each <strong>NLP<\/strong> task such as text cleaning, tokenization, embedding, etc.<\/p><p>It&#8217;s a simple function to use: a pipeline is instantiated via the pipeline() method. Simply specify the task to be launched.<\/p><p>The following is a non-exhaustive list of the pipelines available in the <strong>Hugging Face Transformers library:<\/strong><\/p><ul><li>feature-extraction (to obtain a vector representation of text)<\/li><li>fill-mask (to predict masked words)<\/li><li>ner (named entity recognition)<\/li><li>question-answering (to answer questions)<\/li><li>sentiment-analysis (to analyze feelings)<\/li><li>summarization (to summarize text)<\/li><li>text-generation (to produce new text)<\/li><li>translation (to translate a text)<\/li><li>zero-shot-classification (to classify new examples from unpublished classes)<\/li><\/ul>\t\t\n\t\t\t<h4>Text generation<\/h4>\t\t\n\t\t<p>Nowadays, text generation has become a trend with the release and evolution of <a href=\"https:\/\/liora.io\/en\/autogpt-discover-the-new-tool-that-makes-chatgpt-autonomous\">ChatGPT<\/a>. In this context, the pipeline enables us to use advanced text generation models in a single line of code.<\/p>\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t<figure>\n\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/liora.io\/app\/uploads\/2023\/04\/image2-6.png\" title=\"\" alt=\"\" loading=\"lazy\">\t\t\t\t\t\t\t\t\t\t\t<figcaption><\/figcaption>\n\t\t\t\t\t\t\t\t\t\t<\/figure>\n\t\t<p>In the line above, we specify the use case we want (text generation), and it will automatically download the default model (in this case GPT-2). We can use other versions by specifying the &#8216;model&#8217; parameter.<\/p><p>Now let&#8217;s start generating the text by passing a phrase as a parameter.<\/p>\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t<figure>\n\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/liora.io\/app\/uploads\/2023\/04\/image4-5.png\" title=\"\" alt=\"\" loading=\"lazy\">\t\t\t\t\t\t\t\t\t\t\t<figcaption><\/figcaption>\n\t\t\t\t\t\t\t\t\t\t<\/figure>\n\t\t<p>In this example, we asked the model &#8220;Why <a href=\"https:\/\/liora.io\/en\/xai-or-explainable-artificial-intelligence-what-is-it-about\">artificial intelligence<\/a>? &#8220;.<\/p>\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/liora.io\/app\/uploads\/2023\/04\/image3-4.png\" title=\"\" alt=\"\" loading=\"lazy\">\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t<p>The model has generated a response in the &#8216;generated_text&#8217; key, and we can generate another response by executing the code one more time. This example shows how Transformers can help us implement a chatbot in a very short time.<\/p>\t\t\n\t\t\t<h4>Image classification<\/h4>\t\t\n\t\t<p>Image classification is a <strong>Machine Learning<\/strong> problem. With the Transformers API, it&#8217;s easy to acquire the model for this problem.<\/p>\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t<figure>\n\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/liora.io\/app\/uploads\/2023\/04\/image6-4.png\" title=\"\" alt=\"\" loading=\"lazy\">\t\t\t\t\t\t\t\t\t\t\t<figcaption><\/figcaption>\n\t\t\t\t\t\t\t\t\t\t<\/figure>\n\t\t<p>In one line, we acquired the Image Classifier template. We could easily pass an image file from any source to identify the image with this template.<\/p><p>For example, we&#8217;ll pass the image link below and let the template classify it.<\/p>\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t<figure>\n\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/liora.io\/app\/uploads\/2023\/04\/image5-3.png\" title=\"\" alt=\"\" loading=\"lazy\">\t\t\t\t\t\t\t\t\t\t\t<figcaption><\/figcaption>\n\t\t\t\t\t\t\t\t\t\t<\/figure>\n\t\t<p>The Pipeline helps us do this in a single line of code. The model proposes five classifications, each associated with a probability. The first has the highest score, so we can take &#8220;sports car&#8221; as our main prediction, and it&#8217;s indeed right.<\/p>\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t<figure>\n\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/liora.io\/app\/uploads\/2023\/04\/image1-4.png\" title=\"\" alt=\"\" loading=\"lazy\">\t\t\t\t\t\t\t\t\t\t\t<figcaption><\/figcaption>\n\t\t\t\t\t\t\t\t\t\t<\/figure>\n\t\t<p>This example proves that we can implement a sophisticated model in a short space of time and without a great deal of AI knowledge.<\/p>\t\t\n\t\t\t<h4>Model specification in a pipeline<\/h4>\t\t\n\t\t<p>In the previous examples we have used the default model for the task in question, but it is also possible to choose a particular Hub model and use it in a pipeline for a specific task.<\/p>\t\t\n\t\t\t<h3>Conclusion<\/h3>\t\t\n\t\t<p>Today, Hugging Face Transformers is the most widely adopted software library by beginners and professionals alike for machine learning models to handle <strong>NLP tasks.<\/strong> It provides the user with a pre-trained model that was previously inaccessible due to technical requirements.<\/p>\t\t\n\t\t\t\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\"><div class=\"wp-block-button \"><a class=\"wp-block-button__link wp-element-button \" href=\"\/en\/courses\/data-ai\/deep-learning\">Starting a Deep Learning course<\/a><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>For years, AI has been attracting growing interest with the performance achieved through machine learning (Machine Learning) and, more particularly, deep learning (Deep Learning). These technologies have played a decisive role in the evolution of natural language processing (NLP). They have helped automate the most common NLP tasks, such as word and sentence classification, text [&hellip;]<\/p>\n","protected":false},"author":76,"featured_media":176412,"comment_status":"open","ping_status":"open","sticky":false,"template":"elementor_theme","format":"standard","meta":{"_acf_changed":false,"editor_notices":[],"footnotes":""},"categories":[2433],"class_list":["post-176407","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-ai"],"acf":[],"_links":{"self":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/176407","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/users\/76"}],"replies":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/comments?post=176407"}],"version-history":[{"count":1,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/176407\/revisions"}],"predecessor-version":[{"id":206120,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/176407\/revisions\/206120"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media\/176412"}],"wp:attachment":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media?parent=176407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/categories?post=176407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}