Readme

Usage:

import { ai } from "https://esm.town/v/yawnxyz/ai";
import { AudioManager } from "https://esm.town/v/yawnxyz/audioManager";

let audio = new AudioManager();

let joke = await ai("tell me a joke in chinese!");
console.log('text', joke)
let result = await audio.textToSpeechUpload(joke, {key: "random-joke.mp3"});

console.log('result:', result)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { OpenAI } from "https://esm.town/v/yawnxyz/OpenAI";
import { fetch } from "https://esm.town/v/std/fetch";
import { getUrl } from "https://esm.town/v/yawnxyz/download";
import { blobby } from "https://esm.town/v/yawnxyz/blobby";
export const LANGUAGE_CODES = {
english: "en",
spanish: "es",
chinese: "zh",
french: "fr",
german: "de",
japanese: "ja",
portuguese: "pt",
russian: "ru",
italian: "it",
korean: "ko",
arabic: "ar",
hindi: "hi",
turkish: "tr",
vietnamese: "vi",
polish: "pl",
ukrainian: "uk",
thai: "th",
dutch: "nl",
indonesian: "id",
czech: "cs",
swedish: "sv",
romanian: "ro",
greek: "el",
hungarian: "hu",
danish: "da",
finnish: "fi",
norwegian: "no",
hebrew: "he",
malay: "ms",
filipino: "fil",
};
export class AudioManager {
constructor(apiKey=null, uploadFunction = null, downloadFunction = null) {
this.openai = new OpenAI(apiKey);
this.uploadFunction = uploadFunction || this.blobUpload;
}
// valtown blob upload function
async blobUpload(buffer, key) {
try {
console.log("[uploading audio to blob:]", key);
await blobby.upload(key, buffer);
return getUrl(key);
} catch (e) {
console.error("blobUpload errors:", e);
return null; // or throw an error, depending on your desired behavior
}
};
async speechToTranscript(audioUrl, options = {}) {
const buffer = await this.getAudioBuffer(audioUrl);
const defaultOptions = {
file: buffer,
model: "whisper-1",
};
const mergedOptions = { ...defaultOptions, ...options };
const transcription = await this.openai.audio.transcriptions.create(mergedOptions);
return transcription;
}
async speechToTranslation(audioUrl, options = {}) {
const buffer = await this.getAudioBuffer(audioUrl);
const defaultOptions = {
file: buffer,
model: "whisper-1",
};
const mergedOptions = { ...defaultOptions, ...options };
const translation = await this.openai.audio.translations.create(mergedOptions);
return translation;
}
// returns an openai speech object
async textToSpeech(text, options = {}) {
const defaultOptions = {
model: "tts-1",
voice: "alloy",
input: text,
};
const mergedOptions = { ...defaultOptions, ...options };
const speech = await this.openai.audio.speech.create(mergedOptions);
const arrayBuffer = await speech.arrayBuffer();
const blob = new Blob([arrayBuffer], { type: "audio/mpeg" });
return { speech, blob, arrayBuffer};
}
// returns a blob of the audio, useful for downloading
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
July 16, 2024