Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

1.6 KB
Raw Blame
import { EMPTY_ARRAY } from 'app/store/constants';
import { useCallback } from 'react';
import { modelConfigsAdapterSelectors, useGetModelConfigsQuery } from 'services/api/endpoints/models';
import type { StarterModel } from 'services/api/types';

type ModelInstallArg = {
  config: Pick<StarterModel, 'name' | 'base' | 'type' | 'description' | 'format' | 'variant'>;
  source: string;
};

/**
 * Flattens a starter model and its dependencies into a list of models, including the starter model itself.
 */
export const flattenStarterModel = (starterModel: StarterModel): StarterModel[] => {
  return [starterModel, ...(starterModel.dependencies || [])];
};

export const useBuildModelInstallArg = () => {
  const { modelList } = useGetModelConfigsQuery(undefined, {
    selectFromResult: ({ data }) => ({ modelList: data ? modelConfigsAdapterSelectors.selectAll(data) : EMPTY_ARRAY }),
  });

  const getIsInstalled = useCallback(
    ({ source, name, base, type, is_installed, previous_names }: StarterModel): boolean =>
      modelList.some(
        (mc) =>
          is_installed ||
          source === mc.source ||
          (base === mc.base && (name === mc.name || previous_names?.includes(name)) && type === mc.type)
      ),
    [modelList]
  );

  const buildModelInstallArg = useCallback((starterModel: StarterModel): ModelInstallArg => {
    const { name, base, type, source, description, format, variant } = starterModel;

    return {
      config: {
        name,
        base,
        type,
        description,
        format,
        variant,
      },
      source,
    };
  }, []);

  return { getIsInstalled, buildModelInstallArg };
};