1
00:00:00,150 --> 00:00:02,150
Hello, guys, and welcome back.

2
00:00:02,160 --> 00:00:11,040
In the session, we'll see how to build our own APIs using the first API framework and Python language.

3
00:00:11,070 --> 00:00:17,880
Previously we had seen that we could wrap our deep learning model or more precisely, our object detection

4
00:00:17,880 --> 00:00:25,080
model into an API and then let this clients consume this API.

5
00:00:25,110 --> 00:00:32,760
That is, allow them send requests like this and then get responses.

6
00:00:33,210 --> 00:00:38,670
So here in black we have the requests and then now we have the responses in red.

7
00:00:38,670 --> 00:00:43,770
And all this is done using the HTTP protocol.

8
00:00:43,770 --> 00:00:52,260
And so now that we understand the concept of an API and how this can interact with millions of clients

9
00:00:52,260 --> 00:00:59,400
everywhere in the world, we are going to move straight to build in our own API and to build our own

10
00:00:59,400 --> 00:00:59,940
API.

11
00:00:59,940 --> 00:01:05,160
We are going to make use of this Python framework known as Fast API.

12
00:01:05,670 --> 00:01:15,030
Now other python based frameworks which can be used in building APIs are Django and flask.

13
00:01:15,210 --> 00:01:22,050
But in the recent years, the first API framework has gained much popularity amongst Python developers,

14
00:01:22,050 --> 00:01:27,390
thanks to the fact that it comes with some key features like speed.

15
00:01:27,390 --> 00:01:29,820
So it's a high performance framework.

16
00:01:30,510 --> 00:01:36,840
Time taken to code is reduced that now developers could now build features even faster.

17
00:01:37,170 --> 00:01:40,350
With fast API, you even have fewer bugs.

18
00:01:40,620 --> 00:01:47,880
It's more intuitive, it's easy, it's kind of short, robust and is standards based.

19
00:01:48,360 --> 00:01:56,610
So because we need all these features while building our object detection API, we kind of have little

20
00:01:56,610 --> 00:02:03,600
or no choice but to turn to the fast API framework and you're going to see how easy it is to create

21
00:02:03,600 --> 00:02:12,030
or build a fast, highly performant and robust API very easily using the fast API framework.

22
00:02:12,030 --> 00:02:17,520
First and first, feel free to check out this documentation at fast API tango dot com.

23
00:02:17,520 --> 00:02:25,650
Working with the fast API documentation doesn't really feel like a usual documentation as most of the

24
00:02:25,650 --> 00:02:34,590
concepts are well explained and broken down to enable just anybody with minimal python knowledge, understand

25
00:02:34,590 --> 00:02:37,320
and use fast API efficiently.

26
00:02:37,320 --> 00:02:41,940
So right here we have the features for API people.

27
00:02:41,940 --> 00:02:44,790
You could get straight into this tutorial, your user guide.

28
00:02:44,790 --> 00:02:50,910
You see you have this well written user guide, you also have the advanced guide.

29
00:02:50,910 --> 00:02:54,630
So once you're done with this user guide, you could get to the advanced user guide.

30
00:02:54,630 --> 00:03:00,720
You have special topics like Concurrency Deployment, Project Generation, and all this.

31
00:03:00,720 --> 00:03:06,300
And so before we move on, don't forget to star the Fast API GitHub repository.

32
00:03:06,300 --> 00:03:12,480
One of those course prerequisites is having used Python at a very basic level.

33
00:03:12,570 --> 00:03:18,060
And so we're supposing that you've already installed python without which you could head on to python

34
00:03:18,060 --> 00:03:22,680
dot org download and install python based on your own operating system.

35
00:03:22,680 --> 00:03:31,320
So here you could see we have Python installed Python version, check that out 2.7 and then python three

36
00:03:32,040 --> 00:03:35,490
version we have 3.6.

37
00:03:35,490 --> 00:03:37,080
This should be 3.6.

38
00:03:37,620 --> 00:03:38,880
Okay, there's an error there.

39
00:03:38,880 --> 00:03:40,590
We have 3.6.

40
00:03:40,590 --> 00:03:41,250
Okay.

41
00:03:41,520 --> 00:03:44,250
Now the next step will be to install fast API.

42
00:03:44,250 --> 00:03:46,500
So we head back to the documentation.

43
00:03:46,500 --> 00:03:49,200
You have your this installation.

44
00:03:49,200 --> 00:03:56,970
All you need to do is PIP install fast API or you could do pip install fast API all so you get other

45
00:03:56,970 --> 00:03:59,040
optional dependencies and features.

46
00:03:59,040 --> 00:04:01,320
We'll start by installing fast API.

47
00:04:01,320 --> 00:04:04,950
We have pip three install fast API.

48
00:04:05,100 --> 00:04:06,390
There we go.

49
00:04:06,540 --> 00:04:07,620
API now installed.

50
00:04:07,620 --> 00:04:12,330
We could do python three and then you import fast API.

51
00:04:13,140 --> 00:04:15,270
You could check out the version right here.

52
00:04:15,270 --> 00:04:22,290
We have fast API and then we have the version OC.

53
00:04:22,350 --> 00:04:22,920
So that's it.

54
00:04:22,920 --> 00:04:25,440
0.78.0.

55
00:04:25,440 --> 00:04:33,390
So the next problem we may face will be what if we have this project, this object detection project

56
00:04:33,390 --> 00:04:44,640
in which we have the fast API version 0.73.0, for example, installed, let's say 0.73.8 installed.

57
00:04:44,640 --> 00:04:52,710
You have this other project on optical character recognition, which needs a different version of fast

58
00:04:52,710 --> 00:04:56,100
API that is 0.75.0.

59
00:04:56,130 --> 00:04:58,800
You find that with this there will be a conflict.

60
00:04:58,800 --> 00:04:59,790
As what?

61
00:04:59,940 --> 00:05:03,920
Have installed is this 0.78.0 version.

62
00:05:03,930 --> 00:05:09,060
Now, the way we could solve this is by making use of virtual environments.

63
00:05:09,450 --> 00:05:16,620
The way virtual environments work is you could have this project, this object detection YOLO X project

64
00:05:17,040 --> 00:05:18,720
have its own.

65
00:05:20,120 --> 00:05:28,370
Isolated Python environment with its own interpreter where you can install this libraries.

66
00:05:29,800 --> 00:05:40,120
In this Python environment without it affecting this other project, which is with another or which

67
00:05:40,120 --> 00:05:51,160
is built in another virtual environment where we have a different set of libraries and library versions

68
00:05:51,160 --> 00:05:52,000
installed.

69
00:05:52,030 --> 00:05:57,880
So this means that in this environment here we could install a version of non pi C, let's say whatever

70
00:05:57,880 --> 00:05:58,540
version.

71
00:05:58,540 --> 00:06:04,540
And then here again we could install another version of Non Pi, which could even be the same as this

72
00:06:04,540 --> 00:06:05,140
version.

73
00:06:05,140 --> 00:06:13,510
But the difference now is in this virtual environment here we have a version of Fast API which is 0700780

74
00:06:13,510 --> 00:06:20,680
and in this other virtual environment we have this other version which is 0750 and now this problem

75
00:06:20,680 --> 00:06:28,240
of conflict at the level of the dependencies is resolved now that we want to create our Python virtual

76
00:06:28,240 --> 00:06:34,870
environments, we are going to make use of this python module, which is the Python vamp module.

77
00:06:34,870 --> 00:06:43,660
So you will simply do apt install the python python three valve and that's it.

78
00:06:44,260 --> 00:06:46,270
So you're yours.

79
00:06:46,660 --> 00:06:47,290
Okay.

80
00:06:47,290 --> 00:06:49,480
And that should be installed.

81
00:06:49,510 --> 00:06:51,490
Now we have the valve installed.

82
00:06:51,490 --> 00:06:57,790
Let's get into our neural learning projects directory and then from here what we'll do is we are going

83
00:06:57,790 --> 00:07:00,610
to create our virtual environment.

84
00:07:00,610 --> 00:07:09,880
We have Python two V valve, and then let's call our virtual environment valve, emotion detection,

85
00:07:10,180 --> 00:07:12,250
the detection.

86
00:07:12,940 --> 00:07:13,990
There we go.

87
00:07:13,990 --> 00:07:15,660
We should have this created.

88
00:07:15,670 --> 00:07:17,560
And from here you could do this.

89
00:07:17,560 --> 00:07:22,600
And you see that we have this valve motion detection here created.

90
00:07:23,560 --> 00:07:27,100
Let's get into this valve emotional detection directory.

91
00:07:27,100 --> 00:07:30,820
So let's do valve emotion detection.

92
00:07:30,820 --> 00:07:32,800
And there we go.

93
00:07:32,800 --> 00:07:38,320
You see, we have been include leap leap 64 by Valve and share.

94
00:07:38,350 --> 00:07:40,850
Now let's get into the the bean.

95
00:07:40,870 --> 00:07:42,220
So there we go.

96
00:07:42,220 --> 00:07:47,020
We have been and then you see we have this activate right here.

97
00:07:47,050 --> 00:07:51,460
Now let's get out of this and then let's do source.

98
00:07:51,820 --> 00:07:54,190
Well, let's get back again another step.

99
00:07:54,190 --> 00:08:01,660
So let's do source bean activate and we get in this error.

100
00:08:01,660 --> 00:08:10,150
Well, let's get back into valve emotions detection and then let's reduce source bean activate.

101
00:08:10,150 --> 00:08:11,080
And there we go.

102
00:08:11,080 --> 00:08:18,160
So one thing you can notice now is the fact that previously we had this root and this, but now we have

103
00:08:18,160 --> 00:08:20,920
this valve before this.

104
00:08:20,920 --> 00:08:26,290
So we see that everything we're going to be doing from now, henceforth will be in the context of this

105
00:08:26,560 --> 00:08:28,100
virtual environment.

106
00:08:28,120 --> 00:08:30,460
Now, let's do Python three.

107
00:08:30,490 --> 00:08:31,390
There we go.

108
00:08:31,390 --> 00:08:33,670
Let's import fast API.

109
00:08:33,670 --> 00:08:36,040
You can see that we have modeled, not found.

110
00:08:36,040 --> 00:08:39,790
So it's telling us that fast API is not installed.

111
00:08:39,790 --> 00:08:47,710
Now let's get let's exit and then deactivate the way we deactivated by simply typing in deactivate.

112
00:08:47,710 --> 00:08:52,180
And you find that when you deactivate you do not have this any longer.

113
00:08:52,180 --> 00:08:55,840
So we are no more in the context of the virtual environment.

114
00:08:55,840 --> 00:09:02,650
So now let's do again Python three and then let's import fast API.

115
00:09:02,650 --> 00:09:06,820
You find that here we have fast API installed.

116
00:09:07,510 --> 00:09:13,120
You see, we could also get the version you see we have fast API installed, but in this virtual environment

117
00:09:13,120 --> 00:09:15,280
Fast API isn't installed.

118
00:09:16,180 --> 00:09:19,600
Now let's exit and get back to our virtual environment.

119
00:09:19,600 --> 00:09:23,560
So we have source being activate.

120
00:09:23,560 --> 00:09:24,460
There we go.

121
00:09:24,460 --> 00:09:28,510
And now let's do PIP install fast API.

122
00:09:28,660 --> 00:09:34,960
So one thing you could note is the fact that out of the scope of this virtual environment, we have

123
00:09:34,960 --> 00:09:41,110
this version of API and now we could install another even different version of the first API.

124
00:09:41,110 --> 00:09:42,430
So that's it.

125
00:09:42,430 --> 00:09:50,110
We install fast API and now that fast API has been installed, we could do that in this virtual environment.

126
00:09:50,110 --> 00:09:57,430
We could do import or let's do Python three and then let's import the first API and there we go.

127
00:09:57,430 --> 00:09:59,200
So we have fast API now installed.

128
00:09:59,200 --> 00:10:05,440
We could get this version and you could see clearly that this version is different from the other version

129
00:10:05,440 --> 00:10:06,490
of fast API.

130
00:10:07,750 --> 00:10:12,880
Another thing we could do is create our virtual environments with a specific version of Python.

131
00:10:12,880 --> 00:10:23,980
So yeah, we could have Python 3.8 and then we have Valve, let's say emotion or let's say IMO detection.

132
00:10:24,130 --> 00:10:28,870
Okay, so there we go, we run that and then we create this.

133
00:10:29,430 --> 00:10:35,760
New virtual environment where the default version of Python is that of 3.8.

