Build An Audio AI App Transcripts
Chapter: Feature 1: Transcripts
Lecture: Starting and Monitoring Background Jobs

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well, this little echo of this dictionary was just so that we could have something to verify that the data exchange is working
0:09 and play with that for a bit. Let's get more real about it. So what we're gonna do is we're gonna bring in a view model.
0:13 Remember, I told you a lot of parts of this app are already built because I don't want you to have to juggle with like,
0:19 how do we create a new asynchronous background task system? So guess what? That's done. So I'm gonna create a view model
0:26 and it's gonna be called a start job view model. And we're gonna need to import that. And let's just go look at it real quick.
0:33 So it takes, well, what you might expect, podcast ID, episode ID, and an action, which is one of these enumerations. It also takes the FastAPI request,
0:42 or more specifically the starlet request, because the stuff underneath, there's a view model base down here
0:50 where it does things like hold the request for you and handle a couple other things. Like for example, check if somebody's logged in,
0:59 which requires to check the cookies of that new incoming request, that kind of stuff. So don't worry too much about it,
1:05 but we got to pass that along as well. And then we're just gonna store different things about this. All right, we're gonna say, look, if there's an,
1:14 I'm gonna switch on the action, say for transcribing, and then we're gonna set some values. Otherwise we'll set summarizing and so on.
1:21 And then finally, there's no asynchronous stuff we're doing here. So this is just about the data exchange. We know forms to parse asynchronously
1:29 like you do in FastAPI. So this will be a pretty simple one. And what we need to do is just pass it the information that it needs.
1:36 And we're also gonna need to pass in the requests right here and let's import that. As I said, from starlet.requests.request.
1:45 Okay, so then we'll pass requests. So then I get that order, right? Let's see requests. No, it did not. Good guess, PyCharm, guess better.
1:57 Okay, episode and action. So this view model is gonna store that data. You might wonder, well, we already have it kind of parsed here.
2:06 What are we doing? There's HTML on the other side that expects all those different variables, those self variables from that class to work with.
2:15 And that's kind of the role of the view model. So that's part of its job. And the next thing we wanna do, speaking of jobs,
2:20 is we wanna go to this thing called a background service over in services. And say, create a background job. We'll look at this in just a minute.
2:30 So what we're gonna do is pass in one of these job actions, which is the action, the podcast ID, and the episode number, which is episode ID.
2:39 I kind of would prefer to rename that thing to episode number and we're gonna have to sync that up there as well. Excellent.
2:51 So then in order for this to actually run, we'd look at this, we'll say, yes, async def. So in order for this to do anything interesting,
3:01 we have to await it. And that makes this an async function. It requires it to be an async function. And we can just print started job, job.ID.
3:13 And then we'll return nothing. And let's go ahead and return the job ID as well, just so it's in this data. (silence) Let's run it, see what happens.
3:27 All right, here we go. It turns out for some weird reason, and for unable to get that to show up, I had to call str on it,
3:36 because that's technically a object ID out of the database. And its default string representation was weird, but there we go.
3:42 So we've started a new job, it's called this. And if you look over here, you can see started job, such and such.
3:50 This theoretically is off to do something. It's not quite yet. That's going to be one of the core parts where we plug in our assembly AI magic,
3:57 but we're off to a good start. All these pieces and data are passing along. One final thing here we could do is this is going to have a job ID.
4:06 And as you can imagine, this is going to be a, we'll just let it be a string over there. And we may need a request as well.
4:14 So let's go ahead and take that. And we can go to our background service and ask, is job finished? Passing in this job ID, we'll say await.
4:27 Now, what does this want? Does it want a object ID? So we can just say bson object ID. That'll parse this across like that.
4:44 And for us to call await, we have to say a single say print the job, whatever it is, is finished. I have to need to start a new one here and then
4:59 say check status. And it printed out. Is it finished? No, it's still running.
5:16 We didn't return true or false, but it returned anything, so it just came up nothing. But here we go. The job is not yet finished.
5:22 And because it's not really running yet, that's not a surprise. But there we go. We've got our two important moving parts here.
5:29 We're going to have to plug in a little bit of more UI, but let's go ahead and work on making this, make the jobs actually run over in the system
5:40 and do their thing there. Then we'll put the UI in place, okay?


Talk Python's Mastodon Michael Kennedy's Mastodon