hume 0.3.3 → 0.4.1
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/Client.js +5 -5
- package/README.md +40 -11
- package/api/types/EmotionScore.d.ts +1 -1
- package/dist/Client.js +5 -5
- package/dist/api/types/EmotionScore.d.ts +1 -1
- package/dist/serialization/types/EmotionScore.d.ts +1 -1
- package/dist/serialization/types/EmotionScore.js +1 -1
- package/dist/wrapper/HumeStreamingClient.d.ts +10 -5
- package/dist/wrapper/HumeStreamingClient.js +68 -74
- package/dist/wrapper/StreamSocket.d.ts +15 -25
- package/dist/wrapper/StreamSocket.js +172 -45
- package/dist/wrapper/base64Encode.d.ts +1 -0
- package/dist/wrapper/base64Encode.js +17 -0
- package/package.json +1 -1
- package/serialization/types/EmotionScore.d.ts +1 -1
- package/serialization/types/EmotionScore.js +1 -1
- package/wrapper/HumeStreamingClient.d.ts +10 -5
- package/wrapper/HumeStreamingClient.js +68 -74
- package/wrapper/StreamSocket.d.ts +15 -25
- package/wrapper/StreamSocket.js +172 -45
- package/wrapper/base64Encode.d.ts +1 -0
- package/wrapper/base64Encode.js +17 -0
package/wrapper/StreamSocket.js
CHANGED
|
@@ -1,4 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
26
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
27
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -8,9 +31,25 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
31
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
32
|
});
|
|
10
33
|
};
|
|
34
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
35
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
36
|
+
var m = o[Symbol.asyncIterator], i;
|
|
37
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
38
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
39
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
40
|
+
};
|
|
41
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
42
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
43
|
+
};
|
|
11
44
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
45
|
exports.StreamSocket = void 0;
|
|
13
|
-
const
|
|
46
|
+
const ws_1 = __importDefault(require("ws"));
|
|
47
|
+
const uuid_1 = require("uuid");
|
|
48
|
+
const HumeStreamingClient_1 = require("./HumeStreamingClient");
|
|
49
|
+
const base64Encode_1 = require("./base64Encode");
|
|
50
|
+
const serializers = __importStar(require("../serialization"));
|
|
51
|
+
const errors = __importStar(require("../errors"));
|
|
52
|
+
const fs = __importStar(require("fs"));
|
|
14
53
|
class StreamSocket {
|
|
15
54
|
constructor({ websocket, config, streamWindowMs }) {
|
|
16
55
|
this.websocket = websocket;
|
|
@@ -18,33 +57,62 @@ class StreamSocket {
|
|
|
18
57
|
this.streamWindowMs = streamWindowMs;
|
|
19
58
|
}
|
|
20
59
|
/**
|
|
21
|
-
* Send
|
|
60
|
+
* Send file on the `StreamSocket`
|
|
22
61
|
*
|
|
23
|
-
* @param
|
|
24
|
-
* @param config
|
|
25
|
-
*
|
|
26
|
-
* set when initializing the `StreamSocket`
|
|
62
|
+
* @param file A fs.ReadStream | File | Blob
|
|
63
|
+
* @param config This method is intended for use with a `LanguageConfig`.
|
|
64
|
+
* When the socket is configured for other modalities this method will fail.
|
|
27
65
|
*/
|
|
28
|
-
sendFile({
|
|
66
|
+
sendFile({ file, config, }) {
|
|
67
|
+
var file_1, file_1_1;
|
|
68
|
+
var e_1, _a;
|
|
29
69
|
return __awaiter(this, void 0, void 0, function* () {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
70
|
+
if (config != null) {
|
|
71
|
+
this.config = config;
|
|
72
|
+
}
|
|
73
|
+
let contents = "";
|
|
74
|
+
if (file instanceof fs.ReadStream) {
|
|
75
|
+
const chunks = [];
|
|
76
|
+
try {
|
|
77
|
+
for (file_1 = __asyncValues(file); file_1_1 = yield file_1.next(), !file_1_1.done;) {
|
|
78
|
+
const chunk = file_1_1.value;
|
|
79
|
+
chunks.push(Buffer.from(chunk));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
83
|
+
finally {
|
|
84
|
+
try {
|
|
85
|
+
if (file_1_1 && !file_1_1.done && (_a = file_1.return)) yield _a.call(file_1);
|
|
86
|
+
}
|
|
87
|
+
finally { if (e_1) throw e_1.error; }
|
|
88
|
+
}
|
|
89
|
+
contents = Buffer.concat(chunks).toString("base64");
|
|
90
|
+
}
|
|
91
|
+
else if (file instanceof Blob) {
|
|
92
|
+
const toBase64 = (file) => new Promise((res, err) => {
|
|
93
|
+
const reader = new FileReader();
|
|
94
|
+
reader.readAsDataURL(file);
|
|
95
|
+
reader.onload = () => res(reader.result);
|
|
96
|
+
});
|
|
97
|
+
contents = yield toBase64(file);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
throw new errors.HumeError({ message: `file must be one of ReadStream or Blob.` });
|
|
101
|
+
}
|
|
102
|
+
const request = {
|
|
103
|
+
payloadId: (0, uuid_1.v4)(),
|
|
104
|
+
data: contents,
|
|
105
|
+
models: this.config,
|
|
106
|
+
rawText: false,
|
|
107
|
+
};
|
|
108
|
+
if (this.streamWindowMs != null) {
|
|
109
|
+
request.streamWindowMs = this.streamWindowMs;
|
|
110
|
+
}
|
|
111
|
+
const response = yield this.send(request);
|
|
112
|
+
if (response == null) {
|
|
113
|
+
throw new errors.HumeError({ message: `Received no response after sending file: ${file}` });
|
|
114
|
+
}
|
|
115
|
+
return response;
|
|
48
116
|
});
|
|
49
117
|
}
|
|
50
118
|
/**
|
|
@@ -54,16 +122,26 @@ class StreamSocket {
|
|
|
54
122
|
* @param config This method is intended for use with a `LanguageConfig`.
|
|
55
123
|
* When the socket is configured for other modalities this method will fail.
|
|
56
124
|
*/
|
|
57
|
-
sendText({ text, config
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
125
|
+
sendText({ text, config }) {
|
|
126
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
127
|
+
if (config != null) {
|
|
128
|
+
this.config = config;
|
|
129
|
+
}
|
|
130
|
+
const request = {
|
|
131
|
+
payloadId: (0, uuid_1.v4)(),
|
|
132
|
+
data: text,
|
|
133
|
+
rawText: true,
|
|
134
|
+
models: this.config,
|
|
135
|
+
};
|
|
136
|
+
if (this.streamWindowMs != null) {
|
|
137
|
+
request.streamWindowMs = this.streamWindowMs;
|
|
138
|
+
}
|
|
139
|
+
const response = yield this.send(request);
|
|
140
|
+
if (response == null) {
|
|
141
|
+
throw new errors.HumeError({ message: `Received no response after sending text: ${text}` });
|
|
142
|
+
}
|
|
143
|
+
return response;
|
|
144
|
+
});
|
|
67
145
|
}
|
|
68
146
|
/**
|
|
69
147
|
* Send facemesh landmarks on the `StreamSocket`
|
|
@@ -76,10 +154,12 @@ class StreamSocket {
|
|
|
76
154
|
* If set these configurations will overwrite existing configurations
|
|
77
155
|
*/
|
|
78
156
|
sendFacemesh({ landmarks, config, }) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
157
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
158
|
+
const response = this.sendText({
|
|
159
|
+
text: (0, base64Encode_1.base64Encode)(JSON.stringify(landmarks)),
|
|
160
|
+
config,
|
|
161
|
+
});
|
|
162
|
+
return response;
|
|
83
163
|
});
|
|
84
164
|
}
|
|
85
165
|
/**
|
|
@@ -90,9 +170,11 @@ class StreamSocket {
|
|
|
90
170
|
* streaming connection without leaking context across media samples.
|
|
91
171
|
*/
|
|
92
172
|
reset() {
|
|
93
|
-
this
|
|
94
|
-
|
|
95
|
-
|
|
173
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
174
|
+
yield this.send({
|
|
175
|
+
resetStream: true,
|
|
176
|
+
});
|
|
177
|
+
});
|
|
96
178
|
}
|
|
97
179
|
/**
|
|
98
180
|
*
|
|
@@ -100,9 +182,11 @@ class StreamSocket {
|
|
|
100
182
|
*
|
|
101
183
|
*/
|
|
102
184
|
getJobDetails() {
|
|
103
|
-
this
|
|
104
|
-
|
|
105
|
-
|
|
185
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
186
|
+
yield this.send({
|
|
187
|
+
jobDetails: true,
|
|
188
|
+
});
|
|
189
|
+
});
|
|
106
190
|
}
|
|
107
191
|
/**
|
|
108
192
|
* Closes the underlying socket.
|
|
@@ -110,5 +194,48 @@ class StreamSocket {
|
|
|
110
194
|
close() {
|
|
111
195
|
this.websocket.close();
|
|
112
196
|
}
|
|
197
|
+
send(payload) {
|
|
198
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
199
|
+
yield this.tillSocketOpen();
|
|
200
|
+
const jsonPayload = yield serializers.ModelsInput.jsonOrThrow(payload, {
|
|
201
|
+
unrecognizedObjectKeys: "strip",
|
|
202
|
+
});
|
|
203
|
+
this.websocket.send(JSON.stringify(jsonPayload));
|
|
204
|
+
const response = yield new Promise((resolve, reject) => {
|
|
205
|
+
this.websocket.addEventListener("message", (event) => {
|
|
206
|
+
const response = (0, HumeStreamingClient_1.parse)(event.data);
|
|
207
|
+
resolve(response);
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
if (response != null && isError(response)) {
|
|
211
|
+
throw new errors.HumeError({ message: `CODE ${response.code}: ${response.error}` });
|
|
212
|
+
}
|
|
213
|
+
if (response != null && isWarning(response)) {
|
|
214
|
+
throw new errors.HumeError({ message: `CODE ${response.code}: ${response.warning}` });
|
|
215
|
+
}
|
|
216
|
+
return response;
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
tillSocketOpen() {
|
|
220
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
221
|
+
if (this.websocket.readyState === ws_1.default.OPEN) {
|
|
222
|
+
return this.websocket;
|
|
223
|
+
}
|
|
224
|
+
return new Promise((resolve, reject) => {
|
|
225
|
+
this.websocket.addEventListener("open", () => {
|
|
226
|
+
resolve(this.websocket);
|
|
227
|
+
});
|
|
228
|
+
this.websocket.addEventListener("error", (event) => {
|
|
229
|
+
reject(event);
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
}
|
|
113
234
|
}
|
|
114
235
|
exports.StreamSocket = StreamSocket;
|
|
236
|
+
function isError(response) {
|
|
237
|
+
return response.error != null;
|
|
238
|
+
}
|
|
239
|
+
function isWarning(response) {
|
|
240
|
+
return response.warning != null;
|
|
241
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function base64Encode(str: string): string;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.base64Encode = void 0;
|
|
4
|
+
function base64Encode(str) {
|
|
5
|
+
if (typeof Buffer === "function") {
|
|
6
|
+
// Node.js environment
|
|
7
|
+
return Buffer.from(str).toString("base64");
|
|
8
|
+
}
|
|
9
|
+
else if (typeof btoa === "function") {
|
|
10
|
+
// Browser environment
|
|
11
|
+
return btoa(str);
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
throw new Error("Base64 encoding not supported in this environment.");
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.base64Encode = base64Encode;
|