sofya.transcription
Version:
a JavaScript library that provides a robust and flexible solution for real-time audio transcription. It is designed to transcribe audio streams and can be easily integrated into web applications.
279 lines • 14.2 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SofyaTranscriber = void 0;
var TranscriptionServiceFactory_1 = require("./TranscriptionServiceFactory");
var api_1 = require("../../api/api");
var EventEmitter_1 = __importDefault(require("../../shared/EventEmitter"));
var SofyaTranscriber = /** @class */ (function (_super) {
__extends(SofyaTranscriber, _super);
function SofyaTranscriber(connection) {
var _this = _super.call(this) || this;
_this.transcriptionService = null;
_this.initializeTranscriptionService(connection);
return _this;
}
SofyaTranscriber.prototype.startTranscription = function (mediaStream) {
if (!this.transcriptionService) {
throw new Error("Transcription Service Not Ready. Try again.");
}
this.transcriptionService.startTranscription(mediaStream);
};
SofyaTranscriber.prototype.stopTranscription = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!this.transcriptionService) {
throw new Error("Transcription Service Not Ready. Try again.");
}
return [4 /*yield*/, this.transcriptionService.stopTranscription()];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
SofyaTranscriber.prototype.pauseTranscription = function () {
if (!this.transcriptionService) {
throw new Error("Transcription Service Not Ready. Try again.");
}
this.transcriptionService.pauseTranscription();
};
SofyaTranscriber.prototype.resumeTranscription = function () {
if (!this.transcriptionService) {
throw new Error("Transcription Service Not Ready. Try again.");
}
this.transcriptionService.resumeTranscription();
};
SofyaTranscriber.prototype.initializeTranscriptionService = function (connection) {
return __awaiter(this, void 0, void 0, function () {
var endpointConfig, _a, endpointError_1;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
if (!("apiKey" in connection)) return [3 /*break*/, 2];
return [4 /*yield*/, this.getProvider(connection.apiKey, connection.config)];
case 1:
_b.sent();
_b.label = 2;
case 2:
if (!("endpoint" in connection)) return [3 /*break*/, 6];
_b.label = 3;
case 3:
_b.trys.push([3, 5, , 6]);
endpointConfig = __assign(__assign({}, connection.config), { endpoint: { endpoint: connection.endpoint } });
_a = this;
return [4 /*yield*/, TranscriptionServiceFactory_1.TranscriptionServiceFactory.create(connection.provider, endpointConfig)];
case 4:
_a.transcriptionService = _b.sent();
this.addTranscriptionListeners();
this.emit("ready");
return [2 /*return*/];
case 5:
endpointError_1 = _b.sent();
console.warn("Failed to connect to endpoint: ", endpointError_1);
return [3 /*break*/, 6];
case 6: return [2 /*return*/];
}
});
});
};
SofyaTranscriber.prototype.getProvider = function (apiKey, config) {
return __awaiter(this, void 0, void 0, function () {
var providersInfo, providers, _i, providers_1, providerInfo, provider, additional_info, connected, _a, _b, endpoint, endpointConfig, _c, endpointError_2, providerConfig, _d, providerError_1, error_1;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
_e.trys.push([0, 14, , 15]);
return [4 /*yield*/, this.authenticate(apiKey)];
case 1:
providersInfo = _e.sent();
providers = providersInfo.providers;
_i = 0, providers_1 = providers;
_e.label = 2;
case 2:
if (!(_i < providers_1.length)) return [3 /*break*/, 13];
providerInfo = providers_1[_i];
provider = providerInfo.provider, additional_info = providerInfo.additional_info;
if (!(additional_info.endpoints && additional_info.endpoints.length > 0)) return [3 /*break*/, 9];
connected = false;
_a = 0, _b = additional_info.endpoints;
_e.label = 3;
case 3:
if (!(_a < _b.length)) return [3 /*break*/, 8];
endpoint = _b[_a];
_e.label = 4;
case 4:
_e.trys.push([4, 6, , 7]);
endpointConfig = __assign(__assign(__assign({}, config), additional_info), { endpoint: endpoint });
_c = this;
return [4 /*yield*/, TranscriptionServiceFactory_1.TranscriptionServiceFactory.create(provider, endpointConfig)];
case 5:
_c.transcriptionService =
_e.sent();
this.addTranscriptionListeners();
connected = true;
this.emit("ready");
return [2 /*return*/];
case 6:
endpointError_2 = _e.sent();
console.warn("Failed to connect to endpoint: ", endpointError_2);
return [3 /*break*/, 7];
case 7:
_a++;
return [3 /*break*/, 3];
case 8:
// If no endpoints succeed, move to the next provider
if (!connected) {
return [3 /*break*/, 12];
}
return [3 /*break*/, 12];
case 9:
_e.trys.push([9, 11, , 12]);
providerConfig = __assign(__assign({}, config), additional_info);
_d = this;
return [4 /*yield*/, TranscriptionServiceFactory_1.TranscriptionServiceFactory.create(provider, providerConfig)];
case 10:
_d.transcriptionService =
_e.sent();
this.addTranscriptionListeners();
this.emit("ready");
return [2 /*return*/];
case 11:
providerError_1 = _e.sent();
console.warn("Failed to initialize provider: ", providerError_1);
return [3 /*break*/, 12];
case 12:
_i++;
return [3 /*break*/, 2];
case 13: throw new Error("Failed to connect to any provider.");
case 14:
error_1 = _e.sent();
console.error("Error during initialization:", error_1);
return [3 /*break*/, 15];
case 15: return [2 /*return*/];
}
});
});
};
SofyaTranscriber.prototype.addTranscriptionListeners = function () {
var _this = this;
if (this.transcriptionService) {
// @ts-ignore
this.transcriptionService.on("recognizing", function (text) {
_this.emit("recognizing", text);
});
// @ts-ignore
this.transcriptionService.on("recognized", function (text) {
_this.emit("recognized", text);
});
// @ts-ignore
this.transcriptionService.on("recognized_diarization", function (diarization) {
_this.emit("recognized_diarization", diarization);
});
// @ts-ignore
this.transcriptionService.on("error", function (error) {
_this.emit("error", error);
});
// @ts-ignore
this.transcriptionService.on("stopped", function () {
_this.emit("stopped");
});
// @ts-ignore
this.transcriptionService.on("connected", function () {
_this.emit("connected");
});
}
};
SofyaTranscriber.prototype.authenticate = function (apiKey) {
return __awaiter(this, void 0, void 0, function () {
var authData, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 2, , 3]);
return [4 /*yield*/, (0, api_1.authService)(apiKey)];
case 1:
authData = _b.sent();
if ((authData === null || authData === void 0 ? void 0 : authData.providers) && Array.isArray(authData.providers)) {
return [2 /*return*/, authData];
}
throw new Error("Invalid authentication data format.");
case 2:
_a = _b.sent();
throw new Error("Could not authenticate. Validate your apiKey");
case 3: return [2 /*return*/];
}
});
});
};
return SofyaTranscriber;
}(EventEmitter_1.default));
exports.SofyaTranscriber = SofyaTranscriber;
//# sourceMappingURL=SofyaTranscriber.js.map