From d12aae8362fb3d52742f7c217e593f6a21f3c383 Mon Sep 17 00:00:00 2001 From: daisyfaithauma Date: Tue, 4 Feb 2025 15:53:31 +0000 Subject: [PATCH 1/9] base64 example --- .../code/AutomaticSpeechRecognitionCode.astro | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/components/models/code/AutomaticSpeechRecognitionCode.astro b/src/components/models/code/AutomaticSpeechRecognitionCode.astro index 7feecb5bc6f253..a09f12da70c782 100644 --- a/src/components/models/code/AutomaticSpeechRecognitionCode.astro +++ b/src/components/models/code/AutomaticSpeechRecognitionCode.astro @@ -37,6 +37,39 @@ export default { } satisfies ExportedHandler; `; +const workers = `import { Buffer } from 'node:buffer'; + +export interface Env { + AI: Ai; +} + +const URL = "https://pub-dbcf9f0bd3af47ca9d40971179ee62de.r2.dev/02f6edc0-1f7b-4272-bd17-f05335104725/audio.mp3"; + +export default { + async fetch(request, env, ctx): Promise { + + const mp3 = await fetch(URL); + if (!mp3.ok) { + throw new Error(`Response status: ${mp3.status}`); + } + const mp3Buffer = await mp3.arrayBuffer(); + const base64 = Buffer.from(mp3Buffer, 'binary').toString("base64"); + + try { + const res = await env.AI.run("@cf/openai/whisper-large-v3-turbo", { + "audio": base64 + }); + + return Response.json(res); + } + catch (e) { + console.log(JSON.stringify(env.AI)); + } + return new Response(JSON.stringify({ho:"to"})); + }, +} satisfies ExportedHandler + `; + const curl = ` curl https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/run/${name} \\ -X POST \\ From 91d89cf23cd749dffba3f8fe74ac8be038e2a0a7 Mon Sep 17 00:00:00 2001 From: kodster28 Date: Wed, 5 Feb 2025 06:00:27 -0600 Subject: [PATCH 2/9] Update code sample --- .../code/AutomaticSpeechRecognitionCode.astro | 40 -------------- .../models/code/WhisperBase64Code.astro | 54 +++++++++++++++++++ 2 files changed, 54 insertions(+), 40 deletions(-) create mode 100644 src/components/models/code/WhisperBase64Code.astro diff --git a/src/components/models/code/AutomaticSpeechRecognitionCode.astro b/src/components/models/code/AutomaticSpeechRecognitionCode.astro index a09f12da70c782..74d0930229fd49 100644 --- a/src/components/models/code/AutomaticSpeechRecognitionCode.astro +++ b/src/components/models/code/AutomaticSpeechRecognitionCode.astro @@ -36,46 +36,6 @@ export default { }, } satisfies ExportedHandler; `; - -const workers = `import { Buffer } from 'node:buffer'; - -export interface Env { - AI: Ai; -} - -const URL = "https://pub-dbcf9f0bd3af47ca9d40971179ee62de.r2.dev/02f6edc0-1f7b-4272-bd17-f05335104725/audio.mp3"; - -export default { - async fetch(request, env, ctx): Promise { - - const mp3 = await fetch(URL); - if (!mp3.ok) { - throw new Error(`Response status: ${mp3.status}`); - } - const mp3Buffer = await mp3.arrayBuffer(); - const base64 = Buffer.from(mp3Buffer, 'binary').toString("base64"); - - try { - const res = await env.AI.run("@cf/openai/whisper-large-v3-turbo", { - "audio": base64 - }); - - return Response.json(res); - } - catch (e) { - console.log(JSON.stringify(env.AI)); - } - return new Response(JSON.stringify({ho:"to"})); - }, -} satisfies ExportedHandler - `; - -const curl = ` -curl https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/run/${name} \\ - -X POST \\ - -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \\ - --data-binary "@talking-llama.mp3" -`; ---
diff --git a/src/components/models/code/WhisperBase64Code.astro b/src/components/models/code/WhisperBase64Code.astro new file mode 100644 index 00000000000000..0f9fc6e5023265 --- /dev/null +++ b/src/components/models/code/WhisperBase64Code.astro @@ -0,0 +1,54 @@ +--- +import { z } from "astro:schema"; +import { Code } from "@astrojs/starlight/components"; +import Details from "~/components/Details.astro"; + +type Props = z.infer; + +const props = z.object({ + name: z.string(), +}); + +const { name } = props.parse(Astro.props); + +const worker = `import { Buffer } from 'node:buffer'; + +export interface Env { + AI: Ai; +} + +const URL = "https://pub-dbcf9f0bd3af47ca9d40971179ee62de.r2.dev/02f6edc0-1f7b-4272-bd17-f05335104725/audio.mp3"; + +export default { + async fetch(request, env, ctx): Promise { + + const mp3 = await fetch(URL); + if (!mp3.ok) { + throw new Error('Response status from mp3'); + } + const mp3Buffer = await mp3.arrayBuffer(); + const base64 = Buffer.from(mp3Buffer, 'binary').toString("base64"); + + try { + const res = await env.AI.run("${name}", { + "audio": base64 + }); + + return Response.json(res); + } + catch (e) { + console.log(JSON.stringify(env.AI)); + } + return new Response(JSON.stringify({ho:"to"})); + }, +} satisfies ExportedHandler + `; +--- + +
+ +
+ +
+ +
From 49b5901a57395a1a6e93d3190b6cde300c10403a Mon Sep 17 00:00:00 2001 From: kodster28 Date: Wed, 5 Feb 2025 06:01:51 -0600 Subject: [PATCH 3/9] slight update --- .../models/code/AutomaticSpeechRecognitionCode.astro | 7 +++++++ src/components/models/code/WhisperBase64Code.astro | 4 ---- src/pages/workers-ai/models/[name].astro | 5 +++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/components/models/code/AutomaticSpeechRecognitionCode.astro b/src/components/models/code/AutomaticSpeechRecognitionCode.astro index 74d0930229fd49..7feecb5bc6f253 100644 --- a/src/components/models/code/AutomaticSpeechRecognitionCode.astro +++ b/src/components/models/code/AutomaticSpeechRecognitionCode.astro @@ -36,6 +36,13 @@ export default { }, } satisfies ExportedHandler; `; + +const curl = ` +curl https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/run/${name} \\ + -X POST \\ + -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \\ + --data-binary "@talking-llama.mp3" +`; ---
diff --git a/src/components/models/code/WhisperBase64Code.astro b/src/components/models/code/WhisperBase64Code.astro index 0f9fc6e5023265..3897c1eb79e36e 100644 --- a/src/components/models/code/WhisperBase64Code.astro +++ b/src/components/models/code/WhisperBase64Code.astro @@ -48,7 +48,3 @@ export default {
- -
- -
diff --git a/src/pages/workers-ai/models/[name].astro b/src/pages/workers-ai/models/[name].astro index 38142cdf549e3b..98616136c0f809 100644 --- a/src/pages/workers-ai/models/[name].astro +++ b/src/pages/workers-ai/models/[name].astro @@ -22,6 +22,7 @@ import TranslationCode from "~/components/models/code/TranslationCode.astro"; import StableDiffusionV15Img2ImgCode from "~/components/models/code/StableDiffusion-v1-5-img2imgCode.astro"; import StableDiffusionV15InpaintingCode from "~/components/models/code/StableDiffusion-v1-5-inpaintingCode.astro"; import Flux1Schnell from "~/components/models/code/Flux-1-Schnell.astro"; +import WhisperBase64Code from "~/components/models/code/WhisperBase64Code.astro"; import { authorData } from "~/components/models/data"; @@ -88,6 +89,10 @@ if (model.name === "@cf/black-forest-labs/flux-1-schnell") { CodeExamples = Flux1Schnell; } +if (model.name === "@cf/black-forest-labs/flux-1-schnell") { + CodeExamples = Flux1Schnell; +} + const description = model.description; const terms = model.properties.find((x) => x.property_id === "terms"); From c49e696243501fd924e707aef14f5496d33a5149 Mon Sep 17 00:00:00 2001 From: kodster28 Date: Wed, 5 Feb 2025 06:42:41 -0600 Subject: [PATCH 4/9] Update --- src/components/models/code/WhisperBase64Code.astro | 2 +- src/pages/workers-ai/models/[name].astro | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/models/code/WhisperBase64Code.astro b/src/components/models/code/WhisperBase64Code.astro index 3897c1eb79e36e..b6c4067bd65599 100644 --- a/src/components/models/code/WhisperBase64Code.astro +++ b/src/components/models/code/WhisperBase64Code.astro @@ -24,7 +24,7 @@ export default { const mp3 = await fetch(URL); if (!mp3.ok) { - throw new Error('Response status from mp3'); + throw new Error('Response error from mp3'); } const mp3Buffer = await mp3.arrayBuffer(); const base64 = Buffer.from(mp3Buffer, 'binary').toString("base64"); diff --git a/src/pages/workers-ai/models/[name].astro b/src/pages/workers-ai/models/[name].astro index 98616136c0f809..465bddbf50c60e 100644 --- a/src/pages/workers-ai/models/[name].astro +++ b/src/pages/workers-ai/models/[name].astro @@ -89,8 +89,8 @@ if (model.name === "@cf/black-forest-labs/flux-1-schnell") { CodeExamples = Flux1Schnell; } -if (model.name === "@cf/black-forest-labs/flux-1-schnell") { - CodeExamples = Flux1Schnell; +if (model.name === "@cf/openai/whisper-large-v3-turbo") { + CodeExamples = WhisperBase64Code; } const description = model.description; From 2a5c300dc1bcb8030a084841a1053e1dca990edf Mon Sep 17 00:00:00 2001 From: daisyfaithauma Date: Mon, 10 Feb 2025 18:50:41 +0000 Subject: [PATCH 5/9] Update WhisperBase64Code.astro Added example that does not need Node flagging --- .../models/code/WhisperBase64Code.astro | 38 +++++++++++-------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/src/components/models/code/WhisperBase64Code.astro b/src/components/models/code/WhisperBase64Code.astro index b6c4067bd65599..73c6e5f16c9d8b 100644 --- a/src/components/models/code/WhisperBase64Code.astro +++ b/src/components/models/code/WhisperBase64Code.astro @@ -11,8 +11,6 @@ const props = z.object({ const { name } = props.parse(Astro.props); -const worker = `import { Buffer } from 'node:buffer'; - export interface Env { AI: Ai; } @@ -21,28 +19,38 @@ const URL = "https://pub-dbcf9f0bd3af47ca9d40971179ee62de.r2.dev/02f6edc0-1f7b-4 export default { async fetch(request, env, ctx): Promise { - const mp3 = await fetch(URL); if (!mp3.ok) { - throw new Error('Response error from mp3'); - } + throw new Error('Response error from mp3'); + } + const mp3Buffer = await mp3.arrayBuffer(); - const base64 = Buffer.from(mp3Buffer, 'binary').toString("base64"); + + // Convert ArrayBuffer to Base64 using a loop + const base64 = arrayBufferToBase64(mp3Buffer); try { - const res = await env.AI.run("${name}", { - "audio": base64 + const res = await env.AI.run("audio-analysis", { + audio: base64 }); - return Response.json(res); + } catch (e) { + console.log("AI Service Error:", e); } - catch (e) { - console.log(JSON.stringify(env.AI)); - } - return new Response(JSON.stringify({ho:"to"})); + + return new Response(JSON.stringify({ message: "Fallback response" })); }, -} satisfies ExportedHandler - `; +}; + +// Utility to convert ArrayBuffer to Base64 using a loop +function arrayBufferToBase64(buffer: ArrayBuffer): string { + const bytes = new Uint8Array(buffer); + let binary = ''; + for (let i = 0; i < bytes.length; i++) { + binary += String.fromCharCode(bytes[i]); + } + return btoa(binary); +}` ; ---
From 0fb051c7fc59a4b7042dfefc30477efc26ae5b46 Mon Sep 17 00:00:00 2001 From: kodster28 Date: Wed, 12 Feb 2025 08:26:42 -0600 Subject: [PATCH 6/9] Added note --- src/components/models/code/WhisperBase64Code.astro | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/components/models/code/WhisperBase64Code.astro b/src/components/models/code/WhisperBase64Code.astro index 73c6e5f16c9d8b..ec1da7b7df8f5b 100644 --- a/src/components/models/code/WhisperBase64Code.astro +++ b/src/components/models/code/WhisperBase64Code.astro @@ -2,6 +2,7 @@ import { z } from "astro:schema"; import { Code } from "@astrojs/starlight/components"; import Details from "~/components/Details.astro"; +import Render from "~/components/Render.astro"; type Props = z.infer; @@ -11,6 +12,7 @@ const props = z.object({ const { name } = props.parse(Astro.props); +const worker = ` export interface Env { AI: Ai; } @@ -50,9 +52,11 @@ function arrayBufferToBase64(buffer: ArrayBuffer): string { binary += String.fromCharCode(bytes[i]); } return btoa(binary); -}` ; +}`; ---
+ + From bff19d2bd24d755827948e48c612009a82d6c522 Mon Sep 17 00:00:00 2001 From: kodster28 Date: Wed, 12 Feb 2025 10:12:23 -0600 Subject: [PATCH 7/9] update --- .../models/code/WhisperBase64Code.astro | 39 ++++++------------- 1 file changed, 12 insertions(+), 27 deletions(-) diff --git a/src/components/models/code/WhisperBase64Code.astro b/src/components/models/code/WhisperBase64Code.astro index ec1da7b7df8f5b..5d83af46ff56fe 100644 --- a/src/components/models/code/WhisperBase64Code.astro +++ b/src/components/models/code/WhisperBase64Code.astro @@ -12,47 +12,32 @@ const props = z.object({ const { name } = props.parse(Astro.props); -const worker = ` +const worker = `import { Buffer } from 'node:buffer'; export interface Env { AI: Ai; } - const URL = "https://pub-dbcf9f0bd3af47ca9d40971179ee62de.r2.dev/02f6edc0-1f7b-4272-bd17-f05335104725/audio.mp3"; - export default { async fetch(request, env, ctx): Promise { const mp3 = await fetch(URL); if (!mp3.ok) { - throw new Error('Response error from mp3'); - } - + throw new Error(\`Response status: \${mp3.status}\`); + } const mp3Buffer = await mp3.arrayBuffer(); - - // Convert ArrayBuffer to Base64 using a loop - const base64 = arrayBufferToBase64(mp3Buffer); - + const base64 = Buffer.from(mp3Buffer, 'binary').toString("base64"); try { - const res = await env.AI.run("audio-analysis", { - audio: base64 + const res = await env.AI.run("${name}", { + "audio": base64 }); return Response.json(res); - } catch (e) { - console.log("AI Service Error:", e); } - - return new Response(JSON.stringify({ message: "Fallback response" })); + catch (e) { + console.log(JSON.stringify(env.AI)); + } + return new Response(JSON.stringify({ho:"to"})); }, -}; - -// Utility to convert ArrayBuffer to Base64 using a loop -function arrayBufferToBase64(buffer: ArrayBuffer): string { - const bytes = new Uint8Array(buffer); - let binary = ''; - for (let i = 0; i < bytes.length; i++) { - binary += String.fromCharCode(bytes[i]); - } - return btoa(binary); -}`; +} satisfies ExportedHandler + `; ---
From a79a2361ae16c9348788c6d80c6322a22bae92cd Mon Sep 17 00:00:00 2001 From: Kody Jackson Date: Wed, 12 Feb 2025 10:43:58 -0600 Subject: [PATCH 8/9] Apply suggestions from code review Co-authored-by: Kian --- src/components/models/code/WhisperBase64Code.astro | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/models/code/WhisperBase64Code.astro b/src/components/models/code/WhisperBase64Code.astro index 5d83af46ff56fe..aca195a8035291 100644 --- a/src/components/models/code/WhisperBase64Code.astro +++ b/src/components/models/code/WhisperBase64Code.astro @@ -21,7 +21,7 @@ export default { async fetch(request, env, ctx): Promise { const mp3 = await fetch(URL); if (!mp3.ok) { - throw new Error(\`Response status: \${mp3.status}\`); + return Response.json({ error: \`Failed to fetch MP3: \${mp3.status}\` }); } const mp3Buffer = await mp3.arrayBuffer(); const base64 = Buffer.from(mp3Buffer, 'binary').toString("base64"); @@ -32,9 +32,9 @@ export default { return Response.json(res); } catch (e) { - console.log(JSON.stringify(env.AI)); + console.error(e); + return Response.json({ error: "An unexpected error occurred" }); } - return new Response(JSON.stringify({ho:"to"})); }, } satisfies ExportedHandler `; From 82b0493b2051625667c972a72449d6a423bc3a41 Mon Sep 17 00:00:00 2001 From: kodster28 Date: Wed, 12 Feb 2025 10:45:14 -0600 Subject: [PATCH 9/9] update --- src/components/models/code/WhisperBase64Code.astro | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/components/models/code/WhisperBase64Code.astro b/src/components/models/code/WhisperBase64Code.astro index aca195a8035291..35ecb38e69b5af 100644 --- a/src/components/models/code/WhisperBase64Code.astro +++ b/src/components/models/code/WhisperBase64Code.astro @@ -42,6 +42,5 @@ export default {
+
- -