WEBVTT

00:05.070 --> 00:06.530
Hi everyone welcome back.

00:06.750 --> 00:13.050
So in this video we're going to be completing a second part of the API and that is to build the detection

00:13.120 --> 00:19.590
right to detect the similarity of two documents or two strings that are basically posted to us.

00:19.620 --> 00:20.690
OK.

00:21.120 --> 00:22.320
So let's get started.

00:22.320 --> 00:27.030
So the first thing is you want to write a classical detector right so we're going to write class.

00:27.070 --> 00:30.250
So we're going to give your class detect.

00:30.450 --> 00:31.120
OK.

00:31.410 --> 00:33.730
And then we're going to inherit from resource right.

00:33.750 --> 00:36.720
Because this is a resource for the API.

00:37.260 --> 00:37.990
Okay great.

00:38.030 --> 00:39.330
So far so good.

00:39.330 --> 00:40.200
Now what do we need.

00:40.200 --> 00:45.880
Well we know we know that it should be a post right so we're going to be defining a post here.

00:45.900 --> 00:47.240
So let's go back.

00:47.550 --> 00:51.450
So we're going to define post and then we self.

00:52.080 --> 00:55.080
And that again there's some steps which we always do.

00:55.090 --> 00:59.300
So the first thing with a post is we get the posted data is posted.

00:59.520 --> 01:05.710
Data is equal to request dot get miscall Jaison on.

01:05.940 --> 01:06.500
OK.

01:06.600 --> 01:13.170
So now we get there the posted data then we need to get a couple of things we need to get the user name

01:13.170 --> 01:19.770
of the user we need to get the password we need to get the text one that he sent the string and the

01:19.770 --> 01:21.010
text to that he said.

01:21.030 --> 01:23.210
OK so we need to get four things we need.

01:23.360 --> 01:24.520
Username.

01:24.720 --> 01:25.130
OK.

01:25.140 --> 01:34.870
So let's not capitalized that user name is equal to and then posted to and then it's at usually right.

01:35.220 --> 01:38.550
So the user posts it with the name usually.

01:38.790 --> 01:44.040
Second thing is password to password is equal to posted.

01:44.520 --> 01:52.800
And then inside password gate the third thing is we need text 1 and then we can say that the user sends

01:52.800 --> 01:54.030
it at Text 1.

01:54.060 --> 01:54.650
Right.

01:54.680 --> 01:55.580
There is no problem.

01:55.620 --> 02:02.370
You're the one who decides where what field should be filled to the user when you explain the API to

02:02.370 --> 02:03.130
them.

02:03.210 --> 02:09.170
And the second thing is text 2 is equal to as you might have guessed text to get.

02:09.180 --> 02:10.880
So we get the text to field.

02:11.190 --> 02:12.570
OK so we have the four things.

02:12.570 --> 02:15.400
Now there is a lot of checks that we have to do.

02:15.450 --> 02:21.300
We said first of all we have to check for invalid user names incorrect password or out of tokens.

02:21.300 --> 02:26.760
So we have three checks that we have to do before we service the users request.

02:26.760 --> 02:27.280
OK.

02:27.450 --> 02:31.470
So the first thing is we're going to check if not user exist.

02:31.470 --> 02:35.540
So if you remember the previous video we implemented the function user exists.

02:35.580 --> 02:42.060
So I'm saying if the user does not exist with this username then I'm going to have a problem with them

02:42.060 --> 02:48.270
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

02:48.270 --> 02:55.440
then return the invalid code which is 3 0 1 for invalid username and then I'm going to write a message

02:56.300 --> 02:58.820
and then just invalid using.

02:58.850 --> 02:59.700
OK.

02:59.980 --> 03:06.620
And then return Jason if I see your return Jaser OK.

03:06.930 --> 03:07.630
That's the first.

03:07.710 --> 03:09.340
First check for three or one.

03:09.540 --> 03:10.770
What about three or two.

03:10.800 --> 03:14.880
Well we want to verify that his password is correct.

03:14.880 --> 03:21.190
So we're going to say correct password is equal to verify password.

03:21.210 --> 03:27.360
So we're going to write a function called verify password and then verify function.

03:27.360 --> 03:29.990
We need the user name and the password right.

03:30.300 --> 03:37.140
And we're going to check if the user if if so the correct password is basically going to be a boolean

03:37.410 --> 03:42.490
which is true if this user name matches this password and false otherwise.

03:42.630 --> 03:43.160
OK.

03:43.310 --> 03:48.600
So we're going to verify the password using a function that we're going to write later on so we're going

