From 8781416cfb28aefa75d60c78968c1196433c10fd Mon Sep 17 00:00:00 2001 From: josc146 Date: Tue, 21 Nov 2023 00:04:23 +0800 Subject: [PATCH] add hf-mirror for cn users --- frontend/src/_locales/ja/main.json | 3 ++- frontend/src/_locales/zh-hans/main.json | 3 ++- frontend/src/components/RunButton.tsx | 4 ++-- frontend/src/pages/Models.tsx | 24 ++++++++++++++++++------ frontend/src/stores/commonStore.ts | 1 + frontend/src/types/settings.ts | 1 + frontend/src/utils/index.tsx | 6 ++++++ 7 files changed, 32 insertions(+), 10 deletions(-) diff --git a/frontend/src/_locales/ja/main.json b/frontend/src/_locales/ja/main.json index 8042868..14ff4a4 100644 --- a/frontend/src/_locales/ja/main.json +++ b/frontend/src/_locales/ja/main.json @@ -270,5 +270,6 @@ "Server is working on deployment mode, please exit the program manually to stop the server": "サーバーはデプロイモードで動作しています、サーバーを停止するにはプログラムを手動で終了してください", "You can increase the number of stored layers in Configs page to improve performance": "パフォーマンスを向上させるために、保存されるレイヤーの数を設定ページで増やすことができます", "Failed to load model, try to increase the virtual memory (Swap of WSL) or use a smaller base model.": "モデルの読み込みに失敗しました、仮想メモリ (WSL Swap) を増やすか小さなベースモデルを使用してみてください。", - "Save Conversation": "会話を保存" + "Save Conversation": "会話を保存", + "Use Hugging Face Mirror": "Hugging Faceミラーを使用" } \ No newline at end of file diff --git a/frontend/src/_locales/zh-hans/main.json b/frontend/src/_locales/zh-hans/main.json index 7f68e64..d2b5e2d 100644 --- a/frontend/src/_locales/zh-hans/main.json +++ b/frontend/src/_locales/zh-hans/main.json @@ -270,5 +270,6 @@ "Server is working on deployment mode, please exit the program manually to stop the server": "服务器正在部署模式下运行,请手动退出程序以停止服务器", "You can increase the number of stored layers in Configs page to improve performance": "你可以在配置页面增加载入显存层数以提升性能", "Failed to load model, try to increase the virtual memory (Swap of WSL) or use a smaller base model.": "模型载入失败,尝试增加虚拟内存(WSL Swap),或使用一个更小规模的基底模型", - "Save Conversation": "保存对话" + "Save Conversation": "保存对话", + "Use Hugging Face Mirror": "使用Hugging Face镜像源" } \ No newline at end of file diff --git a/frontend/src/components/RunButton.tsx b/frontend/src/components/RunButton.tsx index fdb1629..de5a7ac 100644 --- a/frontend/src/components/RunButton.tsx +++ b/frontend/src/components/RunButton.tsx @@ -11,7 +11,7 @@ import { Button } from '@fluentui/react-components'; import { observer } from 'mobx-react-lite'; import { exit, getStatus, readRoot, switchModel, updateConfig } from '../apis'; import { toast } from 'react-toastify'; -import { checkDependencies, getStrategy, toastWithButton } from '../utils'; +import { checkDependencies, getHfDownloadUrl, getStrategy, toastWithButton } from '../utils'; import { useTranslation } from 'react-i18next'; import { ToolTipButton } from './ToolTipButton'; import { Play16Regular, Stop16Regular } from '@fluentui/react-icons'; @@ -94,7 +94,7 @@ export const RunButton: FC<{ onClickRun?: MouseEventHandler, iconMode?: boolean navigate({ pathname: '/downloads' }); }, { autoClose: 3000 }); - AddToDownloadList(modelPath, downloadUrl); + AddToDownloadList(modelPath, getHfDownloadUrl(downloadUrl)); } else { toast(t('Can not find download url'), { type: 'error' }); } diff --git a/frontend/src/pages/Models.tsx b/frontend/src/pages/Models.tsx index 54f2e5f..3b28d7b 100644 --- a/frontend/src/pages/Models.tsx +++ b/frontend/src/pages/Models.tsx @@ -1,5 +1,6 @@ import React, { FC } from 'react'; import { + Checkbox, createTableColumn, DataGrid, DataGridBody, @@ -19,7 +20,7 @@ import commonStore from '../stores/commonStore'; import { BrowserOpenURL } from '../../wailsjs/runtime'; import { AddToDownloadList, OpenFileFolder } from '../../wailsjs/go/backend_golang/App'; import { Page } from '../components/Page'; -import { bytesToGb, refreshModels, saveConfigs, toastWithButton } from '../utils'; +import { bytesToGb, getHfDownloadUrl, refreshModels, saveConfigs, toastWithButton } from '../utils'; import { useTranslation } from 'react-i18next'; import { useNavigate } from 'react-router'; import { ModelSourceItem } from '../types/models'; @@ -139,7 +140,7 @@ const columns: TableColumnDefinition[] = [ navigate({ pathname: '/downloads' }); }, { autoClose: 3000 }); - AddToDownloadList(`${commonStore.settings.customModelsPath}/${item.name}`, item.downloadUrl!); + AddToDownloadList(`${commonStore.settings.customModelsPath}/${item.name}`, getHfDownloadUrl(item.downloadUrl!)); }} />} {item.url && } onClick={() => { BrowserOpenURL(item.url!); @@ -160,10 +161,21 @@ const Models: FC = observer(() => {
{t('Model Source Manifest List')} - } onClick={() => { - refreshModels(false); - saveConfigs(); - }} /> +
+ {commonStore.settings.language === 'zh' && + { + commonStore.setSettings({ + useHfMirror: data.checked as boolean + }); + }} />} + } onClick={() => { + refreshModels(false); + saveConfigs(); + }} /> +
{t('Provide JSON file URLs for the models manifest. Separate URLs with semicolons. The "models" field in JSON files will be parsed into the following table.')} diff --git a/frontend/src/stores/commonStore.ts b/frontend/src/stores/commonStore.ts index 6e9221a..229d0b8 100644 --- a/frontend/src/stores/commonStore.ts +++ b/frontend/src/stores/commonStore.ts @@ -142,6 +142,7 @@ class CommonStore { autoUpdatesCheck: true, giteeUpdatesSource: getUserLanguage() === 'zh', cnMirror: getUserLanguage() === 'zh', + useHfMirror: false, host: '127.0.0.1', dpiScaling: 100, customModelsPath: './models', diff --git a/frontend/src/types/settings.ts b/frontend/src/types/settings.ts index 2fe3299..2b9357b 100644 --- a/frontend/src/types/settings.ts +++ b/frontend/src/types/settings.ts @@ -10,6 +10,7 @@ export type SettingsType = { autoUpdatesCheck: boolean giteeUpdatesSource: boolean cnMirror: boolean + useHfMirror: boolean host: string dpiScaling: number customModelsPath: string diff --git a/frontend/src/utils/index.tsx b/frontend/src/utils/index.tsx index f6d1a1e..b101284 100644 --- a/frontend/src/utils/index.tsx +++ b/frontend/src/utils/index.tsx @@ -474,6 +474,12 @@ export function toastWithButton(text: string, buttonText: string, onClickButton: return id; } +export function getHfDownloadUrl(url: string) { + if (commonStore.settings.useHfMirror && url.includes('huggingface.co') && url.includes('resolve')) + return url.replace('huggingface.co', 'hf-mirror.com'); + return url; +} + export function getSupportedCustomCudaFile(isBeta: boolean) { if ([' 10', ' 16', ' 20', ' 30', 'MX', 'Tesla P', 'Quadro P', 'NVIDIA P', 'TITAN X', 'TITAN RTX', 'RTX A', 'Quadro RTX 4000', 'Quadro RTX 5000', 'Tesla T4', 'NVIDIA A10', 'NVIDIA A40'].some(v => commonStore.status.device_name.includes(v)))