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.9 KB
Raw Blame
import { Button } from '@invoke-ai/ui-library';
import { toast } from 'features/toast/toast';
import { memo, useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import { PiSparkleFill } from 'react-icons/pi';
import { useReidentifyModelMutation } from 'services/api/endpoints/models';
import { type AnyModelConfigWithExternal, isExternalApiModelConfig } from 'services/api/types';

import { isExternalModel } from './isExternalModel';

interface Props {
  modelConfig: AnyModelConfigWithExternal;
}

export const ModelReidentifyButton = memo(({ modelConfig }: Props) => {
  const { t } = useTranslation();
  const [reidentifyModel, { isLoading }] = useReidentifyModelMutation();
  const isExternal = isExternalApiModelConfig(modelConfig) || isExternalModel(modelConfig.path);

  const onClick = useCallback(() => {
    reidentifyModel({ key: modelConfig.key })
      .unwrap()
      .then(({ type }) => {
        if (type === 'unknown') {
          toast({
            id: 'MODEL_REIDENTIFY_UNKNOWN',
            title: t('modelManager.reidentifyUnknown'),
            status: 'warning',
          });
        }
        toast({
          id: 'MODEL_REIDENTIFY_SUCCESS',
          title: t('modelManager.reidentifySuccess'),
          status: 'success',
        });
      })
      .catch((_) => {
        toast({
          id: 'MODEL_REIDENTIFY_ERROR',
          title: t('modelManager.reidentifyError'),
          status: 'error',
        });
      });
  }, [modelConfig.key, reidentifyModel, t]);

  if (isExternal) {
    return null;
  }

  return (
    <Button
      onClick={onClick}
      size="sm"
      aria-label={t('modelManager.reidentifyTooltip')}
      tooltip={t('modelManager.reidentifyTooltip')}
      isLoading={isLoading}
      flexShrink={0}
      leftIcon={<PiSparkleFill />}
    >
      {t('modelManager.reidentify')}
    </Button>
  );
});

ModelReidentifyButton.displayName = 'ModelReidentifyButton';