134
00:10:35,790 --> 00:10:37,820
Now let's again have this.

135
00:10:37,830 --> 00:10:47,250
You see, we have this year we have IMO detection and then we could get into that IMO detection.

136
00:10:47,250 --> 00:10:50,250
And then let's activate this.

137
00:10:51,150 --> 00:10:53,460
There we go and see this right here.

138
00:10:54,540 --> 00:11:00,990
Now when you do Python, you see that the default version is this 3.8.13.

139
00:11:01,020 --> 00:11:06,690
So again here, if you try to import fast API, you see model not found.

140
00:11:06,810 --> 00:11:14,770
Let's exit and then peep install fast API all and run that.

141
00:11:14,790 --> 00:11:20,970
You'll get to see now with this pip freeze that we have many more dependencies installed because we

142
00:11:20,970 --> 00:11:22,080
decided to do that.

143
00:11:22,080 --> 00:11:29,190
So here you see we have type extensions, toilets and if I go pedantic, which is very useful and this

144
00:11:29,190 --> 00:11:29,810
others.

145
00:11:29,820 --> 00:11:39,990
Now the next step will be to install unicorn and unicorn is an s g server, so as g here signifies asynchronous

146
00:11:39,990 --> 00:11:50,910
server gateway interface Being a HTTP server, this unicorn right here is responsible for taking requests

147
00:11:50,910 --> 00:11:52,440
from different users.

148
00:11:52,440 --> 00:11:59,490
So supposing we have this three users right here, the one to get, for example, some predictions from

149
00:11:59,490 --> 00:12:06,120
a model which is here in our fast API, which we have bundled in this Alphas API code.

150
00:12:06,120 --> 00:12:15,660
So what will go on here is this user for example, will make a request, see to this server and then

151
00:12:15,660 --> 00:12:25,350
the server gets that request, interacts with fast API, obtains a response and then sends back this

152
00:12:25,350 --> 00:12:28,030
response to this user.

153
00:12:28,050 --> 00:12:36,630
Now if you want to connect with this web server locally, we could pass in this IP address here, which

154
00:12:36,630 --> 00:12:39,270
is that of the local host.

155
00:12:39,270 --> 00:12:49,140
And then we specify a specific part such that we now here does the client sends the request which contains

156
00:12:49,140 --> 00:12:57,090
the method, which could be, for example, let's say a post method and then the request body and headers

157
00:12:57,090 --> 00:13:05,160
such that once this web server received this, it processes the information and produces the required

158
00:13:05,160 --> 00:13:06,210
responses.

159
00:13:06,210 --> 00:13:09,390
We are now going to dive straight away into some protocols.

160
00:13:09,390 --> 00:13:17,220
So here you get this fast API, you have this simple tutorial here which we are going to follow and

161
00:13:17,220 --> 00:13:21,030
see how to easily work with fast API.

162
00:13:21,210 --> 00:13:26,400
Right here we have this which we are going to copy to open up the code with Visual Studio.

163
00:13:26,400 --> 00:13:30,750
We will get back and then we'll just write code and there we go.

164
00:13:31,200 --> 00:13:42,180
So now let's actually get out of the superuser mode and then we get back in here, learn.

165
00:13:42,660 --> 00:13:43,650
There we go.

166
00:13:43,650 --> 00:13:46,260
And then now let's just do this again.

167
00:13:46,260 --> 00:13:51,780
And you see we have our Visual Studio, which pops up.

168
00:13:51,780 --> 00:13:56,160
We have our neural and projects folder and we could create this new file.

169
00:13:56,160 --> 00:14:01,960
So here we have main P, we create this new file and then we paste out that code.

170
00:14:01,980 --> 00:14:06,960
Now, if you're working with VTS code, it's recommended to install this Python extension, so we just

171
00:14:06,960 --> 00:14:08,970
go ahead and install this extensions.

172
00:14:08,970 --> 00:14:11,370
See, we have that installing.

173
00:14:11,370 --> 00:14:14,340
Let's get back to the code or let's just get back to this.

174
00:14:14,340 --> 00:14:20,550
So here, for example, let's analyze this with Claire here, for example, you see, we told to create

175
00:14:20,550 --> 00:14:28,020
a file in the PR y and with this code we are going to put in there now to keep things simple.

176
00:14:28,020 --> 00:14:33,720
We are going to command this region and then also command this right here.

177
00:14:33,720 --> 00:14:35,340
So we're not going to make use of this.

178
00:14:35,340 --> 00:14:39,090
We're going to command that and that should be fine.

179
00:14:39,090 --> 00:14:39,660
Okay.

180
00:14:39,660 --> 00:14:43,280
So we just have this portion of this part here.

181
00:14:43,290 --> 00:14:48,990
Now, you see here we have this app, which is an instance of fast API.

182
00:14:49,020 --> 00:15:00,420
C, we've imported this from first API, and then we have this decorator here and we have this read

183
00:15:00,420 --> 00:15:01,370
Root Method.

184
00:15:01,380 --> 00:15:09,720
Now this root method, we could actually change this so we could say read neural learn route or whatever

185
00:15:09,990 --> 00:15:10,890
name you want to give it.

186
00:15:10,890 --> 00:15:13,800
So let's call that learn or let's just keep it short.

187
00:15:13,800 --> 00:15:14,910
Let's just say read.

188
00:15:14,910 --> 00:15:25,050
So we have this read method takes a nothing and then it returns this dictionary or this JSON halo for

189
00:15:25,050 --> 00:15:27,780
the key and word for the value.

190
00:15:27,840 --> 00:15:28,980
We have this.

191
00:15:29,060 --> 00:15:33,290
The corridor right here where we specify the method type recall.

192
00:15:33,290 --> 00:15:39,590
We had different methods like the get the post, put the delete and so on and so forth.

193
00:15:39,590 --> 00:15:45,380
So you have, for example, we specify this method, so this is the get method.

194
00:15:45,380 --> 00:15:53,270
And so if we are making use of this metal right here, we have this path, here we have this path.

195
00:15:53,270 --> 00:16:02,300
And then this means that if our web server is hosted on an address, for example, let's say HTTP s,

196
00:16:02,660 --> 00:16:12,380
whatever or whatever dot com, then this route path will correspond to this path right here.

197
00:16:12,380 --> 00:16:16,670
And then if we want to do say a login, then we'll have your login.

198
00:16:16,670 --> 00:16:22,700
This login path will correspond to having this logging right here.

199
00:16:22,700 --> 00:16:27,980
Obviously we'll have a post, so change this to post and that's basically how it works.

200
00:16:27,980 --> 00:16:35,750
You see how easy it is for us to create these kinds of paths with Fast API and also specify the methods

201
00:16:36,200 --> 00:16:37,160
very easily.

202
00:16:37,160 --> 00:16:44,120
So here we have that and we could have slash or let's say a whatever we want to have there.

203
00:16:44,120 --> 00:16:46,700
And then all we need to do here is to have this here.

204
00:16:46,700 --> 00:16:52,760
A Now obviously we could have some variables here and we're going to look at that shortly with this

205
00:16:52,760 --> 00:16:54,020
other example below.

206
00:16:54,020 --> 00:17:02,120
Anyways, we we understand that we want to get to an address or your whatever dot com or let's, let's

207
00:17:02,120 --> 00:17:06,350
even say google.com, let's say we're trying to build google.com.

208
00:17:06,350 --> 00:17:10,490
Then we will just have here this and that will be it.

209
00:17:10,490 --> 00:17:20,630
And so with this now specifying this is going to get into this method and return this JSON now to run

210
00:17:20,630 --> 00:17:23,930
this, we'll just use what we've been given here in the documentation.

211
00:17:23,930 --> 00:17:28,310
Let's copy this out and simply paste this out here.

212
00:17:28,310 --> 00:17:30,740
So here we're going to open up the terminal.

213
00:17:30,740 --> 00:17:32,600
So we open up this new terminal.

214
00:17:32,600 --> 00:17:33,590
There we go.

215
00:17:33,860 --> 00:17:35,810
Claire That's it.

216
00:17:35,810 --> 00:17:37,940
And we in project.

217
00:17:37,940 --> 00:17:43,640
So make sure that you're in the same folder as where your main copy is found.

218
00:17:43,640 --> 00:17:52,670
We could do you've con and we run that error in the app attribute app not final model mean.

219
00:17:53,120 --> 00:17:55,250
So let's check this out.

220
00:17:56,270 --> 00:18:00,380
Let's save this and then run this again.

221
00:18:00,380 --> 00:18:01,280
There we go.

222
00:18:01,370 --> 00:18:02,270
See, now it works.

223
00:18:02,270 --> 00:18:07,460
We have started server process with the application startup application startup complete.

224
00:18:07,460 --> 00:18:09,080
So we wait and then it's completed.

225
00:18:09,080 --> 00:18:13,820
And now you've gone running on this address on our local host.

226
00:18:13,820 --> 00:18:18,110
So what we could do here is we simply control and we click open this.

227
00:18:18,110 --> 00:18:22,610
So control click and it opens up this page right here.

228
00:18:22,640 --> 00:18:23,870
There we go.

229
00:18:24,170 --> 00:18:28,940
The first thing we notice is this message where we have method not allowed, but why?

230
00:18:29,180 --> 00:18:32,030
Where will we have this method not allowed message.

231
00:18:32,030 --> 00:18:35,660
The reason why we have this is because your let's open this.

232
00:18:35,660 --> 00:18:38,210
The reason why we have this is because you're in the code.

233
00:18:38,600 --> 00:18:41,600
We actually use this wrong method.

234
00:18:41,690 --> 00:18:49,340
See, we're supposed to use a gate method instead, so we should change this and have this get.

235
00:18:49,340 --> 00:18:55,190
So let's save that again and then we'll stop this and then run that again.

236
00:18:55,190 --> 00:18:57,280
Click on this, open this up.

237
00:18:57,320 --> 00:18:58,220
And there we go.

238
00:18:58,220 --> 00:19:02,180
We have, as we expected, the JSON output.

239
00:19:02,180 --> 00:19:05,690
Here we have the ID or the key.

240
00:19:05,720 --> 00:19:08,390
Hello and the value world.

241
00:19:08,390 --> 00:19:11,840
Now another thing we could do is we could get back here.

242
00:19:11,840 --> 00:19:13,160
Let's stop this.

243
00:19:13,160 --> 00:19:19,250
So we stop that and then let's do, let's say entry point, let's have that.

244
00:19:19,250 --> 00:19:20,120
And we save this.

245
00:19:20,120 --> 00:19:29,090
So we save this and then we run this again, and then we click on this and you see that are not found

246
00:19:29,090 --> 00:19:35,150
So here because we do not have this path, we outputs, not found.

247
00:19:35,150 --> 00:19:39,980
So let's let's do this, let's say entry points and run that.

248
00:19:39,980 --> 00:19:41,780
And you should have an answer.

249
00:19:42,890 --> 00:19:44,870
Let's check this out here.

250
00:19:44,870 --> 00:19:51,860
We have entry point and then here we have entry point.

251
00:19:53,150 --> 00:19:54,590
Let's get back here.

252
00:19:55,400 --> 00:19:59,480
And then we should have this to resolve this issue.

253
00:19:59,480 --> 00:20:02,690
We have to put the slash before the entry point.

254
00:20:02,690 --> 00:20:05,350
So you have to be very careful with the way you write this.

255
00:20:05,360 --> 00:20:07,760
Now you save that again and then you reload.

256
00:20:07,760 --> 00:20:11,060
But aren't you tired of always having to reload?

257
00:20:11,360 --> 00:20:15,230
I guess as myself, you would also get tired of doing this.

258
00:20:15,230 --> 00:20:17,090
So let's do this.

259
00:20:17,090 --> 00:20:23,060
Instead of having that, we would add this reload your search that once we save or do modifications

260
00:20:23,060 --> 00:20:26,480
and save here, automatically our server is reloaded.

261
00:20:26,480 --> 00:20:28,670
So now we will run this.

262
00:20:29,260 --> 00:20:30,850
Uh, application start up started.

263
00:20:30,970 --> 00:20:31,990
Let's click on that.

264
00:20:32,080 --> 00:20:36,400
We have this and then we do entry points.

265
00:20:36,580 --> 00:20:37,510
That should be fine.

266
00:20:37,510 --> 00:20:38,070
So that's it.

267
00:20:38,080 --> 00:20:40,160
We have Hello World, as we expect.

