1. Definition of Webhook Webhooks allow a system to send real-time data to another system as soon as an event occurs. In the context of handling model inference, you can seamlessly integrate webhooks by incorporating them directly into the inference function.

  2. Defining Webhook URL When we use a model like Stable Diffusion XL, or we chain multiple models together it takes longer inference time. In such a case, we can utilize webhooks. You can define your webhook URL as a global variable or you can fetch it from the .env file.

from os.env import get

  1. Using Webhooks in the Inference Function In the def infer function, you can use the webhook. After obtaining the inference result, it will send an HTTP POST request to the webhook endpoint (**WEBHOOK_URL**) with the inference result.
def infer(self, inputs):
        prompt = inputs["prompt"]
        image = self.pipe(prompt).images[0]
        buff = BytesIO()
        image.save(buff, format="JPEG")
        img_str = base64.b64encode(buff.get value()).decode()
        data = { "generated_image_base64" : img_str }
        // Call the Webhook 
        response = requests.post(WEBHOOK_URL, json=data)
        return {"response": "success"}