File size: 11,561 Bytes
ad5cef3 59a1fe9 6ebf2fd 85a4687 59a1fe9 6ebf2fd e7ba29d 6ebf2fd 59a1fe9 6ebf2fd 59a1fe9 6ebf2fd 59a1fe9 6ebf2fd ad5cef3 59a1fe9 ad5cef3 59a1fe9 ad5cef3 59a1fe9 ad5cef3 59a1fe9 6ebf2fd 59a1fe9 6ebf2fd 59a1fe9 ad5cef3 59a1fe9 6ebf2fd 59a1fe9 85a4687 ad5cef3 6ebf2fd 85a4687 59a1fe9 ad5cef3 6ebf2fd ad5cef3 6ebf2fd ad5cef3 6ebf2fd ad5cef3 6ebf2fd 59a1fe9 6ebf2fd ad5cef3 6ebf2fd ad5cef3 6ebf2fd ad5cef3 6ebf2fd ad5cef3 6ebf2fd ad5cef3 6ebf2fd ad5cef3 6ebf2fd ad5cef3 6ebf2fd ad5cef3 6ebf2fd 59a1fe9 6ebf2fd 59a1fe9 6ebf2fd 59a1fe9 6ebf2fd 59a1fe9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
import React, { useCallback, useEffect, useState } from 'react'
import {
Listbox,
ListboxButton,
ListboxOption,
ListboxOptions,
Transition
} from '@headlessui/react'
import { useModel } from '../contexts/ModelContext'
import { getModelInfo } from '../lib/huggingface'
import { Heart, Download, ChevronDown, Check, ArrowUpDown } from 'lucide-react'
type SortOption = 'likes' | 'downloads' | 'createdAt' | 'name'
const ModelSelector: React.FC = () => {
const { models, setModelInfo, modelInfo, pipeline } = useModel()
const [sortBy, setSortBy] = useState<SortOption>('downloads')
const [sortOrder, setSortOrder] = useState<'asc' | 'desc'>('desc')
const formatNumber = (num: number) => {
if (num >= 1000000000) {
return (num / 1000000000).toFixed(1) + 'B'
} else if (num >= 1000000) {
return (num / 1000000).toFixed(1) + 'M'
} else if (num >= 1000) {
return (num / 1000).toFixed(1) + 'K'
}
return num.toString()
}
// Sort models based on current sort criteria
const sortedModels = React.useMemo(() => {
return [...models].sort((a, b) => {
let comparison = 0
switch (sortBy) {
case 'downloads':
comparison = (a.downloads || 0) - (b.downloads || 0)
break
case 'createdAt':
const dateA = new Date(a.createdAt || '').getTime()
const dateB = new Date(b.createdAt || '').getTime()
comparison = dateA - dateB
break
case 'name':
comparison = a.id.localeCompare(b.id)
break
case 'likes':
default:
comparison = (a.likes || 0) - (b.likes || 0)
break
}
return sortOrder === 'desc' ? -comparison : comparison
})
}, [models, sortBy, sortOrder])
// Function to fetch detailed model info and set as selected
const fetchAndSetModelInfo = useCallback(
async (modelId: string) => {
try {
const modelInfoResponse = await getModelInfo(modelId)
let parameters = 0
if (modelInfoResponse.safetensors) {
const safetensors = modelInfoResponse.safetensors
parameters =
safetensors.parameters.BF16 ||
safetensors.parameters.F16 ||
safetensors.parameters.F32 ||
safetensors.parameters.total ||
0
}
const modelInfo = {
id: modelId,
name: modelInfoResponse.id || modelId,
architecture:
modelInfoResponse.config?.architectures?.[0] || 'Unknown',
parameters,
likes: modelInfoResponse.likes || 0,
downloads: modelInfoResponse.downloads || 0,
createdAt: modelInfoResponse.createdAt || '',
isCompatible: modelInfoResponse.isCompatible,
incompatibilityReason: modelInfoResponse.incompatibilityReason,
supportedQuantizations: modelInfoResponse.supportedQuantizations,
baseId: modelInfoResponse.baseId
}
console.log('Fetched model info:', modelInfoResponse)
setModelInfo(modelInfo)
} catch (error) {
console.error('Error fetching model info:', error)
}
},
[setModelInfo]
)
// Update modelInfo to first model when pipeline changes
useEffect(() => {
if (models.length > 0) {
const firstModel = models[0]
fetchAndSetModelInfo(firstModel.id)
}
}, [pipeline, models, fetchAndSetModelInfo])
const handleModelSelect = (modelId: string) => {
fetchAndSetModelInfo(modelId)
}
const handleSortChange = (newSortBy: SortOption) => {
if (sortBy === newSortBy) {
setSortOrder(sortOrder === 'asc' ? 'desc' : 'asc')
} else {
setSortBy(newSortBy)
setSortOrder('desc')
}
}
const selectedModel =
models.find((model) => model.id === modelInfo.id) || models[0]
return (
<div className="relative">
<Listbox
value={selectedModel}
onChange={(model) => handleModelSelect(model.id)}
>
<div className="relative">
<ListboxButton className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent bg-white text-left flex items-center justify-between">
<div className="flex items-center justify-between w-full">
<div className="flex flex-col flex-1 min-w-0">
<span className="truncate font-medium">
{modelInfo.id || 'Select a model'}
</span>
</div>
<div className="flex items-center space-x-3">
{selectedModel &&
(selectedModel.likes > 0 || selectedModel.downloads > 0) && (
<div className="flex items-center space-x-3 text-xs text-gray-500">
{selectedModel.likes > 0 && (
<div className="flex items-center space-x-1">
<Heart className="w-3 h-3 text-red-500" />
<span>{formatNumber(selectedModel.likes)}</span>
</div>
)}
{selectedModel.downloads > 0 && (
<div className="flex items-center space-x-1">
<Download className="w-3 h-3 text-green-500" />
<span>{formatNumber(selectedModel.downloads)}</span>
</div>
)}
</div>
)}
<ChevronDown className="w-4 h-4 ui-open:rotate-180 transition-transform flex-shrink-0" />
</div>
</div>
</ListboxButton>
<Transition
enter="transition duration-100 ease-out"
enterFrom="transform scale-95 opacity-0"
enterTo="transform scale-100 opacity-100"
leave="transition duration-75 ease-out"
leaveFrom="transform scale-100 opacity-100"
leaveTo="transform scale-95 opacity-0"
>
<ListboxOptions className="absolute z-10 w-full mt-1 bg-white border border-gray-300 rounded-md shadow-lg max-h-60 overflow-hidden focus:outline-none">
{/* Sort Controls - Always Visible */}
<div className="px-3 py-2 border-b border-gray-200 bg-gray-50 sticky top-0 z-10">
<div className="flex items-center space-x-2 text-xs">
<span className="text-gray-600 font-medium">Sort by:</span>
<button
onClick={() => handleSortChange('name')}
className={`px-2 py-1 rounded flex items-center space-x-1 ${
sortBy === 'name'
? 'bg-blue-100 text-blue-700'
: 'text-gray-600 hover:bg-gray-100'
}`}
>
<span>Name</span>
{sortBy === 'name' && <ArrowUpDown className="w-3 h-3" />}
</button>
<button
onClick={() => handleSortChange('likes')}
className={`px-2 py-1 rounded flex items-center space-x-1 ${
sortBy === 'likes'
? 'bg-blue-100 text-blue-700'
: 'text-gray-600 hover:bg-gray-100'
}`}
>
<Heart className="w-3 h-3" />
<span>Likes</span>
{sortBy === 'likes' && <ArrowUpDown className="w-3 h-3" />}
</button>
<button
onClick={() => handleSortChange('downloads')}
className={`px-2 py-1 rounded flex items-center space-x-1 ${
sortBy === 'downloads'
? 'bg-blue-100 text-blue-700'
: 'text-gray-600 hover:bg-gray-100'
}`}
>
<Download className="w-3 h-3" />
<span>Downloads</span>
{sortBy === 'downloads' && (
<ArrowUpDown className="w-3 h-3" />
)}
</button>
<button
onClick={() => handleSortChange('createdAt')}
className={`px-2 py-1 rounded flex items-center space-x-1 ${
sortBy === 'createdAt'
? 'bg-blue-100 text-blue-700'
: 'text-gray-600 hover:bg-gray-100'
}`}
>
<span>Date</span>
{sortBy === 'createdAt' && (
<ArrowUpDown className="w-3 h-3" />
)}
</button>
</div>
</div>
{/* Model Options - Scrollable */}
<div className="overflow-auto max-h-48">
{sortedModels.map((model) => {
const hasStats = model.likes > 0 || model.downloads > 0
return (
<ListboxOption
key={model.id}
value={model}
className={({ active, selected }) =>
`px-3 py-2 cursor-pointer border-b border-gray-100 last:border-b-0 ${
active ? 'bg-gray-50' : ''
} ${selected ? 'bg-blue-50' : ''}`
}
>
{({ selected }) => (
<div className="flex items-center justify-between">
<div className="flex items-center flex-1 mr-2">
<span className="text-sm font-medium truncate">
{model.id}
</span>
{selected && (
<Check className="w-4 h-4 text-blue-600 ml-2 flex-shrink-0" />
)}
</div>
{/* Stats Display */}
{hasStats && (
<div className="flex items-center space-x-3 text-xs text-gray-500 flex-shrink-0">
{model.likes > 0 && (
<div className="flex items-center space-x-1">
<Heart className="w-3 h-3 text-red-500" />
<span>{formatNumber(model.likes)}</span>
</div>
)}
{model.downloads > 0 && (
<div className="flex items-center space-x-1">
<Download className="w-3 h-3 text-green-500" />
<span>{formatNumber(model.downloads)}</span>
</div>
)}
{model.createdAt && (
<span className="text-xs text-gray-400">
{model.createdAt.split('T')[0]}
</span>
)}
</div>
)}
</div>
)}
</ListboxOption>
)
})}
</div>
</ListboxOptions>
</Transition>
</div>
</Listbox>
</div>
)
}
export default ModelSelector
|