268
00:20:40,180 --> 00:20:44,010
Now, let's go ahead and update this.

269
00:20:44,020 --> 00:20:45,250
Let's just say entry.

270
00:20:45,580 --> 00:20:47,910
You see, when you save now use here.

271
00:20:47,920 --> 00:20:49,960
You see it's shutting down.

272
00:20:49,960 --> 00:20:54,730
Waiting for application shutdown, application shutdown, complete finished server process start is

273
00:20:54,730 --> 00:20:58,330
have a process within for application startup and application startup completed.

274
00:20:58,330 --> 00:21:02,500
So now we have this which has been reloaded.

275
00:21:02,500 --> 00:21:06,610
So unlike previously where we had to stop and then start again.

276
00:21:06,610 --> 00:21:10,320
Now once we make the modifications, it's automatically reloaded.

277
00:21:10,330 --> 00:21:14,950
So let's get back here or not here, let's get back here.

278
00:21:15,220 --> 00:21:18,460
Oh yeah, actually, let's get back here.

279
00:21:19,150 --> 00:21:24,730
And then we are going to have let's run this, you see, doesn't work.

280
00:21:24,730 --> 00:21:27,460
But when we do entry, it works now.

281
00:21:27,460 --> 00:21:30,590
And we didn't have to stop the server for that.

282
00:21:30,610 --> 00:21:38,420
The next thing we'll look at is another interesting feature of fast API, which is the swagger UI.

283
00:21:38,440 --> 00:21:44,980
So here we've saved this, reloaded our server and then we get into this and then instead of having

284
00:21:44,980 --> 00:21:46,840
that, we'll do docs.

285
00:21:46,840 --> 00:21:52,990
So you see we have DOCS, so we have this page which pops up and you'll notice that we didn't need to

286
00:21:52,990 --> 00:21:55,210
specify that in our code.

287
00:21:55,210 --> 00:21:57,130
So let's get back here.

288
00:21:57,130 --> 00:22:06,370
And what we see is we have this method here and then when we open this up, see that does a path and

289
00:22:06,370 --> 00:22:09,310
then we have the parameters, there are no parameters.

290
00:22:09,310 --> 00:22:14,590
So we have this simple get request and then we have the response.

291
00:22:14,590 --> 00:22:23,260
So we are the the code 200 and the successful response and then the major type JSON and that's it.

292
00:22:23,260 --> 00:22:26,170
So now let's go ahead and try this out.

293
00:22:26,170 --> 00:22:31,870
So we see when we try it out, we click on try that out and then this comes up, we click on Execute.

294
00:22:31,870 --> 00:22:36,520
And what we have is this output right here.

295
00:22:36,520 --> 00:22:42,370
So here you can see the response body and then you can have the response headers.

296
00:22:42,370 --> 00:22:43,390
So that's it.

297
00:22:43,390 --> 00:22:46,450
You could also repeat this on the postman.

298
00:22:46,450 --> 00:22:52,990
So we have that, you see the request URL and then you have the URL.

299
00:22:53,770 --> 00:22:58,270
This you could use also to make the API calls.

300
00:22:58,270 --> 00:22:59,590
So that's it.

301
00:22:59,590 --> 00:23:02,020
We see how to make use of this docs.

302
00:23:02,020 --> 00:23:07,960
And now what we'll do is we are going to uncomment this to parts here.

303
00:23:07,960 --> 00:23:14,050
So here we recommend this and then we'll uncomment this part and then we save this.

304
00:23:14,050 --> 00:23:16,700
And then you see here we have this items.

305
00:23:16,720 --> 00:23:24,430
Now we will notice that when we get back to our code and then we refresh this, we have this automatically

306
00:23:24,430 --> 00:23:25,420
put out here.

307
00:23:25,420 --> 00:23:34,960
So this means that no matter the number of different paths we create, we can automatically test them

308
00:23:34,960 --> 00:23:40,810
without any extra effort thanks to this fast APIs swagger UI.

309
00:23:40,810 --> 00:23:45,820
So like your for example, you not only can test, but you can also see the documentation.

310
00:23:45,820 --> 00:23:50,650
Like here we told that this is an item, ID is an integer is required.

311
00:23:50,650 --> 00:23:53,740
Then this is a string cure query.

312
00:23:53,860 --> 00:23:58,690
It is optional because here you see is not required and then you could try this out.

313
00:23:58,690 --> 00:23:59,600
So let's go again.

314
00:23:59,620 --> 00:24:00,430
Try that out.

315
00:24:00,430 --> 00:24:05,530
We see, well, let's cancel and then you see, you cannot do anything here.

316
00:24:05,560 --> 00:24:09,640
Once you want to try out, you can put in an ID, So let's let's not put anything there.

317
00:24:09,640 --> 00:24:10,720
Let's just execute.

318
00:24:10,720 --> 00:24:12,700
You see, we must put something.

319
00:24:12,700 --> 00:24:19,240
So let's have that 20, 20, the string, let's, let's leave that for now and execute.

320
00:24:19,240 --> 00:24:21,160
So what do we get as results?

321
00:24:21,160 --> 00:24:25,240
Is this output item ID 20 cure null.

322
00:24:25,240 --> 00:24:34,810
Now if we modify the code right here, so let's modify the code and then let's just say item ID, save

323
00:24:34,810 --> 00:24:42,310
that we get back here and then let's re execute.

324
00:24:42,310 --> 00:24:43,900
Actually, let's we execute.

325
00:24:44,560 --> 00:24:45,250
What do we get?

326
00:24:45,250 --> 00:24:53,770
You see it's automatically we have this modifications and then now we could do this, let's say hello,

327
00:24:53,800 --> 00:25:01,390
we execute that and you see here we have our optional string which has been shown to us in this JSON.

328
00:25:01,390 --> 00:25:03,580
So we had seen the JSON format already.

329
00:25:03,580 --> 00:25:08,020
Here we have the key, we have the value, the key, we have the value and so on and so forth.

330
00:25:08,140 --> 00:25:11,170
And then also we have the response headers.

331
00:25:11,200 --> 00:25:16,330
Getting back to the documentation, you could see here the scroll back up.

332
00:25:17,110 --> 00:25:19,210
No, let's, let's get down.

333
00:25:19,330 --> 00:25:21,130
You could open this up.

334
00:25:21,130 --> 00:25:28,180
You see the main, the file, the ab, the object created inside of main pie with a line of API reload.

335
00:25:28,180 --> 00:25:28,780
Make the server.

336
00:25:28,970 --> 00:25:32,060
Start after chord changes only do this for development.

337
00:25:32,060 --> 00:25:37,520
So they're saying that if you're in a production setting, that if you've deployed this as we'll see

338
00:25:38,270 --> 00:25:43,700
in the next sections where you want to deploy this, you don't want a situation where you get to have

339
00:25:43,700 --> 00:25:45,800
this reloads automatically.

340
00:25:46,070 --> 00:25:50,300
So from this now you see this.

341
00:25:50,600 --> 00:25:56,300
Basically, we explained what we've said already receives HTTP requests in this path and in this other

342
00:25:56,300 --> 00:25:56,630
path.

343
00:25:56,630 --> 00:25:57,800
So we seen this already.

344
00:25:57,800 --> 00:25:59,840
Now both packets get requests.

345
00:25:59,840 --> 00:26:02,030
So we have the gate methods.

346
00:26:02,030 --> 00:26:08,180
The path this path has a parameter item ID that should be an instance or a path has an optional string

347
00:26:08,180 --> 00:26:09,380
query cure.

348
00:26:09,560 --> 00:26:10,370
So that's it.

349
00:26:10,550 --> 00:26:18,530
And then you're you you get to talk about the swagger UI, and then you also have this alternative doc

350
00:26:18,530 --> 00:26:20,210
API docs here, which is re doc.

351
00:26:20,210 --> 00:26:26,360
So basically what we'll do is we just get here and then instead of having all this, let's say we have

352
00:26:26,360 --> 00:26:33,950
that, we just do re doc, we run that and you have this alternative UI, here's where you could still

353
00:26:33,950 --> 00:26:36,590
again test this APIs.

354
00:26:36,590 --> 00:26:37,940
So that's it.

355
00:26:38,360 --> 00:26:40,340
You could test this very easily.

356
00:26:40,340 --> 00:26:46,000
You see we have that and then you should be able to test this.

357
00:26:46,010 --> 00:26:52,880
Now you have the response samples from your SO for this, you have this response sample, you have the

358
00:26:52,880 --> 00:26:59,030
others, a successful response or the response is unsuccessful.

359
00:26:59,030 --> 00:27:05,540
Where we have this photo to now, you could check out in the Mozilla platform where you see the exact

360
00:27:05,540 --> 00:27:06,770
meaning of this photo to.

361
00:27:06,770 --> 00:27:13,430
So let's let's check out developer Mozilla 422.

362
00:27:13,430 --> 00:27:21,380
So you could always get back to all this so you don't have any issues understanding any code you receive

363
00:27:21,380 --> 00:27:22,040
as output.

364
00:27:22,040 --> 00:27:26,660
So here you see unprocessed possible entity and that's it.

365
00:27:27,500 --> 00:27:29,330
That's it for this.

366
00:27:29,330 --> 00:27:32,690
Here we have different responses.

367
00:27:32,690 --> 00:27:33,560
That's fine.

368
00:27:33,560 --> 00:27:37,290
The next feature we'll be looking at will be that of schemas.

369
00:27:37,310 --> 00:27:39,950
What we'll do here is we'll simply take this off.

370
00:27:39,950 --> 00:27:41,180
We'll keep it simple.

371
00:27:41,180 --> 00:27:46,070
We will not use this and then we'll change this into a post request.

372
00:27:46,070 --> 00:27:51,530
So we have this post request right here which takes in a body.

373
00:27:51,530 --> 00:27:57,200
Obviously we have a body and then it returns the elements in this body.

374
00:27:57,200 --> 00:28:03,170
Now here will take this off and then we have our post request.

375
00:28:03,170 --> 00:28:05,270
So there we go, we have our post request.

376
00:28:05,270 --> 00:28:09,920
It's no more read item, but now add item.

377
00:28:09,920 --> 00:28:15,080
Now here we're going to have an item which is going to be of type item.

378
00:28:15,080 --> 00:28:17,750
So this is going to be a special type we're going to create.

379
00:28:17,750 --> 00:28:28,760
Now here we are going to import pedantic or from from pedantic, we're going to import the base model.

380
00:28:28,760 --> 00:28:29,840
So that's what we're going to do.

381
00:28:29,840 --> 00:28:35,480
We're going to import this base model from pedantic, and then we're going to create this item, this

382
00:28:35,480 --> 00:28:43,070
class item, which is going to be a subclass of this base model class right here.

383
00:28:43,070 --> 00:28:49,340
So we have this item and then we have base model.

384
00:28:49,340 --> 00:28:50,270
So that's it.

385
00:28:50,270 --> 00:28:55,490
And then here we're going to say, okay, we want that an item should have a name and a price.

386
00:28:55,490 --> 00:29:01,970
So we have name which is a string, and we have a price price.

387
00:29:01,970 --> 00:29:05,510
Let's, let's say integer, let's say our price an integer.

388
00:29:05,900 --> 00:29:06,560
So that's it.

389
00:29:06,770 --> 00:29:08,270
So there we go.

390
00:29:08,600 --> 00:29:12,980
What we're going to receive here is this item of type item.

391
00:29:12,980 --> 00:29:19,310
It's no longer the usual types, but now this special type item which we just created.

392
00:29:19,310 --> 00:29:22,220
So now here we have item.

393
00:29:22,700 --> 00:29:31,790
And then what we'll do is we're going to return this, so let's call this item name here.

394
00:29:31,790 --> 00:29:34,850
So we have item dot name.

395
00:29:34,850 --> 00:29:43,760
So we get the item, we return it's name and then we also have the items price, So item price, and

396
00:29:43,760 --> 00:29:48,350
then we return the item price.

397
00:29:48,350 --> 00:29:50,900
Okay, so that looks fine.

398
00:29:50,900 --> 00:29:58,370
Let's save this and check our code here or check this API.

399
00:29:58,400 --> 00:30:02,060
Let's refresh this and then you see we have our post.

400
00:30:02,270 --> 00:30:07,070
We could check that out or let's try this out, execute.

401
00:30:07,160 --> 00:30:12,880
Or do we get we get in response body item string item price zero.

402
00:30:12,890 --> 00:30:22,490
Now if we come here, if we get here, you see a level of this name and then we put, let's say back

