import {z} from 'zod' export type KerasModelTypeEnum = 'int8' | 'float32' | 'akida' | 'requiresRetrain'; export type KerasVisualLayerType = 'dense' | 'conv1d' | 'conv2d' | 'reshape' | 'flatten' | 'dropout' | 'batchNormalization' | 'transfer_mobilenetv2_a35' | 'transfer_mobilenetv2_a1' | 'transfer_mobilenetv2_a05' | 'transfer_mobilenetv2_160_a1' | 'transfer_mobilenetv2_160_a75' | 'transfer_mobilenetv2_160_a5' | 'transfer_mobilenetv2_160_a35' | 'transfer_mobilenetv1_a25_d100' | 'transfer_mobilenetv1_a2_d100' | 'transfer_mobilenetv1_a1_d100' | 'transfer_kws_mobilenetv1_a1_d100' | 'transfer_kws_mobilenetv2_a35_d100' | 'transfer_kws_syntiant_ndp10x' | 'transfer_kws_conv2d_tiny' | 'object_ssd_mobilenet_v2_fpnlite_320x320' | 'fomo_mobilenet_v2_a01' | 'fomo_mobilenet_v2_a35' | 'transfer_organization' | 'transfer_akidanet_imagenet_160_a100' | 'transfer_akidanet_imagenet_160_a50' | 'transfer_akidanet_imagenet_160_a25' | 'transfer_akidanet_imagenet_224_a100' | 'transfer_akidanet_imagenet_224_a50' | 'transfer_akidanet_imagenet_224_a25' | 'fomo_akidanet_a50' | 'fomo_ad_gmm' | 'fomo_ad_patchcore'; export type KerasVisualLayer = {type: KerasVisualLayerType, neurons?: number | undefined, kernelSize?: number | undefined, dropoutRate?: number | undefined, columns?: number | undefined, stack?: number | undefined, enabled?: boolean | undefined, organizationModelId?: number | undefined}; export type AugmentationPolicyImageEnum = 'none' | 'all'; export type AugmentationPolicySpectrogram = {enabled: boolean, warping?: boolean | undefined, freqMasking?: ('none' | 'low' | 'high') | undefined, timeMasking?: ('none' | 'low' | 'high') | undefined, gaussianNoise?: ('none' | 'low' | 'high') | undefined}; export type AkidaEdgeLearningConfig = {enabled: boolean, additionalClasses?: number | undefined, neuronsPerClass?: number | undefined}; export type AnomalyCapacity = 'low' | 'medium' | 'high'; export type KerasModelVariantEnum = 'int8' | 'float32' | 'akida'; export type BlockParamsVisualAnomalyPatchcore = {backbone?: string | undefined, numLayers?: number | undefined, poolSize?: number | undefined, samplingRatio?: number | undefined, numNearestNeighbors?: number | undefined}; export type BlockParamsVisualAnomalyGmm = {backbone?: string | undefined}; export type BlockParameters = BlockParamsVisualAnomalyPatchcore | BlockParamsVisualAnomalyGmm; export type ImpulseSpecificGpuTrainingProcessor = 'cpu' | 'gpu'; export type KFoldsValidationConfig = {numFolds: number, includeFinalTrainingRun?: boolean | undefined, extraEpochs?: number | undefined}; export type SetKerasParameterRequest = {mode?: ('expert' | 'visual') | undefined, minimumConfidenceRating?: number | undefined, selectedModelType?: KerasModelTypeEnum | undefined, script?: string | undefined, visualLayers?: Array | undefined, trainingCycles?: number | undefined, learningRate?: number | undefined, batchSize?: number | undefined, trainTestSplit?: number | undefined, autoClassWeights?: boolean | undefined, useLearnedOptimizer?: boolean | undefined, augmentationPolicyImage?: AugmentationPolicyImageEnum | undefined, augmentationPolicySpectrogram?: AugmentationPolicySpectrogram | undefined, profileInt8?: boolean | undefined, skipEmbeddingsAndMemory?: boolean | undefined, akidaEdgeLearningConfig?: AkidaEdgeLearningConfig | undefined, customValidationMetadataKey?: string | undefined, showAdvancedTrainingSettings?: boolean | undefined, showAugmentationTrainingSettings?: boolean | undefined, customParameters?: ({[key: string]: string | null}) | undefined, anomalyCapacity?: AnomalyCapacity | undefined, lastShownModelVariant?: KerasModelVariantEnum | undefined, blockParameters?: BlockParameters | undefined, trainingProcessor?: ImpulseSpecificGpuTrainingProcessor | undefined, kFoldsValidationConfig?: KFoldsValidationConfig | undefined, assertGpuUsagePercentage?: (number | null) | undefined}; export type CreateApiApiProjectIdTrainingKerasLearnIdArgs = {projectId: number, learnId: number, body: SetKerasParameterRequest}; export type CreateApiApiProjectIdTrainingKerasLearnIdOptions = {baseUrl?: string | undefined, ApiKeyAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const genericApiResponse = z.object({success: z.boolean(), error: z.string().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Keras settings * * POST /api/{projectId}/training/keras/{learnId} * * Also accepts auth: JWTAuthentication, JWTHttpHeaderAuthentication, OAuth2 */ export const createApiApiProjectIdTrainingKerasLearnId = async (args: CreateApiApiProjectIdTrainingKerasLearnIdArgs, options?: CreateApiApiProjectIdTrainingKerasLearnIdOptions) => { const {baseUrl = "https://studio.edgeimpulse.com/v1", fetch: fetchFn = fetch, ApiKeyAuthentication} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuthentication !== undefined) headers.set("x-api-key", ApiKeyAuthentication) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.projectId))}/training/keras/${encodeURIComponent(String(args.learnId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(genericApiResponse, await res.json()) };