Hayo Launches AI-Powered IoT Platform to Enhance Operations in Developing Markets
Effective Strategies to Safeguard Yourself and Loved Ones from AI Scam Calls
Why does AI hallucinate?

On April 2, the World Health Organization launched a chatbot named SARAH to raise health awareness about things like how to eat well, quit smoking, and more.
But like any other chatbot, SARAH started giving incorrect answers. Leading to a lot of internet trolls and finally, the usual disclaimer: The answers from the chatbot might not be accurate. This tendency to make things up, known as hallucination, is one of the biggest obstacles chatbots face. Why does this happen? And why can’t we fix it?
Let’s explore why large language models hallucinate by looking at how they work. First, making stuff up is exactly what LLMs are designed to do. The chatbot draws responses from the large language model without looking up information in a database or using a search engine.
A large language model contains billions and billions of numbers. It uses these numbers to calculate its responses from scratch, producing new sequences of words on the fly. A large language model is more like a vector than an encyclopedia.
https://medium.com/media/260ffdc0d69d4e1186a4969c41c75c63/href
Large language models generate text by predicting the next word in the sequence. Then the new sequence is fed back into the model, which will guess the next word. This cycle then goes on. Generating almost any kind of text possible. LLMs just love dreaming.
The model captures the statistical likelihood of a word being predicted with certain words. The likelihood is set when a model is trained, where the values in the model are adjusted over and over again until they meet the linguistic patterns of the training data. Once trained, the model calculates the score for each word in the vocabulary, calculating its likelihood to come next.
So basically, all these hyped-up large language models do is hallucinate. But we only notice when it’s wrong. And the problem is that you won’t notice it because these models are so good at what they do. And that makes trusting them hard.
Can we control what these large language models generate? Even though these models are too complicated to be tinkered with, few believe that training them on even more data will reduce the error rate.
You can also ensure performance by breaking responses step-by-step. This method, known as chain-of-thought prompting, can help the model feel confident about the outputs they produce, preventing them from going out of control.
But this does not guarantee 100 percent accuracy. As long as the models are probabilistic, there is a chance that they will produce the wrong output. It is similar to rolling a dice even if you tamper with it to produce a result, there is a small chance it will produce something else.
Another thing is that people believe these models and let their guard down. And these errors go unnoticed. Perhaps, the best fix for hallucinations is to manage the expectations we have of these chatbots and cross-verify the facts.
Why does AI hallucinate? was originally published in Becoming Human: Artificial Intelligence Magazine on Medium, where people are continuing the conversation by highlighting and responding to this story.
How AI and Cloud Technology are Revolutionizing the 2024 Olympics
The Role of AI and IoT in Modern Business
Google Cloud Execs Highlight India’s Role in GenAI Revolution
Nasscom and Meta Partner to Launch Open-Source and Gen AI Startup Challenge
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.


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”.

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.

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.

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.

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.