1
00:00:00,570 --> 00:00:07,710
And welcome back that, of course, in this section will talk about something or form a project that

2
00:00:07,710 --> 00:00:10,530
actually is quite popular and in demand.

3
00:00:10,950 --> 00:00:16,830
And that's how do we actually deploy our computer vision models and one of the many ways to deploy,

4
00:00:16,830 --> 00:00:21,960
actually, I'm going to record a whole section on production deployment later on.

5
00:00:22,560 --> 00:00:29,760
However, the easiest way right now is to create a rest API that allows you to send images to that API

6
00:00:29,760 --> 00:00:36,930
access point, and that API then returns its class or whatever you wanted to do with that image.

7
00:00:37,200 --> 00:00:42,200
You can actually turn the entire image if you were doing something like low light enhancement or neural

8
00:00:42,200 --> 00:00:47,640
old style transfer, or we can just give you an object detector overlay, or it can give you the class

9
00:00:47,640 --> 00:00:48,300
of the image.

10
00:00:48,870 --> 00:00:56,400
So let's take a look at how we can use Flask, which is a Python micro web framework for micro sites

11
00:00:57,000 --> 00:01:00,480
that we we're going to use that to create, firstly, an API.

12
00:01:00,900 --> 00:01:06,600
So let's create the API civil opening notebook 73 here, and we'll begin.

13
00:01:06,750 --> 00:01:12,750
So before I begin, actually, I just wanted to mention a bit of what flask of Flask is a web framework

14
00:01:13,080 --> 00:01:14,370
development library.

15
00:01:14,730 --> 00:01:16,170
It's a very, very good one.

16
00:01:16,170 --> 00:01:18,540
I strongly preferred the jungle.

17
00:01:18,600 --> 00:01:23,880
Even though Django is better, Django makes things a lot more complicated than it needs to be, in my

18
00:01:23,880 --> 00:01:24,300
opinion.

19
00:01:25,200 --> 00:01:27,930
And Flask is actually quite easy to get up and running.

20
00:01:28,380 --> 00:01:35,040
It's quite easy to understand how it's how it's structured, how the templates work, how everything

21
00:01:35,040 --> 00:01:36,090
kind of goes together.

22
00:01:36,510 --> 00:01:38,930
And it actually has a lot of advanced features.

23
00:01:38,940 --> 00:01:46,950
So Flask is pretty much capable of almost everything Django is, except maybe some more advanced functionality

24
00:01:46,950 --> 00:01:48,180
and management tools.

25
00:01:48,300 --> 00:01:50,840
So that's it for Flask.

26
00:01:50,850 --> 00:01:58,200
And now we're also going to need to install and grow and grow up allows us to access those local ports

27
00:01:58,200 --> 00:02:01,050
here on this call out machine on the internet.

28
00:02:01,260 --> 00:02:04,890
So which is what we're going to be doing, exposing our API?

29
00:02:05,100 --> 00:02:06,300
So let's get started.

30
00:02:06,300 --> 00:02:13,320
So clearly does my repo here with the flask up, then we install and grok here pip, install, flash

31
00:02:13,320 --> 00:02:13,920
and grok.

32
00:02:14,670 --> 00:02:21,330
Next, we need to actually download the grok system for Linux, so we don't want that file here and

33
00:02:21,330 --> 00:02:22,540
then we need to install it.

34
00:02:22,560 --> 00:02:30,930
This takes about 10 11 seconds to install and know what I need you to do is actually put a link in the

35
00:02:30,930 --> 00:02:33,680
notebook for you guys to sign up for and grok accounts.

36
00:02:33,690 --> 00:02:35,910
Can you see which gets better?

37
00:02:35,910 --> 00:02:37,410
But Google is quite quick.

38
00:02:37,920 --> 00:02:45,120
And then navigate to getting to your token and you just put exclamation mark and grok or token.

39
00:02:45,510 --> 00:02:47,190
And this saves your configuration.

40
00:02:47,400 --> 00:02:50,010
You took into a configuration file here.

41
00:02:50,430 --> 00:02:56,970
I've already done it, which is why I've run this number before and I've had my could my token here,

42
00:02:56,970 --> 00:02:58,470
so you guys can't access it.

43
00:02:59,100 --> 00:03:02,110
So in the meantime, let's just put a link here.

44
00:03:02,130 --> 00:03:04,740
This is the signup link.

45
00:03:07,420 --> 00:03:12,610
So once we do that now, we never get back into the directory and we're ready to get started here.

46
00:03:13,030 --> 00:03:17,650
So Flask is quite easy to use, so all you have to do is just import your libraries that you're going

47
00:03:17,650 --> 00:03:18,310
to be using.

48
00:03:18,310 --> 00:03:23,110
So we're using are you adjusting the torch vision for the model because we're going to be loading a

49
00:03:23,110 --> 00:03:25,630
pre-trained image that pie PyTorch model?

50
00:03:26,320 --> 00:03:30,160
We import pills and image processing processing tools.

51
00:03:30,160 --> 00:03:33,520
We import different things on flask to request that you solidify.

52
00:03:34,210 --> 00:03:37,330
And we also use flash and drop to run with ink.

