Question Answering using Transformers Hugging Face Library || BERT QA Python Demo

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey data enthusiasts in this video we are going to develop a question answering model in Python using the Transformer architecture have you heard of chat GPT well it is safe to assume that because you are watching this video you have even used charge APD for some of your tasks well chat GPT is built on the Transformer architecture and in this video we are going to use the Transformer architecture to build our very own question answering model we are going to use a pre-trained model for the purpose and then build a pipeline so for that we need to install certain libraries certain packages here we need two primary installations the pi torch and Transformers from hugging phase you can also use tensorflow 2 for the purposes and it works just the same here I do not want to make a permanent installation in my environment therefore I am using this exclamation before this pip installation however you are free to make the installation in your environment as well if you do not specify this exclamation this will install it directly on your environment this exclamation is used to denote that all the installations made here are only for this particular session so let me go ahead and install this now the two libraries are installed next we need to import these libraries we are importing here currently we don't need pandas we shall import torch and we are going to import this Auto model for question answering Auto tokenizer and pipeline so we are going to see two methods of building the question answering model using the Transformer architecture let me import these libraries quickly the next thing we need is to specify a set of question and context pairs these question and context pairs are passed to your Transformer model which gives the relevant answer now here I have two pairs of question and context the first is why is conversion important based on this context the second question is how many programming languages does Bloom support remember this is an extractive question answering model which means based on the context provided the model cell find the relevant answer eight so let me run this now the important step that comes here is we are going to see a step-by-step approach as to how to build the end-to-end pipeline so the very important step here is selecting the right model for your purposes since we are using a pre-trained model let us head over to the hugging phase home page and select see the list of models this is the hugging face page and here click on the models tab now under models you see a list of actions or applications these are the multimodal applications feature extraction text to image image to text visual question answering then computer vision based applications we are currently working with text based applications which are the which comes under NLP and we shall select this question answering as we select this question answering a list of pre-trained models here we have currently 5699 pre-trained models for the purposes each of these models have been trained on a separate data set and have certain differences in terms of the configuration of the architecture say the number of parameters the number of hidden States the number of encoder layers etc etc you also see that first we have the Deep set Roberta base Square 2 the other model we have the lamba 2 model which is trained on the Chinese data similarly you have other models based on the Spanish data set so depending on the use case you can go ahead and pick any pre-trained model of the choice remember these models are like nearly 500 MBS right so do not go ahead and just download them use them cautiously and judiciously in order to make best use of your resources once you click on this you will find the description about how this model was fine-tuned on what were the some of the parameters and Hyper parameters used for the training and then whether how do we want to use this model you also have a code base as to how to implement this model in your python notebook and so on and so forth so all this information are provided here any any you pick up you will find the relevant information there so what we need to do here is we need to pick the name of this free Trend model this will automatically get the model from the hugging face hub now we create our create that means initialize our model and tokenizer every model has a corresponding tokenizer in Transformer you cannot use a model which is based on deep set with a tokenizer that is based on another Salama 2. it has to be a corresponding tokenizer the reason being tokenizers are used to denote that corresponding to the text provided how this text should be converted into the token IDs that are further passed to the model because models deal with numbers only right so you pass the text the tokenizer will convert the text to the corresponding token IDs and that token ID are passed to the model so first here we call the tokenizer function and we pass the corresponding question and the context and return the output as tenses right and the output is generated as model and we pass the input here let me run this first okay I haven't run this okay our model is noted our tokenizer is loaded since I already ran this notebook earlier so it did not download the model again it was already downloaded in the environment and we are able to use it next I will run the input and the output let me show you the corresponding output right see the output that you get is something like this you get the start logits you get the end logits the corresponding loss hidden States attentions Etc currently since we are only interested in getting the answer we haven't specified this hidden State as true these attentions is true or loss because loss is used when you are training it now these start logits denote the starting token ID for your answer and the end logic denotes the corresponding end token ID right now based on this start logit and end logic we are going to select the starting index and the ending index for our answer these starting and ending index are passed to the input IDs or the token IDs that are already converted from based on the text so we just filter out the corresponding region from the context and then decode it tokenizer.d code is used to convert these token IDs back to the original text form and we get our corresponding answer right so our question was why is conversion important and based on the context that we had the option to convert models between farm and Transformers gives freedom to the user right so we get the right answer from our context similarly for another question how many programming languages does Bloom support the answer is 13 and we see from the context that bloom has 176 billion parameters can generate text in 46 natural languages and 13 programming languages right so this is the power of Transformer based models the question answering model for Transformers and very easily you can build and deploy these models now another advantage of initializing these models yourself models and tokenizes ourselves is that we can even get the top ranked answers like instead of getting the top rank one answer we can get the top three answers right because you see we have all the possible logits for the start logits and the end logits we have all the possible combinations here so we can instead of picking the r Max first you can get the top two three answers correspondingly right now another method that we are going to see and which is a more simple and straightforward method of deploying or building applications using Transformers is using the pipeline this is similar to the scikit scikit-learn pipeline that we already know right so let us head over to the documentation and quickly show you how this works right so under the pipelines you need to specify the task which task you want to solve so there are multiple tasks that are already listed within the pipeline and you are free to choose any of the corresponding tasks depending on the application the next thing you need to specify the model name and the tokenizer name as we are doing here the model name and the tokenizer name there is one limitation with these five lines is currently not all pre-shain models have been designed to suit the pipeline architecture right they there there is certain limitation in terms of Which models which region models can currently be used by the pipeline so make sure of that that is one of the major limitations there current our current model deep set Roberta Squad 2 is compatible with the pipeline and we are able to use it easily the next is we generate the output correspondingly and we get the score start and end logits and the corresponding answer similarly for the other question we get our desired answer as 13. so now you understand how to build a question answering model using the Transformers of hugging face it is very easy and very simple to use if you want me to create a separate video on why Transformers are more powerful over its other architectures such as the RNN based lstms trus by lstns I've already written an article on it and attach it in the description let me know if you want a separate video on it comment interested below see you in the next lecture have a smooth learning experience bye bye if you like the content make sure to give it a thumbs up
Info
Channel: Datahat -- Simplified AI
Views: 3,527
Rating: undefined out of 5
Keywords: data science, machine learning, data analysis, python, transformers, hugging face, generative ai, question answering model, question answering using bert, question answering, question answering model using bert, fine tune bert for question answering pytorch, bert, how can we make question answering using bert, question answering nlp, question and answering, fine tuning bert for question answering, nlp question answering python, simple transformers question answering
Id: DNRiUGtKlVU
Channel Id: undefined
Length: 9min 56sec (596 seconds)
Published: Sat Jul 22 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.