CoolFace
Apppublic

HarshvardhanCn01/Voice-Assistant

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
README.md435 linesDownload Raw Back to playht
1<div align="center">2  <a href="https://play.ht">3    <img4      width="200"5      alt="playht playai logo"6      src="https://github.com/user-attachments/assets/c97afbf8-0fe2-4cbb-8d32-9af0ca8901c0"7    />8  </a>9<p></p>10<p>AI Powered Voice Generation Platform</p>11 12</div>13 14---15 16<!--17[![GitHub Actions CI](https://github.com/playht/workflows/CI/badge.svg)](https://github.com/playht/actions?query=workflow%3ACI)18-->19 20[![npm version](https://badge.fury.io/js/playht.svg)](https://www.npmjs.com/package/playht) [![Downloads](https://img.shields.io/npm/dm/playht.svg)](https://www.npmjs.com/package/playht)21 22 23The PlayHT SDK provides easy to use methods to wrap the [PlayHT API](https://docs.play.ht/reference/api-getting-started).24 25<!-- START doctoc generated TOC please keep comment here to allow auto update -->26<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->27 28**Table of Contents**29 30- [Usage](#usage)31  - [Initializing the library](#initializing-the-library)32  - [Generating Speech](#generating-speech)33  - [Streaming Speech](#streaming-speech)34  - [Generating Speech Options](#generating-speech-options)35    - [PlayHT 2.0 Voices](#playht-20-voices)36    - [PlayHT 1.0 Voices](#playht-10-voices)37    - [Standard Voices](#standard-voices)38  - [Listing Available Voices](#listing-available-voices)39  - [Instant Clone a Voice](#instant-clone-a-voice)40    - [Deleting a Cloned Voice](#deleting-a-cloned-voice)41- [SDK Examples](#sdk-examples)42  - [Example Server](#example-server)43  - [ChatGPT Integration Example](#chatgpt-integration-example)44 45<!-- END doctoc generated TOC please keep comment here to allow auto update -->46 47# Usage48 49This module is distributed via [npm](https://www.npmjs.com/) and should be installed as one of your project's dependencies:50 51```shell52npm install --save playht53```54 55or for installation with [yarn](https://yarnpkg.com/) package manager:56 57```shell58yarn add playht59```60 61## Initializing the library62 63Before using the SDK, you need to initialize the library with your credentials. You will need your API Secret Key and your User ID. If you already have a PlayHT account, navigate to the [API access page](https://play.ht/studio/api-access). For more details [see the API documentation](https://docs.play.ht/reference/api-authentication#generating-your-api-secret-key-and-obtaining-your-user-id).64 65_**Important:** Keep your API Secret Key confidential. Do not share it with anyone or include it in publicly accessible code repositories._66 67Import methods from the library and call `init()` with your credentials to set up the SDK:68 69```javascript70import * as PlayHT from 'playht';71 72PlayHT.init({73  apiKey: '<YOUR API KEY>',74  userId: '<YOUR API KEY>',75});76```77 78**_Note: All the examples below require that you call the init() method with your credentials first._**79 80When initializing the library, you can also set a default voice and default voice engine to be used for any subsequent speech generation methods when a voice is not defined:81 82```javascript83import * as PlayHT from 'playht';84 85PlayHT.init({86  apiKey: '<YOUR API KEY>',87  userId: '<YOUR API KEY>',88  defaultVoiceId: 's3://peregrine-voices/oliver_narrative2_parrot_saad/manifest.json',89  defaultVoiceEngine: 'Play3.0-mini',90});91```92 93## Generating Speech94 95To get a URL with the audio for a generated file using the default settings, call the `generate()` method with the text you wish to convert.96 97```javascript98import * as PlayHT from 'playht';99 100// Generate audio from text101const generated = await PlayHT.generate('Computers can speak now!');102 103// Grab the generated file URL104const { audioUrl } = generated;105 106console.log('The url for the audio file is', audioUrl);107```108 109The output also contains a `generationId` field and an optional `message` field. `generationId` is a unique identifier for the generation request, which can be used for tracking and referencing the specific generation job. The optional `message` field gives additional information about the generation such as status or error messages.110 111For more speech generation options, see [Generating Speech Options](#generating-speech-options) below.112 113## Streaming Speech114 115The `stream()` method streams audio from a text. It returns a readable stream where the audio bytes will flow to as soon as they're ready. For example, to use the default settings to convert text into an audio stream and write it into a file:116 117```javascript118import * as PlayHT from 'playht';119import fs from 'fs';120 121// Create a file stream122const fileStream = fs.createWriteStream('hello-playht.mp3');123 124// Stream audio from text125const stream = await PlayHT.stream('This sounds very realistic.');126 127// Pipe stream into file128stream.pipe(fileStream);129```130 131The `stream()` method also allows you to stream audio from a text stream input. For example, to convert a text stream into an audio file using the default settings:132 133```javascript134import * as PlayHT from 'playht';135import { Readable } from 'stream';136import fs from 'fs';137 138// Create a test stream139const textStream = new Readable({140  read() {141    this.push('You can stream ');142    this.push('text right into ');143    this.push('an audio stream!');144    this.push(null); // End of data145  },146});147 148// Stream audio from text149const stream = await PlayHT.stream(textStream);150 151// Create a file stream152const fileStream = fs.createWriteStream('hello-playht.mp3');153stream.pipe(fileStream);154```155 156For a full example of using the streaming speech from input stream API, see our [ChatGPT Integration Example](packages/gpt-example/README.md).157 158For more speech generation options, see [Generating Speech Options](#generating-speech-options).159 160**_Note: For the lowest possible latency, use the streaming API with the `Play3.0-mini` model._**161 162## Generating Speech Options163 164All text-to-speech methods above accept an optional `options` parameter. You can use it to generate audio with different voices, AI models, output file formats and much more.165 166The options available will depend on the AI model that synthesizes the selected voice. PlayHT API supports different types of models: `Play3.0-mini`, `PlayHT2.0`, `PlayHT2.0-turbo`, `PlayHT1.0` and `Standard`. For all available options, see the TypeScript type definitions [in the code](packages/playht/src/index.ts).167 168### Play3.0-mini Voices (Recommended)169 170Our newest conversational voice AI model with added languages, lowest latency, and instant cloning. Compatible with `PlayHT2.0` and `PlayHT2.0-turbo`, our most reliable and fastest model for streaming.171 172To stream using the `Play3.0-mini` model:173 174```javascript175import * as PlayHT from 'playht';176import fs from 'fs';177 178// Create a file stream179const fileStream = fs.createWriteStream('play_3.mp3');180 181// Stream audio from text182const stream = await PlayHT.stream('Stream realistic voices that say what you want!', {183  voiceEngine: 'Play3.0-mini',184  voiceId: 's3://voice-cloning-zero-shot/d9ff78ba-d016-47f6-b0ef-dd630f59414e/female-cs/manifest.json',185  outputFormat: 'mp3',186});187 188// Pipe stream into file189stream.pipe(fileStream);190```191 192### PlayHT 2.0 Voices193 194Our newest conversational voice AI model with added emotion direction and instant cloning. Compatible with `PlayHT2.0-turbo`. Supports english only.195 196To generate an audio file using a PlayHT 2.0 voice with emotion and other options:197 198```javascript199import * as PlayHT from 'playht';200 201const text = 'Am I a conversational voice with options?';202 203// Generate audio from text204const generated = await PlayHT.generate(text, {205  voiceEngine: 'PlayHT2.0',206  voiceId: 's3://peregrine-voices/oliver_narrative2_parrot_saad/manifest.json',207  outputFormat: 'mp3',208  temperature: 1.5,209  quality: 'high',210  speed: 0.8,211  emotion: 'male_fearful',212  styleGuidance: 20,213});214 215// Grab the generated file URL216const { audioUrl } = generated;217 218console.log('The url for the audio file is', audioUrl);219```220 221To stream using the `PlayHT2.0-turbo` model:222 223```javascript224import * as PlayHT from 'playht';225import fs from 'fs';226 227// Create a file stream228const fileStream = fs.createWriteStream('turbo-playht.mp3');229 230// Stream audio from text231const stream = await PlayHT.stream('Stream realistic voices that say what you want!', {232  voiceEngine: 'PlayHT2.0-turbo',233  voiceId: 's3://voice-cloning-zero-shot/d9ff78ba-d016-47f6-b0ef-dd630f59414e/female-cs/manifest.json',234  outputFormat: 'mp3',235  emotion: 'female_happy',236  styleGuidance: 10,237});238 239// Pipe stream into file240stream.pipe(fileStream);241```242 243### PlayHT 1.0 Voices244 245Lifelike voices ideal for expressive and conversational content. Supports english only.246 247To generate audio with a PlayHT 1.0 voice:248 249```javascript250import * as PlayHT from 'playht';251 252const text = 'Options are never enough.';253 254// Generate audio from text255const generated = await PlayHT.generate(text, {256  voiceEngine: 'PlayHT1.0',257  voiceId: 'susan',258  outputFormat: 'wav',259  temperature: 0.5,260  quality: 'medium',261  seed: 11,262});263 264// Grab the generated file URL265const { audioUrl } = generated;266 267console.log('The url for the audio file is', audioUrl);268```269 270### Standard Voices271 272For multilingual text-to speech generations, changing pitches, and adding pauses. Voices with reliable outputs and support for Speech Synthesis Markup Language (SSML). Supports 100+ languages.273 274And an example with standard voice in Spanish:275 276```javascript277import * as PlayHT from 'playht';278 279const text = 'La inteligencia artificial puede hablar español.';280 281// Generate audio from text282const generated = await PlayHT.generate(text, {283  voiceEngine: 'Standard',284  voiceId: 'Mia',285  quality: 'low',286  speed: 1.2,287});288 289// Grab the generated file URL290const { audioUrl } = generated;291 292console.log('The url for the audio file is', audioUrl);293```294 295## Listing Available Voices296 297To list all available voices in our platform, including voices you cloned, you can call the `listVoices()` method with no parameters:298 299```javascript300import * as PlayHT from 'playht';301 302// Fetch all available voices303const voices = await PlayHT.listVoices();304 305// Output them to the console.306console.log(JSON.stringify(voices, null, 2));307```308 309The `listVoices()` method also takes in an optional parameter to filter the voices by different fields. To get all stock female PlayHT 2.0 voices:310 311```javascript312import * as PlayHT from 'playht';313 314// Fetch stock female PlayHT 2.0 voices315const voices = await PlayHT.listVoices({316  gender: 'female',317  voiceEngine: ['PlayHT2.0'],318  isCloned: false,319});320 321// Output them to the console.322console.log(JSON.stringify(voices, null, 2));323```324 325## Instant Clone a Voice326 327You can use the `clone()` method to create a cloned voice from audio data. The cloned voice is ready to be used straight away.328 329```javascript330import * as PlayHT from 'playht';331import fs from 'fs';332 333// Load an audio file334const fileBlob = fs.readFileSync('voice-to-clone.mp3');335 336// Clone the voice337const clonedVoice = await PlayHT.clone('dolly', fileBlob, 'male');338 339// Display the cloned voice information in the console340console.log('Cloned voice info\n', JSON.stringify(clonedVoice, null, 2));341 342// Use the cloned voice straight away to generate an audio file343const fileStream = fs.createWriteStream('hello-dolly.mp3');344const stream = await PlayHT.stream('Cloned voices sound realistic too.', {345  voiceEngine: clonedVoice.voiceEngine,346  voiceId: clonedVoice.id,347});348stream.pipe(fileStream);349```350 351The `clone()` method can also take in a URL string as input:352 353```javascript354import * as PlayHT from 'playht';355import fs from 'fs';356 357// Audio file url358const fileUrl = 'https://peregrine-samples.s3.amazonaws.com/peregrine-voice-cloning/Neil-DeGrasse-Tyson-sample.wav';359 360// Clone the voice361const clonedVoice = await PlayHT.clone('neil', fileUrl, 'male');362 363// Display the cloned voice information in the console364console.log('Cloned voice info\n', JSON.stringify(clonedVoice, null, 2));365 366// Use the cloned voice straight away to generate an audio file367const fileStream = fs.createWriteStream('hello-neil.mp3');368const stream = await PlayHT.stream('Cloned voices are pure science.', {369  voiceEngine: clonedVoice.voiceEngine,370  voiceId: clonedVoice.id,371});372stream.pipe(fileStream);373```374 375### Deleting a Cloned Voice376 377Use the `deleteClone()` method to delete cloned voices.378 379```javascript380import * as PlayHT from 'playht';381 382const cloneId = 's3://voice-cloning-zero-shot/abcdefgh-01d3-4613-asdf-9a8b7774dbc2/my-clone/manifest.json';383 384const message = await PlayHT.deleteClone(cloneId);385 386console.log('deleteClone result message is', message);387```388 389Keep in mind, this action cannot be undone.390 391# SDK Examples392 393This repository contains an implementation example for the API and an example of integrating with ChatGPT API.394 395To authenticate requests for the examples, you need to generate an API Secret Key and get your User ID. If you already have a PlayHT account, navigate to the [API access page](https://play.ht/studio/api-access). For more details [see the API documentation](https://docs.play.ht/reference/api-authentication#generating-your-api-secret-key-and-obtaining-your-user-id).396 397Before running the examples, build the SDK:398 399```shell400cd packages/playht401yarn install402yarn build403```404 405## Example Server406 407Create a new `.env` file in the `packages/sdk-example` folder by copying the `.env.example` file provided. Then edit the file with your credentials.408 409To run it locally:410 411```shell412cd packages/sdk-example413yarn414yarn install:all415yarn start416```417 418Navigate to http://localhost:3000/ to see the example server.419 420## ChatGPT Integration Example421 422Create a new `.env` file in the `packages/gpt-example/server` folder by copying the `.env.example` file provided. Then edit the file with your credentials.423This example requires your [OpenAI credentials](https://help.openai.com/en/articles/4936850-where-do-i-find-my-secret-api-key) too, the example `.env` file for details.424 425To run it locally:426 427```shell428cd packages/gpt-example429yarn430yarn install:all431yarn start432```433 434See the [full ChatGPT Integration Example documentation](packages/gpt-example/README.md).435