@realitydefender/realitydefender
Version:
SDK for the Reality Defender API for deepfake detection
83 lines (82 loc) • 3.26 kB
JavaScript
;
/**
* Handling detection results
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMediaResult = getMediaResult;
exports.formatResult = formatResult;
exports.getDetectionResult = getDetectionResult;
const constants_1 = require("../core/constants");
const errors_1 = require("../errors");
const async_1 = require("../utils/async");
/**
* Get the raw media result from the API
* @param client HTTP client
* @param requestId Request ID
* @returns Raw media response
*/
async function getMediaResult(client, requestId) {
try {
const path = `${constants_1.API_PATHS.MEDIA_RESULT}/${requestId}`;
return await client.get(path);
}
catch (error) {
if (error instanceof errors_1.RealityDefenderError) {
throw error;
}
throw new errors_1.RealityDefenderError(`Failed to get result: ${error.message}`, 'unknown_error');
}
}
/**
* Format the raw API response into a user-friendly result
* @param response Raw API response
* @returns Simplified detection result
*/
function formatResult(response) {
// Extract active models (not NOT_APPLICABLE)
const activeModels = response.models.filter(model => model.status !== 'NOT_APPLICABLE' && model.code !== 'not_applicable');
// Replace FAKE with ARTIFICIAL in response status
const status = response.resultsSummary.status === 'FAKE' ? 'ARTIFICIAL' : response.resultsSummary.status;
// Normalize the final score from 0-100 to 0-1 range
const normalizedScore = response.resultsSummary.metadata.finalScore !== null
? response.resultsSummary.metadata.finalScore / 100
: null;
return {
status: status,
score: normalizedScore,
models: activeModels.map(model => ({
name: model.name,
// Replace FAKE with ARTIFICIAL in model status
status: model.status === 'FAKE' ? 'ARTIFICIAL' : model.status,
// Score between 0-1 range or null if not available
score: model.predictionNumber,
})),
};
}
/**
* Get detection results for a media request
* @param client HTTP client
* @param requestId Request ID
* @param options Detection options for polling configuration
* @returns Detection results
*/
async function getDetectionResult(client, requestId, options = {}) {
let attempts = 0;
const { maxAttempts = Number.MAX_SAFE_INTEGER, pollingInterval = constants_1.DEFAULT_POLLING_INTERVAL } = options;
while (attempts < maxAttempts) {
const mediaResult = await getMediaResult(client, requestId);
// If the status is not ANALYZING, return the results immediately
if (mediaResult.resultsSummary.status !== 'ANALYZING') {
return formatResult(mediaResult);
}
// If we've reached the maximum attempts, return the results even if still analyzing
if (++attempts >= maxAttempts) {
return formatResult(mediaResult);
}
// Wait for the polling interval before trying again
await (0, async_1.sleep)(pollingInterval);
}
// This should never be reached, but TypeScript needs it
const mediaResult = await getMediaResult(client, requestId);
return formatResult(mediaResult);
}