JSFiddle - React, Tailwind, and code Playground

by morphcast

JavaScript 1.7

// Singleton to ensure only one instance of the SDK is running
let aiSdkInstance;

// Function to download AI SDK scripts
async function downloadAiSdk() {
    if (globalThis.CY) {
        throw new Error("AI-SDK has already been downloaded.");
    }
    await loadScript("https://sdk.morphcast.com/mphtools/v1.1/mphtools.js", "cameraPrivacyPopup, compatibilityUI, compatibilityAutoCheck");
    await loadScript("https://ai-sdk.morphcast.com/v1.16/ai-sdk.js");
}

// Function to load external scripts
async function loadScript(url, config = null) {
    return new Promise((resolve, reject) => {
        const script = document.createElement("script");
        script.type = "text/javascript";
        script.onload = () => resolve();
        script.onerror = () => reject(new Error(`Script load error for ${url}`));
        if (config) script.setAttribute('data-config', config);
        script.src = url;
        document.head.appendChild(script);
    });
}

// Function to initialize AI SDK with configured modules
async function initAiSdk() {
    if (aiSdkInstance) {
        throw new Error("An instance of the AI-SDK is already running.");
    }

    aiSdkInstance = await globalThis.CY.loader()
        .licenseKey("9087cfd2037a28a79769fdcaa4962252379cd2e2fd0b") // Replace with your license key
        .maxInputFrameSize(640)
        .addModule(CY.modules().FACE_DETECTOR.name, {
          maxInputFrameSize: 640,
          multiFace: true,
        })
        .addModule(globalThis.CY.modules().FACE_EMOTION.name, {
          smoothness: 0.5, // custom setting
        })
        .addModule(globalThis.CY.modules().FACE_GENDER.name, {})
        .addModule(globalThis.CY.modules().FACE_AGE.name, {
          windowSizeMs: 4000, // custom setting
          maxVarianceCutoff: Math.pow(7, 2),
          numericalStability: 1,
        })
        .addModule(globalThis.CY.modules().FACE_AROUSAL_VALENCE.name, {
          smoothness: 0.9, // custom setting
        })
       ...