0
Create transcription
One script reply has been approved by the moderators Verified

Transcribes audio into the input language.

Created by hugo697 156 days ago Viewed 5898 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 156 days ago
1
type Openai = {
2
  api_key: string;
3
  organization_id: string;
4
};
5
type Base64 = string;
6
/**
7
 * Create transcription
8
 * Transcribes audio into the input language.
9
 */
10
export async function main(
11
  auth: Openai,
12
  body: {
13
    file: {
14
      base64: Base64;
15
      type:
16
        | "image/png"
17
        | "image/jpeg"
18
        | "image/gif"
19
        | "application/pdf"
20
        | "appication/json"
21
        | "text/csv"
22
        | "text/plain"
23
        | "audio/mpeg"
24
        | "audio/wav"
25
        | "video/mp4";
26
      name: string;
27
    };
28
    model: string | "whisper-1";
29
    language?: string;
30
    prompt?: string;
31
    response_format?: "json" | "text" | "srt" | "verbose_json" | "vtt";
32
    temperature?: number;
33
    "timestamp_granularities[]"?: ("word" | "segment")[];
34
  }
35
) {
36
  const url = new URL(`https://api.openai.com/v1/audio/transcriptions`);
37

38
  const formData = new FormData();
39
  for (const [k, v] of Object.entries(body)) {
40
    if (v !== undefined && v !== "") {
41
      if (["file"].includes(k)) {
42
        const { base64, type, name } = v as {
43
          base64: Base64;
44
          type: string;
45
          name: string;
46
        };
47
        formData.append(
48
          k,
49
          new Blob([Uint8Array.from(atob(base64), (m) => m.codePointAt(0)!)], {
50
            type,
51
          }),
52
          name
53
        );
54
      } else {
55
        formData.append(k, String(v));
56
      }
57
    }
58
  }
59
  const response = await fetch(url, {
60
    method: "POST",
61
    headers: {
62
      "OpenAI-Organization": auth.organization_id,
63
      Authorization: "Bearer " + auth.api_key,
64
    },
65
    body: formData,
66
  });
67
  if (!response.ok) {
68
    const text = await response.text();
69
    throw new Error(`${response.status} ${text}`);
70
  }
71
  return await response.json();
72
}
73