Webhooks server
Last updated
Last updated
Webhooks are a foundation for MLOps-related features. They allow you to listen for new changes on specific repos or to all repos belonging to particular users/organizations you’re interested in following. This guide will explain how to leverage boincai_hub
to create a server listening to webhooks and deploy it to a Space. It assumes you are familiar with the concept of webhooks on the BOINC AI Hub. To learn more about webhooks themselves, you can read this first.
The base class that we will use in this guide is . It is a class for easily configuring a server that can receive webhooks from the BOINC AI Hub. The server is based on a app. It has a UI to display instructions for you or your users and an API to listen to webhooks.
To see a running example of a webhook server, check out the one. It is a Space that launches ephemeral environments when a PR is opened on a Space.
This is an . This means that we are still working on improving the API. Breaking changes might be introduced in the future without prior notice. Make sure to pin the version of boincai_hub
in your requirements.
Implementing a webhook endpoint is as simple as decorating a function. Let’s see a first example to explain the main concepts:
Copied
Save this snippet in a file called 'app.py'
and run it with 'python app.py'
. You should see a message like this:
Copied
Good job! You just launched a webhook server! Let’s break down what happened exactly:
Finally, the logs also tell you that your server is currently not secured by a secret. This is not problematic for local debugging but is to keep in mind for later.
By default, the server is started at the end of your script. If you are running it in a notebook, you can start the server manually by calling decorated_function.run()
. Since a unique server is used, you only have to start the server once even if you have multiple endpoints.
And that’s it! You can now trigger that webhook by updating the target repository (e.g. push a commit). Check the Activity tab of your Webhook to see the events that have been triggered. Now that you have a working setup, you can test it and quickly iterate. If you modify your code and restart the server, your public URL might change. Make sure to update the webhook configuration on the Hub if needed.
You can register multiple endpoints on the same server. For example, you might want to have one endpoint to trigger a training job and another one to trigger a model evaluation. You can do this by adding multiple @webhook_endpoint
decorators:
Copied
Which will create two endpoints:
Copied
Here is a complete example:
Copied
We define a custom UI using Gradio blocks. This UI will be displayed on the landing page of the server.
We register a webhook with an explicit name. This will create an endpoint at /webhooks/say_hello
.
We register a webhook with an implicit name. This will create an endpoint at /webhooks/goodbye
.
We start the server. This is optional as your server will automatically be started at the end of the script.
By decorating a function with , a object has been created in the background. As you can see, this server is a Gradio app running on . If you open this URL in your browser, you will see a landing page with instructions about the registered webhooks.
A Gradio app is a FastAPI server under the hood. A new POST route /webhooks/trigger_training
has been added to it. This is the route that will listen to webhooks and run the trigger_training
function when triggered. FastAPI will automatically parse the payload and pass it to the function as a object. This is a pydantic
object that contains all the information about the event that triggered the webhook.
The Gradio app also opened a tunnel to receive requests from the internet. This is the interesting part: you can configure a Webhook on pointing to your local machine. This is useful for debugging your webhook server and quickly iterating before deploying it to a Space.
Now that you have a webhook server running, you want to configure a Webhook to start receiving messages. Go to , click on “Add a new webhook” and configure your Webhook. Set the target repositories you want to watch and the Webhook URL, here https://1fadb0f52d8bf825fc.gradio.live/webhooks/trigger_training
.
Now that you have a working webhook server, the goal is to deploy it to a Space. Go to to create a Space. Give it a name, select the Gradio SDK and click on “Create Space”. Upload your code to the Space in a file called app.py
. Your Space will start automatically! For more details about Spaces, please refer to this .
Your webhook server is now running on a public Space. If most cases, you will want to secure it with a secret. Go to your Space settings > Section “Repository secrets” > “Add a secret”. Set the WEBHOOK_SECRET
environment variable to the value of your choice. Go back to the and set the secret in the webhook configuration. Now, only requests with the correct secret will be accepted by your server.
And this is it! Your Space is now ready to receive webhooks from the Hub. Please keep in mind that if you run the Space on a free ‘cpu-basic’ hardware, it will be shut down after 48 hours of inactivity. If you need a permanent Space, you should consider setting to an .
The guide above explained the quickest way to setup a . In this section, we will see how to customize it further.
To get more flexibility, you can also create a object directly. This is useful if you want to customize the landing page of your server. You can do this by passing a that will overwrite the default one. For example, you can add instructions for your users or add a form to manually trigger the webhooks. When creating a , you can register new webhooks using the add_webhook()
decorator.
We create a object with a custom UI and a secret. The secret is optional and can be set with the WEBHOOK_SECRET
environment variable.