assemblyai 1.0.1 → 2.0.0-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.
Files changed (81) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +93 -84
  3. package/dist/index.d.ts +2 -0
  4. package/dist/index.esm.js +378 -0
  5. package/dist/index.js +380 -0
  6. package/dist/services/base.d.ts +15 -0
  7. package/dist/services/files/index.d.ts +9 -0
  8. package/dist/services/index.d.ts +31 -0
  9. package/dist/services/lemur/index.d.ts +8 -0
  10. package/dist/services/realtime/index.d.ts +16 -0
  11. package/dist/services/transcripts/index.d.ts +42 -0
  12. package/dist/services/transcripts/redactions.d.ts +14 -0
  13. package/dist/services/transcripts/subtitles.d.ts +12 -0
  14. package/dist/types/core/index.d.ts +2 -0
  15. package/dist/types/core/params.d.ts +2 -0
  16. package/dist/types/core/segments.d.ts +28 -0
  17. package/dist/types/core/transcript.d.ts +113 -0
  18. package/dist/types/index.d.ts +6 -0
  19. package/dist/types/lemur/index.d.ts +79 -0
  20. package/dist/types/models/auto_chapter.d.ts +8 -0
  21. package/dist/types/models/auto_highlights.d.ts +11 -0
  22. package/dist/types/models/content_safety.d.ts +25 -0
  23. package/dist/types/models/entity_detection.d.ts +7 -0
  24. package/dist/types/models/index.d.ts +8 -0
  25. package/dist/types/models/pii.d.ts +3 -0
  26. package/dist/types/models/sentiment_analysis.d.ts +9 -0
  27. package/dist/types/models/shared.d.ts +3 -0
  28. package/dist/types/models/summarization.d.ts +3 -0
  29. package/dist/types/models/topic_detection.d.ts +17 -0
  30. package/dist/types/realtime/index.d.ts +17 -0
  31. package/dist/types/services/abstractions.d.ts +80 -0
  32. package/dist/types/services/index.d.ts +9 -0
  33. package/dist/types/shared/index.d.ts +2 -0
  34. package/dist/types/shared/pagination.d.ts +7 -0
  35. package/dist/types/shared/timestamp.d.ts +5 -0
  36. package/dist/utils/errors/index.d.ts +1 -0
  37. package/dist/utils/errors/realtime.d.ts +26 -0
  38. package/package.json +55 -21
  39. package/src/index.ts +2 -0
  40. package/src/services/base.ts +16 -0
  41. package/src/services/files/index.ts +22 -0
  42. package/src/services/index.ts +66 -0
  43. package/src/services/lemur/index.ts +45 -0
  44. package/src/services/realtime/index.ts +106 -0
  45. package/src/services/transcripts/index.ts +156 -0
  46. package/src/services/transcripts/redactions.ts +27 -0
  47. package/src/services/transcripts/subtitles.ts +26 -0
  48. package/src/types/core/index.ts +2 -0
  49. package/src/types/core/params.ts +3 -0
  50. package/src/types/core/segments.ts +29 -0
  51. package/src/types/core/transcript.ts +159 -0
  52. package/src/types/index.ts +6 -0
  53. package/src/types/lemur/index.ts +97 -0
  54. package/src/types/models/auto_chapter.ts +9 -0
  55. package/src/types/models/auto_highlights.ts +14 -0
  56. package/src/types/models/content_safety.ts +34 -0
  57. package/src/types/models/entity_detection.ts +8 -0
  58. package/src/types/models/index.ts +8 -0
  59. package/src/types/models/pii.ts +32 -0
  60. package/src/types/models/sentiment_analysis.ts +11 -0
  61. package/src/types/models/shared.ts +4 -0
  62. package/src/types/models/summarization.ts +10 -0
  63. package/src/types/models/topic_detection.ts +21 -0
  64. package/src/types/realtime/index.ts +28 -0
  65. package/src/types/services/abstractions.ts +88 -0
  66. package/src/types/services/index.ts +11 -0
  67. package/src/types/shared/index.ts +2 -0
  68. package/src/types/shared/pagination.ts +8 -0
  69. package/src/types/shared/timestamp.ts +6 -0
  70. package/src/utils/.gitkeep +0 -0
  71. package/src/utils/errors/index.ts +5 -0
  72. package/src/utils/errors/realtime.ts +58 -0
  73. package/.eslintrc.json +0 -3
  74. package/index.js +0 -15
  75. package/src/Client.js +0 -28
  76. package/src/api/Http/Request.js +0 -108
  77. package/src/api/Http/Response.js +0 -23
  78. package/src/api/Model.js +0 -17
  79. package/src/api/Transcript.js +0 -16
  80. package/src/api/Upload.js +0 -41
  81. package/src/api/util.js +0 -48
