How to use the BLOOM Model using python

Introduction

In the previous blog, we discussed the Bloom model and how Bloom has been trained. In this blog, we will discuss how we can use Bloom Model in python. 46 natural languages, 13 programming languages, and 1.6 terabytes of pre-processed text that were turned into 350 billion distinct tokens were used to train Bloom. Some people believe that bloom marks a turning point in artificial intelligence and is the most important model to emerge in the last 10 years.

 

 

Libraries Required

  1. Transformers
  2. Torch
  3. Numpy

 

 

Implementation of BLOOM

Step – 1: Importing Libraries

Firstly, we need to install the transformers Library as BLOOM Model is available in it.  We can install it using ! pip install transformers command in python.

 

# importing the required libraries for BLOOM Model

from transformers import AutoModelForCausalLM, AutoTokenizer, set_seed

#  to create a tensor on GPU as default

import torch
torch.set_default_tensor_type(torch.cuda.FloatTensor)

 

 

Step-2: Building the model

we use the prebuilt model of bloom from the Transformers library. It may take time for downloading as it takes around 3.5 GB of data.

 

# to create a model we use AutoModelForCausalLM function which is imported from transformers library

model = AutoModelForCausalLM.from_pretrained("bigscience/bloom-7b1", use_cache=True)

# to create tokenizer we use AutoTokenizer.from_pretrained function which is imported from transformers library

tokenizer = AutoTokenizer.from_pretrained("bigscience/bloom-7b1")

 

 

Alternatively, we can use other available models.

BLOOM is available in the following versions:

 

If we needed the name of the model, we can use model.__class__.__name__ function. So we have now built our Bloom model using Transformers Library.  Now Let us test our model.

 

 

Step – 3: Testing the model

Now, We test the model which we have built.

 

prompt = 'Write code for finding the prime number in python ?'

# it pt set, it will return tensors instead of list of python integers and tokenize the prompts
input_ids = tokenizer(prompt, return_tensors="pt").to(0)

# it will get the result and we can provide the number of max words as parameter
# we can reduce the plagarism with setting temperature value closer to 1

sample = model.generate(**input_ids, max_length=100, top_k=0, temperature=0.7)

# print the result
print(tokenizer.decode(sample[0]))

The output of the code is :

 

 

If the Input is  prompt = ' Write a blog about Artificial Intelligence in 100 words ?'

 

The output is :

 

In recent years, artificial intelligence (AI) has become one of the hottest topics in both the academic and business worlds. AI is no longer the stuff of science fiction; it is increasingly becoming a reality in our everyday lives. With the rapid expansion of AI capabilities, businesses are beginning to explore how they can use AI to improve their operations and better serve their customers.

AI can be used for a variety of tasks, including data analysis, pattern recognition, and prediction. By harnessing the power of AI, businesses can gain a competitive advantage by automating tasks that are currently being done manually.

 

 

Also read – Manual for GPT-3 Model

 

Share this post

2 thoughts on “How to use the BLOOM Model using python

Leave a Reply

Your email address will not be published. Required fields are marked *