Skip to content

Commit 4deaa6a

Browse files
refactor(web-app): extract download handler in ModelDownloadAction
1 parent 958ad06 commit 4deaa6a

File tree

1 file changed

+96
-86
lines changed

1 file changed

+96
-86
lines changed

web-app/src/containers/ModelDownloadAction.tsx

Lines changed: 96 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,101 @@ export const ModelDownloadAction = ({
5555
[navigate]
5656
)
5757

58+
const handleDownloadModel = useCallback(async () => {
59+
const preflight = await serviceHub
60+
.models()
61+
.preflightArtifactAccess(variant.path, huggingfaceToken)
62+
63+
const repoPage = `https://huggingface.co/${model.model_name}`
64+
65+
if (!preflight.ok) {
66+
if (preflight.reason === 'AUTH_REQUIRED') {
67+
toast.error('Hugging Face token required', {
68+
description:
69+
'This model requires a Hugging Face access token. Add your token in Settings and retry.',
70+
action: {
71+
label: 'Open Settings',
72+
onClick: () =>
73+
navigate({
74+
to: route.settings.general,
75+
params: {},
76+
}),
77+
},
78+
})
79+
return
80+
}
81+
82+
if (preflight.reason === 'LICENSE_NOT_ACCEPTED') {
83+
toast.error('Accept model license on Hugging Face', {
84+
description:
85+
'You must accept the model’s license on its Hugging Face page before downloading.',
86+
action: {
87+
label: 'Open model page',
88+
onClick: () => window.open(repoPage, '_blank'),
89+
},
90+
})
91+
return
92+
}
93+
94+
if (preflight.reason === 'RATE_LIMITED') {
95+
toast.error('Rate limited by Hugging Face', {
96+
description:
97+
'You have been rate-limited. Adding a token can increase rate limits. Please try again later.',
98+
action: {
99+
label: 'Open Settings',
100+
onClick: () =>
101+
navigate({
102+
to: route.settings.general,
103+
params: {},
104+
}),
105+
},
106+
})
107+
return
108+
}
109+
110+
if (preflight.reason === 'NOT_FOUND') {
111+
toast.error('File not found', {
112+
description:
113+
'The requested artifact was not found in the repository. Try another quant or check the model page.',
114+
action: {
115+
label: 'Open model page',
116+
onClick: () => window.open(repoPage, '_blank'),
117+
},
118+
})
119+
return
120+
}
121+
122+
toast.error('Model download error', {
123+
description:
124+
'We could not start the download. Check your network or try again later.',
125+
})
126+
return
127+
}
128+
129+
addLocalDownloadingModel(variant.model_id)
130+
serviceHub
131+
.models()
132+
.pullModelWithMetadata(
133+
variant.model_id,
134+
variant.path,
135+
(
136+
model.mmproj_models?.find(
137+
(e) => e.model_id.toLowerCase() === 'mmproj-f16'
138+
) || model.mmproj_models?.[0]
139+
)?.path,
140+
huggingfaceToken
141+
)
142+
}, [
143+
serviceHub,
144+
variant.path,
145+
variant.model_id,
146+
huggingfaceToken,
147+
model.model_name,
148+
model.mmproj_models,
149+
navigate,
150+
addLocalDownloadingModel,
151+
])
152+
58153
const isDownloading =
59154
localDownloadingModels.has(variant.model_id) ||
60155
downloadProcesses.some((e) => e.id === variant.model_id)
@@ -99,92 +194,7 @@ export const ModelDownloadAction = ({
99194
<div
100195
className="size-6 cursor-pointer flex items-center justify-center rounded hover:bg-main-view-fg/10 transition-all duration-200 ease-in-out"
101196
title={t('hub:downloadModel')}
102-
onClick={async () => {
103-
const preflight = await serviceHub
104-
.models()
105-
.preflightArtifactAccess(variant.path, huggingfaceToken)
106-
107-
const repoPage = `https://huggingface.co/${model.model_name}`
108-
109-
if (!preflight.ok) {
110-
if (preflight.reason === 'AUTH_REQUIRED') {
111-
toast.error('Hugging Face token required', {
112-
description:
113-
'This model requires a Hugging Face access token. Add your token in Settings and retry.',
114-
action: {
115-
label: 'Open Settings',
116-
onClick: () =>
117-
navigate({
118-
to: route.settings.general,
119-
params: {},
120-
}),
121-
},
122-
})
123-
return
124-
}
125-
126-
if (preflight.reason === 'LICENSE_NOT_ACCEPTED') {
127-
toast.error('Accept model license on Hugging Face', {
128-
description:
129-
'You must accept the model’s license on its Hugging Face page before downloading.',
130-
action: {
131-
label: 'Open model page',
132-
onClick: () => window.open(repoPage, '_blank'),
133-
},
134-
})
135-
return
136-
}
137-
138-
if (preflight.reason === 'RATE_LIMITED') {
139-
toast.error('Rate limited by Hugging Face', {
140-
description:
141-
'You have been rate-limited. Adding a token can increase rate limits. Please try again later.',
142-
action: {
143-
label: 'Open Settings',
144-
onClick: () =>
145-
navigate({
146-
to: route.settings.general,
147-
params: {},
148-
}),
149-
},
150-
})
151-
return
152-
}
153-
154-
if (preflight.reason === 'NOT_FOUND') {
155-
toast.error('File not found', {
156-
description:
157-
'The requested artifact was not found in the repository. Try another quant or check the model page.',
158-
action: {
159-
label: 'Open model page',
160-
onClick: () => window.open(repoPage, '_blank'),
161-
},
162-
})
163-
return
164-
}
165-
166-
167-
toast.error('Model download error', {
168-
description:
169-
'We could not start the download. Check your network or try again later.',
170-
})
171-
return
172-
}
173-
174-
addLocalDownloadingModel(variant.model_id)
175-
serviceHub
176-
.models()
177-
.pullModelWithMetadata(
178-
variant.model_id,
179-
variant.path,
180-
(
181-
model.mmproj_models?.find(
182-
(e) => e.model_id.toLowerCase() === 'mmproj-f16'
183-
) || model.mmproj_models?.[0]
184-
)?.path,
185-
huggingfaceToken
186-
)
187-
}}
197+
onClick={handleDownloadModel}
188198
>
189199
<IconDownload size={16} className="text-main-view-fg/80" />
190200
</div>

0 commit comments

Comments
 (0)