03:48.600 --> 03:50.250
to write some function here.

03:50.250 --> 03:58.800
So define verify so verify desperate with a user name and password.

03:59.090 --> 04:05.000
OK so I'm going to pass for now just for now please pass OK.

04:05.140 --> 04:10.740
And that less complete writing is here before we go back to the password right.

04:10.810 --> 04:15.920
So correct password is to verify and then I'm going to say if not correct password.

04:15.920 --> 04:19.310
So if this password is not correct then what do I do.

04:19.360 --> 04:20.920
Well I went to doing three or two.

04:20.920 --> 04:21.880
Sorry your.

04:22.000 --> 04:23.560
You have an incorrect password.

04:23.770 --> 04:32.200
So if not correct password then my return Jason is going to be to have a status of three or two.

04:32.240 --> 04:34.310
Sorry there's a mistake.

04:34.480 --> 04:38.650
And the message is invalid password.

04:38.920 --> 04:39.680
OK.

04:40.570 --> 04:41.470
All right.

04:41.540 --> 04:46.760
So the third thing we're going to do is to verify that the user has enough tokens.

04:46.760 --> 04:47.450
OK.

04:47.570 --> 04:49.580
So for that we're going to write another function.

04:49.580 --> 04:55.470
So number two is equal to count tokens.

04:55.940 --> 04:57.830
And then I'm going to write for this using.

04:57.870 --> 05:02.920
So I'm going to say hey count me how many times this year how many tokens This user has.

05:03.030 --> 05:06.680
So I'm going to implement a function so I can even copy the whole thing.

05:06.710 --> 05:10.980
Your above above does detect functions I'm going to write.

05:10.980 --> 05:13.780
Define count tokens.

05:13.880 --> 05:19.490
And then I'm going to pass for now and then I'm going to count how many how many tokens does this user.

05:19.700 --> 05:20.610
OK.

05:21.080 --> 05:22.520
All right.

05:22.520 --> 05:27.030
So we have the we've counted the number of tokens and then I'm going to check.

05:27.050 --> 05:27.450
OK.

05:27.500 --> 05:36.660
If the number of tokens is bigger than or equal to or less than or equal to zero excuse me.

05:36.690 --> 05:41.270
So if this number is less than or equal to zero then there's a problem that he needs to refill.

05:41.460 --> 05:49.390
So the returns he said is going to be have a status off this time is going to be 3 0 3 right.

05:49.390 --> 05:57.440
Free go back it so three or three out of tokens he's going to have three or three and then I'm going

05:57.440 --> 06:02.570
I have a message which tells the user sorry you're out of tokens please refills.

06:02.630 --> 06:08.270
Message is you're out of tokens.

06:08.270 --> 06:15.610
Please refill it and then we're going to return again signifie the message.

06:15.620 --> 06:16.190
OK.

06:16.370 --> 06:19.930
So return Grace on my return geez.

06:20.300 --> 06:21.350
OK.

06:21.670 --> 06:26.330
All right so if we have the correct username and password and we have enough.

06:26.350 --> 06:26.900
OK.

06:27.020 --> 06:29.370
So now we can finally service these.

06:29.390 --> 06:37.440
Right now we can finally calculate the and so calculate calculate the editors.

06:37.640 --> 06:40.100
Now here's where we need space.

06:40.100 --> 06:40.640
OK.

06:40.790 --> 06:44.410
So we're going to go back up above and import spacier first.

06:44.430 --> 06:47.520
I'm going to go import spacier Okay.

06:47.600 --> 06:51.280
Now I need Spacey and I'm going to go down.

06:51.560 --> 06:53.280
And then how are we going to use space.

06:53.300 --> 06:56.510
So space is a very very very simple to use.

06:56.720 --> 07:03.600
So first of all you're right a.p natural language processing is equal to speccy dock load.

07:03.980 --> 07:08.930
And then you're going to write the name off your model which is an underscore web underscore and chikor

07:08.930 --> 07:10.750
core underscore webpage for us.

07:10.970 --> 07:16.770
So I'm going to write and underscore core underscore web underscore asset.

07:17.000 --> 07:17.370
OK.

07:17.420 --> 07:24.680
So now I've loaded this model and I'm ready to predict with spacing using this model how to predict

07:25.580 --> 07:28.760
whether to dock the similarity between two dog.

07:28.970 --> 07:37.010
So how do we do as we write text 1 is equal to a.p text 1.

07:37.050 --> 07:43.500
Because if you remember it takes one here is posted data right so I'm going to change that one from

07:43.500 --> 07:45.550
a string to an LP.

07:45.630 --> 07:45.870
Right.

