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 { PageAction } from '@/config'; import { PageActionType } from '@/config/types'; import { useIntl } from '@umijs/max'; import { Form, Modal } from 'antd'; import _ from 'lodash'; import React, { memo, useEffect, useMemo, useState } from 'react'; import SimpleBar from 'simplebar-react'; import 'simplebar-react/dist/simplebar.min.css'; import { queryGPUList } from '../apis'; import { backendOptionsMap, modelSourceMap, setSourceRepoConfigValue } from '../config'; import { FormData, GPUListItem, ListItem } from '../config/types'; import AdvanceConfig from './advance-config'; type AddModalProps = { title: string; action: PageActionType; open: boolean; data?: ListItem; onOk: (values: FormData) => void; onCancel: () => void; }; const SEARCH_SOURCE = [ modelSourceMap.huggingface_value, modelSourceMap.modelscope_value ]; const UpdateModal: React.FC = (props) => { console.log('addmodel===='); const { title, action, open, onOk, onCancel } = props || {}; const [form] = Form.useForm(); const intl = useIntl(); const [gpuOptions, setGpuOptions] = useState([]); const [isGGUF, setIsGGUF] = useState(false); const [loading, setLoading] = useState(false); const getGPUList = async () => { const data = await queryGPUList(); const list = _.map(data.items, (item: GPUListItem) => { return { ...item, label: item.name, value: `${item.worker_name}-${item.name}-${item.index}` }; }); setGpuOptions(list); }; const sourceOptions = [ { label: 'Hugging Face', value: modelSourceMap.huggingface_value, key: 'huggingface' }, { label: 'Ollama Library', value: modelSourceMap.ollama_library_value, key: 'ollama_library' }, { label: 'ModelScope', value: modelSourceMap.modelscope_value, key: 'model_scope' }, { label: intl.formatMessage({ id: 'models.form.localPath' }), value: modelSourceMap.local_path_value, key: 'local_path' } ]; useEffect(() => { if (action === PageAction.EDIT && open) { const result = setSourceRepoConfigValue( props.data?.source || '', props.data ); const formData = { ...result.values, ..._.omit(props.data, result.omits), scheduleType: props.data?.gpu_selector ? 'manual' : 'auto', gpu_selector: props.data?.gpu_selector ? `${props.data?.gpu_selector.worker_name}-${props.data?.gpu_selector.gpu_name}-${props.data?.gpu_selector.gpu_index}` : null }; form.setFieldsValue(formData); } }, [open]); useEffect(() => { setIsGGUF(props.data?.backend === backendOptionsMap.llamaBox); }, [props.data?.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 renderS3Fields = () => { return ( <> name="s3_address" rules={[ { required: true, message: intl.formatMessage( { id: 'common.form.rule.input' }, { name: intl.formatMessage({ id: 'models.form.s3address' }) } ) } ]} > ); }; 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' }) } ) } ]} > ); }; 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' }) } ) } ]} > ); }; const renderFieldsBySource = useMemo(() => { if (SEARCH_SOURCE.includes(props.data?.source || '')) { return renderHuggingfaceFields(); } if (props.data?.source === modelSourceMap.ollama_library_value) { return renderOllamaModelFields(); } if (props.data?.source === modelSourceMap.s3_value) { return renderS3Fields(); } if (props.data?.source === modelSourceMap.local_path_value) { return renderLocalPathFields(); } return null; }, [props.data?.source, isGGUF, intl]); const handleSumit = () => { form.submit(); }; const handleOk = (formdata: FormData) => { if (formdata.scheduleType === 'manual') { const gpu = _.find(gpuOptions, (item: any) => { return item.value === formdata.gpu_selector; }); onOk({ ..._.omit(formdata, ['scheduleType']), worker_selector: null, gpu_selector: gpu ? { gpu_name: gpu.name, gpu_index: gpu.index, worker_name: gpu.worker_name } : null }); } else { onOk({ ..._.omit(formdata, ['scheduleType']), gpu_selector: null }); } }; const handleOnClose = () => { onCancel?.(); }; useEffect(() => { getGPUList(); }, []); return ( } >
name="name" rules={[ { required: true, message: intl.formatMessage( { id: 'common.form.rule.input' }, { name: intl.formatMessage({ id: 'common.table.name' }) } ) } ]} > name="source" rules={[ { required: true, message: intl.formatMessage( { id: 'common.form.rule.select' }, { name: intl.formatMessage({ id: 'models.form.source' }) } ) } ]} > {action === PageAction.EDIT && ( )} {renderFieldsBySource} name="replicas" rules={[ { required: true, message: intl.formatMessage( { id: 'common.form.rule.input' }, { name: intl.formatMessage({ id: 'models.form.replicas' }) } ) } ]} > name="description">
); }; export default memo(UpdateModal);