53
00:03:37,870 --> 00:03:43,390
So we declared up here optical flask on the school underscore name, underscore underscore.

54
00:03:44,140 --> 00:03:47,740
Then we have run away and grow the app that we named him.

55
00:03:48,790 --> 00:03:53,980
Next, we need to load our pre-trained model, which is what we do in these three lines here, and we

56
00:03:53,980 --> 00:03:55,750
set the model to evaluation mode.

57
00:03:55,780 --> 00:04:02,260
And now we have a helper function that allows us to take an image that we are uploading from an outside

58
00:04:02,740 --> 00:04:11,350
the client and basically transform that that image to running these pre-processing functions so that

59
00:04:11,350 --> 00:04:16,270
then we can now send it to our we use it with another get prediction function.

60
00:04:16,780 --> 00:04:22,660
So that takes the image that we get in bytes, transforms the image into a tensor and then passes that

61
00:04:22,660 --> 00:04:24,010
tensor to the model.

62
00:04:24,430 --> 00:04:27,070
Then and then it returns to Class Hill.

63
00:04:27,460 --> 00:04:29,290
So that's quite simple.

64
00:04:29,410 --> 00:04:35,740
And now all you need to do in Flask Flask basically runs is to arrest API takes requests.

65
00:04:36,130 --> 00:04:42,110
So if we send a post request to it with the image and it sends it to slash predicted ABC, the local

66
00:04:42,190 --> 00:04:47,290
slash predict, you can send a file to our server.

67
00:04:48,010 --> 00:04:53,890
And once it receives a file, it executes this function here and it returns a class.

68
00:04:54,610 --> 00:04:56,130
So let's see this in action.

69
00:04:56,140 --> 00:04:57,730
So let's run this block of code.

70
00:04:57,730 --> 00:05:00,520
It takes maybe about five to 10 seconds to actually quicker.

71
00:05:01,060 --> 00:05:03,500
So it's running here on this link.

72
00:05:03,850 --> 00:05:10,400
That's so API link and you can see we just need to go to to visit the safe site because it doesn't have

73
00:05:10,400 --> 00:05:11,650
an SSL certificate.

74
00:05:12,380 --> 00:05:13,780
And this happens within.

75
00:05:13,780 --> 00:05:15,630
You have to refresh and then we go.

76
00:05:15,640 --> 00:05:20,470
So no starts running is just this is the it's not an actual webpage yet.

77
00:05:20,470 --> 00:05:21,220
It's an API.

78
00:05:21,700 --> 00:05:29,070
So now to accessto API, what we need to do, we need to open a new cloud notebook to open this one.

79
00:05:29,080 --> 00:05:29,890
That's our client.

80
00:05:34,430 --> 00:05:37,670
And let's download test image here of a bird.

81
00:05:38,840 --> 00:05:47,690
And let's get our previous and Gerlach link here pasted into their.

82
00:05:49,930 --> 00:05:54,700
Keeping this stash predict and let's see if we get to class, and there we go.

83
00:05:54,940 --> 00:05:56,770
So we've got the image here.

84
00:05:57,490 --> 00:06:01,010
So this image that you've downloaded want to visualize the image quickly?

85
00:06:01,010 --> 00:06:02,020
We can see it here.

86
00:06:02,620 --> 00:06:03,640
This cute little bird.

87
00:06:04,570 --> 00:06:09,250
So we send this picture out to the EPA and returns here.

88
00:06:09,400 --> 00:06:16,720
And so, no, you can actually post this from any machine across the world using this link, using any

89
00:06:16,810 --> 00:06:22,840
requests or any any sort of like the EPA tool interfacing tool we can do to any language you want.

90
00:06:23,380 --> 00:06:29,980
Or you can use something like postman to test these APIs and you get to know we just don't need this

91
00:06:29,980 --> 00:06:32,620
line here, but we get the class in return.

92
00:06:32,950 --> 00:06:38,590
So we get a class I.D. and the image, the class name lorikeets, a bid, by the way, in case you did

93
00:06:38,590 --> 00:06:41,440
not know, I wasn't sure it was until I googled it.

94
00:06:41,480 --> 00:06:44,020
We can actually google it to prove it.

95
00:06:45,160 --> 00:06:47,200
And you can see it's a type of parrot.

96
00:06:48,490 --> 00:06:49,180
Very pretty one.

97
00:06:49,570 --> 00:06:53,260
It's not exactly the same kind of bird, but it's very close as you can see.

98
00:06:53,290 --> 00:06:55,210
So we know our model is working well.

99
00:06:56,380 --> 00:06:58,060
So that's it for this lesson.

100
00:06:58,630 --> 00:07:06,790
In the next lesson, we're going to use Flask and Grok to create an actual website that allows you to

101
00:07:06,790 --> 00:07:12,520
access it, and you can accept some anywhere in the world and allows you to upload a picture and returns

102
00:07:12,520 --> 00:07:13,660
to class back to you.

103
00:07:14,200 --> 00:07:18,800
So let's stop there and we'll begin that flask web app section.

104
00:07:18,850 --> 00:07:20,020
No, thank you.
