Google Cloud Execs Highlight India’s Role in GenAI Revolution

India is emerging as a significant hub for startup innovation. And it is basically driven by the rapid adoption of generative artificial intelligence (GenAI). Google Cloud executives Chen Goldberg and Subram Natarajan lately discussed how Indian startups are leveraging it to drive value and the challenges. Google recognizes the vibrant entrepreneurial ecosystem of India and […]

Use of AI and ML in the financial services industry – MobiKwik

How is AI transforming the financial services industry, and what impact will the emergence of generative AI have on this sector? Generative AI applications are becoming a vanguard in many industries across global economies. Among industry sectors, the digital financial services industry is poised to further leverage this opportunity to enhance efficiency, improve customer experience, […]

Fintech Revolutionizing financial accessibility with Technology – Rupee112

What cutting-edge technologies does Rupee112 leverage to provide instant access to emergency cash, and how do they enhance the user experience? Rupee112 utilizes a combination of AI, machine learning, and data analytics to provide instant access to emergency cash. Our AI algorithms rapidly assess creditworthiness by analysing a vast array of data points, enabling us […]

India’s Startup Ecosystem Expands to 1.4 Lakh with Government Boost

India’s startup ecosystem has reached to more than 1.4 lakh startups, reveals Union Minister of State for Commerce and Industry, Jitin Prasada. The figure highlights remarkable growth and dynamism of entrepreneurial landscape under the ‘Startup India’ initiative of the Indian government. Startup India was formed in 2016 and has been playing important role in fostering […]

Raipur Teams Up with AirChains to Decentralize Real Estate Records

Chhattisgarh capital Raipur has partnered with AirChains to bring real estate records onto the blockchain. The effort is set to transform how real estate transactions are managed. It ensures security and authenticity. Hence, it is believed there would be no more forgery of documents and bureaucratic delays. The Raipur Municipal Corporation processes more than 8,000 […]

The Future of Electronic Component Sourcing: Trends to Watch

In 2023, the electronic components market surged to a robust $363.93 billion, underlining its critical influence across global industries. This vibrant sector is consistently molded by the introduction of innovative technologies and changes in supply chain management. The impact of these components is widespread, touching various industries such as consumer electronics, automotive, telecommunications, and industrial […]

Nasscom and Meta Partner to Launch Open-Source and Gen AI Startup Challenge

The National Association of Software and Service Companies (NASSCOM) has partnered with Meta to launch the ‘Open-Source Generative AI Grand Challenge.’ The initiative is mainly aimed to inspire startups and developers in utilizing open-source and generative AI technologies in the creation of solutions which are equipped with significant socio-economic impact. The Centre for Development of […]

Bengaluru to Leap into Top 10 Global Startup Ecosystems by 2035, Says IT Minister

Bengaluru is one of the hottest hubs for startups in India. The city is simultaneously gaining popularity on the global platform. It is setting sights on becoming one of the top 10 startup ecosystems in the world by 2035. The goal is highly ambitious and was lately announced by Karnataka’s IT Minister Priyank Kharge. He […]

Unlocking the Power of Hugging Face for NLP Tasks

The field of Natural Language Processing (NLP) has seen significant advancements in recent years, largely driven by the development of sophisticated models capable of understanding and generating human language. One of the key players in this revolution is Hugging Face, an open-source AI company that provides state-of-the-art models for a wide range of NLP tasks. Hugging Face’s Transformers library has become the go-to resource for developers and researchers looking to implement powerful NLP solutions.

Inbound-leads-automatically-with-ai. These models are trained on vast amounts of data and fine-tuned to achieve exceptional performance on specific tasks. The platform also provides tools and resources to help users fine-tune these models on their own datasets, making it highly versatile and user-friendly.

In this blog, we’ll delve into how to use the Hugging Face library to perform several NLP tasks. We’ll explore how to set up the environment, and then walk through examples of sentiment analysis, zero-shot classification, text generation, summarization, and translation. By the end of this blog, you’ll have a solid understanding of how to leverage Hugging Face models to tackle various NLP challenges.

Setting Up the Environment

First, we need to install the Hugging Face Transformers library, which provides access to a wide range of pre-trained models. You can install it using the following command:

