1
00:00:00,240 --> 00:00:02,520
In this video, we are going to be taking steps

2
00:00:02,520 --> 00:00:05,280
to ensure that our backend project is ready

3
00:00:05,280 --> 00:00:08,280
for us to start developing our endpoints.

4
00:00:08,280 --> 00:00:09,400
Let's begin.

5
00:00:09,400 --> 00:00:11,600
First, we need to head into the root of our

6
00:00:11,600 --> 00:00:13,020
backend folder.

7
00:00:13,020 --> 00:00:15,320
Here we need to create a virtual environment.

8
00:00:15,320 --> 00:00:19,800
To do that, we need to open up our terminal.

9
00:00:19,800 --> 00:00:23,760
Let's go to backend.

10
00:00:23,760 --> 00:00:25,820
And here we can type the command to create

11
00:00:25,820 --> 00:00:33,000
our virtual environment.

12
00:00:33,000 --> 00:00:35,240
Once the environment is created, as you can

13
00:00:35,240 --> 00:00:38,120
see here we have a new folder, the .vmv

14
00:00:38,120 --> 00:00:38,440
folder

15
00:00:38,440 --> 00:00:40,700
that we specified in our command.

16
00:00:40,700 --> 00:00:43,200
We can now activate this environment.

17
00:00:43,200 --> 00:00:45,320
For that, you can use the following command

18
00:00:45,320 --> 00:00:48,040
on macOS or Linux.

19
00:00:48,040 --> 00:00:50,020
say source.vmv

20
00:00:50,020 --> 00:00:54,900
bain activate.

21
00:00:54,900 --> 00:00:58,200
And now, as indicated by this prefix to our

22
00:00:58,200 --> 00:00:59,320
command prompt,

23
00:00:59,320 --> 00:01:01,360
we are now in our virtual environment.

24
00:01:01,360 --> 00:01:03,340
So we can clear all this.

25
00:01:03,340 --> 00:01:05,180
This activation command is slightly different

26
00:01:05,180 --> 00:01:06,120
on Windows.

27
00:01:06,120 --> 00:01:08,120
You don't need to begin with source.

28
00:01:08,120 --> 00:01:10,960
And you simply replace the bain folder with

29
00:01:10,960 --> 00:01:12,420
a scripts folder.

30
00:01:12,420 --> 00:01:15,040
And just as I said earlier, once this virtual

31
00:01:15,040 --> 00:01:15,640
environment is

32
00:01:15,640 --> 00:01:18,360
activated, you can see it as the prefix to

33
00:01:18,360 --> 00:01:19,780
your command prompt.

34
00:01:19,780 --> 00:01:22,520
Next we'll need to install our dependencies.

35
00:01:22,520 --> 00:01:24,680
These are the dependencies that are listed

36
00:01:24,680 --> 00:01:26,620
in our requirements.txt

37
00:01:26,620 --> 00:01:27,520
file.

38
00:01:27,520 --> 00:01:29,440
We are going to be installing all of them

39
00:01:29,440 --> 00:01:31,800
with one single command.

40
00:01:31,800 --> 00:01:34,760
So let's head over to our command prompt and

41
00:01:34,760 --> 00:01:36,600
at the root of our backend folder, we'll

42
00:01:36,600 --> 00:01:42,820
We'll say pip install –r then we target our

43
00:01:42,820 --> 00:01:44,200
requirements.txt

44
00:01:44,200 --> 00:01:46,180
file.

45
00:01:46,180 --> 00:01:48,020
This will begin the installation and in no

46
00:01:48,020 --> 00:01:52,340
time, we should have all listed packages installed.

47
00:01:52,340 --> 00:01:53,400
Awesome.

48
00:01:53,400 --> 00:01:56,020
Now, our installation is done.

49
00:01:56,020 --> 00:01:58,280
Let's just click out of our terminal for now.

50
00:01:58,280 --> 00:02:00,560
The next thing we have to do is to have our

51
00:02:00,560 --> 00:02:03,820
Gemini API key in place and accessible to

52
00:02:03,820 --> 00:02:05,480
our backend application.

53
00:02:05,480 --> 00:02:08,699
API key is a very sensitive part of your application

54
00:02:08,699 --> 00:02:12,060
and you should always make sure it is securely

55
00:02:12,060 --> 00:02:14,220
stored and accessed.

56
00:02:14,220 --> 00:02:16,560
This is why we installed the python.env

