|
| 1 | +--- |
| 2 | +title: "Convert an image to a cartoon using Fal.ai and Trigger.dev Realtime" |
| 3 | +sidebarTitle: "Fal.ai with Realtime" |
| 4 | +description: "This example demonstrates how to convert an image to a cartoon using Fal.ai and shows the progress of the task on the frontend using Trigger.dev Realtime." |
| 5 | +--- |
| 6 | + |
| 7 | +## Walkthrough |
| 8 | + |
| 9 | +This video walks through the process of creating this task in a Next.js project. |
| 10 | + |
| 11 | +<div className="w-full h-full aspect-video mb-3"> |
| 12 | + <iframe width="100%" height="100%" src="https://www.youtube.com/embed/BWZqYfUaigg?si=XpqVUEIf1j4bsYZ4" title="Trigger.dev walkthrough" frameborder="0" allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen/> |
| 13 | +</div> |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | + |
| 17 | +- An existing project |
| 18 | +- A [Trigger.dev account](https://cloud.trigger.dev) with Trigger.dev [initialized in your project](/quick-start) |
| 19 | +- A [Fal.ai](https://fal.ai/) account |
| 20 | + |
| 21 | +## Task code |
| 22 | + |
| 23 | +This task converts an image to a cartoon using fal AI, and uploads the result to Cloudflare R2. |
| 24 | + |
| 25 | +```ts trigger/fal-ai-image-from-prompt-realtime.ts |
| 26 | +import * as fal from "@fal-ai/serverless-client"; |
| 27 | +import { logger, schemaTask } from "@trigger.dev/sdk/v3"; |
| 28 | +import { z } from "zod"; |
| 29 | + |
| 30 | +export const FalResult = z.object({ |
| 31 | + images: z.tuple([z.object({ url: z.string() })]), |
| 32 | +}); |
| 33 | + |
| 34 | +export const payloadSchema = z.object({ |
| 35 | + imageUrl: z.string().url(), |
| 36 | + prompt: z.string(), |
| 37 | +}); |
| 38 | + |
| 39 | +export const realtimeImageGeneration = schemaTask({ |
| 40 | + id: "realtime-image-generation", |
| 41 | + schema: payloadSchema, |
| 42 | + run: async (payload) => { |
| 43 | + const result = await fal.subscribe("fal-ai/flux/dev/image-to-image", { |
| 44 | + input: { |
| 45 | + image_url: payload.imageUrl, |
| 46 | + prompt: payload.prompt, |
| 47 | + }, |
| 48 | + onQueueUpdate: (update) => { |
| 49 | + logger.info("Fal.ai processing update", { update }); |
| 50 | + }, |
| 51 | + }); |
| 52 | + |
| 53 | + const $result = FalResult.parse(result); |
| 54 | + const [{ url: cartoonUrl }] = $result.images; |
| 55 | + |
| 56 | + return { |
| 57 | + imageUrl: cartoonUrl, |
| 58 | + }; |
| 59 | + }, |
| 60 | +}); |
| 61 | +``` |
| 62 | + |
| 63 | +### Testing your task |
| 64 | + |
| 65 | +You can test your task by triggering it from the Trigger.dev dashboard. Here's an example payload: |
| 66 | + |
| 67 | +```json |
| 68 | +{ |
| 69 | + "imageUrl": "https://static.vecteezy.com/system/resources/previews/005/857/332/non_2x/funny-portrait-of-cute-corgi-dog-outdoors-free-photo.jpg", |
| 70 | + "prompt": "Dress this dog for Christmas" |
| 71 | +} |
| 72 | +``` |
0 commit comments