Skip to content

Commit

Permalink
Merge pull request #301 from appwrite/fix-elevenlabs-400
Browse files Browse the repository at this point in the history
fix: elevenlabs 400
  • Loading branch information
loks0n authored Aug 19, 2024
2 parents cbb7089 + c613a7e commit 46cda7a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion node/speak-with-elevenlabs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
```

Expand Down
37 changes: 21 additions & 16 deletions node/speak-with-elevenlabs/src/main.js
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand All @@ -36,28 +32,37 @@ 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);

const storage = new Storage(client);
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
);
};
2 changes: 1 addition & 1 deletion node/speak-with-elevenlabs/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
alert(json.error);
}

return json.response;
return json.imageUrl;
}
</script>

Expand Down

0 comments on commit 46cda7a

Please sign in to comment.