chore: avoid duplicated fn
This commit is contained in:
parent
5f9f766965
commit
38629afc89
@ -194,48 +194,50 @@ function Hub() {
|
|||||||
fetchSources()
|
fetchSources()
|
||||||
}, [fetchSources])
|
}, [fetchSources])
|
||||||
|
|
||||||
|
const fetchHuggingFaceModel = async (searchValue: string) => {
|
||||||
|
if (
|
||||||
|
!searchValue.length ||
|
||||||
|
(!searchValue.includes('/') && !searchValue.startsWith('http'))
|
||||||
|
) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
setIsSearching(true)
|
||||||
|
if (addModelSourceTimeoutRef.current) {
|
||||||
|
clearTimeout(addModelSourceTimeoutRef.current)
|
||||||
|
}
|
||||||
|
|
||||||
|
addModelSourceTimeoutRef.current = setTimeout(async () => {
|
||||||
|
try {
|
||||||
|
const repoInfo = await fetchHuggingFaceRepo(searchValue, huggingfaceToken)
|
||||||
|
if (repoInfo) {
|
||||||
|
const catalogModel = convertHfRepoToCatalogModel(repoInfo)
|
||||||
|
if (
|
||||||
|
!sources.some(
|
||||||
|
(s) =>
|
||||||
|
catalogModel.model_name.trim().split('/').pop() ===
|
||||||
|
s.model_name.trim() &&
|
||||||
|
catalogModel.developer.trim() === s.developer?.trim()
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
setHuggingFaceRepo(catalogModel)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching repository info:', error)
|
||||||
|
} finally {
|
||||||
|
setIsSearching(false)
|
||||||
|
}
|
||||||
|
}, 500)
|
||||||
|
}
|
||||||
|
|
||||||
const handleSearchChange = (e: ChangeEvent<HTMLInputElement>) => {
|
const handleSearchChange = (e: ChangeEvent<HTMLInputElement>) => {
|
||||||
setIsSearching(false)
|
setIsSearching(false)
|
||||||
setSearchValue(e.target.value)
|
setSearchValue(e.target.value)
|
||||||
setHuggingFaceRepo(null) // Clear previous repo info
|
setHuggingFaceRepo(null) // Clear previous repo info
|
||||||
|
|
||||||
if (addModelSourceTimeoutRef.current) {
|
if (!showOnlyDownloaded) {
|
||||||
clearTimeout(addModelSourceTimeoutRef.current)
|
fetchHuggingFaceModel(e.target.value)
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
e.target.value.length &&
|
|
||||||
(e.target.value.includes('/') || e.target.value.startsWith('http')) &&
|
|
||||||
!showOnlyDownloaded
|
|
||||||
) {
|
|
||||||
setIsSearching(true)
|
|
||||||
|
|
||||||
addModelSourceTimeoutRef.current = setTimeout(async () => {
|
|
||||||
try {
|
|
||||||
// Fetch HuggingFace repository information
|
|
||||||
const repoInfo = await fetchHuggingFaceRepo(
|
|
||||||
e.target.value,
|
|
||||||
huggingfaceToken
|
|
||||||
)
|
|
||||||
if (repoInfo) {
|
|
||||||
const catalogModel = convertHfRepoToCatalogModel(repoInfo)
|
|
||||||
if (
|
|
||||||
!sources.some(
|
|
||||||
(s) =>
|
|
||||||
catalogModel.model_name.trim().split('/').pop() ===
|
|
||||||
s.model_name.trim() &&
|
|
||||||
catalogModel.developer.trim() === s.developer?.trim()
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
setHuggingFaceRepo(catalogModel)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error fetching repository info:', error)
|
|
||||||
} finally {
|
|
||||||
setIsSearching(false)
|
|
||||||
}
|
|
||||||
}, 500)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -514,40 +516,9 @@ function Hub() {
|
|||||||
setShowOnlyDownloaded(checked)
|
setShowOnlyDownloaded(checked)
|
||||||
if (checked) {
|
if (checked) {
|
||||||
setHuggingFaceRepo(null)
|
setHuggingFaceRepo(null)
|
||||||
} else if (
|
} else {
|
||||||
searchValue.length &&
|
|
||||||
(searchValue.includes('/') || searchValue.startsWith('http'))
|
|
||||||
) {
|
|
||||||
// Re-trigger HuggingFace search when switching back to "All models"
|
// Re-trigger HuggingFace search when switching back to "All models"
|
||||||
setIsSearching(true)
|
fetchHuggingFaceModel(searchValue)
|
||||||
if (addModelSourceTimeoutRef.current) {
|
|
||||||
clearTimeout(addModelSourceTimeoutRef.current)
|
|
||||||
}
|
|
||||||
addModelSourceTimeoutRef.current = setTimeout(async () => {
|
|
||||||
try {
|
|
||||||
const repoInfo = await fetchHuggingFaceRepo(
|
|
||||||
searchValue,
|
|
||||||
huggingfaceToken
|
|
||||||
)
|
|
||||||
if (repoInfo) {
|
|
||||||
const catalogModel = convertHfRepoToCatalogModel(repoInfo)
|
|
||||||
if (
|
|
||||||
!sources.some(
|
|
||||||
(s) =>
|
|
||||||
catalogModel.model_name.trim().split('/').pop() ===
|
|
||||||
s.model_name.trim() &&
|
|
||||||
catalogModel.developer.trim() === s.developer?.trim()
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
setHuggingFaceRepo(catalogModel)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error fetching repository info:', error)
|
|
||||||
} finally {
|
|
||||||
setIsSearching(false)
|
|
||||||
}
|
|
||||||
}, 500)
|
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user