Download frontend/src/components/common/HFModelSelector.tsx from sumandas/ostra: direct link, hf CLI and curl.
- Browser
- Download file 12.9 kB
-
https://huggingface.co/spaces/sumandas/ostra/resolve/main/frontend/src/components/common/HFModelSelector.tsx
- Command line
-
hf download hf://spaces/sumandas/ostra/frontend/src/components/common/HFModelSelector.tsx
-
curl -L -o HFModelSelector.tsx https://huggingface.co/spaces/sumandas/ostra/resolve/main/frontend/src/components/common/HFModelSelector.tsx
12.9 kB
| import { useEffect, useMemo, useRef, useState } from "react"; | |
| import { | |
| ChevronDown, | |
| Cpu, | |
| Download, | |
| Loader2, | |
| Search, | |
| Sparkles, | |
| } from "lucide-react"; | |
| import { searchHFModels } from "../../api/client"; | |
| import type { | |
| CuratedModel, | |
| CuratedReranker, | |
| HFModelSearchItem, | |
| } from "../../types/ostra"; | |
| interface HFModelSelectorProps { | |
| task: "embedding" | "reranker"; | |
| value: string; | |
| curatedModels?: CuratedModel[]; | |
| curatedRerankers?: CuratedReranker[]; | |
| isCpuOnly?: boolean; | |
| compact?: boolean; | |
| disabled?: boolean; | |
| placeholder?: string; | |
| onSelect: (modelId: string, item?: HFModelSearchItem) => void; | |
| } | |
| function formatCompactCount(n: number): string { | |
| if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M`; | |
| if (n >= 1_000) return `${(n / 1_000).toFixed(1)}k`; | |
| return String(n); | |
| } | |
| const EMPTY_MODELS: CuratedModel[] = []; | |
| const EMPTY_RERANKERS: CuratedReranker[] = []; | |
| /** | |
| * Unified Hugging Face Hub Model Selector used across Ostra for both: | |
| * - Dense embedding models (Dataset Wizard, Model Bakeoff Swap) | |
| * - Cross-encoder rerankers (Optimize -> Reranker Lab, Debug -> Engine Controls) | |
| */ | |
| export function HFModelSelector({ | |
| task, | |
| value, | |
| curatedModels = EMPTY_MODELS, | |
| curatedRerankers = EMPTY_RERANKERS, | |
| isCpuOnly = false, | |
| compact = false, | |
| disabled = false, | |
| placeholder, | |
| onSelect, | |
| }: HFModelSelectorProps) { | |
| const localCatalog = useMemo<HFModelSearchItem[]>(() => { | |
| if (task === "embedding") { | |
| return curatedModels.map((m) => ({ | |
| model_id: m.model_id, | |
| label: m.label, | |
| task: "embedding", | |
| downloads: 0, | |
| likes: 0, | |
| is_curated: true, | |
| dim: m.dim, | |
| max_seq_len: m.max_seq_len, | |
| size_mb: m.size_mb ?? null, | |
| recommended_doc_prefix: m.recommended_doc_prefix, | |
| recommended_query_prefix: m.recommended_query_prefix, | |
| requires_prefix: m.requires_prefix, | |
| cpu_friendly: m.cpu_friendly ?? true, | |
| notes: m.notes, | |
| })); | |
| } | |
| return curatedRerankers.map((r) => ({ | |
| model_id: r.model_id, | |
| label: r.label, | |
| task: "reranker", | |
| downloads: 0, | |
| likes: 0, | |
| is_curated: true, | |
| dim: null, | |
| max_seq_len: r.max_length, | |
| size_mb: r.size_mb, | |
| recommended_doc_prefix: "", | |
| recommended_query_prefix: "", | |
| requires_prefix: false, | |
| cpu_friendly: r.cpu_friendly ?? r.size_mb <= 500, | |
| notes: r.notes, | |
| })); | |
| }, [task, curatedModels, curatedRerankers]); | |
| const [open, setOpen] = useState(false); | |
| const [inputText, setInputText] = useState(value); | |
| const [searchQuery, setSearchQuery] = useState(""); | |
| const [loading, setLoading] = useState(false); | |
| const [items, setItems] = useState<HFModelSearchItem[]>(localCatalog); | |
| const [source, setSource] = useState<"hub" | "curated_fallback">("hub"); | |
| const containerRef = useRef<HTMLDivElement>(null); | |
| const reqIdRef = useRef(0); | |
| const localCatalogRef = useRef(localCatalog); | |
| localCatalogRef.current = localCatalog; | |
| // Keep input synchronized when external value changes (e.g. preset/apply) | |
| useEffect(() => { | |
| setInputText(value); | |
| }, [value]); | |
| // Seed list when local catalog arrives after bootstrap | |
| useEffect(() => { | |
| if (!open && localCatalog.length > 0) { | |
| setItems(localCatalog); | |
| } | |
| }, [localCatalog, open]); | |
| useEffect(() => { | |
| if (!open) return; | |
| const onDocMouseDown = (ev: MouseEvent) => { | |
| if (containerRef.current && !containerRef.current.contains(ev.target as Node)) { | |
| setOpen(false); | |
| setInputText(value); | |
| setSearchQuery(""); | |
| } | |
| }; | |
| document.addEventListener("mousedown", onDocMouseDown); | |
| return () => document.removeEventListener("mousedown", onDocMouseDown); | |
| }, [open, value]); | |
| // Live debounced Hugging Face Hub search while dropdown is open | |
| useEffect(() => { | |
| if (!open) return; | |
| const reqId = ++reqIdRef.current; | |
| const timer = window.setTimeout(async () => { | |
| setLoading(true); | |
| try { | |
| const res = await searchHFModels(searchQuery, task, 15); | |
| if (reqId === reqIdRef.current) { | |
| setItems(res.items); | |
| setSource(res.source); | |
| } | |
| } catch { | |
| if (reqId === reqIdRef.current) { | |
| const q = searchQuery.trim().toLowerCase(); | |
| setItems( | |
| localCatalogRef.current.filter( | |
| (m) => | |
| !q || | |
| `${m.model_id} ${m.label} ${m.notes}`.toLowerCase().includes(q) | |
| ) | |
| ); | |
| setSource("curated_fallback"); | |
| } | |
| } finally { | |
| if (reqId === reqIdRef.current) { | |
| setLoading(false); | |
| } | |
| } | |
| }, searchQuery ? 240 : 40); | |
| return () => window.clearTimeout(timer); | |
| }, [open, searchQuery, task]); | |
| const commitModel = (modelId: string, item?: HFModelSearchItem) => { | |
| const clean = modelId.trim(); | |
| if (!clean) return; | |
| const matched = | |
| item || | |
| items.find((i) => i.model_id.toLowerCase() === clean.toLowerCase()) || | |
| localCatalog.find((i) => i.model_id.toLowerCase() === clean.toLowerCase()); | |
| setInputText(clean); | |
| setSearchQuery(""); | |
| setOpen(false); | |
| onSelect(clean, matched); | |
| }; | |
| const trimmedSearch = searchQuery.trim(); | |
| const hasExactMatch = | |
| !trimmedSearch || | |
| items.some((i) => i.model_id.toLowerCase() === trimmedSearch.toLowerCase()); | |
| const defaultPlaceholder = | |
| placeholder ?? | |
| (task === "embedding" | |
| ? "Search HF embedding models (e.g. BAAI/bge-small-en-v1.5)" | |
| : "Search HF rerankers (e.g. cross-encoder/ms-marco-MiniLM-L-6-v2)"); | |
| return ( | |
| <div ref={containerRef} className="relative"> | |
| <div className="relative"> | |
| <input | |
| value={inputText} | |
| disabled={disabled} | |
| onFocus={() => { | |
| setOpen(true); | |
| setSearchQuery(""); | |
| }} | |
| onChange={(e) => { | |
| const next = e.target.value; | |
| setInputText(next); | |
| setSearchQuery(next); | |
| if (!open) setOpen(true); | |
| }} | |
| onKeyDown={(e) => { | |
| if (e.key === "Escape") { | |
| setOpen(false); | |
| setInputText(value); | |
| setSearchQuery(""); | |
| } else if (e.key === "Enter" && inputText.trim()) { | |
| e.preventDefault(); | |
| commitModel(inputText.trim()); | |
| } | |
| }} | |
| placeholder={defaultPlaceholder} | |
| className={`w-full bg-white border-[1.5px] border-[#0f172a] font-mono text-[#0f172a] focus:outline-none focus:ring-2 focus:ring-sky-500/25 disabled:opacity-60 ${ | |
| compact | |
| ? "pl-6 pr-6 py-1 text-[10px]" | |
| : "pl-7 pr-8 py-1.5 text-xs" | |
| }`} | |
| /> | |
| <Search | |
| className={`${ | |
| compact ? "w-3 h-3 left-1.5" : "w-3.5 h-3.5 left-2" | |
| } text-violet-700 absolute top-1/2 -translate-y-1/2 pointer-events-none`} | |
| /> | |
| <button | |
| type="button" | |
| disabled={disabled} | |
| onClick={() => { | |
| setOpen((prev) => { | |
| const next = !prev; | |
| if (next) setSearchQuery(""); | |
| return next; | |
| }); | |
| }} | |
| title={`Browse Hugging Face ${task} models`} | |
| className="absolute right-1.5 top-1/2 -translate-y-1/2 p-0.5 text-[#475569] hover:text-[#0f172a] cursor-pointer" | |
| > | |
| {loading ? ( | |
| <Loader2 className="w-3.5 h-3.5 animate-spin text-violet-700" /> | |
| ) : ( | |
| <ChevronDown className="w-3.5 h-3.5" /> | |
| )} | |
| </button> | |
| </div> | |
| {open && ( | |
| <div | |
| className={`absolute left-0 right-0 top-full mt-1 z-50 bg-white border-[1.5px] border-[#0f172a] shadow-[4px_4px_0_rgba(15,23,42,0.18)] ${ | |
| compact ? "max-h-60 min-w-[270px]" : "max-h-72" | |
| } flex flex-col`} | |
| > | |
| <div className="px-2.5 py-1.5 bg-[#f1f4f8] border-b border-[#cbd5e1] flex items-center justify-between gap-2"> | |
| <span className="font-mono text-[9px] font-bold uppercase tracking-wider text-[#334155] flex items-center gap-1"> | |
| <Cpu className="w-3 h-3 text-violet-700" /> | |
| <span> | |
| HF Hub {task === "embedding" ? "Embedding" : "Reranker"} Models | |
| </span> | |
| </span> | |
| <span className="font-mono text-[9px] text-[#64748b]"> | |
| {loading | |
| ? "Searching HF…" | |
| : source === "hub" | |
| ? "Live HF Hub" | |
| : "Curated list"} | |
| </span> | |
| </div> | |
| <div className="overflow-y-auto divide-y divide-[#e2e8f0]"> | |
| {trimmedSearch && !hasExactMatch && ( | |
| <button | |
| type="button" | |
| onClick={() => commitModel(trimmedSearch)} | |
| className="w-full px-2.5 py-2 text-left hover:bg-violet-50 flex items-center justify-between gap-2 cursor-pointer bg-[#f8fafc]" | |
| > | |
| <div className="min-w-0"> | |
| <div className="font-mono text-[11px] font-bold text-violet-950 truncate"> | |
| Use HF model: {trimmedSearch} | |
| </div> | |
| <div className="text-[10px] text-[#64748b]"> | |
| Load directly from huggingface.co/{trimmedSearch} | |
| </div> | |
| </div> | |
| <span className="px-1.5 py-0.5 bg-violet-100 border border-violet-700 font-mono text-[9px] font-bold text-violet-950 shrink-0"> | |
| USE ID | |
| </span> | |
| </button> | |
| )} | |
| {items.map((item) => { | |
| const isSelected = | |
| item.model_id.toLowerCase() === value.trim().toLowerCase(); | |
| return ( | |
| <button | |
| key={item.model_id} | |
| type="button" | |
| onClick={() => commitModel(item.model_id, item)} | |
| className={`w-full px-2.5 py-2 text-left transition-colors cursor-pointer ${ | |
| isSelected ? "bg-violet-50" : "hover:bg-[#f8fafc]" | |
| }`} | |
| > | |
| <div className="flex items-center justify-between gap-1.5"> | |
| <div className="flex items-center gap-1.5 min-w-0"> | |
| <span className="font-mono text-[11px] font-bold text-[#0f172a] truncate"> | |
| {item.model_id} | |
| </span> | |
| {item.is_curated && ( | |
| <span className="px-1 py-0.5 bg-sky-50 border border-sky-700 font-mono text-[8px] font-bold text-sky-900 inline-flex items-center gap-0.5 shrink-0"> | |
| <Sparkles className="w-2.5 h-2.5" /> | |
| Curated | |
| </span> | |
| )} | |
| </div> | |
| <div className="flex items-center gap-1 shrink-0"> | |
| {isCpuOnly && !item.cpu_friendly && ( | |
| <span className="px-1 py-0.5 bg-amber-50 border border-amber-700 font-mono text-[8px] font-bold text-amber-900"> | |
| Heavy CPU | |
| </span> | |
| )} | |
| {isCpuOnly && item.cpu_friendly && item.is_curated && ( | |
| <span className="px-1 py-0.5 bg-emerald-50 border border-emerald-700 font-mono text-[8px] font-bold text-emerald-900"> | |
| CPU Fast | |
| </span> | |
| )} | |
| {item.downloads > 0 && ( | |
| <span className="font-mono text-[9px] text-[#475569] tabular-nums inline-flex items-center gap-0.5"> | |
| <Download className="w-2.5 h-2.5 text-[#64748b]" /> | |
| {formatCompactCount(item.downloads)} | |
| </span> | |
| )} | |
| </div> | |
| </div> | |
| <div className="flex items-center gap-2 mt-0.5 font-mono text-[9px] text-[#475569] flex-wrap"> | |
| {item.dim && <span>{item.dim}d</span>} | |
| {item.max_seq_len && ( | |
| <span>{item.max_seq_len.toLocaleString()} tok</span> | |
| )} | |
| {item.size_mb && ( | |
| <span> | |
| {item.size_mb >= 1000 | |
| ? `${(item.size_mb / 1000).toFixed(1)} GB` | |
| : `${item.size_mb} MB`} | |
| </span> | |
| )} | |
| {item.notes && ( | |
| <span className="font-sans text-[10px] text-[#64748b] truncate"> | |
| · {item.notes} | |
| </span> | |
| )} | |
| </div> | |
| </button> | |
| ); | |
| })} | |
| </div> | |
| </div> | |
| )} | |
| </div> | |
| ); | |
| } | |