From 32b402d4a17b36da8e4ca924593576a143d5a29b Mon Sep 17 00:00:00 2001 From: agatha197 Date: Sat, 21 Sep 2024 01:01:23 +0900 Subject: [PATCH 1/2] refactor: remove checking URL and add more form inputs --- .../components/ImportFromHuggingFaceModal.tsx | 121 ++++++++++++++---- resources/i18n/de.json | 9 +- resources/i18n/el.json | 12 +- resources/i18n/en.json | 13 +- resources/i18n/es.json | 12 +- resources/i18n/fi.json | 12 +- resources/i18n/fr.json | 12 +- resources/i18n/id.json | 12 +- resources/i18n/it.json | 12 +- resources/i18n/ja.json | 12 +- resources/i18n/ko.json | 13 +- resources/i18n/mn.json | 11 +- resources/i18n/ms.json | 12 +- resources/i18n/pl.json | 12 +- resources/i18n/pt-BR.json | 12 +- resources/i18n/pt.json | 12 +- resources/i18n/ru.json | 12 +- resources/i18n/th.json | 12 +- resources/i18n/tr.json | 12 +- resources/i18n/vi.json | 12 +- resources/i18n/zh-CN.json | 12 +- resources/i18n/zh-TW.json | 12 +- 22 files changed, 304 insertions(+), 67 deletions(-) diff --git a/react/src/components/ImportFromHuggingFaceModal.tsx b/react/src/components/ImportFromHuggingFaceModal.tsx index cff19a7b1..0f65667e9 100644 --- a/react/src/components/ImportFromHuggingFaceModal.tsx +++ b/react/src/components/ImportFromHuggingFaceModal.tsx @@ -1,10 +1,11 @@ +import { baiSignedRequestWithPromise } from '../helper'; +import { useSuspendedBackendaiClient } from '../hooks'; +import { useSuspenseTanQuery } from '../hooks/reactQueryAlias'; import BAIModal, { BAIModalProps } from './BAIModal'; import Flex from './Flex'; -import { FilterOutlined } from '@ant-design/icons'; import { useToggle } from 'ahooks'; import { Button, - Card, Form, FormInstance, Input, @@ -12,8 +13,8 @@ import { theme, Typography, } from 'antd'; -import Markdown from 'markdown-to-jsx'; -import React, { useRef } from 'react'; +import _ from 'lodash'; +import React, { useEffect, useRef, useState, useTransition } from 'react'; import { useTranslation } from 'react-i18next'; type Service = { @@ -34,13 +35,61 @@ const ImportFromHuggingFaceModal: React.FC = ({ const { token } = theme.useToken(); const formRef = useRef>(null); const [isImportOnly, { toggle: toggleIsImportOnly }] = useToggle(false); + const [huggingFaceURL, setHuggingFaceURL] = useState(); + const baiClient = useSuspendedBackendaiClient(); + + const [isPendingCheck, startCheckTransition] = useTransition(); + const hugginFaceModelInfo = useSuspenseTanQuery<{ + author?: string; + model_name?: string; + markdown?: string; + isError?: boolean; + url?: string; + }>({ + queryKey: ['huggingFaceReadme', huggingFaceURL], + queryFn: () => { + if (_.isEmpty(huggingFaceURL)) return Promise.resolve({}); + return baiSignedRequestWithPromise({ + method: 'GET', + url: `/services/_/huggingface/models?huggingface_url=${huggingFaceURL}`, + client: baiClient, + }) + .then((result: any) => { + return { + ...result, + url: huggingFaceURL, + }; + }) + .catch(() => { + // TODO: handle error more gracefully + return { + isError: true, + url: huggingFaceURL, + }; + }); + }, + }); + const isHuggingfaceURLExisted = !_.isEmpty( + hugginFaceModelInfo.data.model_name, + ); + + // validate when huggingFaceModelInfo is updated + useEffect(() => { + if (hugginFaceModelInfo.data.url) { + formRef.current?.validateFields().catch(() => {}); + } + }, [hugginFaceModelInfo.data.url]); const handleOnClick = () => { formRef.current ?.validateFields() .then((values) => { + startCheckTransition(() => { + setHuggingFaceURL(values?.url); + }); + // TODO: Implement import from Hugging Face - onRequestClose(); + // onRequestClose(); }) .catch(() => {}); }; @@ -50,7 +99,12 @@ const ImportFromHuggingFaceModal: React.FC = ({ title={t('data.modelStore.ImportFromHuggingFace')} centered footer={ - } onCancel={onRequestClose} + destroyOnClose {...baiModalProps} >