Hugging Face Pipeline Abstraction (original) (raw)

Last Updated : 11 May, 2026

Pipeline abstraction in Hugging Face is an API that hides the complexities of model inference, allowing us to quick use of pretrained models with minimal setup.

Using Pipeline in Hugging Face

Using a Hugging Face pipeline is simple and straightforward. Let's walk through the steps to get started

1. Installing the Required Library

First, we need to install the Transformers and torch library. Run the following command in command prompt

pip install transformers

pip install torch

2. Importing the Pipeline Function

Next, we import the pipeline function which we’ll use to load and run models

Python `

from transformers import pipeline

`

3. Calling the pipeline() Function with the Task

task_pipeline = pipeline('sentiment-analysis')

`

4. Providing the Text for Analysis

We can now provide the text we want to process. For example, to analyze sentiment

Python `

result = task_pipeline("I love using Hugging Face!")

`

5. Displaying the Result

Finally, we can print the result

Python `

print(result)

`

Available Pipeline Tasks

Hugging Face supports several popular NLP tasks through its pipeline API. Below are some of the most commonly used tasks

1. Text Classification (Sentiment Analysis)

Detects the sentiment of a given text like positive, negative, neutral, etc.

**Example:

Python `

from transformers import pipeline classifier = pipeline('sentiment-analysis') result = classifier("I recently started reading a great book on data science.") print(result)

`

**Output:

sentimental

Sentiment Analysis

2. Named Entity Recognition (NER)

Identifies named entities in the text such as names of people, organizations, locations, dates, etc.

**Example:

Python `

from transformers import pipeline ner = pipeline('ner') result = ner("Statue of Liberty is located in New York.") print(result)

`

**Output:

ner

Named Entity Recognition (NER)

3. Text Generation

Text generation creates new content based on a given input and is widely used for tasks like storytelling, chatbots and content writing.

**Example:

Python `

from transformers import pipeline generator = pipeline('text-generation', model='gpt2') result = generator("Once upon a time", max_length=50) print(result)

`

**Output:

text_generation

Text Generation

4. Question Answering

Extracts answers to questions based on a given context.

**Example:

Python `

from transformers import pipeline question_answerer = pipeline('question-answering') context = "Eiffel tower is located in Paris." result = question_answerer(question="Where is eiffel tower located?", context=context) print(result)

`

**Output:

question

Question Answering

5. Translation

Translates text from one language to another. Here in task we mentioned convert english text to french.

**Example:

Python `

from transformers import pipeline translator = pipeline('translation_en_to_fr', model='Helsinki-NLP/opus-mt-en-fr') result = translator("Hello, how are you?") print(result)

`

**Output:

translation

Translation

6. Summarization

Summarizes long pieces of text into shorter versions while retaining the essential information.

**Example:

Python `

from transformers import pipeline summarizer = pipeline('summarization', model='facebook/bart-large-cnn') result = summarizer("The weather today is quite pleasant with a gentle breeze and clear skies. The temperature is comfortably mild, hovering around 22°C, making it a perfect day to spend time outdoors. The sun is shining brightly, but the cool wind provides a refreshing break from the warmth. It's a great day for a walk in the park or enjoying a coffee on the patio. As the day progresses, the skies are expected to remain clear, and temperatures are likely to stay moderate throughout the afternoon.", max_length=25) print(result)

`

**Output:

summerizaton

Summarization

7. Custom Pipelines

Custom pipelines allow you to use your own models and configurations instead of relying on default setups. This provides greater flexibility and control over how inference is performed.

**Example:

Python `

from transformers import pipeline, BertTokenizer, BertForSequenceClassification

tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')

model = BertForSequenceClassification.from_pretrained('bert-base-uncased')

custom_pipeline = pipeline('text-classification', model=model, tokenizer=tokenizer)

result = custom_pipeline("This is a custom classification model.") print(result)

`

**Output:

custom

Custom Pipelines

Advantages

Limitations