07:45.880 --> 07:49.940
And natural language processing model sentence using spaces.

07:49.970 --> 07:58.190
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.

07:58.310 --> 08:06.550
Ok so now I have two instances of text 1 and text to which which are basically and Opis.

08:06.780 --> 08:10.260
Now I want to find the ratio of similarity between them.

08:10.440 --> 08:11.900
So the ratio is a number.

08:11.900 --> 08:16.340
So let me just write this year maybe as a comment.

08:16.350 --> 08:24.870
So ratio is a number between 0 and 1 that closer to 1.

08:25.370 --> 08:34.010
The more similar attacks one and text 2 are.

08:34.180 --> 08:38.740
So the ratio is going to give me a number between this ratio function that I'm going to show or the

08:38.740 --> 08:44.680
similarity function is going to give me a ratio and this ratio tells me how much how similar are these

08:44.680 --> 08:47.140
two deaths or some are close to each other.

08:47.320 --> 08:52.110
So if it gives me a zero then these two strings are very very different than each other.

08:52.360 --> 08:58.510
But if we give that if it gives you a 1 then that means the strings are very very very similar even

08:58.570 --> 09:00.910
if it's one it means they're exactly the same.

09:00.940 --> 09:01.690
Right.

09:01.690 --> 09:07.230
So the closer to 1 the more similar the text text text to our gate.

09:07.240 --> 09:08.650
Hopefully this makes sense.

09:08.650 --> 09:11.350
Next one next one in text.

09:11.740 --> 09:19.880
Ok so I want to say re-issue is equal to and then I'm going to write text one dot similarity.

09:19.960 --> 09:24.310
So similarity and then text do and that's it.

09:24.460 --> 09:25.600
It's as simple as that.

09:25.600 --> 09:26.000
You're right.

09:26.000 --> 09:30.830
Text one similarity takes two and that's it.

09:30.880 --> 09:33.140
And then you write returned Jason.

09:33.280 --> 09:36.350
So now I want to report this result to my reserved.

09:36.610 --> 09:37.260
So I'm going to write.

09:37.290 --> 09:38.060
Return.

09:38.140 --> 09:50.220
Status is 200 success and then the ratio is ratio and then or you can even change that to similarity

09:50.230 --> 09:55.900
right so you might want to change that to similarity and then message.

09:55.920 --> 10:00.420
So what message I'm gonna tell the user is similarity.

10:00.710 --> 10:05.150
Let's say a score calculated successful.

10:06.150 --> 10:06.850
And that's it.

10:06.870 --> 10:08.750
That's it for the weekend.

10:08.880 --> 10:11.250
And then before we return.

10:11.250 --> 10:11.870
Right.

10:12.060 --> 10:17.730
Don't forget that we have to take one token away from the user because he's used our service so we need

10:17.730 --> 10:20.180
to subtract one token from him.

10:20.230 --> 10:25.210
So I'm not to say the current tokens of credit tokens is equal to count tokens.

10:25.220 --> 10:30.890
If you remember we wrote a function of we're going to write a function called Count tokens.

10:30.960 --> 10:33.920
So we're going to count how many tokens this user has.

10:34.160 --> 10:35.110
OK.

10:35.640 --> 10:38.870
And then if and then there there's no right.

10:38.880 --> 10:41.030
We just updated tokens right away.

10:41.040 --> 10:43.980
And when I say users don't update.

10:44.490 --> 10:45.420
OK.

10:45.620 --> 10:53.880
And then I'm going to update the user where the user name user name as the one posted.

10:54.420 --> 10:59.040
And what I'm going to updated with is let's go back here.

10:59.340 --> 11:04.520
So what I'm going to update it with is I'm going to have a set dollar set.

11:04.530 --> 11:09.970
If you remember this is how we update the the elements and that inside of it.

11:09.990 --> 11:17.560
I'm going to set the tokens to be current tokens minus one.

11:17.610 --> 11:18.270
OK.

11:19.110 --> 11:20.370
And then that's it.

11:20.370 --> 11:25.530
So we close here and then close the curly brackets here and that's it.

11:25.530 --> 11:32.070
So I'm updating the user with this user name and I'm selling his tokens so this should be tokens here

11:32.310 --> 11:34.100
to create tokens minus one.

11:34.170 --> 11:35.420
OK.

11:36.150 --> 11:38.350
And then the final thing is we're going to return.

11:38.370 --> 11:45.350
Jason if I was sir Jaison if I return Jesus I'm going to return to him.

11:45.420 --> 11:48.160
What the the your attorney Jason that we did.

