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 19917b93..3a7447f3 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,7 +23,7 @@ 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({ accent: req.bodyJson.accent ?? "british", @@ -36,12 +32,11 @@ export default async ({ req, res }) => { gender: req.bodyJson.gender ?? "female", text: req.bodyJson.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; }