403
00:30:22,490 --> 00:30:28,280
and then price, let's say 20, and then here we execute.

404
00:30:28,720 --> 00:30:29,950
What do we get?

405
00:30:29,980 --> 00:30:31,660
You see, we have back 20.

406
00:30:31,690 --> 00:30:38,320
If there's something you can notice is the fact that this request body here automatically takes this

407
00:30:38,320 --> 00:30:40,150
name and price.

408
00:30:40,150 --> 00:30:42,850
So let's get back to the code.

409
00:30:42,850 --> 00:30:48,610
You see that if we modify this, let's let's say we want to get the name, want to get a price or let's

410
00:30:48,610 --> 00:30:51,520
say we want to get the cost or let's say we want to get a discount.

411
00:30:51,520 --> 00:30:56,740
So let's say there's a discount on the item discounts and then let's put this in.

412
00:30:56,790 --> 00:31:05,140
Okay, We save that and then we get back here, we refresh this, try that out and you see discount

413
00:31:05,140 --> 00:31:06,220
automatically is here.

414
00:31:06,220 --> 00:31:15,980
So the role of this schemas is to provide a model which the request body has to always follow.

415
00:31:16,000 --> 00:31:22,270
So this means that if you get back here, let's get back here and let's take one of this off.

416
00:31:22,270 --> 00:31:27,970
You see, we take one of them off, we execute, take out the response.

417
00:31:27,970 --> 00:31:34,540
You see here, expecting our property name, enclosing double quotes, all of that you see here we have

418
00:31:34,540 --> 00:31:37,760
this unprocessed double entity error.

419
00:31:37,780 --> 00:31:41,710
Now, this is because you're at the level of the request body.

420
00:31:41,710 --> 00:31:49,630
We didn't specify the discount, which we've already said in the schema that we need to always have.

421
00:31:49,630 --> 00:31:57,070
So doing this means that each time anyone has to make a call on this API, on this endpoint right here,

422
00:31:57,070 --> 00:32:02,620
that person has to specify the name, the price and the discount for the request body.

423
00:32:02,860 --> 00:32:09,610
Now, we could also enforce this kind of measure at the level of the output, so we could specify a

424
00:32:09,610 --> 00:32:13,150
response, body response, body.

425
00:32:13,150 --> 00:32:20,710
And then we define say, let's say, let's say this is input item, let's call this input item, and

426
00:32:20,710 --> 00:32:24,190
then yeah, we'll have output item.

427
00:32:24,370 --> 00:32:28,780
So we want to create another schema here.

428
00:32:28,780 --> 00:32:36,910
Let's copy this and then we're going to say that, okay, we want this output to be the price minus

429
00:32:36,910 --> 00:32:37,510
the discount.

430
00:32:37,510 --> 00:32:48,760
So here we have output out put and then instead of the price and the discount, we just want the selling

431
00:32:48,760 --> 00:32:49,480
price.

432
00:32:49,480 --> 00:32:52,880
So here we have selling price and that will be an integer.

433
00:32:53,320 --> 00:32:54,250
So we have that.

434
00:32:54,250 --> 00:32:59,680
Now we have the output, we have the input and we have the output schemas.

435
00:33:00,340 --> 00:33:02,590
There are both subclasses of base model.

436
00:33:02,590 --> 00:33:07,960
And then here's what we'll do is we'll say, okay, we want to have the selling price, will define

437
00:33:07,960 --> 00:33:15,760
the selling price to be the item price minus the item discount.

438
00:33:15,880 --> 00:33:17,170
Okay, So we have that.

439
00:33:17,170 --> 00:33:19,150
So we have this now.

440
00:33:19,150 --> 00:33:24,640
And then here we have the item name still, and then we have the item.

441
00:33:24,640 --> 00:33:26,380
We have to be careful the same as this.

442
00:33:26,380 --> 00:33:30,130
So we have to say selling price anyhow, let's leave that for now.

443
00:33:30,130 --> 00:33:32,830
Let's just say we have here selling price.

444
00:33:32,830 --> 00:33:38,410
So let's say we have selling price and then let's save this and check this out.

445
00:33:38,410 --> 00:33:43,810
You see already post got an unexpected keyword argument response body.

446
00:33:43,810 --> 00:33:47,200
This is actually a response model, not a response body.

447
00:33:47,200 --> 00:33:49,120
So that's the output response model.

448
00:33:49,120 --> 00:33:52,360
Let's let's let's change this to input.

449
00:33:52,360 --> 00:33:58,040
So now this input item for the request and then this output for the response.

450
00:33:58,060 --> 00:33:59,980
Now let's save this.

451
00:33:59,980 --> 00:34:01,180
That's fine.

452
00:34:01,750 --> 00:34:04,630
Let's go ahead and check this out.

453
00:34:04,630 --> 00:34:07,200
So here we try this out.

454
00:34:07,210 --> 00:34:08,050
There we go.

455
00:34:08,050 --> 00:34:09,820
You see, as usual, that's fine.

456
00:34:09,820 --> 00:34:11,830
And then let's let's give this a price.

457
00:34:11,830 --> 00:34:16,390
Let's say 20 and no discount and then we execute.

458
00:34:16,390 --> 00:34:17,440
So what do we get?

459
00:34:17,440 --> 00:34:19,200
Your internal server error.

460
00:34:19,210 --> 00:34:22,960
Now let's get back to our code and check out what's going on here.

461
00:34:22,960 --> 00:34:25,390
If you required this mission.

462
00:34:25,390 --> 00:34:32,920
Now you see that everything looks like normal, but we saw that there's a field that required a level

463
00:34:32,920 --> 00:34:33,730
of the output.

464
00:34:33,730 --> 00:34:40,690
And the reason why we have this is simply because this output expects to see selling price.

465
00:34:40,690 --> 00:34:43,360
You see it expects to have certain price.

466
00:34:43,360 --> 00:34:46,790
So because we do not have selling price here, we get that error.

467
00:34:46,810 --> 00:34:49,720
Now, let's save this get back here.

468
00:34:50,260 --> 00:34:53,350
I'll refresh that and then try this out again.

469
00:34:53,350 --> 00:34:54,160
Execute.

470
00:34:54,160 --> 00:34:57,370
Now let's let's put you at 20 anyway.

471
00:34:57,370 --> 00:34:58,480
You should it should be the same.

472
00:34:58,480 --> 00:35:05,050
It's just that we want to have to see the difference in the order to see how the selling price is computed.

473
00:35:05,050 --> 00:35:07,750
Here again, we have internal server error.

474
00:35:07,780 --> 00:35:10,690
Now this looks funny.

475
00:35:10,870 --> 00:35:15,010
Let's get back and see where we get an error.

476
00:35:15,040 --> 00:35:20,320
We told field required and it happens that we even did not specify this correctly.

477
00:35:20,320 --> 00:35:24,700
So here's this year's name price discount and this is name.

478
00:35:24,700 --> 00:35:28,480
So yeah, we should also have name so not item name.

479
00:35:28,620 --> 00:35:33,000
See if that again, we go ahead and check this out here.

480
00:35:35,430 --> 00:35:36,810
Let's refresh that.

481
00:35:37,260 --> 00:35:38,790
Try this out.

482
00:35:39,060 --> 00:35:42,120
Let's just execute and that's fine.

483
00:35:42,120 --> 00:35:43,620
So you see, we have that now.

484
00:35:43,620 --> 00:35:47,970
And this is because we've put out our outputs correctly.

485
00:35:47,970 --> 00:36:00,780
So let's have your C to 100 and then 180 or let's say 20, and then we execute that and see we have

486
00:36:00,780 --> 00:36:01,470
this output.

487
00:36:01,470 --> 00:36:02,160
So that's it.

488
00:36:02,160 --> 00:36:11,580
We see how thanks to the schemas, you could now build APIs which permit the developer or whoever making

489
00:36:11,580 --> 00:36:18,240
use of your API to have to specify this, without which nothing is really going to work.

490
00:36:18,240 --> 00:36:21,570
So this means that you could have a situation like here.

491
00:36:21,600 --> 00:36:29,370
Let's, let's see, we have this input item and then we let's, let's see if that again and get back

492
00:36:29,370 --> 00:36:29,970
here.

493
00:36:30,060 --> 00:36:36,840
We refresh the page and then we'll try out while taking off this.

494
00:36:36,990 --> 00:36:41,490
So we take this one off and then we go ahead and we execute.

495
00:36:41,760 --> 00:36:47,130
You see that the output here for two two is a client error.

496
00:36:47,340 --> 00:36:51,780
So this means that the client knows that this error is coming from here.

497
00:36:51,780 --> 00:36:59,070
So our side now we get back here and then before we move on, we are going to define this structure

498
00:36:59,070 --> 00:37:05,780
or this code structure for our first API projects we'll be working with.

499
00:37:05,790 --> 00:37:10,560
So what we want to do is separate the APIs.

500
00:37:10,560 --> 00:37:14,010
That's actually the endpoints from the core.

501
00:37:14,040 --> 00:37:19,530
Now when we talk about core, we're talking about let's get back here, we're talking about carrying

502
00:37:19,530 --> 00:37:21,120
out computation like this.

503
00:37:21,120 --> 00:37:22,560
So you see this computation.

504
00:37:22,560 --> 00:37:26,580
We want to have some separate file which is in charge of doing this.

505
00:37:27,150 --> 00:37:31,860
In the case of some deep learning model, one, to have a separate file which will be in charge of,

506
00:37:32,310 --> 00:37:39,270
say, taking the inputs, loading the inputs, passengers input to a model, doing some post processing.

507
00:37:39,270 --> 00:37:46,860
And then from there, now the output, the output from that is passed onto this endpoints because here

508
00:37:46,860 --> 00:37:49,500
this one endpoint and this another endpoint.

509
00:37:49,500 --> 00:37:51,500
So just like read this add item.

510
00:37:51,510 --> 00:37:57,720
Now getting back here, you see this is like having read here this one endpoint, read this endpoint

511
00:37:57,720 --> 00:38:01,620
at item, just maybe on the endpoint, say update item.

512
00:38:01,620 --> 00:38:05,180
So like routes you have get items, post items and so on and so forth.

513
00:38:05,190 --> 00:38:11,460
So now at the level of the core one one half, our business logic in our case, for this simple example,

514
00:38:11,460 --> 00:38:19,620
our business logic was basically just taking the, the price and then subtracting the discount.

515
00:38:19,620 --> 00:38:21,150
So that was our business logic.

516
00:38:21,150 --> 00:38:24,830
But we, we, we want to have this in a separate file.

517
00:38:24,840 --> 00:38:26,910
Now for the schemas.

518
00:38:26,910 --> 00:38:28,980
We want to also have this in some separate files.

519
00:38:28,980 --> 00:38:34,200
So we want to have this kind of structure where we have the car that's business logic schemas and then

520
00:38:34,200 --> 00:38:37,060
we have the API in charge of all the different endpoints.

521
00:38:37,080 --> 00:38:39,930
Now to do that, we are going to create our service.

522
00:38:39,930 --> 00:38:44,850
So from here we have we're going to add a new folder which will call service.

523
00:38:45,030 --> 00:38:52,080
And then in the service we're going to add a new file which is now basically let's copy this main into

524
00:38:52,080 --> 00:38:57,180
the service here so we could do this, we could copy that, move that.

525
00:38:57,720 --> 00:38:59,910
Now let's stop the server.

526
00:39:00,120 --> 00:39:06,120
We've moved this into our service and then in the service we have a new folder which we'll call API,

527
00:39:06,120 --> 00:39:10,570
and then we'll create another new folder which we'll call the car.

528
00:39:10,590 --> 00:39:18,200
Now in this call we have a new folder which we'll call the logic, and we also have another folder which

529
00:39:18,210 --> 00:39:19,680
is called the Schemas.

530
00:39:19,680 --> 00:39:21,450
Schemas.

531
00:39:21,630 --> 00:39:31,350
Okay, So we have this this no, this is this is this call has this new folder which is called schemas

532
00:39:31,350 --> 00:39:33,360
and not the logic.

533
00:39:33,360 --> 00:39:37,590
So let's, let's delete this folder here.

534
00:39:37,620 --> 00:39:40,050
Let's delete this move to trash.

535
00:39:40,050 --> 00:39:40,390
Okay.

536
00:39:40,410 --> 00:39:45,840
So we have that, we have the car which has logic and schemas and then we have the API now which will

537
00:39:45,840 --> 00:39:47,940
have different endpoints.

