Skip to content

Commit 8a8b46e

Browse files
daisyfaithaumakodster28KianNH
authored andcommitted
[]Workers AI] whisper-ai-base64-example (#19726)
* base64 example * Update code sample * slight update * Update * Update WhisperBase64Code.astro Added example that does not need Node flagging * Added note * update * Apply suggestions from code review Co-authored-by: Kian <[email protected]> * update --------- Co-authored-by: kodster28 <[email protected]> Co-authored-by: Kian <[email protected]>
1 parent f75f029 commit 8a8b46e

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
import { z } from "astro:schema";
3+
import { Code } from "@astrojs/starlight/components";
4+
import Details from "~/components/Details.astro";
5+
import Render from "~/components/Render.astro";
6+
7+
type Props = z.infer<typeof props>;
8+
9+
const props = z.object({
10+
name: z.string(),
11+
});
12+
13+
const { name } = props.parse(Astro.props);
14+
15+
const worker = `import { Buffer } from 'node:buffer';
16+
export interface Env {
17+
AI: Ai;
18+
}
19+
const URL = "https://pub-dbcf9f0bd3af47ca9d40971179ee62de.r2.dev/02f6edc0-1f7b-4272-bd17-f05335104725/audio.mp3";
20+
export default {
21+
async fetch(request, env, ctx): Promise<Response> {
22+
const mp3 = await fetch(URL);
23+
if (!mp3.ok) {
24+
return Response.json({ error: \`Failed to fetch MP3: \${mp3.status}\` });
25+
}
26+
const mp3Buffer = await mp3.arrayBuffer();
27+
const base64 = Buffer.from(mp3Buffer, 'binary').toString("base64");
28+
try {
29+
const res = await env.AI.run("${name}", {
30+
"audio": base64
31+
});
32+
return Response.json(res);
33+
}
34+
catch (e) {
35+
console.error(e);
36+
return Response.json({ error: "An unexpected error occurred" });
37+
}
38+
},
39+
} satisfies ExportedHandler<Env>
40+
`;
41+
---
42+
43+
<Details header="Workers - TypeScript">
44+
<Code code={worker} lang="ts" />
45+
<Render file="nodejs-compat-howto" product="workers" />
46+
</Details>

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ 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";
2424
import Flux1Schnell from "~/components/models/code/Flux-1-Schnell.astro";
25+
import WhisperBase64Code from "~/components/models/code/WhisperBase64Code.astro";
2526
2627
import { authorData } from "~/components/models/data";
2728
@@ -88,6 +89,10 @@ if (model.name === "@cf/black-forest-labs/flux-1-schnell") {
8889
CodeExamples = Flux1Schnell;
8990
}
9091
92+
if (model.name === "@cf/openai/whisper-large-v3-turbo") {
93+
CodeExamples = WhisperBase64Code;
94+
}
95+
9196
const description = model.description;
9297
const terms = model.properties.find((x) => x.property_id === "terms");
9398

0 commit comments

Comments
 (0)