@@ -0,0 +1,32 @@
1
+ type PIIRedactionPolicies =
2
+ | "medical_process"
3
+ | "medical_condition"
4
+ | "blood_type"
5
+ | "drug"
6
+ | "injury"
7
+ | "number_sequence"
8
+ | "email_address"
9
+ | "date_of_birth"
10
+ | "phone_number"
11
+ | "us_social_security_number"
12
+ | "credit_card_number"
13
+ | "credit_card_expiration"
14
+ | "credit_card_cvv"
15
+ | "date"
16
+ | "nationality"
17
+ | "event"
18
+ | "language"
19
+ | "location"
20
+ | "money_amount"
21
+ | "person_name"
22
+ | "person_age"
23
+ | "organization"
24
+ | "political_affiliation"
25
+ | "occupation"
26
+ | "religion"
27
+ | "drivers_license"
28
+ | "banking_information";
29
+
30
+ type PIIAudioQuality = "mp3" | "wav";
31
+
32
+ export type { PIIRedactionPolicies, PIIAudioQuality };
@@ -0,0 +1,11 @@
1
+ type Sentiment = "NEUTRAL" | "POSITIVE" | "NEGATIVE";
2
+
3
+ type SentimentAnalysis = {
4
+ text: string;
5
+ start: number;
6
+ end: number;
7
+ sentiment: Sentiment;
8
+ confidence: number;
9
+ };
10
+
11
+ export type { SentimentAnalysis };
@@ -0,0 +1,4 @@
1
+ type ModelResultStatus = "success" | "error";
2
+ type ModelSummary = Record<string, number>;
3
+
4
+ export type { ModelResultStatus, ModelSummary };
@@ -0,0 +1,10 @@
1
+ type SummaryType =
2
+ | "bullets"
3
+ | "bullets_verbose"
4
+ | "gist"
5
+ | "headline"
6
+ | "paragraph";
7
+
8
+ type SummaryModel = "informative" | "conversational" | "catchy";
9
+
10
+ export type { SummaryType, SummaryModel };
@@ -0,0 +1,21 @@
1
+ import { ModelResultStatus, ModelSummary } from "./shared";
2
+ import { Timestamp } from "@/types/shared";
3
+
4
+ type TopicDetectionLabel = {
5
+ relevance: number;
6
+ label: string;
7
+ };
8
+
9
+ type TopicDetectionResults = {
10
+ text: string;
11
+ labels: TopicDetectionLabel[];
12
+ timestamp: Timestamp;
13
+ };
14
+
15
+ type TopicDetection = {
16
+ status: ModelResultStatus;
17
+ results: TopicDetectionResults[];
18
+ summary: ModelSummary;
19
+ };
20
+
21
+ export type { TopicDetection };
@@ -0,0 +1,28 @@
1
+ type RealtimeConnectionParams = {
2
+ sampleRate?: number;
3
+ wordBoost?: string[];
4
+ };
5
+
6
+ type RealtimeListener = "data" | "error" | "open" | "close";
7
+
8
+ type RealtimeTranscriptType = "PartialTranscript" | "FinalTranscript";
9
+
10
+ type RealtimeTranscript = {
11
+ text: string;
12
+ message_type: RealtimeTranscriptType;
13
+ };
14
+
15
+ type RealtimeListeners = {
16
+ data?: (transcript: RealtimeTranscript) => void;
17
+ error?: (error: Error) => void;
18
+ open?: () => void;
19
+ close?: (code: number, reason: string) => void;
20
+ };
21
+
22
+ export type {
23
+ RealtimeConnectionParams,
24
+ RealtimeListener,
25
+ RealtimeTranscript,
26
+ RealtimeTranscriptType,
27
+ RealtimeListeners,
28
+ };
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Interface for classes that can create resources.
3
+ * @template T The type of the resource.
4
+ * @template Props The type of the properties required to create the resource.
5
+ */
6
+ interface Createable<T, Props, Settings = Record<string, any>> {
7
+ /**
8
+ * Create a new resource.
9
+ * @param data The properties of the new resource.
10
+ * @param settings The settings used for creating the new resource.
11
+ * @return A promise that resolves to the newly created resource.
12
+ */
13
+ create(params: Props, settings?: Settings | null): Promise<T>;
14
+ }
15
+
16
+ /**
17
+ * Interface for classes that can retrieve resources.
18
+ * @template T The type of the resource.
19
+ * @template Id The type of the resource's identifier. Defaults to string.
20
+ */
21
+ interface Retrieveable<T, Id = string> {
22
+ /**
23
+ * Retrieve a resource.
24
+ * @param id The identifier of the resource to retrieve.
25
+ * @return A promise that resolves to the retrieved resource.
26
+ */
27
+ retrieve(id: Id): Promise<T>;
28
+ }
29
+
30
+ /**
31
+ * Interface for classes that can delete resources.
32
+ * @template T The type of the resource.
33
+ * @template Id The type of the resource's identifier. Defaults to string.
34
+ */
35
+ interface Deletable<T, Id = string> {
36
+ /**
37
+ * Delete a resource.
38
+ * @param id The identifier of the resource to delete.
39
+ * @return A promise that resolves to a boolean indicating whether the deletion was successful.
40
+ */
41
+ delete(id: Id): Promise<T>;
42
+ }
43
+
44
+ /**
45
+ * Interface for classes that can list resources.
46
+ * @template T The type of the resource.
47
+ */
48
+ interface Listable<T, Page = string> {
49
+ /**
50
+ * List all resources.
51
+ * @return A promise that resolves to an array of resources.
52
+ */
53
+ list(page?: Page): Promise<T>;
54
+ }
55
+
56
+ /**
57
+ * Interface for classes that can connect to resources.
58
+ * @template T The type of the resource's data.
59
+ */
60
+ interface Connectable<T, Params> {
61
+ /**
62
+ * Connect to a resource.
63
+ * @param event The event to listen for.
64
+ * @param listener The listener for the event.
65
+ */
66
+ on(event: "data", listener: (data: T) => void): void;
67
+ on(event: "error", listener: (error: Error) => void): void;
68
+ on(event: "open", listener: () => void): void;
69
+ on(event: "close", listener: (code: number, reason: string) => void): void;
70
+
71
+ /**
72
+ * Send data to the resource.
73
+ * @param data The data to send.
74
+ */
75
+ send(data: ArrayBuffer): void;
76
+
77
+ /**
78
+ * Connect to the resource.
79
+ */
80
+ connect(params: Params): void;
81
+
82
+ /**
83
+ * Disconnect from the resource.
84
+ */
85
+ disconnect(): void;
86
+ }
87
+
88
+ export type { Createable, Retrieveable, Deletable, Listable, Connectable };
@@ -0,0 +1,11 @@
1
+ type BaseServiceParams = {
2
+ token: string;
3
+ baseUrl?: string;
4
+ };
5
+
6
+ type Upload = {
7
+ upload_url: string;
8
+ };
9
+
10
+ export type * from "./abstractions";
11
+ export type { BaseServiceParams, Upload };
@@ -0,0 +1,2 @@
1
+ export type * from "./pagination";
2
+ export type * from "./timestamp";
@@ -0,0 +1,8 @@
1
+ type PageDetails = {
2
+ limit: number;
3
+ result_count: number;
4
+ next_url: string | null;
5
+ previous_url: string | null;
6
+ };
7
+
8
+ export type { PageDetails };
@@ -0,0 +1,6 @@
1
+ type Timestamp = {
2
+ start: number;
3
+ end: number;
4
+ };
5
+
6
+ export type { Timestamp };
File without changes
@@ -0,0 +1,5 @@
1
+ export {
2
+ default as RealtimeError,
3
+ RealtimeErrorType,
4
+ RealtimeErrorMessages,
5
+ } from "./realtime";
@@ -0,0 +1,58 @@
1
+ enum RealtimeErrorType {
2
+ BadSampleRate = 4000,
3
+ AuthFailed = 4001,
4
+ InsufficientFunds = 4002,
5
+ FreeAccount = 4003,
6
+ NonexistentSessionId = 4004,
7
+ SessionExpired = 4008,
8
+ ClosedSession = 4010,
9
+ RateLimited = 4029,
10
+ UniqueSessionViolation = 4030,
11
+ SessionTimeout = 4031,
12
+ AudioTooShort = 4032,
13
+ AudioTooLong = 4033,
14
+ BadJson = 4100,
15
+ BadSchema = 4101,
16
+ TooManyStreams = 4102,
17
+ Reconnected = 4103,
18
+ ReconnectAttemptsExhausted = 1013,
19
+ }
20
+
21
+ const RealtimeErrorMessages: Record<RealtimeErrorType, string> = {
22
+ [RealtimeErrorType.BadSampleRate]: "Sample rate must be a positive integer",
23
+ [RealtimeErrorType.AuthFailed]: "Not Authorized",
24
+ [RealtimeErrorType.InsufficientFunds]: "Insufficient funds",
25
+ [RealtimeErrorType.FreeAccount]:
26
+ "This feature is paid-only and requires you to add a credit card. Please visit https://assemblyai.com/dashboard/ to add a credit card to your account",
27
+ [RealtimeErrorType.NonexistentSessionId]: "Session ID does not exist",
28
+ [RealtimeErrorType.SessionExpired]: "Session has expired",
29
+ [RealtimeErrorType.ClosedSession]: "Session is closed",
30
+ [RealtimeErrorType.RateLimited]: "Rate limited",
31
+ [RealtimeErrorType.UniqueSessionViolation]: "Unique session violation",
32
+ [RealtimeErrorType.SessionTimeout]: "Session Timeout",
33
+ [RealtimeErrorType.AudioTooShort]: "Audio too short",
34
+ [RealtimeErrorType.AudioTooLong]: "Audio too long",
35
+ [RealtimeErrorType.BadJson]: "Bad JSON",
36
+ [RealtimeErrorType.BadSchema]: "Bad schema",
37
+ [RealtimeErrorType.TooManyStreams]: "Too many streams",
38
+ [RealtimeErrorType.Reconnected]: "Reconnected",
39
+ [RealtimeErrorType.ReconnectAttemptsExhausted]:
40
+ "Reconnect attempts exhausted",
41
+ };
42
+
43
+ class RealtimeError extends Error {
44
+ public code: RealtimeErrorType;
45
+
46
+ constructor(code: RealtimeErrorType) {
47
+ const message = RealtimeErrorMessages[code];
48
+ super(message);
49
+
50
+ this.name = this.constructor.name;
51
+ this.code = code;
52
+
53
+ Error.captureStackTrace(this, this.constructor);
54
+ }
55
+ }
56
+
57
+ export { RealtimeErrorType, RealtimeErrorMessages };
58
+ export default RealtimeError;
package/.eslintrc.json DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "extends": "standard"
3
- }
package/index.js DELETED
@@ -1,15 +0,0 @@
1
- const Transcript = require('./src/api/Transcript')
2
- const Model = require('./src/api/Model')
3
- const Upload = require('./src/api/Upload')
4
- const Client = require('./src/Client')
5
-
6
- module.exports = {
7
- Transcript,
8
- Model,
9
- Upload,
10
- setAPIKey (key) {
11
- Client.API_KEY = key
12
- }
13
- }
14
-
15
-
package/src/Client.js DELETED
@@ -1,28 +0,0 @@
1
- let _apiKey = ''
2
-
3
- class Client {
4
- static set API_KEY (key) {
5
- _apiKey = key
6
- }
7
-
8
- static get API_KEY () {
9
- return _apiKey
10
- }
11
-
12
- static checkKey () {
13
- if (process.env.ASSEMBLYAI_API_KEY) {
14
- _apiKey = process.env.ASSEMBLYAI_API_KEY
15
- }
16
- if (!_apiKey) {
17
- throw new Error(`
18
- Unable to find the API Key.
19
- You can set this value by using the setAPIKey method.
20
- Example: """ const assemblyai = require('assemblyai')
21
- assemblyai.setAPIKey('example') """
22
- `)
23
- }
24
- return true
25
- }
26
- }
27
-
28
- module.exports = Client
@@ -1,108 +0,0 @@
1
- const request = require('request')
2
- const Client = require('../../Client')
3
-
4
- class Request {
5
- /**
6
- * Initializes the class
7
- * @param {Object} options The HTTP Options
8
- * @param {String} options.method The HTTP Method
9
- * @param {String} options.url THE HTTP URL
10
- * @param {Object} options.body optional HTTP Body
11
- */
12
- constructor (options) {
13
- this.method = options.method || 'GET'
14
- this.url = options.url || ''
15
- this.body = options.body || ''
16
- }
17
-
18
- /**
19
- * Sends an HTTP request using the node HTTP module
20
- *
21
- * @returns {Promise<Object>} Parsed JSON of the response
22
- */
23
- _request (isJSON) {
24
- const options = {
25
- uri: this.url,
26
- method: this.method,
27
- headers: {
28
- authorization: Client.API_KEY
29
- }
30
- }
31
-
32
- if (this.method === 'POST' || this.method === 'PUT') {
33
- if (this.body && this.body.constructor !== String) {
34
- options.body = JSON.stringify(this.body)
35
- } else {
36
- options.body = ''
37
- }
38
- }
39
-
40
- return new Promise((resolve, reject) => {
41
- request(options, (err, response, body) => {
42
- if (err) return reject(err)
43
- try {
44
- if (isJSON) {
45
- resolve(JSON.parse(body))
46
- } else {
47
- resolve(body)
48
- }
49
- } catch (e) {
50
- console.log('HERE:', e)
51
- reject(new Error(`
52
- Unable to recieve a proper JSON response from the API.
53
- Please contact customer service.
54
- `))
55
- }
56
- })
57
- })
58
- }
59
-
60
- /**
61
- * Sends a request to the API
62
- *
63
- * @throws {Error} the error receieved from the API
64
- */
65
- async send (isJSON = true) {
66
- Client.checkKey()
67
- let response = null
68
- let retries = 1
69
- while (!response) {
70
- if (retries === 5) {
71
- throw new Error(`
72
- Retry limit reached. Some things that could cause this to happen
73
- would be your network connection or slow internet (request timeout).
74
- Please look into this before continuing to use this SDK
75
- `)
76
- }
77
-
78
- response = await new Promise(resolve => {
79
- setTimeout(async () => {
80
- try {
81
- const res = await this._request(isJSON)
82
- resolve(res)
83
- } catch (e) {
84
- retries += 1
85
- /**
86
- * The reason for resolving the null value would be so response is still *falsey*
87
- * allowing the loop to run again. If no value is resolved here, the loop will hang
88
- * until the value is resolved (which would be never)
89
- */
90
- resolve(null)
91
- }
92
- }, (retries * retries) * 100) // TODO: exponential timeout. Change this value to whatever you want
93
- })
94
- }
95
- return response
96
- }
97
- }
98
-
99
- module.exports = Request
100
-
101
- // curl --request POST \
102
- // --url https://api.assemblyai.com/transcript \
103
- // --header 'authorization: 6f33815060fa4eb29e96356a3ec536c8' \
104
- // --data '
105
- // {
106
- // "audio_src_url": "https://s3-us-west-2.amazonaws.com/blog.assemblyai.com/audio/8-7-2018-post/7510.mp3",
107
- // "model_id": 265
108
- // }'
@@ -1,23 +0,0 @@
1
- class Response {
2
- constructor (responseJSON) {
3
- this.json = responseJSON
4
- if (responseJSON.error) {
5
- throw new Error(responseJSON.error)
6
- }
7
- this.type = responseJSON.transcript ? 'transcript' : 'model'
8
- }
9
-
10
- toString () {
11
- return JSON.stringify(this.json[this.type])
12
- }
13
-
14
- stringify () {
15
- return this.toString()
16
- }
17
-
18
- get () {
19
- return this.json[this.type]
20
- }
21
- }
22
-
23
- module.exports = Response
package/src/api/Model.js DELETED
@@ -1,17 +0,0 @@
1
- const { Poll, Create } = require('./util')
2
-
3
- class Model {
4
- constructor () {
5
- this.url = 'https://api.assemblyai.com/model'
6
- }
7
-
8
- poll (id) {
9
- return Poll(this.url, id)
10
- }
11
-
12
- create (options) {
13
- return Create(this.url, options)
14
- }
15
- }
16
-
17
- module.exports = Model
@@ -1,16 +0,0 @@
1
- const { Poll, Create } = require('./util')
2
- class Transcript {
3
- constructor () {
4
- this.url = 'https://api.assemblyai.com/transcript'
5
- }
6
-
7
- poll (id) {
8
- return Poll(this.url, id)
9
- }
10
-
11
- create (options) {
12
- return Create(this.url, options)
13
- }
14
- }
15
-
16
- module.exports = Transcript
package/src/api/Upload.js DELETED
@@ -1,41 +0,0 @@
1
- const fs = require('fs')
2
- const Request = require('./Http/Request')
3
- const request = require('request')
4
-
5
- const Transcribe = require('./Transcript')
6
-
7
- class Upload {
8
- constructor (filePath) {
9
- this.url = 'https://api.assemblyai.com/upload'
10
- this.filePath = filePath
11
- }
12
-
13
- async create () {
14
- const req = new Request({
15
- method: 'POST',
16
- url: this.url
17
- })
18
- const uploadUrl = await req.send(false)
19
-
20
- await new Promise((resolve, reject) => {
21
- fs.readFile(this.filePath, (err, data) => {
22
- if (err) return reject(err)
23
- request.put(uploadUrl, { body: data }, (err, response, body) => {
24
- if (err) return reject(err)
25
- resolve(body)
26
- })
27
- })
28
- })
29
-
30
- const url = uploadUrl.split('?')[0]
31
- const transcribe = new Transcribe()
32
- const response = await transcribe.create({
33
- audio_src_url: url
34
- })
35
- const { id } = response.get()
36
-
37
- return transcribe.poll(id)
38
- }
39
- }
40
-
41
- module.exports = Upload
package/src/api/util.js DELETED
@@ -1,48 +0,0 @@
1
- const Request = require('./Http/Request')
2
- const Response = require('./Http/Response')
3
-
4
- module.exports = {
5
- Poll (url, id) {
6
- return new Promise((resolve, reject) => {
7
- const request = new Request({
8
- method: 'GET',
9
- url: `${url}/${id}`
10
- })
11
-
12
- const interval = setInterval(async () => {
13
- try {
14
- const apiResponse = await request.send()
15
- const response = new Response(apiResponse)
16
- const json = response.get()
17
- if (json.status === 'completed') {
18
- clearInterval(interval)
19
- resolve(response)
20
- }
21
- if (json.status === 'trained') {
22
- clearInterval(interval)
23
- resolve(response)
24
- }
25
- if (json.status === 'error') {
26
- clearInterval(interval)
27
- reject(json.error)
28
- }
29
- } catch (e) {
30
- clearInterval(interval)
31
- reject(e)
32
- }
33
- }, 3000)
34
- })
35
- },
36
- async Create (url, options) {
37
- const request = new Request({
38
- method: 'POST',
39
- url: url,
40
- body: options.upload ? '' : options || {}
41
- })
42
- const apiResponse = await request.send()
43
- if (options.json === false) {
44
- return apiResponse
45
- }
46
- return new Response(apiResponse)
47
- }
48
- }