import IconFont from '@/components/icon-font'; import ModalFooter from '@/components/modal-footer'; import SealAutoComplete from '@/components/seal-form/auto-complete'; import SealInput from '@/components/seal-form/seal-input'; import SealSelect from '@/components/seal-form/seal-select'; import TooltipList from '@/components/tooltip-list'; import { PageAction } from '@/config'; import { PageActionType } from '@/config/types'; import useAppUtils from '@/hooks/use-app-utils'; import { useIntl } from '@umijs/max'; import { Button, Form, Modal, Typography } from 'antd'; import _ from 'lodash'; import React, { useEffect, useMemo, useRef } from 'react'; import { backendOptionsMap, backendTipsList, excludeFields, getSourceRepoConfigValue, localPathTipsList, modelSourceMap, ollamaModelOptions, sourceOptions } from '../config'; import { FormData, ListItem } from '../config/types'; import { useCheckCompatibility } from '../hooks'; import AdvanceConfig from './advance-config'; import ColumnWrapper from './column-wrapper'; import CompatibilityAlert from './compatible-alert'; type AddModalProps = { title: string; action: PageActionType; open: boolean; updateFormInitials: { data?: ListItem; gpuOptions: any[]; isGGUF: boolean; }; onOk: (values: FormData) => void; onCancel: () => void; }; const SEARCH_SOURCE = [ modelSourceMap.huggingface_value, modelSourceMap.modelscope_value ]; const UpdateModal: React.FC = (props) => { const { title, action, open, onOk, onCancel, updateFormInitials: { gpuOptions, isGGUF, data: formData } } = props || {}; const { handleShowCompatibleAlert, handleUpdateWarning, setWarningStatus, handleEvaluate, generateGPUIds, handleOnValuesChange, checkTokenRef, warningStatus } = useCheckCompatibility(); const { getRuleMessage } = useAppUtils(); const [form] = Form.useForm(); const intl = useIntl(); const localPathCache = useRef(''); const submitAnyway = useRef(false); const handleSetGPUIds = (backend: string) => { if (backend === backendOptionsMap.llamaBox) { return; } const gpuids = form.getFieldValue(['gpu_selector', 'gpu_ids']); if (!gpuids?.length) { return; } if (gpuids.length > 1 && Array.isArray(gpuids[0])) { form.setFieldValue(['gpu_selector', 'gpu_ids'], [gpuids[0]]); } }; // trigger from local_path change or backend change const handleBackendChangeBefore = async () => { const localPath = form.getFieldValue?.('local_path'); const backend = form.getFieldValue?.('backend'); const res = handleUpdateWarning?.({ backend, localPath: localPath, source: formData?.source as string }); if (!res.show) { const values = form.getFieldsValue?.(); const data = getSourceRepoConfigValue(formData?.source as string, values); const evalutionData = await handleEvaluate( _.omit(data.values, [ 'cpu_offloading', 'distributed_inference_across_workers' ]) ); handleShowCompatibleAlert?.(evalutionData); } else { setWarningStatus?.(res); } }; const handleBackendChange = (val: string) => { if (val === backendOptionsMap.llamaBox) { form.setFieldsValue({ distributed_inference_across_workers: true, cpu_offloading: true }); } form.setFieldValue('backend_version', ''); handleSetGPUIds(val); handleBackendChangeBefore(); }; const handleOnFocus = () => { localPathCache.current = form.getFieldValue('local_path'); }; const handleLocalPathBlur = (e: any) => { const value = e.target.value; if (value === localPathCache.current && value) { return; } const isEndwithGGUF = _.endsWith(value, '.gguf'); const isBlobFile = value.split('/').pop().includes('sha256'); let backend = backendOptionsMap.llamaBox; if (!isEndwithGGUF || !isBlobFile) { backend = backendOptionsMap.vllm; } handleBackendChange?.(backend); form.setFieldValue('backend', backend); }; const renderHuggingfaceFields = () => { return ( <> name="repo_id" rules={[ { required: true, message: intl.formatMessage( { id: 'common.form.rule.input' }, { name: intl.formatMessage({ id: 'models.form.repoid' }) } ) } ]} > {isGGUF && ( name="file_name" rules={[ { required: true, message: intl.formatMessage( { id: 'common.form.rule.input' }, { name: intl.formatMessage({ id: 'models.form.filename' }) } ) } ]} > )} ); }; const renderOllamaModelFields = () => { return ( <> name="ollama_library_model_name" rules={[ { required: true, message: intl.formatMessage( { id: 'common.form.rule.input' }, { name: intl.formatMessage({ id: 'models.table.name' }) } ) } ]} > {intl.formatMessage({ id: 'models.form.ollamalink' })} } label={intl.formatMessage({ id: 'model.form.ollama.model' })} required > ); }; const renderLocalPathFields = () => { return ( <> name="local_path" key="local_path" rules={[ { required: true, message: intl.formatMessage( { id: 'common.form.rule.input' }, { name: intl.formatMessage({ id: 'models.form.filePath' }) } ) } ]} > } required > ); }; const renderFieldsBySource = useMemo(() => { if (SEARCH_SOURCE.includes(formData?.source || '')) { return renderHuggingfaceFields(); } if (formData?.source === modelSourceMap.ollama_library_value) { return renderOllamaModelFields(); } if (formData?.source === modelSourceMap.local_path_value) { return renderLocalPathFields(); } return null; }, [formData?.source, isGGUF, intl]); const handleSumit = () => { form.submit(); }; const handleSubmitAnyway = async () => { submitAnyway.current = true; form.submit?.(); }; const handleOk = async (data: FormData) => { const formdata = getSourceRepoConfigValue(data.source, data).values; let submitData = {} as FormData; const isVllmOrVoxBox = [ backendOptionsMap.vllm, backendOptionsMap.voxBox ].includes(formdata.backend); submitData = { ..._.omit(formdata, ['scheduleType']), categories: formdata.categories ? [formdata.categories] : [], worker_selector: formdata.scheduleType === 'manual' ? null : formdata.worker_selector, ...(isVllmOrVoxBox ? { distributed_inference_across_workers: false, cpu_offloading: false } : {}), ...generateGPUIds(formdata) }; onOk(submitData); }; const onValuesChange = (changedValues: any, allValues: any) => { const fieldName = Object.keys(changedValues)[0]; if (excludeFields.includes(fieldName)) { return; } handleOnValuesChange({ changedValues, allValues, source: formData?.source as string }); }; const handleOnClose = () => { onCancel?.(); }; useEffect(() => { if (open && formData) { form.setFieldsValue(formData); } if (!open) { checkTokenRef.current?.cancel?.(); setWarningStatus({ show: false, message: '' }); } }, [open, formData]); return ( {intl.formatMessage({ id: 'models.form.submit.anyway' })} ) } > } > { setWarningStatus({ show: false, message: '' }); }} warningStatus={warningStatus} contentStyle={{ paddingInline: 0 }} > } >
name="name" rules={[ { required: true, message: getRuleMessage('input', 'common.table.name') } ]} > name="source" rules={[ { required: true, message: getRuleMessage('select', 'models.form.source') } ]} > {action === PageAction.EDIT && ( )} {renderFieldsBySource} } options={[ { label: `llama-box`, value: backendOptionsMap.llamaBox, disabled: formData?.source === modelSourceMap.local_path_value ? false : !isGGUF }, { label: 'vLLM', value: backendOptionsMap.vllm, disabled: formData?.source === modelSourceMap.local_path_value ? false : isGGUF }, { label: 'vox-box', value: backendOptionsMap.voxBox, disabled: formData?.source === modelSourceMap.local_path_value ? false : isGGUF } ]} disabled={ action === PageAction.EDIT && formData?.source !== modelSourceMap.local_path_value } > name="replicas" rules={[ { required: true, message: getRuleMessage('input', 'models.form.replicas') } ]} > name="description">
); }; export default UpdateModal;