 In the last section, you created a webhook locally. By now, you should understand the basics of webhooks. That is just a web server that is receiving a request that then you can, in response to, execute functions or do anything else in response to. The next step towards making this a little bit more realistic and more production-grade is to actually host the exact same webhook server on the cloud. And the easiest way to do this, in my opinion, would be to use something like a modal. Now modal is a kind of serverless Python environment that is geared towards machine learning engineers. And basically, it's like Cloud Functions, which is a Google Cloud product. But really, it's designed for Python programmers, especially machine learning engineers, who want to deploy code in a serverless way. And I won't go over modal in a lot of depth. All I will say is it's very lightweight, it's really easy to get started, and everybody has access to it. You can sign up and your first $30 of credits are free. So it should be fairly straightforward to get started. So the first thing we're going to do is we're going to take the code. Well, actually, let's look at the GitHub repo. So if you haven't cloned this repo, please go ahead and do it. It's under my GitHub username, HamilSMU. And this is 1DB modal webhook. Go ahead and clone the repo. And in this repo, you'll have all the code that I will use in this particular lesson. Now, I'm going to go ahead and open the readme in this VS Code editor so we can both see it in bigger font. So this repo is about the integration between an example integration that you can achieve with webhooks with weights and biases. And specifically, weights and biases with modal. But because it's webhooks, it's really a minimal example of gluing weights and biases to anything else. Because webhooks are very general. and biases to anything else because webhooks are very general. As we discussed before, webhooks are used by lots of different infrastructure as a way to expose a communication channel with the outside world. And this is why weights and biases has a webhook trigger. But before we get into weights and biases, we want to actually host this webhook server somewhere that weights and biases can access. And so to do that, we're going to have to put it on the cloud. So the first thing you're going to want to do is pip install modal. And I would create a virtual environment first you know using pip pipenv or my favorite is to use conda so pip install modal and then also install them all the modal client when you log into modal you will once you create an account, the next thing you're going to want to do is to create a secret. So if you recall, we had the secret random token before. So let's go ahead and click on this create secret. It'll take you to the secrets area. And when you click create new secret. And what we're going to do is we're going to go down to custom. And you can see here that you might be wondering there's a weights and biases secret here. We're not going to discuss that right now. We're going to go and create a custom secret because that's like very flexible. Okay, I'm going to take the auth token, which is here, paste it there, and then the secret random token here. Then I'm going to say next. I'm going to say the name of my secret is my random secret so just make sure you get that right for this code to work it's easy to make a mistake there it'll say secret is created um you can say like it shows you a minimal example of how to retrieve the secret. If you end up, we will look at the code soon, but you will see this inner code, this My Random Secret, and it will be put in the environment. So let's go ahead and open, this is the web server that we created in the last exercise. So we're going to have to refactor this every so slightly to be able to put it on modal. Now I'm not going to go deep into modal, but what I'll do is I'll show you a diff between the fast API server that we created before and in this. So this is just a diff. So this is just a diff. And basically what you see here is the things that are changed is the most important ones is we're importing modal, some modal utilities here like the secret, something for an image so we can build a Docker image. We are creating a Docker image that has FastAPI in it in Pydantic as well and we're using something called a stub that's just to kind of name the application and instead of this this like FastAPI decorator we're going to be using these decorators from modal. They kind of do the same thing. Basically, we have a special decorator that allows us to get the secret and this web endpoint. The rest of the code is exactly the same. Now, you don't have to get too hung up on what this code does. A lot of it you can think of it as boilerplate. Just know that this is some minimal change that allows this code to be deployed to the cloud. And instead of hard coding the environment variable here, which you know you wouldn't want to do in real life, this is actually going to be pulled from the environment, specifically this authTokenEnvironment variable, which we set in modal. So going back to, let's go back to the README, and let's preview the README so we can actually see it. Let me put it back here. There we go. So the next thing we want to do is deploy this modal webhook. So let's go ahead and do that. We're going to go here and I'll just go modal deploy. And then it will be server.py. and then it will be server.py. Okay, that was fast. Basically, because I've deployed this before, modal actually does cache things intelligently. So this has been deployed. You'll get two links. This link is actually the endpoint of your webhook. Instead of localhost, you can send payloads there. And then this is actually where you can look at the logs, so where you view the deployment. Let's click on that. You can see this has been deployed. And then we can go to logs to see logs. Now let's click into here. It's listening for logs and looks like there's no logs currently. So next thing we want to do is send a payload to that web endpoint. So what we can do is we have this URL and we can use that same curl script. Let me just remind you what's in that curl script. The curl script is just, you know, just sending a curl command to a URLl the same command we sent the local host in but instead of localhost instead of localhost we're gonna send it to that endpoint so to do that we'll just do curl sh whoops curl that sh and this endpoint sorry this, this is the endpoint right here, the one that ends in run. Don't get them confused. All right, it's going to send that payload. Aha, message event processed successfully. We got that back, and we go back in, and you're going to see the logs. You can see on modal, the exact same logs are printed out. So this is exactly the same thing that you did locally, but this is now running in the cloud. And now you have a webhook server running in the cloud. And hopefully because you've done this locally, you should have a good mental model of how this is working and what it's doing. you should have a good mental model of how this is working and what it's doing. The next thing we're going to do is we are going to create an automation that triggers weights and biases to send the webhook payload instead of you sending the payload from your computer. And that's what I'll be going over next.
Hosting a Webhook server
Lets look into how you can deploy your own webhook server. 

Resources: 
W&B Modal Web Hooks repo - code for this lesson!: https://github.com/hamelsmu/wandb-modal-webhook/tree/62c91b055a343f802410ee328e6d70ec602c7eeb

Modal documentation: https://modal.com/docs/examples