UNPKG

noirsight

Version:

Deepfake Detection & Content Analyser Wrapper

184 lines (183 loc) 7.61 kB
"use strict"; 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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ArticleAnalyser = exports.DeepfakeImageAnalyser = exports.DeepfakeVideoAnalyser = exports.NoirSight = void 0; const axios_1 = __importDefault(require("axios")); const refineConfidence_1 = __importDefault(require("./utils/refineConfidence")); const generateImageAnnotation_1 = __importDefault(require("./utils/generateImageAnnotation")); class NoirSight { constructor(apiKey, userId, baseUrl = "http://127.0.0.1:8000/api") { this.apiKey = apiKey; this.userId = userId; this.baseUrl = baseUrl; } } exports.NoirSight = NoirSight; class DeepfakeVideoAnalyser extends NoirSight { constructor(apiKey, userId) { super(apiKey, userId); } analyseVideo(url) { return __awaiter(this, void 0, void 0, function* () { var _a; try { if (!this.apiKey) { throw new Error("Cannot find API KEY"); } if (!this.userId) { throw new Error("Cannot find User ID"); } const response = yield axios_1.default.post(`${this.baseUrl}/predict/video/url`, { url }, { headers: { "x-user-id": this.userId, "x-api-key": this.apiKey } }); const result = (0, refineConfidence_1.default)(Object.assign({}, response.data)); const annotedUrl = (0, generateImageAnnotation_1.default)(url, result.label); return Object.assign(Object.assign({}, result), { annotedUrl }); } catch (error) { if (axios_1.default.isAxiosError(error)) { if (error.response) { return { error: true, status: error.response.status, data: error.response.data, message: ((_a = error.response.data) === null || _a === void 0 ? void 0 : _a.error) || 'Something went wrong! Please try again later' }; } else if (error.request) { return { error: true, message: 'No response from server. Network issue?' }; } } return { error: true, message: 'Unexpected error occurred.', detail: error.message }; } }); } } exports.DeepfakeVideoAnalyser = DeepfakeVideoAnalyser; class DeepfakeImageAnalyser extends NoirSight { constructor(apiKey, userId) { super(apiKey, userId); } analyseImage(url) { return __awaiter(this, void 0, void 0, function* () { var _a; try { if (!this.apiKey) { throw new Error("Cannot find API KEY"); } if (!this.userId) { throw new Error("Cannot find User ID"); } const response = yield axios_1.default.post(`${this.baseUrl}/predict/image/url`, { url }, { headers: { "x-user-id": this.userId, "x-api-key": this.apiKey } }); const result = (0, refineConfidence_1.default)(Object.assign({}, response.data)); const annotedUrl = (0, generateImageAnnotation_1.default)(url, result.label); return Object.assign(Object.assign({}, result), { annotedUrl }); } catch (error) { if (axios_1.default.isAxiosError(error)) { if (error.response) { return { error: true, status: error.response.status, data: error.response.data, message: ((_a = error.response.data) === null || _a === void 0 ? void 0 : _a.error) || 'Something went wrong! Please try again later' }; } else if (error.request) { return { error: true, message: 'No response from server. Network issue?' }; } } return { error: true, message: 'Unexpected error occurred.', detail: error.message }; } }); } } exports.DeepfakeImageAnalyser = DeepfakeImageAnalyser; class ArticleAnalyser extends NoirSight { constructor(apiKey, userId) { super(apiKey, userId); } analyseArticle(text) { return __awaiter(this, void 0, void 0, function* () { var _a; try { if (!this.apiKey) { throw new Error("Cannot find API KEY"); } if (!this.userId) { throw new Error("Cannot find User ID"); } const response = yield axios_1.default.post(`${this.baseUrl}/analyze`, { text }, { headers: { "x-user-id": this.userId, "x-api-key": this.apiKey } }); return response.data; } catch (error) { if (axios_1.default.isAxiosError(error)) { if (error.response) { return { error: true, status: error.response.status, data: error.response.data, message: ((_a = error.response.data) === null || _a === void 0 ? void 0 : _a.error) || 'Something went wrong! Please try again later' }; } else if (error.request) { return { error: true, message: 'No response from server. Network issue?' }; } } return { error: true, message: 'Unexpected error occurred.', detail: error.message }; } }); } } exports.ArticleAnalyser = ArticleAnalyser;