You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.tsx 4.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import { getAccessToken } from '@/access';
  2. import KFIcon from '@/components/KFIcon';
  3. import KFModal from '@/components/KFModal';
  4. import { CategoryData } from '@/pages/Dataset/types';
  5. import { addModel } from '@/services/dataset/index.js';
  6. import { to } from '@/utils/promise';
  7. import { getFileListFromEvent, validateUploadFiles } from '@/utils/ui';
  8. import {
  9. Button,
  10. Form,
  11. Input,
  12. Select,
  13. Upload,
  14. UploadFile,
  15. message,
  16. type ModalProps,
  17. type UploadProps,
  18. } from 'antd';
  19. import { omit } from 'lodash';
  20. import { useState } from 'react';
  21. import styles from '../AddDatasetModal/index.less';
  22. interface AddModelModalProps extends Omit<ModalProps, 'onOk'> {
  23. typeList: CategoryData[];
  24. tagList: CategoryData[];
  25. onOk: () => void;
  26. }
  27. function AddModelModal({ typeList, tagList, onOk, ...rest }: AddModelModalProps) {
  28. const [uuid] = useState(Date.now());
  29. // 上传组件参数
  30. const uploadProps: UploadProps = {
  31. action: '/api/mmp/models/upload',
  32. headers: {
  33. Authorization: getAccessToken() || '',
  34. },
  35. defaultFileList: [],
  36. };
  37. // 上传请求
  38. const createModel = async (params: any) => {
  39. const [res] = await to(addModel(params));
  40. if (res) {
  41. message.success('创建成功');
  42. onOk?.();
  43. }
  44. };
  45. // 提交
  46. const onFinish = (formData: any) => {
  47. const fileList: UploadFile[] = formData['fileList'] ?? [];
  48. if (validateUploadFiles(fileList)) {
  49. const params = {
  50. ...omit(formData, ['fileList']),
  51. models_version_vos: fileList.map((item) => {
  52. const data = item.response?.data?.[0] ?? {};
  53. return {
  54. file_name: data.fileName,
  55. file_size: data.fileSize,
  56. url: data.url,
  57. };
  58. }),
  59. };
  60. createModel(params);
  61. }
  62. };
  63. return (
  64. <KFModal
  65. {...rest}
  66. title="新建模型"
  67. image={require('@/assets/img/create-experiment.png')}
  68. width={825}
  69. okButtonProps={{
  70. htmlType: 'submit',
  71. form: 'form',
  72. }}
  73. >
  74. <Form name="form" layout="vertical" onFinish={onFinish} autoComplete="off">
  75. <Form.Item
  76. label="模型名称"
  77. name="name"
  78. rules={[
  79. {
  80. required: true,
  81. message: '请输入模型名称!',
  82. },
  83. ]}
  84. >
  85. <Input placeholder="请输入模型名称" showCount allowClear maxLength={64} />
  86. </Form.Item>
  87. <Form.Item
  88. label="模型版本"
  89. name="version"
  90. rules={[
  91. {
  92. required: true,
  93. message: '请输入模型版本',
  94. },
  95. ]}
  96. >
  97. <Input placeholder="请输入模型版本" allowClear maxLength={64} />
  98. </Form.Item>
  99. <Form.Item
  100. label="模型简介"
  101. name="description"
  102. rules={[
  103. {
  104. required: true,
  105. message: '请输入模型简介',
  106. },
  107. ]}
  108. >
  109. <Input.TextArea
  110. placeholder="请输入模型简介"
  111. showCount
  112. maxLength={256}
  113. autoSize={{ minRows: 2, maxRows: 6 }}
  114. allowClear
  115. />
  116. </Form.Item>
  117. {/* <Form.Item label="可见范围" name="available_range">
  118. <Radio.Group>
  119. <Radio value="0">仅自己可见</Radio>
  120. <Radio value="1">工作空间可见</Radio>
  121. </Radio.Group>
  122. </Form.Item> */}
  123. <Form.Item label="模型框架" name="model_type">
  124. <Select
  125. allowClear
  126. placeholder="请选择模型类型"
  127. options={typeList}
  128. fieldNames={{ label: 'name', value: 'id' }}
  129. optionFilterProp="name"
  130. showSearch
  131. />
  132. </Form.Item>
  133. <Form.Item label="模型能力" name="model_tag">
  134. <Select
  135. allowClear
  136. placeholder="请选择模型标签"
  137. options={tagList}
  138. fieldNames={{ label: 'name', value: 'id' }}
  139. optionFilterProp="name"
  140. showSearch
  141. />
  142. </Form.Item>
  143. <Form.Item
  144. label="模型文件"
  145. name="fileList"
  146. valuePropName="fileList"
  147. getValueFromEvent={getFileListFromEvent}
  148. rules={[
  149. {
  150. required: true,
  151. message: '请上传模型文件',
  152. },
  153. ]}
  154. >
  155. <Upload {...uploadProps} data={{ uuid: uuid }}>
  156. <Button
  157. className={styles['upload-button']}
  158. type="default"
  159. icon={<KFIcon type="icon-shangchuan" />}
  160. >
  161. 上传文件
  162. </Button>
  163. </Upload>
  164. </Form.Item>
  165. </Form>
  166. </KFModal>
  167. );
  168. }
  169. export default AddModelModal;