57
00:02:16,560 --> 00:02:19,340
package in order to store and access our API

58
00:02:19,340 --> 00:02:20,180
key from

59
00:02:20,180 --> 00:02:21,880
an environment variable.

60
00:02:21,880 --> 00:02:24,320
So let's create a .env

61
00:02:24,320 --> 00:02:27,560
file at the root of our backend folder.

62
00:02:27,560 --> 00:02:29,840
Inside this folder, let us add the environment

63
00:02:29,840 --> 00:02:35,640
variable GeminiAPIKey.

64
00:02:35,640 --> 00:02:37,580
We'll then set the value of this variable

65
00:02:37,580 --> 00:02:40,900
to the API key we just created in Google's

66
00:02:40,900 --> 00:02:43,860
AI Studio.

67
00:02:43,860 --> 00:02:45,080
Perfect.

68
00:02:45,080 --> 00:02:47,420
Now save this file and make sure that it is

69
00:02:47,420 --> 00:02:49,580
ignored in the .gitignore

70
00:02:49,580 --> 00:02:51,040
file.

71
00:02:51,040 --> 00:02:53,620
We have already ignored it in the .gitignore

72
00:02:53,620 --> 00:02:56,500
file that comes with this seed project.

73
00:02:56,500 --> 00:02:58,380
So you're good on that front, but in your

74
00:02:58,380 --> 00:03:00,780
other projects, always make sure that you

75
00:03:00,780 --> 00:03:02,240
never push this .env

76
00:03:02,240 --> 00:03:04,980
file to a remote repository.

77
00:03:04,980 --> 00:03:07,680
Our next task is to create the entry point

78
00:03:07,680 --> 00:03:10,460
Python file to this backend application.

79
00:03:10,460 --> 00:03:12,480
We are going to be setting up this file so

80
00:03:12,480 --> 00:03:14,860
that in subsequent videos, we can start writing

81
00:03:14,860 --> 00:03:17,160
our endpoints right away.

82
00:03:17,160 --> 00:03:20,660
So let's create the file main.py

83
00:03:20,660 --> 00:03:25,860
at the root of the backend folder.

84
00:03:25,860 --> 00:03:28,420
Now inside this file, first, let us take care

85
00:03:28,420 --> 00:03:30,100
of our imports.

86
00:03:30,100 --> 00:03:33,620
We'll be starting with our FastAPI dependencies.

87
00:03:33,620 --> 00:03:41,000
First, let's say from FastAPI, import FastAPI

88
00:03:41,000 --> 00:03:51,140
file, upload file, and HTTP exception.

89
00:03:51,140 --> 00:03:52,700
We'll also be adding some middleware for calls

90
00:03:52,700 --> 00:04:00,920
so I'm going to say from first api.middleware.calls

91
00:04:00,920 --> 00:04:06,060
From there, we import the calls middleware

92
00:04:06,060 --> 00:04:08,220
model.

93
00:04:08,220 --> 00:04:10,820
I think we can put this aside for now.

94
00:04:10,820 --> 00:04:13,680
Next, let us import the dependencies for working

95
00:04:13,680 --> 00:04:15,700
with our Gemini API.

96
00:04:15,700 --> 00:04:22,480
to say from Google, import genai, and from

97
00:04:22,480 --> 00:04:27,140
Google.genai,

98
00:04:27,140 --> 00:04:29,320
import types.

99
00:04:29,320 --> 00:04:32,220
Here we have imported our Gemini Python SDK,

100
00:04:32,220 --> 00:04:33,040
and also

101
00:04:33,040 --> 00:04:34,780
some types on the API that we're going

102
00:04:34,780 --> 00:04:36,860
to be using along the course of building this

103
00:04:36,860 --> 00:04:38,460
project.

104
00:04:38,460 --> 00:04:40,740
Finally, let us get in some helpful system

105
00:04:40,740 --> 00:04:41,700
dependencies

106
00:04:41,700 --> 00:04:43,740
for loading our environment variables

107
00:04:43,740 --> 00:04:45,660
working with files.

108
00:04:45,660 --> 00:04:50,300
Come down here and say from .env,

109
00:04:50,300 --> 00:04:53,120
let us import the

110
00:04:53,120 --> 00:04:53,720
load.env

111
00:04:53,720 --> 00:04:59,720
function and also import the OS package.

112
00:04:59,720 --> 00:05:01,320
And while we're on that, let us