538
00:39:48,330 --> 00:39:56,760
Now, given that we need to carry out our inference making use of this quantized Onix model, we are

539
00:39:56,760 --> 00:40:01,740
going to start by PIP installing on x runtime.

540
00:40:01,740 --> 00:40:07,260
So yeah, we have PIP install x run time and there we go.

541
00:40:07,680 --> 00:40:13,230
Now we get in this error, so you have to make sure you run this as a super user.

542
00:40:13,590 --> 00:40:16,350
We now run this as a super user.

543
00:40:16,350 --> 00:40:22,670
We have PIP installed on x run time that's now installed.

544
00:40:22,680 --> 00:40:28,090
You see, we could do Python and then in part on x ray.

545
00:40:28,290 --> 00:40:31,930
On time, and that's fine.

546
00:40:31,950 --> 00:40:32,610
Okay.

547
00:40:33,300 --> 00:40:38,430
That said, let's do pip install OpenCV python.

548
00:40:40,730 --> 00:40:42,370
We could also check that out.

549
00:40:42,380 --> 00:40:49,010
Let's clear this and a python and in part cv2.

550
00:40:49,010 --> 00:40:50,810
And there we go.

551
00:40:50,810 --> 00:40:56,630
So you see we have now the Onyx runtime and open C installed.

552
00:40:57,200 --> 00:40:59,180
We then move on to create the endpoints.

553
00:40:59,180 --> 00:41:05,780
So in this API, we have this new folder which will call end points.

554
00:41:05,780 --> 00:41:12,680
And then here we'll create new file which will call detect pi.

555
00:41:13,010 --> 00:41:19,870
And in here again we have another file which will call test py.

556
00:41:19,910 --> 00:41:28,220
Now this detect the prior will play the role of actually being that endpoints through which we are going

557
00:41:28,220 --> 00:41:29,600
to carry out a detections.

558
00:41:29,600 --> 00:41:36,860
And then this one will be the endpoints true which will test if our API is alive or not.

559
00:41:37,640 --> 00:41:42,740
Now in our detect py, we are going to import the API router.

560
00:41:42,740 --> 00:41:47,630
So from first API import API router, there we go.

561
00:41:47,630 --> 00:41:53,750
So previously we had imported first API, but this time around we'll be working with this API router

562
00:41:54,260 --> 00:41:54,650
then.

563
00:41:54,650 --> 00:42:06,560
Now we'll create this object IMO router of the API router class where this will play the role of routing

564
00:42:06,560 --> 00:42:12,770
information from the detect the API to the main API.

565
00:42:13,340 --> 00:42:21,140
Now we have our IMO router decorator and then we have we specified I want to have this get method and

566
00:42:21,140 --> 00:42:23,540
our path will be detect.

567
00:42:23,570 --> 00:42:24,200
Okay.

568
00:42:24,200 --> 00:42:26,000
So here's our path.

569
00:42:26,000 --> 00:42:31,400
And then now we could define this detect method in your with this detect method.

570
00:42:31,400 --> 00:42:37,880
Given that we want to carry out detections based on input images, we're going to have as input this

571
00:42:37,880 --> 00:42:46,190
time around this image and then you find that given that it's an image, it is going to be a type upload

572
00:42:46,190 --> 00:42:48,980
file because the user will have to upload this.

573
00:42:48,980 --> 00:42:56,240
So that's the type upload file we we have that we could simply import this from your upload file.

574
00:42:56,240 --> 00:42:57,110
There we go.

575
00:42:57,110 --> 00:43:02,990
And then once we once we define this, we can now get into the body of our method.

576
00:43:02,990 --> 00:43:14,120
So from here we could start by just let's say we output return, return, say hello, Hello world.

577
00:43:14,120 --> 00:43:16,070
So let's just return this.

578
00:43:16,940 --> 00:43:23,090
So now we have this method which takes in the image which is uploaded by the user and then returns Hello

579
00:43:23,090 --> 00:43:23,660
World.

580
00:43:23,660 --> 00:43:32,200
Now to diving into what this detect is going to be doing, we'll need to import this image from pill.

581
00:43:32,210 --> 00:43:37,190
So from pill you're going to import image.

582
00:43:37,190 --> 00:43:37,910
There we go.

583
00:43:37,910 --> 00:43:42,530
So we have that imported and then we are going to open up the file.

584
00:43:42,530 --> 00:43:44,840
So let's let's say we have our image.

585
00:43:44,840 --> 00:43:47,690
We want to get our image image.

586
00:43:48,110 --> 00:43:49,310
There we go.

587
00:43:49,910 --> 00:43:59,390
We open up the file so we have image open the file which has been uploaded by the user.

588
00:43:59,390 --> 00:44:05,180
So we have M, which is this dot file dot read.

589
00:44:05,450 --> 00:44:09,020
But this open method here takes in bytes.

590
00:44:09,020 --> 00:44:18,320
So we have your bytes bytes IO to convert this to byte form and then you would simply import that.

591
00:44:18,320 --> 00:44:24,140
So we have from IO, we import bytes IO and that should be fine.

592
00:44:24,530 --> 00:44:25,070
So that's it.

593
00:44:25,070 --> 00:44:30,410
We now have our image which would then convert into a non pi array.

594
00:44:30,410 --> 00:44:38,330
So now we have image equal non pi array which takes in this image.

595
00:44:38,840 --> 00:44:46,880
And then now instead of returning this, we could have this method emotions detector, emotions detector

596
00:44:46,880 --> 00:44:48,860
which takes in the image.

597
00:44:48,860 --> 00:44:56,060
So it takes in this non pi array or basically takes in our image and then is going to tell us what class

598
00:44:56,060 --> 00:44:57,440
that image belongs to.

599
00:44:57,440 --> 00:45:03,530
So for now we have this let's import non pi that's important on pi.

600
00:45:03,830 --> 00:45:10,800
So we have import non pi as MP.

601
00:45:10,850 --> 00:45:12,110
Okay, so that's it.

602
00:45:12,110 --> 00:45:13,760
We now have this imported.

603
00:45:14,270 --> 00:45:21,530
We are also going to check if the image or whatever file we are uploading is actually an image file.

604
00:45:21,530 --> 00:45:28,670
So we get the file name, we have the file name and then once we obtain the file name we try to get

605
00:45:28,670 --> 00:45:29,780
this extension.

606
00:45:29,780 --> 00:45:32,270
So right here, stick this off.

607
00:45:32,270 --> 00:45:36,410
We have that split method, then we obtain the extension.

608
00:45:36,410 --> 00:45:38,030
And if this is.

609
00:45:39,360 --> 00:45:48,030
Or is part of this that if it's jpg g pack or PNG G, then we know that this is an image and so we're

610
00:45:48,030 --> 00:45:49,380
simply going to pass.

611
00:45:50,010 --> 00:45:53,640
If this is not the case, then we'll raise an exception.

612
00:45:54,210 --> 00:46:10,140
So here we have raise raise HTTP ec ception and then we will make sure we import this HTTP exception.

613
00:46:10,650 --> 00:46:15,900
Then we specify the status code as 415 and our detail is not an image.

614
00:46:15,900 --> 00:46:22,350
So that's what we're going to do before calling on this emotional detector matter right here.

615
00:46:23,580 --> 00:46:30,510
Now, before we move on, let's let's keep install on pillow, because you could see here that we have

616
00:46:30,510 --> 00:46:31,110
this.

617
00:46:34,890 --> 00:46:37,500
We have the pillar library, which is yet to be installed.

618
00:46:37,500 --> 00:46:41,520
So we're just going to do your peep install pillow.

619
00:46:42,060 --> 00:46:43,920
There we go while there's installing.

620
00:46:43,920 --> 00:46:48,960
Also take note of the fact that this is meant to be a post request and not get requests.

621
00:46:48,960 --> 00:46:51,240
So we replace this with post.

622
00:46:52,470 --> 00:46:57,500
Now that we have pillow installed, you see, we do no longer have the one we had before.

623
00:46:57,510 --> 00:46:58,350
So that's it.

624
00:46:58,350 --> 00:47:02,040
Now we could take out on our test pie.

625
00:47:02,820 --> 00:47:07,770
This time around we're going to create this test router.

626
00:47:07,770 --> 00:47:08,850
So here we go.

627
00:47:08,850 --> 00:47:15,750
We have a test router, and then we just call this test testing.

628
00:47:15,750 --> 00:47:20,520
And your could take this off and this is a get request.

629
00:47:20,520 --> 00:47:25,810
So here we have this get request and we would have this test.

630
00:47:25,860 --> 00:47:32,490
So this is our entry point and then we could take all this off.

631
00:47:33,510 --> 00:47:34,680
There we go.

632
00:47:35,130 --> 00:47:38,490
Here we now have we're going to return

633
00:47:41,340 --> 00:47:43,950
testing or let's see.

634
00:47:43,950 --> 00:47:45,300
Yeah, testing,

635
00:47:47,880 --> 00:47:48,740
testing.

636
00:47:49,170 --> 00:47:50,130
So that's it.

637
00:47:50,130 --> 00:47:52,650
We have testing, testing.

638
00:47:53,130 --> 00:47:54,330
Our iPad is working.

639
00:47:54,330 --> 00:48:01,200
So this is what we're going to output when we have this all pass this through the browser.

640
00:48:01,980 --> 00:48:10,260
Now that we have this two endpoints defined as this test and this detect right here, what we are going

641
00:48:10,260 --> 00:48:15,780
to do is we are going to create in this API folder the API API file.

642
00:48:15,780 --> 00:48:19,050
So let's create this new file API API.

643
00:48:19,080 --> 00:48:23,070
Notice how this tool in this endpoint.

644
00:48:23,070 --> 00:48:28,050
So detect and the pie and the endpoints folder or directory.

645
00:48:28,050 --> 00:48:32,970
And then this API, the pie is found in this API directory.

646
00:48:32,970 --> 00:48:39,690
So now in this API API, what we're going to do is we are going to create this main router and the role

647
00:48:39,690 --> 00:48:49,800
of the main router will be to include this detect router and the test router, which itself now will

648
00:48:49,800 --> 00:48:52,710
be included in the main app.

649
00:48:53,370 --> 00:49:00,240
So as usual we have to import this API router from first API and then once we have this imported we

650
00:49:00,240 --> 00:49:03,480
are going to import the detect router and the test router.

651
00:49:03,480 --> 00:49:07,260
So from service, remember we've created this already.

652
00:49:07,260 --> 00:49:12,600
API endpoints detect our detect API right here.

653
00:49:12,600 --> 00:49:19,900
We are going to import detect router and then let's copy this piece out here.

654
00:49:19,920 --> 00:49:24,330
Now we're going to import the test router from test.

655
00:49:24,600 --> 00:49:25,830
So there we go.

656
00:49:25,830 --> 00:49:29,130
So we have the detect and the test routers now imported.

657
00:49:29,130 --> 00:49:31,950
The next thing to do would be to create our main router.

658
00:49:31,950 --> 00:49:40,380
We have this main router API router which was imported already, and then now the main router is going

659
00:49:40,380 --> 00:49:47,250
to include the detect router and it's also going to include the test router.

660
00:49:47,250 --> 00:49:52,230
So we have main router including the detect and the test router.

661
00:49:52,230 --> 00:50:03,150
So there we go again, we can access the app via the detect or test or maybe some other let's say some

662
00:50:03,150 --> 00:50:06,870
lambda lambda router right here.

663
00:50:06,870 --> 00:50:15,630
And then all of this get included in our main router, which now is also included in the app.

664
00:50:16,560 --> 00:50:20,700
Back to our main API we have from Fast API.

665
00:50:20,730 --> 00:50:29,760
This time around we're going to import fast API, take that off and then now we import the main router.

666
00:50:29,760 --> 00:50:38,460
So we have service API to the API in part main router.

667
00:50:38,460 --> 00:50:39,240
There we go.

668
00:50:39,240 --> 00:50:46,920
So we have this main router imported as usual, we have the app first API, then we could specify the

669
00:50:46,920 --> 00:50:48,030
project name.

670
00:50:49,440 --> 00:50:58,710
There we go, emotions detection and then we have to include now our main router.

671
00:50:58,710 --> 00:51:00,120
So this time on our app.

672
00:51:00,120 --> 00:51:07,290
Now with our app we have the include router method which takes in the main router.

673
00:51:07,290 --> 00:51:08,520
So that's it.

674
00:51:08,520 --> 00:51:15,120
We have this now included and we will now turn all this into packages.

