assemblyai 1.0.1 → 2.0.1-beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +192 -83
- package/dist/index.d.ts +4 -0
- package/dist/index.esm.js +473 -0
- package/dist/index.js +482 -0
- package/dist/services/base.d.ts +13 -0
- package/dist/services/files/index.d.ts +9 -0
- package/dist/services/index.d.ts +29 -0
- package/dist/services/lemur/index.d.ts +8 -0
- package/dist/services/realtime/factory.d.ts +10 -0
- package/dist/services/realtime/index.d.ts +2 -0
- package/dist/services/realtime/service.d.ts +22 -0
- package/dist/services/transcripts/index.d.ts +59 -0
- package/dist/types/asyncapi.generated.d.ts +87 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/openapi.generated.d.ts +685 -0
- package/dist/types/realtime/index.d.ts +36 -0
- package/dist/types/services/abstractions.d.ts +52 -0
- package/dist/types/services/index.d.ts +6 -0
- package/dist/types/transcripts/index.d.ts +5 -0
- package/dist/utils/axios.d.ts +3 -0
- package/dist/utils/errors/index.d.ts +1 -0
- package/dist/utils/errors/realtime.d.ts +23 -0
- package/package.json +58 -21
- package/src/index.ts +5 -0
- package/src/services/base.ts +14 -0
- package/src/services/files/index.ts +22 -0
- package/src/services/index.ts +49 -0
- package/src/services/lemur/index.ts +49 -0
- package/src/services/realtime/factory.ts +32 -0
- package/src/services/realtime/index.ts +2 -0
- package/src/services/realtime/service.ts +184 -0
- package/src/services/transcripts/index.ts +178 -0
- package/src/types/asyncapi.generated.ts +124 -0
- package/src/types/index.ts +5 -0
- package/src/types/openapi.generated.ts +834 -0
- package/src/types/realtime/index.ts +68 -0
- package/src/types/services/abstractions.ts +56 -0
- package/src/types/services/index.ts +7 -0
- package/src/types/transcripts/index.ts +5 -0
- package/src/utils/.gitkeep +0 -0
- package/src/utils/axios.ts +19 -0
- package/src/utils/errors/index.ts +5 -0
- package/src/utils/errors/realtime.ts +45 -0
- package/.eslintrc.json +0 -3
- package/index.js +0 -15
- package/src/Client.js +0 -28
- package/src/api/Http/Request.js +0 -108
- package/src/api/Http/Response.js +0 -23
- package/src/api/Model.js +0 -17
- package/src/api/Transcript.js +0 -16
- package/src/api/Upload.js +0 -41
- package/src/api/util.js +0 -48
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import BaseService from "@/services/base";
|
|
2
|
+
import {
|
|
3
|
+
ParagraphsResponse,
|
|
4
|
+
SentencesResponse,
|
|
5
|
+
Transcript,
|
|
6
|
+
TranscriptList,
|
|
7
|
+
CreateTranscriptParameters,
|
|
8
|
+
CreateTranscriptOptions,
|
|
9
|
+
Createable,
|
|
10
|
+
Deletable,
|
|
11
|
+
Listable,
|
|
12
|
+
Retrieveable,
|
|
13
|
+
SubtitleFormat,
|
|
14
|
+
RedactedAudioResponse,
|
|
15
|
+
} from "@/types";
|
|
16
|
+
import { AxiosInstance } from "axios";
|
|
17
|
+
import FileService from "../files";
|
|
18
|
+
|
|
19
|
+
export default class TranscriptService
|
|
20
|
+
extends BaseService
|
|
21
|
+
implements
|
|
22
|
+
Createable<Transcript, CreateTranscriptParameters, CreateTranscriptOptions>,
|
|
23
|
+
Retrieveable<Transcript>,
|
|
24
|
+
Deletable<Transcript>,
|
|
25
|
+
Listable<TranscriptList>
|
|
26
|
+
{
|
|
27
|
+
constructor(client: AxiosInstance, private files: FileService) {
|
|
28
|
+
super(client);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Create a transcript.
|
|
33
|
+
* @param params The parameters to create a transcript.
|
|
34
|
+
* @param options The options used for creating the new transcript.
|
|
35
|
+
* @returns A promise that resolves to the newly created transcript.
|
|
36
|
+
*/
|
|
37
|
+
async create(
|
|
38
|
+
params: CreateTranscriptParameters,
|
|
39
|
+
options?: CreateTranscriptOptions
|
|
40
|
+
): Promise<Transcript> {
|
|
41
|
+
const path = getPath(params.audio_url);
|
|
42
|
+
if (path !== null) {
|
|
43
|
+
const uploadUrl = await this.files.upload(path);
|
|
44
|
+
params.audio_url = uploadUrl;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const res = await this.client.post<Transcript>("/v2/transcript", params);
|
|
48
|
+
|
|
49
|
+
if (options?.poll ?? true) {
|
|
50
|
+
return await this.poll(res.data.id, options);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return res.data;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
private async poll(
|
|
57
|
+
transcriptId: string,
|
|
58
|
+
options?: CreateTranscriptOptions
|
|
59
|
+
): Promise<Transcript> {
|
|
60
|
+
const startTime = Date.now();
|
|
61
|
+
while (true) {
|
|
62
|
+
const transcript = await this.get(transcriptId);
|
|
63
|
+
if (transcript.status === "completed" || transcript.status === "error") {
|
|
64
|
+
return transcript;
|
|
65
|
+
} else if (
|
|
66
|
+
Date.now() - startTime <
|
|
67
|
+
(options?.pollingTimeout ?? 180_000)
|
|
68
|
+
) {
|
|
69
|
+
await new Promise((resolve) =>
|
|
70
|
+
setTimeout(resolve, options?.pollingInterval ?? 3_000)
|
|
71
|
+
);
|
|
72
|
+
} else {
|
|
73
|
+
throw new Error("Polling timeout");
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Retrieve a transcript.
|
|
80
|
+
* @param id The identifier of the transcript.
|
|
81
|
+
* @returns A promise that resolves to the transcript.
|
|
82
|
+
*/
|
|
83
|
+
async get(id: string): Promise<Transcript> {
|
|
84
|
+
const res = await this.client.get<Transcript>(`/v2/transcript/${id}`);
|
|
85
|
+
return res.data;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// TODO: add options overload to support list querystring parameters
|
|
89
|
+
/**
|
|
90
|
+
* Retrieves a paged list of transcript listings.
|
|
91
|
+
* @param nextUrl The URL to retrieve the transcript list from. If not provided, the first page will be retrieved.
|
|
92
|
+
* @returns
|
|
93
|
+
*/
|
|
94
|
+
async list(nextUrl?: string | null): Promise<TranscriptList> {
|
|
95
|
+
const { data } = await this.client.get<TranscriptList>(
|
|
96
|
+
nextUrl ?? "/v2/transcript"
|
|
97
|
+
);
|
|
98
|
+
for (const transcriptListItem of data.transcripts) {
|
|
99
|
+
transcriptListItem.created = new Date(transcriptListItem.created);
|
|
100
|
+
if (transcriptListItem.completed) {
|
|
101
|
+
transcriptListItem.completed = new Date(transcriptListItem.completed);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return data as unknown as TranscriptList;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Delete a transcript
|
|
110
|
+
* @param id The identifier of the transcript.
|
|
111
|
+
* @returns A promise that resolves to the transcript.
|
|
112
|
+
*/
|
|
113
|
+
async delete(id: string): Promise<Transcript> {
|
|
114
|
+
const res = await this.client.delete<Transcript>(`/v2/transcript/${id}`);
|
|
115
|
+
return res.data;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Retrieve all sentences of a transcript.
|
|
120
|
+
* @param id The identifier of the transcript.
|
|
121
|
+
* @return A promise that resolves to the sentences.
|
|
122
|
+
*/
|
|
123
|
+
async sentences(id: string): Promise<SentencesResponse> {
|
|
124
|
+
const { data } = await this.client.get<SentencesResponse>(
|
|
125
|
+
`/v2/transcript/${id}/sentences`
|
|
126
|
+
);
|
|
127
|
+
return data;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Retrieve all paragraphs of a transcript.
|
|
132
|
+
* @param id The identifier of the transcript.
|
|
133
|
+
* @return A promise that resolves to the paragraphs.
|
|
134
|
+
*/
|
|
135
|
+
async paragraphs(id: string): Promise<ParagraphsResponse> {
|
|
136
|
+
const { data } = await this.client.get<ParagraphsResponse>(
|
|
137
|
+
`/v2/transcript/${id}/paragraphs`
|
|
138
|
+
);
|
|
139
|
+
return data;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Retrieve subtitles of a transcript.
|
|
144
|
+
* @param id The identifier of the transcript.
|
|
145
|
+
* @param format The format of the subtitles.
|
|
146
|
+
* @return A promise that resolves to the subtitles text.
|
|
147
|
+
*/
|
|
148
|
+
async subtitles(id: string, format: SubtitleFormat = "srt"): Promise<string> {
|
|
149
|
+
const { data } = await this.client.get<string>(
|
|
150
|
+
`/v2/transcript/${id}/${format}`
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
return data;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Retrieve redactions of a transcript.
|
|
158
|
+
* @param id The identifier of the transcript.
|
|
159
|
+
* @return A promise that resolves to the subtitles text.
|
|
160
|
+
*/
|
|
161
|
+
async redactions(id: string): Promise<RedactedAudioResponse> {
|
|
162
|
+
const { data } = await this.client.get<RedactedAudioResponse>(
|
|
163
|
+
`/v2/transcript/${id}/redacted-audio`
|
|
164
|
+
);
|
|
165
|
+
return data;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function getPath(path: string) {
|
|
170
|
+
let url: URL;
|
|
171
|
+
try {
|
|
172
|
+
url = new URL(path);
|
|
173
|
+
if (url.protocol === "file:") return url.pathname;
|
|
174
|
+
else return null;
|
|
175
|
+
} catch {
|
|
176
|
+
return path;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// this file is generated by typescript/scripts/generate-types.ts
|
|
2
|
+
/* tslint:disable */
|
|
3
|
+
/* eslint-disable */
|
|
4
|
+
|
|
5
|
+
/** OneOf type helpers */
|
|
6
|
+
type Without<T, U> = { [P in Exclude<keyof T, keyof U>]?: never };
|
|
7
|
+
type XOR<T, U> = T | U extends object
|
|
8
|
+
? (Without<T, U> & U) | (Without<U, T> & T)
|
|
9
|
+
: T | U;
|
|
10
|
+
type OneOf<T extends any[]> = T extends [infer Only]
|
|
11
|
+
? Only
|
|
12
|
+
: T extends [infer A, infer B, ...infer Rest]
|
|
13
|
+
? OneOf<[XOR<A, B>, ...Rest]>
|
|
14
|
+
: never;
|
|
15
|
+
|
|
16
|
+
export type AudioData = {
|
|
17
|
+
/** @description Raw audio data, base64 encoded. This can be the raw data recorded directly from a microphone or read from an audio file. */
|
|
18
|
+
audio_data: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type FinalTranscript = RealtimeBaseTranscript & {
|
|
22
|
+
/**
|
|
23
|
+
* @description Describes the type of message.
|
|
24
|
+
* @constant
|
|
25
|
+
*/
|
|
26
|
+
message_type: "FinalTranscript";
|
|
27
|
+
/** @description Whether the text has been punctuated and cased. */
|
|
28
|
+
punctuated: boolean;
|
|
29
|
+
/** @description Whether the text has been formatted (e.g. Dollar -> $) */
|
|
30
|
+
text_formatted: boolean;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/** @enum {string} */
|
|
34
|
+
export type MessageType =
|
|
35
|
+
| "SessionBegins"
|
|
36
|
+
| "PartialTranscript"
|
|
37
|
+
| "FinalTranscript"
|
|
38
|
+
| "SessionTerminated";
|
|
39
|
+
|
|
40
|
+
export type PartialTranscript = RealtimeBaseTranscript & {
|
|
41
|
+
/**
|
|
42
|
+
* @description Describes the type of message.
|
|
43
|
+
* @constant
|
|
44
|
+
*/
|
|
45
|
+
message_type: "PartialTranscript";
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export type RealtimeBaseMessage = {
|
|
49
|
+
/** @description Describes the type of the message. */
|
|
50
|
+
message_type: MessageType;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export type RealtimeBaseTranscript = {
|
|
54
|
+
/** @description End time of audio sample relative to session start, in milliseconds. */
|
|
55
|
+
audio_end: number;
|
|
56
|
+
/** @description Start time of audio sample relative to session start, in milliseconds. */
|
|
57
|
+
audio_start: number;
|
|
58
|
+
/**
|
|
59
|
+
* Format: double
|
|
60
|
+
* @description The confidence score of the entire transcription, between 0 and 1.
|
|
61
|
+
*/
|
|
62
|
+
confidence: number;
|
|
63
|
+
/** @description The timestamp for the partial transcript. */
|
|
64
|
+
created: Date;
|
|
65
|
+
/** @description The partial transcript for your audio. */
|
|
66
|
+
text: string;
|
|
67
|
+
/** @description An array of objects, with the information for each word in the transcription text. Includes the start/end time (in milliseconds) of the word, the confidence score of the word, and the text (i.e. the word itself). */
|
|
68
|
+
words: Word[];
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export type RealtimeError = {
|
|
72
|
+
error: string;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export type RealtimeMessage =
|
|
76
|
+
| SessionBegins
|
|
77
|
+
| PartialTranscript
|
|
78
|
+
| FinalTranscript
|
|
79
|
+
| SessionTerminated
|
|
80
|
+
| RealtimeError;
|
|
81
|
+
|
|
82
|
+
export type RealtimeTranscript = PartialTranscript | FinalTranscript;
|
|
83
|
+
|
|
84
|
+
/** @enum {string} */
|
|
85
|
+
export type RealtimeTranscriptType = "PartialTranscript" | "FinalTranscript";
|
|
86
|
+
|
|
87
|
+
export type SessionBegins = RealtimeBaseMessage & {
|
|
88
|
+
/** @description Timestamp when this session will expire. */
|
|
89
|
+
expires_at: Date;
|
|
90
|
+
/**
|
|
91
|
+
* @description Describes the type of the message.
|
|
92
|
+
* @constant
|
|
93
|
+
*/
|
|
94
|
+
message_type: "SessionBegins";
|
|
95
|
+
/** @description Unique identifier for the established session. */
|
|
96
|
+
session_id: string;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
export type SessionTerminated = RealtimeBaseMessage & {
|
|
100
|
+
/**
|
|
101
|
+
* @description Describes the type of the message.
|
|
102
|
+
* @constant
|
|
103
|
+
*/
|
|
104
|
+
message_type: "SessionTerminated";
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
export type TerminateSession = RealtimeBaseMessage & {
|
|
108
|
+
/** @description A boolean value to communicate that you wish to end your real-time session forever. */
|
|
109
|
+
terminate_session: boolean;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
export type Word = {
|
|
113
|
+
/**
|
|
114
|
+
* Format: double
|
|
115
|
+
* @description Confidence score of the word
|
|
116
|
+
*/
|
|
117
|
+
confidence: number;
|
|
118
|
+
/** @description End time of the word in milliseconds */
|
|
119
|
+
end: number;
|
|
120
|
+
/** @description Start time of the word in milliseconds */
|
|
121
|
+
start: number;
|
|
122
|
+
/** @description The word itself */
|
|
123
|
+
text: string;
|
|
124
|
+
};
|