Hi everyone welcome back.
So in this video we're going to be completing a second part of the API and that is to build the detection
right to detect the similarity of two documents or two strings that are basically posted to us.
OK.
So let's get started.
So the first thing is you want to write a classical detector right so we're going to write class.
So we're going to give your class detect.
OK.
And then we're going to inherit from resource right.
Because this is a resource for the API.
Okay great.
So far so good.
Now what do we need.
Well we know we know that it should be a post right so we're going to be defining a post here.
So let's go back.
So we're going to define post and then we self.
And that again there's some steps which we always do.
So the first thing with a post is we get the posted data is posted.
Data is equal to request dot get miscall Jaison on.
OK.
So now we get there the posted data then we need to get a couple of things we need to get the user name
of the user we need to get the password we need to get the text one that he sent the string and the
text to that he said.
OK so we need to get four things we need.
Username.
OK.
So let's not capitalized that user name is equal to and then posted to and then it's at usually right.
So the user posts it with the name usually.
Second thing is password to password is equal to posted.
And then inside password gate the third thing is we need text 1 and then we can say that the user sends
it at Text 1.
Right.
There is no problem.
You're the one who decides where what field should be filled to the user when you explain the API to
them.
And the second thing is text 2 is equal to as you might have guessed text to get.
So we get the text to field.
OK so we have the four things.
Now there is a lot of checks that we have to do.
We said first of all we have to check for invalid user names incorrect password or out of tokens.
So we have three checks that we have to do before we service the users request.
OK.
So the first thing is we're going to check if not user exist.
So if you remember the previous video we implemented the function user exists.
So I'm saying if the user does not exist with this username then I'm going to have a problem with them
are going to say hey sorry you don't exist in my system I can't help you I'm going to say status and
then return the invalid code which is 3 0 1 for invalid username and then I'm going to write a message
and then just invalid using.
OK.
And then return Jason if I see your return Jaser OK.
That's the first.
First check for three or one.
What about three or two.
Well we want to verify that his password is correct.
So we're going to say correct password is equal to verify password.
So we're going to write a function called verify password and then verify function.
We need the user name and the password right.
And we're going to check if the user if if so the correct password is basically going to be a boolean
which is true if this user name matches this password and false otherwise.
OK.
So we're going to verify the password using a function that we're going to write later on so we're going
to write some function here.
So define verify so verify desperate with a user name and password.
OK so I'm going to pass for now just for now please pass OK.
And that less complete writing is here before we go back to the password right.
So correct password is to verify and then I'm going to say if not correct password.
So if this password is not correct then what do I do.
Well I went to doing three or two.
Sorry your.
You have an incorrect password.
So if not correct password then my return Jason is going to be to have a status of three or two.
Sorry there's a mistake.
And the message is invalid password.
OK.
All right.
So the third thing we're going to do is to verify that the user has enough tokens.
OK.
So for that we're going to write another function.
So number two is equal to count tokens.
And then I'm going to write for this using.
So I'm going to say hey count me how many times this year how many tokens This user has.
So I'm going to implement a function so I can even copy the whole thing.
Your above above does detect functions I'm going to write.
Define count tokens.
And then I'm going to pass for now and then I'm going to count how many how many tokens does this user.
OK.
All right.
So we have the we've counted the number of tokens and then I'm going to check.
OK.
If the number of tokens is bigger than or equal to or less than or equal to zero excuse me.
So if this number is less than or equal to zero then there's a problem that he needs to refill.
So the returns he said is going to be have a status off this time is going to be 3 0 3 right.
Free go back it so three or three out of tokens he's going to have three or three and then I'm going
I have a message which tells the user sorry you're out of tokens please refills.
Message is you're out of tokens.
Please refill it and then we're going to return again signifie the message.
OK.
So return Grace on my return geez.
OK.
All right so if we have the correct username and password and we have enough.
OK.
So now we can finally service these.
Right now we can finally calculate the and so calculate calculate the editors.
Now here's where we need space.
OK.
So we're going to go back up above and import spacier first.
I'm going to go import spacier Okay.
Now I need Spacey and I'm going to go down.
And then how are we going to use space.
So space is a very very very simple to use.
So first of all you're right a.p natural language processing is equal to speccy dock load.
And then you're going to write the name off your model which is an underscore web underscore and chikor
core underscore webpage for us.
So I'm going to write and underscore core underscore web underscore asset.
OK.
So now I've loaded this model and I'm ready to predict with spacing using this model how to predict
whether to dock the similarity between two dog.
So how do we do as we write text 1 is equal to a.p text 1.
Because if you remember it takes one here is posted data right so I'm going to change that one from
a string to an LP.
Right.
And natural language processing model sentence using spaces.
I mean to write text one is equal to an LP of x 1 and text two is equal to a.p of text 2.
Ok so now I have two instances of text 1 and text to which which are basically and Opis.
Now I want to find the ratio of similarity between them.
So the ratio is a number.
So let me just write this year maybe as a comment.
So ratio is a number between 0 and 1 that closer to 1.
The more similar attacks one and text 2 are.
So the ratio is going to give me a number between this ratio function that I'm going to show or the
similarity function is going to give me a ratio and this ratio tells me how much how similar are these
two deaths or some are close to each other.
So if it gives me a zero then these two strings are very very different than each other.
But if we give that if it gives you a 1 then that means the strings are very very very similar even
if it's one it means they're exactly the same.
Right.
So the closer to 1 the more similar the text text text to our gate.
Hopefully this makes sense.
Next one next one in text.
Ok so I want to say re-issue is equal to and then I'm going to write text one dot similarity.
So similarity and then text do and that's it.
It's as simple as that.
You're right.
Text one similarity takes two and that's it.
And then you write returned Jason.
So now I want to report this result to my reserved.
So I'm going to write.
Return.
Status is 200 success and then the ratio is ratio and then or you can even change that to similarity
right so you might want to change that to similarity and then message.
So what message I'm gonna tell the user is similarity.
Let's say a score calculated successful.
And that's it.
That's it for the weekend.
And then before we return.
Right.
Don't forget that we have to take one token away from the user because he's used our service so we need
to subtract one token from him.
So I'm not to say the current tokens of credit tokens is equal to count tokens.
If you remember we wrote a function of we're going to write a function called Count tokens.
So we're going to count how many tokens this user has.
OK.
And then if and then there there's no right.
We just updated tokens right away.
And when I say users don't update.
OK.
And then I'm going to update the user where the user name user name as the one posted.
And what I'm going to updated with is let's go back here.
So what I'm going to update it with is I'm going to have a set dollar set.
If you remember this is how we update the the elements and that inside of it.
I'm going to set the tokens to be current tokens minus one.
OK.
And then that's it.
So we close here and then close the curly brackets here and that's it.
So I'm updating the user with this user name and I'm selling his tokens so this should be tokens here
to create tokens minus one.
OK.
And then the final thing is we're going to return.
Jason if I was sir Jaison if I return Jesus I'm going to return to him.
What the the your attorney Jason that we did.
OK.
And that should be it.
So the thing we have to do now is to write the two functions to help or functions or verify a password.
And the second function which is the count tokens function that counts how many tokens a user has.
OK so the first one to verify passwords shouldn't be too.
OK.
So I'm going to say I'm going to use the existing function user exists first.
So let's say first of all if not user exist username.
So if this user doesn't exist their return false right.
This username password combination doesn't it is incorrect.
OK.
But the user exists then I'm going to check if his hashed password is correct or if the password he
sent me after I hash it is the same as the hash password that I've seen.
So I'm going to say hashed password is equal to users that find OK then I've got to find the user with
the username username.
OK.
And then I'm going to access the first user because there's only one user like this so I'm going to
access the zeroth user first user and I'm going to access his password.
OK.
So my access the user's password and then I'm going to say if we crypt.
So if the script that hash password and then password encodes on the hash the password that the user
sent me but encoding it with UTF 8.
So if the past the hash password he sent me with the hashed password is equal to the hashed password
then I'm going to return true right.
Then this user is indeed who he claims to be.
He has the user name and the password match.
Else I'm going to return false.
I'm going to say sorry you're not the right user.
You're not the same user who signed up.
This is an incorrect guess.
OK.
So this is for the very 2:05 So we first check that the user exists.
If he doesn't return false.
Else we check his hashed password and then we have just passed through and he sent us and we check if
it matches the hash password.
If it if it doesn't return true elsewhere returns.
So hopefully this made sense.
The second one is a function to count the number of students.
So we have tokens is equal to users that find and then we want to find the user with a username that
is given to us.
And we also want to see how many tokens you have.
So we want to get the first user or the only user.
And then the element tokens get all right and then we you just return the tokens.
So this is just the number of tokens inside for this user for this username and then we return the number
of tokens.
OK.
So we save that and that should be it.
OK.
So we've written the varify password and count tokens.
And so the detection is done.
And basically all of the logic that is you know our API is actually in this area over here.
So we're using this free train model to train to predict the similarity of two strings that are given
to us.
OK.
So hopefully this makes sense.
And we're going to stop here and in the next video we're going to do the last part which is the refill
feature and we're also going to try to test the API after we're done.
See how everything's going.
And so.
Okay.
So Yet for now we're going to stop here and until the next video I become an.