113
00:05:01,320 --> 00:05:02,960
call our load.env

114
00:05:02,960 --> 00:05:05,440
method to load all our environment variables

115
00:05:05,440 --> 00:05:06,700
from our .env

116
00:05:06,700 --> 00:05:07,540
file.

117
00:05:07,540 --> 00:05:10,600
Go down here and say load.env

118
00:05:10,600 --> 00:05:15,480
and call it.

119
00:05:15,480 --> 00:05:16,620
Perfect.

120
00:05:16,620 --> 00:05:17,360
The next thing we're

121
00:05:17,360 --> 00:05:19,320
going to do is to get a reference to our Gemini

122
00:05:19,320 --> 00:05:21,720
API key so that we can use it in

123
00:05:21,720 --> 00:05:23,920
our Gemini API requests.

124
00:05:23,920 --> 00:05:26,060
So let's do that.

125
00:05:26,060 --> 00:05:30,160
I'm going to go down a little bit,

126
00:05:30,160 --> 00:05:36,920
Here, I'm going to say Gemini API key and

127
00:05:36,920 --> 00:05:40,600
let us get it from the environment.

128
00:05:40,600 --> 00:05:43,060
After that, let us check if we have it.

129
00:05:43,060 --> 00:05:46,560
So if not Gemini API key, let us simply raise

130
00:05:46,560 --> 00:05:51,260
a value error.

131
00:05:51,260 --> 00:05:55,400
And here, we'll say Gemini API key not found

132
00:05:55,400 --> 00:05:57,680
in the .env

133
00:05:57,680 --> 00:06:00,060
file.

134
00:06:00,060 --> 00:06:02,960
Let us also define the Gemini model we'll

135
00:06:02,960 --> 00:06:05,360
be using.

136
00:06:05,360 --> 00:06:07,640
We're setting that using a constant.

137
00:06:07,640 --> 00:06:09,420
And for this course, we're going to be using

138
00:06:09,420 --> 00:06:12,620
Gemini 2.5

139
00:06:12,620 --> 00:06:18,340
Flash, a powerful, fast, multimodal model.

140
00:06:18,340 --> 00:06:21,740
And we'll wrap up our Gemini setup section

141
00:06:21,740 --> 00:06:25,040
by creating a new instance of our Gemini API

142
00:06:25,040 --> 00:06:27,960
client.

143
00:06:27,960 --> 00:06:30,020
Awesome.

144
00:06:30,020 --> 00:06:31,580
The next setup we're going to be doing

145
00:06:31,580 --> 00:06:33,700
is for our FastAPI server.

146
00:06:33,700 --> 00:06:36,500
So let us go ahead and start that

147
00:06:36,500 --> 00:06:40,020
by first creating a new instance of the FastAPI

148
00:06:40,020 --> 00:06:41,420
server.

149
00:06:41,420 --> 00:06:46,260
We say app equals FastAPI.

150
00:06:46,260 --> 00:06:50,320
simply passing a title for our application.

151
00:06:50,320 --> 00:06:53,800
We're going to be calling that the Travel

152
00:06:53,800 --> 00:06:58,560
Companion AVI.

153
00:06:58,560 --> 00:07:01,420
Next, let us enable some call settings for

154
00:07:01,420 --> 00:07:04,980
smooth communication with the front-end.

155
00:07:04,980 --> 00:07:16,080
down here by adding the middleware, say app.addMiddleware,

156
00:07:16,080 --> 00:07:25,000
we're passing causeMiddleware, and then impute

157
00:07:25,000 --> 00:07:29,160
our settings to allow the client to communicate

158
00:07:29,160 --> 00:07:32,740
with our server without being blocked by any

159
00:07:32,740 --> 00:07:37,740
calls rules.

160
00:07:37,740 --> 00:07:42,260
Need to put some quotes around that.

161
00:07:42,260 --> 00:07:46,320
Say allowCredentials.

162
00:07:46,320 --> 00:07:52,160
Set that to true.

163
00:07:52,160 --> 00:07:58,120
is allowMethods, we want to allowAllMethods,

164
00:07:58,120 --> 00:08:04,220
and finally, allowHeaders, and we also want

165
00:08:04,220 --> 00:08:06,200
to allowAllHeaders.

166
00:08:06,200 --> 00:08:09,060
Good.

167
00:08:09,060 --> 00:08:11,860
Now, this is for a demo application in production.

