Skip to content

Commit 187ebff

Browse files
committed
Adds Flux 1 Schnell code examples
1 parent a4f221f commit 187ebff

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
import { z } from "astro:schema";
3+
import { Code } from "@astrojs/starlight/components";
4+
import Details from "~/components/Details.astro";
5+
6+
type Props = z.infer<typeof props>;
7+
8+
const props = z.object({
9+
name: z.string(),
10+
});
11+
12+
const { name } = props.parse(Astro.props);
13+
14+
const workerReturningDataURI = `
15+
export interface Env {
16+
AI: Ai;
17+
}
18+
19+
export default {
20+
async fetch(request, env): Promise<Response> {
21+
const response = await env.AI.run('@cf/black-forest-labs/flux-1-schnell', {
22+
prompt: 'a cyberpunk lizard',
23+
});
24+
// response.image is base64 encoded which can be used directly as an <img src=""> data URI
25+
const dataURI = \`data:image/jpeg;charset=utf-8;base64,\${response.image}\`;
26+
return Response.json({ dataURI });
27+
},
28+
} satisfies ExportedHandler<Env>;
29+
30+
`;
31+
32+
const workerReturningImage = `
33+
export interface Env {
34+
AI: Ai;
35+
}
36+
37+
export default {
38+
async fetch(request, env): Promise<Response> {
39+
const response = await env.AI.run('@cf/black-forest-labs/flux-1-schnell', {
40+
prompt: 'a cyberpunk lizard',
41+
});
42+
// Convert from base64 string
43+
const binaryString = atob(response.image);
44+
// Create byte representation
45+
const img = Uint8Array.from(binaryString, (m) => m.codePointAt(0));
46+
return new Response(img, {
47+
headers: {
48+
'Content-Type': 'image/jpeg',
49+
},
50+
});
51+
},
52+
} satisfies ExportedHandler<Env>;
53+
`;
54+
55+
const curl = `
56+
curl https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/run/${name} \\
57+
-X POST \\
58+
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \\
59+
-d '{ "prompt": "cyberpunk cat" }'
60+
`;
61+
---
62+
63+
<Details header="Workers - Returning a data URI - TypeScript">
64+
<Code code={workerReturningDataURI} lang="ts" />
65+
</Details>
66+
67+
<Details header="Workers - Returning an image - TypeScript">
68+
<Code code={workerReturningImage} lang="ts" />
69+
</Details>
70+
71+
<Details header="curl">
72+
<Code code={curl} lang="sh" />
73+
</Details>

src/pages/workers-ai/models/[name].astro

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import TextToImageCode from "~/components/models/code/TextToImageCode.astro";
2121
import TranslationCode from "~/components/models/code/TranslationCode.astro";
2222
import StableDiffusionV15Img2ImgCode from "~/components/models/code/StableDiffusion-v1-5-img2imgCode.astro";
2323
import StableDiffusionV15InpaintingCode from "~/components/models/code/StableDiffusion-v1-5-inpaintingCode.astro";
24+
import Flux1Schnell from "~/components/models/code/Flux-1-Schnell.astro";
2425
2526
import { authorData } from "~/components/models/data";
2627
@@ -74,6 +75,7 @@ switch (model.task.name) {
7475
break;
7576
}
7677
78+
// Overrides
7779
if (model.name === "@cf/runwayml/stable-diffusion-v1-5-img2img") {
7880
CodeExamples = StableDiffusionV15Img2ImgCode;
7981
}
@@ -82,6 +84,10 @@ if (model.name === "@cf/runwayml/stable-diffusion-v1-5-inpainting") {
8284
CodeExamples = StableDiffusionV15InpaintingCode;
8385
}
8486
87+
if (model.name === "@cf/black-forest-labs/flux-1-schnell") {
88+
CodeExamples = Flux1Schnell;
89+
}
90+
8591
const description = model.description;
8692
const terms = model.properties.find((x) => x.property_id === "terms");
8793

0 commit comments

Comments
 (0)