675
00:51:15,120 --> 00:51:21,870
So this endpoints, we're going to create a new file in neat dot pie.

676
00:51:22,110 --> 00:51:29,760
There we go on the API API, new file we have in neat dot pie.

677
00:51:30,600 --> 00:51:33,660
Then for the car we have under logic we are going.

678
00:51:33,830 --> 00:51:36,020
Create this new file in it.

679
00:51:36,380 --> 00:51:38,840
Dot pie schemas.

680
00:51:38,870 --> 00:51:39,980
New file.

681
00:51:40,460 --> 00:51:47,420
You need the pi on the car, hoops car, new file.

682
00:51:47,930 --> 00:51:50,570
You need that pi.

683
00:51:51,170 --> 00:51:57,250
Then finally, for the service itself, we create this new file we have init pi.

684
00:51:57,260 --> 00:52:01,820
So we have our service package, API package and core package.

685
00:52:01,970 --> 00:52:08,060
Now remember we have this emotion detector or matter layer which we had to define.

686
00:52:08,090 --> 00:52:10,700
We are going to get that defined in the logic.

687
00:52:10,700 --> 00:52:13,730
So on the logic, we're going to create a new file.

688
00:52:13,730 --> 00:52:18,830
We'll call this Onyx in France PI There we go.

689
00:52:18,830 --> 00:52:21,710
And then here will simply create this method.

690
00:52:22,880 --> 00:52:30,050
Emotions detector, emotion detector, which takes in the image array.

691
00:52:30,950 --> 00:52:36,800
And then we're going to follow up this simple steps we had seen already from the lab notebook.

692
00:52:36,800 --> 00:52:41,000
We start by the import, then we specify the providers.

693
00:52:41,000 --> 00:52:42,560
Then there we go.

694
00:52:42,560 --> 00:52:49,100
We have the quantized model, the image path, the test image which is created.

695
00:52:49,100 --> 00:52:53,810
And then now we finally run to obtain our predictions.

696
00:52:53,810 --> 00:52:58,310
So let's just copy all this out and then put out in the code.

697
00:52:58,550 --> 00:52:59,570
There we go.

698
00:52:59,570 --> 00:53:01,010
Let's pass it out here.

699
00:53:02,690 --> 00:53:03,650
Back here.

700
00:53:06,250 --> 00:53:07,450
There we go.

701
00:53:07,570 --> 00:53:10,360
And then finally, we have this predictions.

702
00:53:10,690 --> 00:53:12,310
So that's it.

703
00:53:12,640 --> 00:53:21,940
Now let's make sure we import on X time, C on x, import on x, runtime on X run time.

704
00:53:21,940 --> 00:53:25,270
And then also we'll need to import OpenCV.

705
00:53:25,390 --> 00:53:27,920
And what do we need again, we need non py.

706
00:53:27,940 --> 00:53:31,570
So import non py as MP.

707
00:53:32,650 --> 00:53:33,300
There we go.

708
00:53:33,310 --> 00:53:38,050
So we we import on x runtime import opencv and then non py.

709
00:53:38,080 --> 00:53:42,670
Now this is our method which we are about to define.

710
00:53:43,660 --> 00:53:46,060
Send this this way.

711
00:53:46,090 --> 00:53:47,190
There we go.

712
00:53:47,200 --> 00:53:49,420
And then now we're going to return this output.

713
00:53:49,420 --> 00:53:52,160
Actually it's practically this year.

714
00:53:52,180 --> 00:53:55,330
So let's copy this from here.

715
00:53:56,890 --> 00:54:00,700
Let's copy this from here and then paste out here.

716
00:54:01,060 --> 00:54:02,230
Well, so that's it.

717
00:54:02,230 --> 00:54:03,530
We have this.

718
00:54:03,550 --> 00:54:05,470
Now let's modify.

719
00:54:05,470 --> 00:54:08,310
Remember, what we're getting here will be the image re.

720
00:54:08,320 --> 00:54:14,480
And so we no longer need to specify this image path.

721
00:54:14,500 --> 00:54:15,990
Let's take all this off.

722
00:54:16,000 --> 00:54:17,260
Take this off.

723
00:54:17,260 --> 00:54:22,870
And then from here would have this quantized model.

724
00:54:23,350 --> 00:54:24,040
There we go.

725
00:54:24,040 --> 00:54:25,390
We have our quantized model.

726
00:54:25,390 --> 00:54:27,760
We've already specified the the providers.

727
00:54:27,760 --> 00:54:31,940
So this are quantized model for our test image.

728
00:54:31,960 --> 00:54:36,460
You see, we're going to take this image array and then resize it.

729
00:54:36,460 --> 00:54:39,490
So let's take this off from here.

730
00:54:39,490 --> 00:54:41,560
We have 256.

731
00:54:41,560 --> 00:54:48,460
By 256, we take all this off and there we go.

732
00:54:49,180 --> 00:54:52,330
Now import this on this runtime as our TT.

733
00:54:52,330 --> 00:54:53,740
So there should be fine.

734
00:54:53,740 --> 00:54:59,380
Now, that said, let's get in here and see what we have.

735
00:55:00,370 --> 00:55:01,880
We print this out.

736
00:55:02,000 --> 00:55:04,120
Let's well, let's just let that for now.

737
00:55:04,120 --> 00:55:07,840
And then we see we have our predictions as we know already.

738
00:55:07,840 --> 00:55:10,030
And then from here we have the output.

739
00:55:10,150 --> 00:55:18,460
Now, one thing which is left out is this this file actually we had to get or put this file in this

740
00:55:18,460 --> 00:55:19,450
right position.

741
00:55:19,960 --> 00:55:25,390
We've copied this on this file here, which we trained already.

742
00:55:25,390 --> 00:55:33,700
And then let's update this then once is fine, let's get back to our detect that py and make sure that

743
00:55:33,700 --> 00:55:36,820
we import this emotion's detector.

744
00:55:36,820 --> 00:55:50,440
So here we have from service dot core, dot logic, dot onex in France we're going to import emotions

745
00:55:50,440 --> 00:55:51,220
detector.

746
00:55:51,220 --> 00:55:52,620
So that should be fine.

747
00:55:52,630 --> 00:56:00,790
We have now import this imported that's check that out and then let's let's save this get back to our

748
00:56:00,800 --> 00:56:02,050
own inference.

749
00:56:02,050 --> 00:56:12,850
And then now at this point, we're going to say if if NPR marks unexpressed, which is this year, let's

750
00:56:12,850 --> 00:56:25,720
copy this and paste out your if this is equal zero, then what we'll do is we're going to have the emotion

751
00:56:25,720 --> 00:56:26,470
here.

752
00:56:26,620 --> 00:56:28,000
Let's have emotion

753
00:56:30,760 --> 00:56:31,180
set.

754
00:56:31,180 --> 00:56:35,310
So angry, happy or sad.

755
00:56:35,320 --> 00:56:42,520
So here we have emotion equal, angry here that's angry.

756
00:56:42,520 --> 00:56:46,780
And then let's copy this.

757
00:56:46,780 --> 00:56:50,620
Well, let's say let's have this here.

758
00:56:51,160 --> 00:56:54,580
Let's copy this year piece out here.

759
00:56:55,150 --> 00:56:59,200
Now, leaf leaf.

760
00:56:59,200 --> 00:57:00,790
This is equal one.

761
00:57:01,810 --> 00:57:12,790
Then this is happy and else the emotion is sad.

762
00:57:13,600 --> 00:57:14,650
Okay, so that's it.

763
00:57:14,650 --> 00:57:17,020
We have angry, Happy or sad?

764
00:57:17,020 --> 00:57:17,620
And then what?

765
00:57:17,620 --> 00:57:24,970
We're going to return your would be this dictionary where we have emotion and then the output will be

766
00:57:24,970 --> 00:57:26,830
simply the emotion.

767
00:57:26,830 --> 00:57:28,090
So that's it.

768
00:57:28,210 --> 00:57:29,770
Let's take this off now.

769
00:57:29,800 --> 00:57:30,640
There we go.

770
00:57:30,640 --> 00:57:32,170
We have that set.

771
00:57:32,170 --> 00:57:37,210
And when we get back to the detector pie, you see that this fine, we're going to pass the image and

772
00:57:37,210 --> 00:57:40,020
we're going to get the corresponding emotion.

773
00:57:40,030 --> 00:57:41,500
Now we have all this set.

774
00:57:41,500 --> 00:57:47,110
What we could simply do is run our server before launching the server.

775
00:57:47,110 --> 00:57:49,870
Let's add this root endpoint right here.

776
00:57:50,200 --> 00:57:54,160
And then now let's do let's launch the server.

777
00:57:54,160 --> 00:57:56,770
You've called service main app Reload.

778
00:57:57,600 --> 00:58:04,950
So that's what we have we get in this error cannot import name detect, detect route from service API

779
00:58:04,950 --> 00:58:06,170
endpoints detect.

780
00:58:06,180 --> 00:58:11,910
Well what we'll do now is we're going to create this new folder and we'll well, this shouldn't be in

781
00:58:11,910 --> 00:58:12,290
here.

782
00:58:12,300 --> 00:58:13,560
This is going to be here.

783
00:58:13,560 --> 00:58:25,350
So in neural projects we'll create this new folder, new folder which will call emotion detection.

784
00:58:25,350 --> 00:58:30,780
And then now we'll copy the service in our emotional detection folder.

785
00:58:31,650 --> 00:58:32,700
Let's move that.

786
00:58:32,700 --> 00:58:33,390
There we go.

787
00:58:33,390 --> 00:58:36,330
So now we have an emotional detection.

788
00:58:36,330 --> 00:58:37,860
We have the service.

789
00:58:38,670 --> 00:58:40,860
We're going to relaunch the server again.

790
00:58:41,130 --> 00:58:43,240
You see, we still get this error.

791
00:58:43,260 --> 00:58:48,000
Now let's get into this detect file.

792
00:58:48,510 --> 00:58:51,180
You see here we have this.

793
00:58:51,180 --> 00:58:55,530
You see it's actually email, router and now detect router.

794
00:58:55,530 --> 00:59:04,980
So if you get your we should have IMO router there we go e mail router and here we have IMO router.

795
00:59:05,070 --> 00:59:05,640
Okay.

796
00:59:05,640 --> 00:59:11,130
So we load that we cannot import name test router.

797
00:59:11,280 --> 00:59:13,620
Well let's get into tests.

798
00:59:14,730 --> 00:59:17,400
We have test router right here.

799
00:59:17,670 --> 00:59:19,020
This should be fine.

800
00:59:19,680 --> 00:59:23,640
Now that we're getting this error, let's make sure we have this here.

801
00:59:23,640 --> 00:59:30,420
So we we created this main router which is an object of API router class.

802
00:59:30,420 --> 00:59:34,470
We save that and then this should be fine this time around.

803
00:59:34,470 --> 00:59:36,570
So application startup complete.

804
00:59:37,890 --> 00:59:39,150
Get into the local host.

805
00:59:39,150 --> 00:59:42,060
You see we get what we expect we have Hello world.

806
00:59:42,060 --> 00:59:48,690
So this is what we get when we pass this in our browser.

807
00:59:49,080 --> 00:59:51,150
We now open up the swagger UI.

808
00:59:51,180 --> 00:59:57,750
You see, we have this detect here as expected, we have testing and then we have the root.

809
00:59:57,750 --> 00:59:58,980
Okay, so that's it.

810
59:58.980 --> 1:00:03.510
Let's open up this detect and you see, we could try this out.

811
1:00:03.510 --> 1:00:07.620
Then we could pick out a file which we could actually test.

812
1:00:08.370 --> 1:00:10.520
So let's go ahead and test with this file.

813
1:00:10.530 --> 1:00:13.860
Let's open this up and now let's execute.

814
1:00:13.860 --> 1:00:18.360
So let's check out the output, fail to execute this.

815
1:00:18.360 --> 1:00:20.910
And so that's what we get in.

816
1:00:20.910 --> 1:00:23.610
We get in an error.

817
1:00:24.030 --> 1:00:25.440
Let's get back here.

818
1:00:27.600 --> 1:00:33.210
Once you could see the fact that this request, you are error right here isn't what we expect.

819
1:00:33.210 --> 1:00:35.490
We expect to have a slash before the detect.

820
1:00:35.490 --> 1:00:39.630
So let's get back to the code and then modify this.

821
1:00:39.630 --> 1:00:44.010
So take this up from here, save this and that should be fine.