!pip install transformers

This library simplifies the process of working with advanced NLP models, allowing you to focus on building your application rather than dealing with the complexities of model training and optimization.

Task 1: Sentiment Analysis

Sentiment analysis determines the emotional tone behind a body of text, identifying it as positive, negative, or neutral. Here’s how it’s done using Hugging Face:

from transformers import pipeline
classifier = pipeline("sentiment-analysis", token = access_token, model='distilbert-base-uncased-finetuned-sst-2-english')
classifier("This is by far the best product I have ever used; it exceeded all my expectations.")

In this example, we use the sentiment-analysis pipeline to classify the sentiments of sentences, determining whether they are positive or negative.

Classifying one single sentence
Classifying multiple sentences

Task 2: Zero-Shot Classification

Zero-shot classification allows the model to classify text into categories without any prior training on those specific categories. Here’s an example:

classifier = pipeline("zero-shot-classification")
classifier(
"Photosynthesis is the process by which green plants use sunlight to synthesize nutrients from carbon dioxide and water.",
candidate_labels=["education", "science", "business"],
)

The zero-shot-classification pipeline classifies the given text into one of the provided labels. In this case, it correctly identifies the text as being related to “science”.

Zero-Shot Classification

Task 3: Text Generation

In this task, we explore text generation using a pre-trained model. The code snippet below demonstrates how to generate text using the GPT-2 model:

generator = pipeline("text-generation", model="distilgpt2")
generator(
"Just finished an amazing book",
max_length=40, num_return_sequences=2,
)

Here, we use the pipeline function to create a text generation pipeline with the distilgpt2 model. We provide a prompt (“Just finished an amazing book”) and specify the maximum length of the generated text. The result is a continuation of the provided prompt.

Text generation model

Task 4: Text Summarization

Next, we use Hugging Face to summarize a long text. The following code shows how to summarize a piece of text using the BART model:

summarizer = pipeline("summarization")
text = """
San Francisco, officially the City and County of San Francisco, is a commercial and cultural center in the northern region of the U.S. state of California. San Francisco is the fourth most populous city in California and the 17th most populous in the United States, with 808,437 residents as of 2022.
"""
summary = summarizer(text, max_length=50, min_length=25, do_sample=False)
print(summary)

The summarization pipeline is used here, and we pass a lengthy piece of text about San Francisco. The model returns a concise summary of the input text.

Text Summarization

Task 5: Translation

In the final task, we demonstrate how to translate text from one language to another. The code snippet below shows how to translate French text to English using the Helsinki-NLP model:

translator = pipeline("translation", model="Helsinki-NLP/opus-mt-fr-en")
translation = translator("L'engagement de l'entreprise envers l'innovation et l'excellence est véritablement inspirant.")
print(translation)

Here, we use the translation pipeline with the Helsinki-NLP/opus-mt-fr-en model. The French input text is translated into English, showcasing the model’s ability to understand and translate between languages.

Text Translation — French to English Language

Conclusion

The Hugging Face library offers powerful tools for a variety of NLP tasks. By using simple pipelines, we can perform sentiment analysis, zero-shot classification, text generation, summarization, and translation with just a few lines of code. This notebook serves as an excellent starting point for exploring the capabilities of Hugging Face models in NLP projects.

Feel free to experiment with different models and tasks to see the full potential of Hugging Face in action!

This brings us to the end of this article. I hope you have understood everything clearly. Make sure you practice as much as possible.

If you wish to check out more resources related to Data Science, Machine Learning, and Deep Learning, you can refer to my GitHub account.

You can connect with me on LinkedIn — Ravjot Singh.

P.S. Claps and follows are highly appreciated.


Unlocking the Power of Hugging Face for NLP Tasks was originally published in Becoming Human: Artificial Intelligence Magazine on Medium, where people are continuing the conversation by highlighting and responding to this story.

Small Business, Big Opportunities: Conquering the Online Realm

Doing business in the online space means engaging in specific entrepreneurial activities on the internet, aimed at offering clients products, services, or other solutions. To ensure a startup quickly gains initial traction, it is essential to start by creating a web resource and registering on popular social media platforms. These are the minimal steps for […]