Skip to content

Commit 2a5c300

Browse files
Update WhisperBase64Code.astro
Added example that does not need Node flagging
1 parent c49e696 commit 2a5c300

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

src/components/models/code/WhisperBase64Code.astro

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ const props = z.object({
1111
1212
const { name } = props.parse(Astro.props);
1313
14-
const worker = `import { Buffer } from 'node:buffer';
15-
1614
export interface Env {
1715
AI: Ai;
1816
}
@@ -21,28 +19,38 @@ const URL = "https://pub-dbcf9f0bd3af47ca9d40971179ee62de.r2.dev/02f6edc0-1f7b-4
2119
2220
export default {
2321
async fetch(request, env, ctx): Promise<Response> {
24-
2522
const mp3 = await fetch(URL);
2623
if (!mp3.ok) {
27-
throw new Error('Response error from mp3');
28-
}
24+
throw new Error('Response error from mp3');
25+
}
26+
2927
const mp3Buffer = await mp3.arrayBuffer();
30-
const base64 = Buffer.from(mp3Buffer, 'binary').toString("base64");
28+
29+
// Convert ArrayBuffer to Base64 using a loop
30+
const base64 = arrayBufferToBase64(mp3Buffer);
3131
3232
try {
33-
const res = await env.AI.run("${name}", {
34-
"audio": base64
33+
const res = await env.AI.run("audio-analysis", {
34+
audio: base64
3535
});
36-
3736
return Response.json(res);
37+
} catch (e) {
38+
console.log("AI Service Error:", e);
3839
}
39-
catch (e) {
40-
console.log(JSON.stringify(env.AI));
41-
}
42-
return new Response(JSON.stringify({ho:"to"}));
40+
41+
return new Response(JSON.stringify({ message: "Fallback response" }));
4342
},
44-
} satisfies ExportedHandler<Env>
45-
`;
43+
};
44+
45+
// Utility to convert ArrayBuffer to Base64 using a loop
46+
function arrayBufferToBase64(buffer: ArrayBuffer): string {
47+
const bytes = new Uint8Array(buffer);
48+
let binary = '';
49+
for (let i = 0; i < bytes.length; i++) {
50+
binary += String.fromCharCode(bytes[i]);
51+
}
52+
return btoa(binary);
53+
}` ;
4654
---
4755
4856
<Details header="Workers - TypeScript">

0 commit comments

Comments
 (0)