Speaking Example
Generating Speech from Text
Suppose we want to speak the sentence "I'm a real boy!".
Terminal (via CURL)
curl -X POST \
"https://api.geppetto.app/speak" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${GEPPETTO_API_KEY}" \
-d '{
"text": "I'\''m a real boy!"
}'\
--output speech.mp3
Python
import os
import requests
# load env var
GEPPETTO_API_KEY = os.environ.get("GEPPETTO_API_KEY")
# set up the request headers and data
url = 'https://api.geppetto.app/speak'
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {GEPPETTO_API_KEY}",
}
data = {
'text': "I'm a real boy!"
}
# send the request
response = requests.post(url, headers=headers, json=data, stream=True)
# if the request is valid write it to speech.mp3
if response.status_code == 200:
with open("speech.mp3", "wb") as file:
for chunk in response.iter_content(chunk_size=1024):
file.write(chunk)
else:
print(f"{response.status_code}: {response.text}")
Node.js
- Install the Geppetto Typescript Library
npm install @geppetto-app/geppetto
- Use the library to make a 'speak' request and save it to a file
import fs from "fs";
import {
Geppetto
} from '@geppetto-app/geppetto';
// the library will load the env var GEPPETTO_API_KEY automatically
const geppetto = new Geppetto();
// you can also pass it directly in like so
// const geppetto = new Geppetto({apiKey: "your-api-key"});
async function main() {
const response = await geppetto.speak({
"text": "I'm a real boy!"
})
.catch((err) => {
throw err;
});
fs.writeFileSync('output.mp3', response);
}
main();