822
1:00:44.010 --> 1:00:46.440
So we should have the same with test.

823
1:00:47.040 --> 1:00:50.250
Save that and you're okay.

824
1:00:50.250 --> 1:00:51.120
So that's fine.

825
1:00:51.120 --> 1:00:55.170
So let's now get back and then test this out.

826
1:00:55.170 --> 1:00:57.780
Let's refresh.

827
1:00:58.290 --> 1:00:59.160
There we go.

828
1:00:59.160 --> 1:01:00.300
We have the text.

829
1:01:00.300 --> 1:01:02.010
We want to try this out.

830
1:01:02.040 --> 1:01:03.630
We want to choose a file.

831
1:01:04.230 --> 1:01:10.170
Let's go ahead and pick a file, this file and then let's execute.

832
1:01:10.800 --> 1:01:11.340
Okay.

833
1:01:11.340 --> 1:01:13.500
So let's check this out.

834
1:01:13.500 --> 1:01:16.830
We get an internal server error.

835
1:01:17.010 --> 1:01:21.150
Let's get back here and try to see where we have the problem.

836
1:01:21.840 --> 1:01:25.520
So we've been told that this file doesn't exist.

837
1:01:25.530 --> 1:01:29.880
What we'll do is we'll make sure that we put out the right path.

838
1:01:30.030 --> 1:01:33.300
Now we have service and there we go.

839
1:01:33.300 --> 1:01:39.150
So we have this efficient, quantized Onix model which is in our service directory.

840
1:01:39.150 --> 1:01:41.270
You could find service right here.

841
1:01:41.280 --> 1:01:42.750
So that is it.

842
1:01:42.750 --> 1:01:45.870
Let's save this.

843
1:01:45.870 --> 1:01:51.570
I saved this and then let's go ahead and retest this.

844
1:01:51.570 --> 1:01:59.040
So reset console, try out and then let's retest this once more.

845
1:01:59.730 --> 1:02:01.050
Execute.

846
1:02:01.110 --> 1:02:03.030
Seems like it's going to work this time around.

847
1:02:03.390 --> 1:02:04.110
Let's go ahead.

848
1:02:04.110 --> 1:02:05.370
We still have an error.

849
1:02:05.400 --> 1:02:09.020
Now let's get back to the code and try to understand why we have this arrow.

850
1:02:09.030 --> 1:02:17.720
We told invalid rank for inputs input got three expected for Please fix either the inputs or the model.

851
1:02:17.730 --> 1:02:19.610
So this is what we are getting here.

852
1:02:19.620 --> 1:02:24.270
Now let's get back again to our code.

853
1:02:24.270 --> 1:02:29.340
We thought that what the model gets isn't what is expected.

854
1:02:29.340 --> 1:02:40.770
So let's print out the shape of this image array right here, this print out shape, image array, shape.

855
1:02:41.010 --> 1:02:52.560
And let's save this and then try out run this again, Research council try out, and then we test this

856
1:02:52.560 --> 1:02:53.400
once more.

857
1:02:53.430 --> 1:02:54.990
Just execute that.

858
1:02:55.080 --> 1:02:57.090
And obviously we.

859
1:02:57.230 --> 1:02:59.010
Have the internal several error.

860
1:02:59.060 --> 1:03:02.930
But now we want to see exactly what gets into the model.

861
1:03:02.930 --> 1:03:09.830
So let's scroll and we should get the output.

862
1:03:09.830 --> 1:03:11.920
Okay, so this is the output we get.

863
1:03:11.930 --> 1:03:20.030
Now you notice that here we have this image which is of shape 90 by 90, but what we would have expected

864
1:03:20.030 --> 1:03:22.610
here should have been 90 by 90 by three.

865
1:03:22.610 --> 1:03:28.000
So it's a grayscale image when we expect an RGV image.

866
1:03:28.010 --> 1:03:38.090
Now what we'll do is we'll say here if the shape is grayscale or rather if the image is grayscale,

867
1:03:38.090 --> 1:03:41.600
then we are going to make sure we modify that.

868
1:03:41.600 --> 1:03:53.240
So if the length of image array shape is equal to, then what we'll do is we'll ensure that we convert

869
1:03:53.240 --> 1:03:55.790
this from gray to RGV.

870
1:03:55.790 --> 1:04:04.930
So let's do this, scroll this way, let's do this, and then let's print what we have from here.

871
1:04:04.940 --> 1:04:08.660
So copy this and then paste out here.

872
1:04:08.690 --> 1:04:11.480
Okay, So there we go.

873
1:04:11.480 --> 1:04:13.010
We're going to paste this again.

874
1:04:13.670 --> 1:04:20.240
So we see what we get after we convert the gray to RGV.

875
1:04:20.240 --> 1:04:21.500
So that's it.

876
1:04:21.500 --> 1:04:23.720
Let's save this again.

877
1:04:24.590 --> 1:04:26.270
Let's save this.

878
1:04:26.270 --> 1:04:27.440
This should be fine.

879
1:04:27.440 --> 1:04:30.410
So we save that, then we get back here.

880
1:04:30.710 --> 1:04:40.520
Okay, so let's test this again out, reset console, try out, pick the image, execute.

881
1:04:40.520 --> 1:04:43.040
And there we go.

882
1:04:43.040 --> 1:04:45.890
You see, we have the emotion happy right here.

883
1:04:45.890 --> 1:04:49.160
So we have the expected response.

884
1:04:49.160 --> 1:04:51.830
So we have here emotion happy.

885
1:04:51.830 --> 1:04:54.440
Now let's get back here and see.

886
1:04:54.440 --> 1:04:56.420
You see we have 90 by 90.

887
1:04:56.420 --> 1:04:59.180
Now it's 90 by 90 by three after the conversion.

888
1:04:59.180 --> 1:05:04.730
And then we have after resize, we now pass into the model and we have the expected output.

889
1:05:04.730 --> 1:05:06.080
So that's it.

890
1:05:06.080 --> 1:05:10.190
Let's take this off and that's it.

891
1:05:10.550 --> 1:05:12.110
So there we go.

892
1:05:12.110 --> 1:05:17.090
We've just tested our API and we see that it works just fine.

893
1:05:17.480 --> 1:05:26.600
Now we could take off whatever prints we have here, take this off, take this off, and there we go.

894
1:05:27.650 --> 1:05:34.580
We then move on to our schemas where right here will create this input of and this output of p file.

895
1:05:34.580 --> 1:05:38.870
Now, in our output of P file, we'll start by importing base model.

896
1:05:38.870 --> 1:05:45.980
Then we'll create this API output class which inherits from base model, and then we'll specify what

897
1:05:45.980 --> 1:05:47.810
we want our output to look like.

898
1:05:47.810 --> 1:05:53.390
Now, given that we've seen already from here, our output is this string emotion.

899
1:05:53.390 --> 1:05:59.810
So let's get back to the code and see that this is exactly what we want to have.

900
1:05:59.810 --> 1:06:05.540
So let's save this or let's, let's say we put this as an int and save this.

901
1:06:05.540 --> 1:06:07.700
Then let's go ahead and test.

902
1:06:07.700 --> 1:06:14.900
So let's well, before doing any testing, we have to make sure we integrate this with the detector.

903
1:06:14.900 --> 1:06:19.160
PI So let's get into this here.

904
1:06:19.310 --> 1:06:21.920
Endpoints detect pi.

905
1:06:23.390 --> 1:06:28.550
Well you have this response model here, but normally this isn't there.

906
1:06:28.550 --> 1:06:30.380
So before we did, we didn't have this.

907
1:06:30.380 --> 1:06:36.920
So we have we should have this now you see response model and then specify API output.

908
1:06:36.920 --> 1:06:43.520
So you are controlling what or how this output here should look like.

909
1:06:43.520 --> 1:06:49.670
So now we have that make sure you you import API output from here and that should be it.

910
1:06:49.670 --> 1:06:53.570
So let's save now again and then let's test this out.

911
1:06:53.570 --> 1:06:56.870
So we execute and then we check out the response.

912
1:06:56.870 --> 1:07:00.950
You see, we get a response internal server arrow, which makes sense.

913
1:07:00.980 --> 1:07:03.290
Value is not a valid integer.

914
1:07:03.290 --> 1:07:08.960
So you see that we are able to tell our API what we want our output to look like.

915
1:07:08.960 --> 1:07:12.260
Now let's get back to our schemas.

916
1:07:12.380 --> 1:07:17.150
Let's take down here output and then modify this and say we want to string.

917
1:07:17.150 --> 1:07:21.260
So let's save that now and then go ahead and retest this.

918
1:07:21.260 --> 1:07:23.390
So there we go.

919
1:07:24.710 --> 1:07:28.160
We execute and we check all the response.

920
1:07:28.160 --> 1:07:30.980
You see now we have exactly what we expect.

921
1:07:30.980 --> 1:07:33.420
So that's the role of the schemas.

922
1:07:33.440 --> 1:07:39.410
Now we are going to do the same for the inputs, just we just do only for the output.

923
1:07:39.410 --> 1:07:42.470
And then from here we'll move on to the next part.

924
1:07:43.280 --> 1:07:47.690
We'll then go ahead to measure our the time taken by the model to produce the output.

925
1:07:47.690 --> 1:07:57.020
So we get into this Onix inference of pi and then just before this well let's say at this level once.

926
1:07:57.350 --> 1:07:58.610
Obtain our image.

927
1:07:58.610 --> 1:08:07.580
We're going to have time in its time it needs, which is going to be equal time, the time so we get

928
1:08:07.580 --> 1:08:08.300
a time.

929
1:08:08.300 --> 1:08:11.570
This initial time, let's impart time.

930
1:08:11.930 --> 1:08:12.290
Right?

931
1:08:12.290 --> 1:08:14.390
Your import time.

932
1:08:14.390 --> 1:08:15.320
There we go.

933
1:08:15.320 --> 1:08:22.010
And then once we import time and we have set this time it needs we want to get the time elapsed.

934
1:08:22.010 --> 1:08:27.200
So after we've gotten a prediction, we want to get a time elapsed.

935
1:08:27.260 --> 1:08:35.210
You have time elapsed, which is simply the current time minus that initial time.

936
1:08:35.480 --> 1:08:42.200
And then for the outputs, we want to have this emotion and also want to get the time elapsed.

937
1:08:42.200 --> 1:08:47.480
So let's have this well, let's return emotion.

938
1:08:48.350 --> 1:08:56.960
And then we also want to return this time elapsed, which is simply our time elapsed.

939
1:08:56.960 --> 1:08:58.460
We'll convert this into a string.

940
1:08:58.460 --> 1:08:59.810
So we have that.

941
1:09:00.110 --> 1:09:02.150
Now let's save this.

942
1:09:02.150 --> 1:09:05.750
Well, this should be time elapsed, let's save this.

943
1:09:05.750 --> 1:09:08.480
And then now let's go ahead and retest.

944
1:09:08.660 --> 1:09:14.390
So right here we're going to execute and see what we get.

945
1:09:14.420 --> 1:09:18.770
We get in this output, but we are not getting this here.

946
1:09:18.770 --> 1:09:22.610
So let's let's reduce this and save this again.

947
1:09:22.640 --> 1:09:23.750
Let's get back here.

948
1:09:24.320 --> 1:09:26.090
Let's save this again.

949
1:09:28.220 --> 1:09:29.510
We run this.

950
1:09:29.510 --> 1:09:30.560
Execute.

951
1:09:32.350 --> 1:09:33.540
There we go.

952
1:09:33.550 --> 1:09:34.480
We're still getting this.

953
1:09:34.480 --> 1:09:36.910
We doubt the time elapsed.

954
1:09:38.050 --> 1:09:43.180
Well, what we'll do now is we'll get back to our schemas to get back to the output and then include

955
1:09:43.180 --> 1:09:46.270
this time elapsed and it's a string.

956
1:09:46.270 --> 1:09:49.750
So let's save that and then check back here.

957
1:09:50.830 --> 1:09:52.960
Um, there we go.

958
1:09:52.960 --> 1:09:55.120
We are going to execute.

959
1:09:56.160 --> 1:09:58.630
And we should get this response.

960
1:09:58.720 --> 1:10:02.670
You see, it takes about one second to produce our output.

961
1:10:03.480 --> 1:10:07.430
Now that we've seen that, it takes us about a second to produce the output.

962
1:10:07.440 --> 1:10:11.580
What if we look for means to reduce this time?

963
1:10:11.580 --> 1:10:13.860
So let's get back into our code.

