Skip to content

Commit c00068a

Browse files
Add support for custom keys (#36)
* Rework all providers into classes * Switch from consts to types * Update tests * Update docs * Update providers.tsx * Last fixes * Resolves #30 * Misc key fixes * Misc fixes
1 parent 278ec58 commit c00068a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+3240
-3011
lines changed

Diff for: __tests__/assembly.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { readFile } from 'node:fs/promises';
22
import { describe, expect, it } from 'vitest';
33
import { transcribe } from '../src';
4-
import { assembly } from '../src/assembly';
4+
import { AssemblyAI } from '../src/assembly';
55

66
describe('AssemblyAI Tests', () => {
7+
const assembly = new AssemblyAI();
8+
79
it('should convert speech to text', async () => {
810
const file = await readFile('./__tests__/test.mp3');
911
const audio = new File([file], 'test.mp3', { type: 'audio/mp3' });

Diff for: __tests__/azure.openai.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { readFile, writeFile } from 'node:fs/promises';
22
import { describe, expect, it } from 'vitest';
33
import { speak, transcribe } from '../src';
4-
import { openaiAzure } from '../src/azure.openai';
4+
import { AzureOpenAI } from '../src/azure.openai';
55

66
describe('Azure OpenAI Tests', () => {
7+
const openaiAzure = new AzureOpenAI();
8+
79
it('should convert text to speech', async () => {
810
const speech = await speak({
911
model: openaiAzure.tts('tts-hd', 'alloy'),

Diff for: __tests__/azure.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { readFile, writeFile } from 'node:fs/promises';
22
import { describe, expect, it } from 'vitest';
33
import { speak, transcribe } from '../src';
4-
import { azure } from '../src/azure';
4+
import { Azure } from '../src/azure';
55

66
describe('Azure Tests', () => {
7+
const azure = new Azure();
8+
79
it('should convert text to speech', async () => {
810
const speech = await speak({
911
model: azure.tts('en-US-KaiNeural'),

Diff for: __tests__/cleanvoice.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { readFile, writeFile } from 'node:fs/promises';
22
import { describe, expect, it } from 'vitest';
33
import { isolate, transcribe } from '../src';
4-
import { cleanvoice } from '../src/cleanvoice';
4+
import { CleanVoice } from '../src/cleanvoice';
55

66
describe('CleanVoice Tests', () => {
7+
const cleanvoice = new CleanVoice();
8+
79
it('should convert speech to text', async () => {
810
const file = await readFile('./__tests__/test.mp3');
911
const audio = new File([file], 'test.mp3', { type: 'audio/mp3' });

Diff for: __tests__/deepgram.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { readFile, writeFile } from 'node:fs/promises';
22
import { describe, expect, it } from 'vitest';
33
import { speak, transcribe } from '../src';
4-
import { deepgram } from '../src/deepgram';
4+
import { Deepgram } from '../src/deepgram';
55

66
describe('Deepgram Tests', () => {
7+
const deepgram = new Deepgram();
8+
79
it('should convert text to speech', async () => {
810
const speech = await speak({
911
model: deepgram.tts('aura', 'arcas-en'),

Diff for: __tests__/elevenlabs.test.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { readFile, writeFile } from 'node:fs/promises';
22
import { describe, expect, it } from 'vitest';
33
import { change, isolate, speak, transcribe } from '../src';
4-
import { elevenlabs } from '../src/elevenlabs';
4+
import { ElevenLabs } from '../src/elevenlabs';
55

66
describe('ElevenLabs Tests', () => {
7+
const elevenlabs = new ElevenLabs();
8+
79
it('should convert text to speech', async () => {
810
const speech = await speak({
9-
model: elevenlabs.tts('multilingual_v2', 'bill'),
11+
model: elevenlabs.tts('eleven_multilingual_v2', 'bill'),
1012
prompt: 'Friends, Romans, countrymen, lend me your ears!',
1113
});
1214

@@ -35,7 +37,7 @@ describe('ElevenLabs Tests', () => {
3537
it('should convert text to speech with a custom voice', async () => {
3638
const speech = await speak({
3739
model: elevenlabs.tts(
38-
'multilingual_v2',
40+
'eleven_multilingual_sts_v2',
3941
process.env.ELEVENLABS_CUSTOM_VOICE_ID
4042
),
4143
prompt: 'Friends, Romans, countrymen, lend me your ears!',

Diff for: __tests__/fal.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { readFile } from 'node:fs/promises';
22
import { describe, expect, it } from 'vitest';
33
import { transcribe } from '../src';
4-
import { fal } from '../src/fal';
4+
import { Fal } from '../src/fal';
55

66
describe('fal Tests', () => {
7+
const fal = new Fal();
8+
79
it('should convert speech to text', async () => {
810
const file = await readFile('./__tests__/test.mp3');
911
const audio = new File([file], 'test.mp3', { type: 'audio/mp3' });

Diff for: __tests__/gladia.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { readFile } from 'node:fs/promises';
22
import { describe, expect, it } from 'vitest';
33
import { transcribe } from '../src';
4-
import { gladia } from '../src/gladia';
4+
import { Gladia } from '../src/gladia';
55

66
describe('Gladia Tests', () => {
7+
const gladia = new Gladia();
8+
79
it('should convert speech to text', async () => {
810
const file = await readFile('./__tests__/test.mp3');
911
const audio = new File([file], 'test.mp3', { type: 'audio/mp3' });

Diff for: __tests__/google.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { readFile, writeFile } from 'node:fs/promises';
22
import { describe, expect, it } from 'vitest';
33
import { speak, transcribe } from '../src';
4-
import { google } from '../src/google';
4+
import { Google } from '../src/google';
55

66
describe('Google Tests', () => {
7+
const google = new Google();
8+
79
it('should convert text to speech', async () => {
810
const speech = await speak({
911
model: google.tts('en-US-Journey-D'),

Diff for: __tests__/groq.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { readFile } from 'node:fs/promises';
22
import { describe, expect, it } from 'vitest';
33
import { transcribe } from '../src';
4-
import { groq } from '../src/groq';
4+
import { Groq } from '../src/groq';
55

66
describe('Groq Tests', () => {
7+
const groq = new Groq();
8+
79
it('should convert speech to text', async () => {
810
const file = await readFile('./__tests__/test.mp3');
911
const audio = new File([file], 'test.mp3', { type: 'audio/mp3' });

Diff for: __tests__/hume.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { writeFile } from 'node:fs/promises';
22
import { describe, expect, it } from 'vitest';
33
import { speak } from '../src';
4-
import { hume } from '../src/hume';
4+
import { Hume } from '../src/hume';
55

66
describe('Hume Tests', () => {
7+
const hume = new Hume();
8+
79
it('should convert text to speech', async () => {
810
const speech = await speak({
911
model: hume.tts('A Roman senator'),

Diff for: __tests__/ibm.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { readFile, writeFile } from 'node:fs/promises';
22
import { describe, expect, it } from 'vitest';
33
import { speak, transcribe } from '../src';
4-
import { ibm } from '../src/ibm';
4+
import { IBM } from '../src/ibm';
55

66
describe('IBM Tests', () => {
7+
const ibm = new IBM();
8+
79
it('should convert text to speech', async () => {
810
const speech = await speak({
911
model: ibm.tts('en-US_MichaelExpressive'),

Diff for: __tests__/lmnt.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { readFile, writeFile } from 'node:fs/promises';
22
import { describe, expect, it } from 'vitest';
33
import { change, speak } from '../src';
4-
import { lmnt } from '../src/lmnt';
4+
import { LMNT } from '../src/lmnt';
55

66
describe('LMNT Tests', () => {
7+
const lmnt = new LMNT();
8+
79
it('should convert text to speech', async () => {
810
const speech = await speak({
911
model: lmnt.tts('blizzard', 'zeke'),

Diff for: __tests__/murf.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { writeFile } from 'node:fs/promises';
22
import { describe, expect, it } from 'vitest';
33
import { speak } from '../src';
4-
import { murf } from '../src/murf';
4+
import { Murf } from '../src/murf';
55

66
describe('Murf Tests', () => {
7+
const murf = new Murf();
8+
79
it('should convert text to speech', async () => {
810
const speech = await speak({
911
model: murf.tts('GEN2', 'en-US-terrell'),

Diff for: __tests__/openai.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { readFile, writeFile } from 'node:fs/promises';
22
import { describe, expect, it } from 'vitest';
33
import { speak, transcribe } from '../src';
4-
import { openai } from '../src/openai';
4+
import { OpenAI } from '../src/openai';
55

66
describe('OpenAI Tests', () => {
7+
const openai = new OpenAI();
8+
79
it('should convert text to speech', async () => {
810
const speech = await speak({
911
model: openai.tts('tts-1', 'ash'),

Diff for: __tests__/play.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { writeFile } from 'node:fs/promises';
22
import { describe, expect, it } from 'vitest';
33
import { speak } from '../src';
4-
import { play } from '../src/play';
4+
import { Play } from '../src/play';
55

66
describe('Play.ai Tests', () => {
7+
const play = new Play();
8+
79
it('should convert text to speech', async () => {
810
const speech = await speak({
911
model: play.tts('Play3.0-mini', 'Angelo'),

Diff for: __tests__/replicate.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { readFile, writeFile } from 'node:fs/promises';
22
import { describe, expect, it } from 'vitest';
33
import { isolate, speak, transcribe } from '../src';
4-
import { replicate } from '../src/replicate';
4+
import { Replicate } from '../src/replicate';
55

66
describe('Replicate Tests', () => {
7+
const replicate = new Replicate();
8+
79
it('should convert text to speech using jaaari/kokoro-82m', async () => {
810
const speech = await speak({
911
model: replicate.tts(

Diff for: __tests__/rev.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { readFile } from 'node:fs/promises';
22
import { describe, expect, it } from 'vitest';
33
import { transcribe } from '../src';
4-
import { rev } from '../src/rev';
4+
import { Rev } from '../src/rev';
55

66
describe('Rev Tests', () => {
7+
const rev = new Rev();
8+
79
it('should convert speech to text', async () => {
810
const file = await readFile('./__tests__/test.mp3');
911
const audio = new File([file], 'test.mp3', { type: 'audio/mp3' });

Diff for: __tests__/speechify.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { writeFile } from 'node:fs/promises';
22
import { describe, expect, it } from 'vitest';
33
import { speak } from '../src';
4-
import { speechify } from '../src/speechify';
4+
import { Speechify } from '../src/speechify';
55

66
describe('Speechify Tests', () => {
7+
const speechify = new Speechify();
8+
79
it('should convert text to speech', async () => {
810
const speech = await speak({
911
model: speechify.tts('simba-english', 'henry'),

Diff for: src/assembly.ts

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
1-
import { AssemblyAI } from 'assemblyai';
1+
import { AssemblyAI as AssemblyAISDK } from 'assemblyai';
22
import type { SpeechModel, TranscribeParams } from 'assemblyai';
33

4-
const createProvider = () => {
5-
const apiKey = process.env.ASSEMBLYAI_API_KEY;
4+
export class AssemblyAI {
5+
private provider: AssemblyAISDK;
66

7-
if (!apiKey) {
8-
throw new Error('ASSEMBLYAI_API_KEY is not set');
9-
}
7+
constructor(apiKey?: string) {
8+
const key = apiKey || process.env.ASSEMBLYAI_API_KEY;
9+
10+
if (!key) {
11+
throw new Error('ASSEMBLYAI_API_KEY is not set');
12+
}
1013

11-
return new AssemblyAI({ apiKey });
12-
};
14+
this.provider = new AssemblyAISDK({ apiKey: key });
15+
}
1316

14-
export const assembly = {
1517
/**
1618
* Creates a speech-to-text transcription function using AssemblyAI
1719
* @param {SpeechModel} model - The speech model to use for transcription. Defaults to 'best'
1820
* @param {Omit<TranscribeParams, 'audio' | 'speech_model'>} options - Additional options for the transcription
1921
* @returns {Function} Async function that takes audio and returns transcribed text
2022
*/
21-
stt: (
23+
stt(
2224
model: SpeechModel = 'best',
2325
options?: Omit<TranscribeParams, 'audio' | 'speech_model'>
24-
) => {
25-
const provider = createProvider();
26-
26+
) {
2727
return async (audio: File) => {
2828
const buffer = await audio.arrayBuffer();
2929
const audioBuffer = Buffer.from(buffer);
3030

31-
const response = await provider.transcripts.transcribe({
31+
const response = await this.provider.transcripts.transcribe({
3232
audio: audioBuffer,
3333
speech_model: model,
3434
...options,
@@ -39,7 +39,7 @@ export const assembly = {
3939
}
4040

4141
while (true) {
42-
const transcript = await provider.transcripts.get(response.id);
42+
const transcript = await this.provider.transcripts.get(response.id);
4343

4444
if (transcript.status === 'error') {
4545
throw new Error(transcript.error);
@@ -56,5 +56,5 @@ export const assembly = {
5656
await new Promise((resolve) => setTimeout(resolve, 1000));
5757
}
5858
};
59-
},
60-
};
59+
}
60+
}

0 commit comments

Comments
 (0)