@lobehub/tts
Version:
A high-quality & reliable TTS React Hooks library
54 lines (52 loc) • 1.78 kB
JavaScript
import { OPENAI_BASE_URL } from "../const/api.mjs";
import { getRecordMineType } from "../utils/getRecordMineType.mjs";
import urlJoin from "url-join";
//#region src/core/OpenAISTT/index.ts
const genSTTBody = ({ speech, options }) => {
const mineType = options?.mineType || getRecordMineType();
const filename = `${Date.now()}.${mineType.extension}`;
const file = new File([speech], filename, { type: mineType.mineType });
const body = new FormData();
body.append("file", file);
body.append("model", options?.model || "whisper-1");
return body;
};
const genServiceSTTBody = ({ speech, options }) => {
const mineType = options?.mineType || getRecordMineType();
const filename = `${Date.now()}.${mineType.extension}`;
const body = new FormData();
body.append("options", JSON.stringify(options));
body.append("speech", speech, filename);
return body;
};
var OpenaiSTT = class {
constructor(api = {}) {
this.fetch = async (payload) => {
const url = urlJoin(this.OPENAI_BASE_URL, "audio/transcriptions");
return this.serviceUrl ? fetch(this.serviceUrl, {
body: genServiceSTTBody(payload),
headers: this.headers,
method: "POST"
}) : fetch(url, {
body: genSTTBody(payload),
headers: new Headers({ Authorization: `Bearer ${this.OPENAI_API_KEY}` }),
method: "POST"
});
};
this.create = async (payload) => {
return await this.fetch(payload);
};
this.createText = async (payload) => {
return (await (await this.fetch(payload)).json()).text;
};
this.OPENAI_BASE_URL = api.OPENAI_PROXY_URL || OPENAI_BASE_URL;
this.OPENAI_API_KEY = api.OPENAI_API_KEY;
this.serviceUrl = api.serviceUrl;
this.headers = api.headers;
}
static {
this.safeRecordMineType = getRecordMineType;
}
};
//#endregion
export { OpenaiSTT };