964
1:10:13.860 --> 1:10:21.420
You see, we have the provider specified, we have the model from here, and then we pre process the

965
1:10:21.420 --> 1:10:27.210
image and then pass the model, pass this input into the model, and then we obtain the output, which

966
1:10:27.210 --> 1:10:34.950
is this Now this model loading process here, we don't know exactly how much time it takes.

967
1:10:34.950 --> 1:10:39.300
So what we'll do is we are going to include this time elapsed loading.

968
1:10:39.300 --> 1:10:45.270
So we have time elapsed as the total time elapsed for loading and running.

969
1:10:45.300 --> 1:10:50.760
Now, I want to also get the time elapsed for just loading the model like this.

970
1:10:50.760 --> 1:11:01.170
So what we'll do is now we're going to include this in the output we have time elapsed loading and then

971
1:11:01.170 --> 1:11:04.200
we have time elapsed loading.

972
1:11:04.200 --> 1:11:04.810
Okay.

973
1:11:04.860 --> 1:11:06.660
So we'll include this in the output.

974
1:11:06.660 --> 1:11:12.450
Let's see if that even output of P, we have this already, so we have time elapsed and tab elapsed

975
1:11:12.480 --> 1:11:13.110
loading.

976
1:11:13.140 --> 1:11:16.410
Let's save this and then get back here.

977
1:11:16.890 --> 1:11:21.390
We execute and let's check out the output.

978
1:11:21.390 --> 1:11:21.900
Okay.

979
1:11:21.900 --> 1:11:28.950
So you see here we have about 1.1 second total time, but the time for loading is 0.72.

980
1:11:28.950 --> 1:11:37.860
So this tells us clearly that a huge portion of this time taken to produce the output is wasted on the

981
1:11:38.280 --> 1:11:39.110
model loading.

982
1:11:39.120 --> 1:11:45.870
See, this is about 66% of the time is for model loading, even though this is one, this is 0.66.

983
1:11:45.870 --> 1:11:52.820
So let's see how to load this model once we launch our API.

984
1:11:52.830 --> 1:12:01.320
So instead of loading the model each and every time we make an API call, what if we take this off from

985
1:12:01.320 --> 1:12:01.890
here?

986
1:12:01.890 --> 1:12:09.840
So instead of having this year, let's take this off, we are going to put this in our mean p y mean

987
1:12:09.840 --> 1:12:15.000
that pie year, we will have the min, we have our model loading.

988
1:12:15.000 --> 1:12:22.380
So import import on x runtime as our T, there we go.

989
1:12:22.380 --> 1:12:26.490
Then year now we're going to load our model.

990
1:12:27.840 --> 1:12:29.880
So we have our model now loaded year.

991
1:12:29.880 --> 1:12:35.310
We'll save that and then get it back right year and then say, okay, every time I want to make use

992
1:12:35.310 --> 1:12:41.640
of this, we're going to have this year we're going to import, import.

993
1:12:44.050 --> 1:12:49.810
We impart service doesn't mean as se.

994
1:12:49.810 --> 1:12:58.090
So we have that service with Manus and then you will have se dot model quantized.

995
1:12:58.090 --> 1:13:02.310
So we taken this from our main pie from year.

996
1:13:02.320 --> 1:13:03.790
So that is it.

997
1:13:03.790 --> 1:13:05.590
We save this now.

998
1:13:05.590 --> 1:13:11.920
And we had we still we took off let's take off this time elapsed loading.

999
1:13:12.340 --> 1:13:16.120
We don't need this any longer because we we no longer load the model.

1000
1:13:16.120 --> 1:13:26.080
Well let's, let's cut this from here and compute the time it takes to reprocess this image here.

1001
1:13:26.080 --> 1:13:31.900
So time elapsed pre process took that off.

1002
1:13:32.110 --> 1:13:43.090
Then let's modify this here time elapsed pre process well let's pre process so we have now just taken

1003
1:13:43.090 --> 1:13:53.710
off pre process okay so let's get into the schemas we have output time elapsed pre process save that

1004
1:13:53.720 --> 1:13:57.550
then go ahead and run this right here.

1005
1:13:57.550 --> 1:13:59.800
So let's execute once more.

1006
1:14:02.230 --> 1:14:02.980
There we go.

1007
1:14:02.980 --> 1:14:06.070
We execute and we check out the output.

1008
1:14:06.070 --> 1:14:16.900
You can see here time elapsed now is 0.4 5 seconds, which is about half or more than half of the time

1009
1:14:16.900 --> 1:14:18.730
we were taken previously.

1010
1:14:18.730 --> 1:14:21.940
So all less than half of the time was taken previously actually.

1011
1:14:21.940 --> 1:14:25.300
So yeah we have the time elapsed for pre process is actually very small.

1012
1:14:25.300 --> 1:14:32.140
So all of this was on, most of this was for the model to compute the output.

1013
1:14:32.140 --> 1:14:34.270
So let's, let's rerun this again.

1014
1:14:35.230 --> 1:14:36.580
Let's execute that again.

1015
1:14:36.610 --> 1:14:39.450
See 0.4 9 seconds.

1016
1:14:39.460 --> 1:14:42.640
Now we've we understand how this works.

1017
1:14:42.640 --> 1:14:55.480
Let's stop this, run this again and then check out the time when we just start our server execute and

1018
1:14:55.480 --> 1:14:56.830
let's go ahead and check the time.

1019
1:14:56.830 --> 1:14:57.280
Okay?

1020
1:14:57.280 --> 1:15:00.640
So you see you have 0.41 second.

1021
1:15:00.640 --> 1:15:03.220
That's practically about 400.

1022
1:15:03.220 --> 1:15:05.380
Well, 450 milliseconds.

1023
1:15:05.380 --> 1:15:07.600
So that is the time it takes to produce the output.

1024
1:15:07.600 --> 1:15:12.790
Now, we went we left from one second to now 0.4 5 seconds.

1025
1:15:13.720 --> 1:15:18.790
One other great advantage of working with a fast API and building your APIs.

1026
1:15:18.860 --> 1:15:26.380
The fact that you could build asynchronous API is very easily and it's just by simply specifying your

1027
1:15:26.380 --> 1:15:28.480
this async sync keyword.

1028
1:15:28.480 --> 1:15:34.960
So here we have async save that test your async.

1029
1:15:35.530 --> 1:15:36.760
There we go.

1030
1:15:36.760 --> 1:15:43.780
And the main we have async and that should be it.

1031
1:15:43.780 --> 1:15:49.720
So that's all you need to do to make your code asynchronous.

1032
1:15:49.720 --> 1:15:56.980
And so this means that when working with fast API, if we have this task, for example, this one which

1033
1:15:56.980 --> 1:16:03.790
takes much time to be completed, what goes on is this other tasks can be run asynchronously.

1034
1:16:03.790 --> 1:16:08.080
And so while waiting for this text, for example, let's do it here.

1035
1:16:08.080 --> 1:16:12.910
While waiting for this task, we could already start working on this other task and then while waiting

1036
1:16:12.910 --> 1:16:17.070
for this other task to be completed, we could also start working on some other tasks.

1037
1:16:17.080 --> 1:16:24.670
This means that overall we have taken up this time to complete this three tasks, whereas for synchronous

1038
1:16:24.670 --> 1:16:29.410
code we have to wait for each and every tag to be completed before taking on the next one.

1039
1:16:29.410 --> 1:16:33.820
So you see the time difference between these two different methods.

1040
1:16:33.820 --> 1:16:36.340
We have your the difference.

1041
1:16:36.340 --> 1:16:37.510
We see this difference here.

1042
1:16:37.510 --> 1:16:45.580
So this is our gain in time when working with fast APIs, asynchronous manner of running code.

1043
1:16:45.580 --> 1:16:51.550
Nonetheless, it should be noted that there are some tasks which are compute bound that's actually CPU

1044
1:16:51.550 --> 1:17:00.520
bound, meaning that even if you have to run this code asynchronously, this first task will still take

1045
1:17:00.520 --> 1:17:01.720
up so much time.

1046
1:17:01.720 --> 1:17:03.580
So you would end up with something like this.

1047
1:17:03.580 --> 1:17:08.830
So there will be no real gain even though you had to run this other tasks asynchronously.

1048
1:17:08.830 --> 1:17:11.380
And this is very common in computer vision.

1049
1:17:11.380 --> 1:17:17.170
And since we're dealing with computer vision, more specifically object detection, we have to understand

1050
1:17:17.170 --> 1:17:24.310
that even though our code is running asynchronously, if at the level of this detect or P, we have

1051
1:17:24.310 --> 1:17:33.760
this call on the emotions detector and we're still waiting for that to be completed no matter that we've

1052
1:17:33.760 --> 1:17:40.480
already completed this to task right here, we'll still have to wait for this to be complete.

1053
1:17:40.480 --> 1:17:43.360
So for such CPU bound tasks like.

1054
1:17:43.440 --> 1:17:50.110
Computer vision machine learning deep learning will instead take advantage of parallelism with parallelism.

1055
1:17:50.120 --> 1:17:57.110
Instead of having one worker, one CPU worker to run all these different tasks, what we could do is

1056
1:17:57.110 --> 1:18:00.710
we could allocate, say, for example, let's say three workers.

1057
1:18:00.710 --> 1:18:09.920
So we have now three workers where each worker can focus on a given task and now we can now take advantage

1058
1:18:09.920 --> 1:18:16.780
of the fact that our code runs asynchronously and takes or makes use of parallelism.

1059
1:18:16.790 --> 1:18:25.520
Now, so far we've been using this you've can HTTP server, which is an SGI web server implementation

1060
1:18:25.520 --> 1:18:26.360
for Python.

1061
1:18:26.390 --> 1:18:33.920
SGI actually means asynchronous server gateway interface, which now serves as a minimal low level server

1062
1:18:33.920 --> 1:18:38.150
interface for and async framework like fast API.

1063
1:18:38.360 --> 1:18:45.740
Now, although Yunicorn is coming with speed, it isn't mature enough to be used in a production setting.

1064
1:18:45.770 --> 1:18:47.540
Hence we have to.

1065
1:18:47.540 --> 1:18:53.630
Or we tend to use Gunny Conn, which is a mature, fully featured server and process manager for our

1066
1:18:53.630 --> 1:18:55.190
production settings.

1067
1:18:55.610 --> 1:19:03.530
So that's why you've gone includes a unicorn worker class allowing you to run SD as GUI applications

1068
1:19:03.530 --> 1:19:10.040
with all of you've ACORN's performance benefits while also giving you nikon's fully featured process

1069
1:19:10.040 --> 1:19:10.910
management.

1070
1:19:10.910 --> 1:19:16.670
So we are now going to go ahead and PIP install Nikon YUNICORN.

1071
1:19:17.810 --> 1:19:24.050
Once we have Nikon installed, we'll go ahead and run this command which we saw already Yunicorn Service

1072
1:19:24.050 --> 1:19:26.810
main app and then we'll specify a number of workers.

1073
1:19:26.810 --> 1:19:29.390
Let's change number of workers to three, for example.

1074
1:19:29.400 --> 1:19:33.170
So let's say we want to have three workers and then I'll walk.

1075
1:19:33.170 --> 1:19:36.440
Your class is you've gone, so let's run this.

1076
1:19:36.440 --> 1:19:45.770
And as you can see, we have this happening all occurring tries started several process with a specific

1077
1:19:47.030 --> 1:19:53.020
ID, then we have started several process with another ID, another study server process with this other

1078
1:19:53.030 --> 1:19:55.960
ID indicating our three different workers.

1079
1:19:55.970 --> 1:20:06.920
Now we could go ahead here, refresh this page, try this out, choose file, then execute and see.

1080
1:20:06.920 --> 1:20:07.490
There we go.

1081
1:20:07.490 --> 1:20:11.120
So we have our output exactly as we expect.

1082
1:20:11.120 --> 1:20:12.060
So that's it.

1083
1:20:12.080 --> 1:20:14.660
Now, you could obviously reduce a number of workers.

1084
1:20:14.660 --> 1:20:22.730
If you reduce this to two, oops, reduce that to two, you would find that we have only this process

1085
1:20:22.730 --> 1:20:24.920
and this other process.

1086
1:20:25.100 --> 1:20:26.360
So that is it.

1087
1:20:27.560 --> 1:20:32.960
In the section we've just seen how to build our own API and test it out locally.

1088
1:20:32.960 --> 1:20:37.100
In the next section we are going to deploy this on the cloud.
