Skip to content

Commit

Permalink
fix: elevenlabs 400
Browse files Browse the repository at this point in the history
  • Loading branch information
loks0n committed Aug 12, 2024
1 parent 619d2c9 commit e031a67
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 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
39 changes: 22 additions & 17 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,37 +23,46 @@ 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);

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 e031a67

Please sign in to comment.