11:48.230 --> 11:48.800
OK.

11:49.590 --> 11:50.850
And that should be it.

11:50.970 --> 11:57.520
So the thing we have to do now is to write the two functions to help or functions or verify a password.

11:57.570 --> 12:03.890
And the second function which is the count tokens function that counts how many tokens a user has.

12:03.920 --> 12:07.390
OK so the first one to verify passwords shouldn't be too.

12:07.470 --> 12:08.060
OK.

12:08.160 --> 12:13.310
So I'm going to say I'm going to use the existing function user exists first.

12:13.320 --> 12:19.690
So let's say first of all if not user exist username.

12:19.700 --> 12:23.480
So if this user doesn't exist their return false right.

12:23.660 --> 12:28.240
This username password combination doesn't it is incorrect.

12:28.610 --> 12:29.230
OK.

12:29.450 --> 12:37.760
But the user exists then I'm going to check if his hashed password is correct or if the password he

12:37.760 --> 12:42.650
sent me after I hash it is the same as the hash password that I've seen.

12:42.820 --> 12:52.960
So I'm going to say hashed password is equal to users that find OK then I've got to find the user with

12:52.960 --> 12:55.880
the username username.

12:56.870 --> 12:57.590
OK.

12:57.730 --> 13:02.170
And then I'm going to access the first user because there's only one user like this so I'm going to

13:02.170 --> 13:07.240
access the zeroth user first user and I'm going to access his password.

13:07.300 --> 13:08.100
OK.

13:08.380 --> 13:13.620
So my access the user's password and then I'm going to say if we crypt.

13:13.720 --> 13:23.040
So if the script that hash password and then password encodes on the hash the password that the user

13:23.070 --> 13:27.100
sent me but encoding it with UTF 8.

13:27.200 --> 13:35.840
So if the past the hash password he sent me with the hashed password is equal to the hashed password

13:37.040 --> 13:38.650
then I'm going to return true right.

13:38.660 --> 13:42.020
Then this user is indeed who he claims to be.

13:42.020 --> 13:45.460
He has the user name and the password match.

13:45.800 --> 13:47.780
Else I'm going to return false.

13:47.780 --> 13:50.290
I'm going to say sorry you're not the right user.

13:50.290 --> 13:53.390
You're not the same user who signed up.

13:53.390 --> 13:54.780
This is an incorrect guess.

13:54.860 --> 13:55.560
OK.

13:56.060 --> 13:59.450
So this is for the very 2:05 So we first check that the user exists.

13:59.450 --> 14:01.520
If he doesn't return false.

14:01.790 --> 14:08.480
Else we check his hashed password and then we have just passed through and he sent us and we check if

14:08.480 --> 14:10.380
it matches the hash password.

14:10.700 --> 14:14.050
If it if it doesn't return true elsewhere returns.

14:14.070 --> 14:15.610
So hopefully this made sense.

14:16.990 --> 14:20.230
The second one is a function to count the number of students.

14:20.260 --> 14:32.740
So we have tokens is equal to users that find and then we want to find the user with a username that

14:32.740 --> 14:34.350
is given to us.

14:35.890 --> 14:38.890
And we also want to see how many tokens you have.

14:38.890 --> 14:41.390
So we want to get the first user or the only user.

14:41.620 --> 14:49.420
And then the element tokens get all right and then we you just return the tokens.

14:49.520 --> 14:56.720
So this is just the number of tokens inside for this user for this username and then we return the number

14:56.720 --> 14:57.380
of tokens.

14:57.380 --> 14:58.110
OK.

14:58.670 --> 15:01.120
So we save that and that should be it.

15:01.200 --> 15:01.650
OK.

15:01.700 --> 15:04.690
So we've written the varify password and count tokens.

15:04.870 --> 15:06.870
And so the detection is done.

15:07.070 --> 15:12.780
And basically all of the logic that is you know our API is actually in this area over here.

15:12.830 --> 15:22.660
So we're using this free train model to train to predict the similarity of two strings that are given

15:22.660 --> 15:23.040
to us.

15:23.080 --> 15:23.730
OK.

15:24.160 --> 15:26.170
So hopefully this makes sense.

15:26.230 --> 15:32.290
And we're going to stop here and in the next video we're going to do the last part which is the refill

15:32.320 --> 15:37.120
feature and we're also going to try to test the API after we're done.

15:37.210 --> 15:38.790
See how everything's going.

15:38.790 --> 15:39.660
And so.

15:39.870 --> 15:40.420
Okay.

15:40.550 --> 15:44.950
So Yet for now we're going to stop here and until the next video I become an.
