From e031a671d98d1fa77533d6d6e8f703e21ce59650 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 12 Aug 2024 11:19:02 +0100 Subject: [PATCH] fix: elevenlabs 400 --- node/speak-with-elevenlabs/README.md | 2 +- node/speak-with-elevenlabs/src/main.js | 39 +++++++++++--------- node/speak-with-elevenlabs/static/index.html | 2 +- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/node/speak-with-elevenlabs/README.md b/node/speak-with-elevenlabs/README.md index 50d6aa78..b474ca61 100644 --- a/node/speak-with-elevenlabs/README.md +++ b/node/speak-with-elevenlabs/README.md @@ -29,7 +29,7 @@ Response from the model. ```json { "ok": true, - "response": "https://cloud.appwrite.io/v1/storage/buckets/text_to_speech/files/66019da664270f02c20c/view?project=project_id" + "imageUrl": "https://cloud.appwrite.io/v1/storage/buckets/text_to_speech/files/66019da664270f02c20c/view?project=project_id" } ``` diff --git a/node/speak-with-elevenlabs/src/main.js b/node/speak-with-elevenlabs/src/main.js index 25301b92..7298a9db 100644 --- a/node/speak-with-elevenlabs/src/main.js +++ b/node/speak-with-elevenlabs/src/main.js @@ -1,15 +1,11 @@ import { getStaticFile, throwIfMissing } from "./utils.js"; -import { - Client, - Storage, - ID, - InputFile, - Permission, - Role, -} from "node-appwrite"; +import { Client, Storage, ID, Permission, Role } from "node-appwrite"; import { ElevenLabsClient } from "elevenlabs"; import consumers from "stream/consumers"; +const APPWRITE_ENDPOINT = + process.env.APPWRITE_ENDPOINT ?? "https://cloud.appwrite.io/v1"; + export default async ({ req, res }) => { throwIfMissing(process.env, [ "ELEVENLABS_API_KEY", @@ -27,21 +23,20 @@ export default async ({ req, res }) => { return res.json({ ok: false, error: "Missing required field `text`" }, 400); } - const elevenlabs = new ElevenLabsClient(); + const elevenLabs = new ElevenLabsClient(); - const speechAudio = await elevenlabs.voiceGeneration.generate({ + const speechAudio = await elevenLabs.voiceGeneration.generate({ accent: req.body.accent ?? "british", accent_strength: 1.0, age: req.body.age ?? "young", gender: req.body.gender ?? "female", text: req.body.text, }); + const blob = await consumers.blob(speechAudio); const client = new Client() - .setEndpoint( - process.env.APPWRITE_ENDPOINT ?? "https://cloud.appwrite.io/v1", - ) + .setEndpoint(APPWRITE_ENDPOINT) .setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID) .setKey(process.env.APPWRITE_API_KEY); @@ -49,15 +44,25 @@ export default async ({ req, res }) => { const file = await storage.createFile( process.env.APPWRITE_BUCKET_ID, ID.unique(), - InputFile.fromBlob(blob, "audio.mp3"), - [Permission.read(Role.any())], + blob, + [Permission.read(Role.any())] + ); + + const imageUrl = new URL( + `/storage/buckets/${process.env.APPWRITE_BUCKET_ID}/files/${file["$id"]}/view`, + APPWRITE_ENDPOINT + ); + + imageUrl.searchParams.set( + "project", + process.env.APPWRITE_FUNCTION_PROJECT_ID ); return res.json( { ok: true, - response: `${endpoint}/storage/buckets/${process.env.APPWRITE_BUCKET_ID}/files/${file["$id"]}/view?project=${process.env.APPWRITE_FUNCTION_PROJECT_ID}`, + imageUrl: imageUrl.toString(), }, - 200, + 200 ); }; diff --git a/node/speak-with-elevenlabs/static/index.html b/node/speak-with-elevenlabs/static/index.html index fd6f640a..4e5c985f 100644 --- a/node/speak-with-elevenlabs/static/index.html +++ b/node/speak-with-elevenlabs/static/index.html @@ -22,7 +22,7 @@ alert(json.error); } - return json.response; + return json.imageUrl; }