168
00:08:11,860 --> 00:08:13,180
You want to make sure that you have stricter

169
00:08:13,180 --> 00:08:16,000
rules for calls.

170
00:08:16,000 --> 00:08:18,920
But for this application, this is simply great.

171
00:08:18,920 --> 00:08:22,160
Now we can start writing our endpoints.

172
00:08:22,160 --> 00:08:24,640
So we'll be wrapping up this video by creating

173
00:08:24,640 --> 00:08:27,460
a simple base endpoint and running our backend

174
00:08:27,460 --> 00:08:30,580
application to see that it's good to go.

175
00:08:30,580 --> 00:08:33,840
So let's write a simple root endpoint that

176
00:08:33,840 --> 00:08:37,059
simply spits out a generic message.

177
00:08:37,059 --> 00:08:41,460
We'll just scroll down and here we'll say

178
00:08:41,460 --> 00:08:45,520
app.get,

179
00:08:45,520 --> 00:08:50,320
define our root endpoint and define

180
00:08:50,320 --> 00:08:56,120
the handler that is going to undo that.

181
00:08:56,120 --> 00:08:58,880
Say with root and here we can just simply

182
00:08:58,880 --> 00:09:06,360
return a message wrapped in a dictionary and

183
00:09:06,360 --> 00:09:11,380
And this will say Travel Companion API is

184
00:09:11,380 --> 00:09:13,980
running.

185
00:09:13,980 --> 00:09:15,200
Good.

186
00:09:15,200 --> 00:09:16,980
Now we have our route endpoint.

187
00:09:16,980 --> 00:09:18,960
But to run this application, we need to write

188
00:09:18,960 --> 00:09:21,200
the code that allows us to run our server

189
00:09:21,200 --> 00:09:22,260
through this file.

190
00:09:22,260 --> 00:09:23,840
So let us do that.

191
00:09:23,840 --> 00:09:27,520
We're going to be making use of UVicon.

192
00:09:27,520 --> 00:09:31,700
But first, let us make sure that we are checking

193
00:09:31,700 --> 00:09:36,280
that this file is what we are running directly

194
00:09:36,280 --> 00:09:43,280
from our terminal, say ifname="main", then

195
00:09:43,280 --> 00:09:51,040
we simply import uvconn and we can use uvconn

196
00:09:51,040 --> 00:10:03,560
to run this app and set the host to 0.0.0.0

197
00:10:03,560 --> 00:10:11,080
and our port to 8000.

198
00:10:11,080 --> 00:10:13,460
Awesome.

199
00:10:13,460 --> 00:10:15,360
Now, with this in place, we can go to our

200
00:10:15,360 --> 00:10:18,600
terminal and run our server.

201
00:10:18,600 --> 00:10:20,980
So let us pull up the terminal.

202
00:10:20,980 --> 00:10:22,540
Make sure that you are still at the root of

203
00:10:22,540 --> 00:10:24,180
the backend folder.

204
00:10:24,180 --> 00:10:28,640
And here we can simply run python main.py

205
00:10:28,640 --> 00:10:31,120
hit enter.

206
00:10:31,120 --> 00:10:33,180
And now that our server is running, let's

207
00:10:33,180 --> 00:10:36,880
simply visit our base URL in the browser.

208
00:10:36,880 --> 00:10:39,920
So here in the browser, we can simply say

209
00:10:39,920 --> 00:10:42,420
localhost.

210
00:10:42,420 --> 00:10:44,480
Our server is running at the port 8000.

211
00:10:44,480 --> 00:10:50,080
So we say colon 8000 and hit enter.

212
00:10:50,080 --> 00:10:52,620
And here you can see the message travel companion

213
00:10:52,620 --> 00:10:55,200
API is running.

214
00:10:55,200 --> 00:10:56,460
How smooth is that?

215
00:10:56,460 --> 00:10:58,600
I think we should also have a docs endpoint.

216
00:10:58,600 --> 00:11:00,980
So we say slash docs.

217
00:11:00,980 --> 00:11:03,320
And here as you can see, we have our root

218
00:11:03,320 --> 00:11:04,520
endpoint.

219
00:11:04,520 --> 00:11:05,660
Awesome.

220
00:11:05,660 --> 00:11:07,960
So we have our server running and we now have

221
00:11:07,960 --> 00:11:10,460
everything that we need to start building

222
00:11:10,460 --> 00:11:12,000
our application.

