Add component: Hugging Face Hub Search

#6
by hysts HF Staff - opened
components/hugging-face-hub-search.json ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "id": "hugging-face-hub-search",
3
+ "name": "Hugging Face Hub Search",
4
+ "description": "A live search input for finding models, datasets, spaces, users, and organizations on the Hugging Face Hub",
5
+ "author": "hysts",
6
+ "tags": [
7
+ "search",
8
+ "huggingface"
9
+ ],
10
+ "html_template": "<div class=\"hf-search\">\n <script type=\"application/json\" class=\"hf-search-data\">${value}</script>\n <div class=\"hf-search-input-wrap\">\n <svg class=\"hf-search-icon\" width=\"16\" height=\"16\" viewBox=\"0 0 16 16\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\">\n <path d=\"M11.5 7C11.5 9.48528 9.48528 11.5 7 11.5C4.51472 11.5 2.5 9.48528 2.5 7C2.5 4.51472 4.51472 2.5 7 2.5C9.48528 2.5 11.5 4.51472 11.5 7Z\" stroke=\"currentColor\" stroke-width=\"1.5\"/>\n <path d=\"M10.5 10.5L14 14\" stroke=\"currentColor\" stroke-width=\"1.5\" stroke-linecap=\"round\"/>\n </svg>\n <input type=\"text\" class=\"hf-search-input\" placeholder=\"${placeholder}\" autocomplete=\"off\" />\n <svg class=\"hf-search-clear\" width=\"14\" height=\"14\" viewBox=\"0 0 14 14\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\">\n <path d=\"M3.5 3.5L10.5 10.5M10.5 3.5L3.5 10.5\" stroke=\"currentColor\" stroke-width=\"1.5\" stroke-linecap=\"round\"/>\n </svg>\n </div>\n <div class=\"hf-search-filters\">${filters_html}</div>\n <div class=\"hf-search-dropdown\"></div>\n</div>\n",
11
+ "css_template": ".hf-search,\n.hf-search * {\n box-sizing: border-box;\n}\n\n.hf-search {\n position: relative;\n width: 100%;\n font-family: var(--font, system-ui, -apple-system, sans-serif);\n font-size: var(--text-md, 14px);\n}\n\n/* Input wrapper */\n.hf-search .hf-search-input-wrap {\n display: flex;\n align-items: center;\n gap: 8px;\n padding: 0 12px;\n height: 42px;\n border: 1px solid var(--border-color-primary, #e0e0e0);\n border-radius: var(--radius-lg, 8px);\n background: var(--background-fill-primary, #fff);\n transition: border-color 0.15s ease, box-shadow 0.15s ease;\n}\n\n.hf-search .hf-search-input-wrap:focus-within {\n border-color: var(--color-accent, #ff9d00);\n box-shadow: 0 0 0 2px color-mix(in srgb, var(--color-accent, #ff9d00) 20%, transparent);\n}\n\n/* Search icon */\n.hf-search .hf-search-icon {\n flex-shrink: 0;\n color: var(--body-text-color-subdued, #999);\n}\n\n/* Text input */\n.hf-search .hf-search-input {\n all: unset;\n flex: 1;\n height: 100%;\n color: var(--body-text-color, #333);\n font-size: var(--text-md, 14px);\n line-height: 1.5;\n min-width: 0;\n}\n\n.hf-search .hf-search-input::placeholder {\n color: var(--body-text-color-subdued, #999);\n}\n\n/* Clear button \u2014 hidden by default, shown via CSS when input has a value.\n Using :placeholder-shown avoids reliance on inline styles that Gradio's\n DOM-morphing would reset. */\n.hf-search .hf-search-clear {\n display: none;\n flex-shrink: 0;\n cursor: pointer;\n color: var(--body-text-color-subdued, #999);\n padding: 2px;\n border-radius: 3px;\n transition: color 0.15s ease;\n}\n\n.hf-search .hf-search-input:not(:placeholder-shown) ~ .hf-search-clear {\n display: block;\n}\n\n.hf-search .hf-search-clear:hover {\n color: var(--body-text-color, #333);\n}\n\n/* Filter chips */\n.hf-search .hf-search-filters:empty {\n display: none;\n}\n\n.hf-search .hf-search-filters {\n display: flex;\n flex-wrap: wrap;\n gap: 6px;\n padding-top: 8px;\n}\n\n.hf-search .hf-search-filter-chip {\n all: unset;\n display: inline-flex;\n align-items: center;\n gap: 5px;\n padding: 3px 10px;\n border: 1px solid var(--border-color-primary, #e0e0e0);\n border-radius: 999px;\n font-family: inherit;\n font-size: 12px;\n font-weight: 500;\n line-height: 1.5;\n cursor: pointer;\n user-select: none;\n transition: background-color 0.15s ease, opacity 0.15s ease, border-color 0.15s ease;\n color: var(--body-text-color, #333);\n background: var(--background-fill-primary, #fff);\n}\n\n.hf-search .hf-search-filter-chip.active {\n background: var(--background-fill-secondary, #f7f7f7);\n border-color: var(--body-text-color-subdued, #999);\n}\n\n.hf-search .hf-search-filter-chip:not(.active) {\n opacity: 0.45;\n}\n\n.hf-search .hf-search-filter-chip:hover {\n background: var(--background-fill-secondary, #f7f7f7);\n}\n\n.hf-search .hf-search-filter-dot {\n display: inline-block;\n width: 7px;\n height: 7px;\n border-radius: 50%;\n flex-shrink: 0;\n}\n\n/* Dropdown */\n.hf-search .hf-search-dropdown {\n display: none;\n position: absolute;\n top: calc(100% + 4px);\n left: 0;\n right: 0;\n z-index: 1000;\n max-height: 360px;\n overflow-y: auto;\n border: 1px solid var(--border-color-primary, #e0e0e0);\n border-radius: var(--radius-lg, 8px);\n background: var(--background-fill-primary, #fff);\n box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);\n}\n\n.hf-search .hf-search-dropdown.hf-search-open {\n display: block;\n}\n\n/* Category header */\n.hf-search .hf-search-category {\n display: flex;\n align-items: center;\n gap: 6px;\n padding: 8px 12px 4px;\n font-size: 11px;\n font-weight: 600;\n text-transform: uppercase;\n letter-spacing: 0.05em;\n color: var(--body-text-color-subdued, #888);\n}\n\n.hf-search .hf-search-category-dot {\n display: inline-block;\n width: 8px;\n height: 8px;\n border-radius: 50%;\n flex-shrink: 0;\n}\n\n.hf-search .hf-type-model {\n background-color: #6b7ffa;\n}\n\n.hf-search .hf-type-dataset {\n background-color: #e5686d;\n}\n\n.hf-search .hf-type-space {\n background-color: #eabf3f;\n}\n\n.hf-search .hf-type-user,\n.hf-search .hf-type-org {\n background-color: #56b886;\n}\n\n/* Result item */\n.hf-search .hf-search-item {\n all: unset;\n display: flex;\n align-items: center;\n gap: 8px;\n width: 100%;\n padding: 8px 12px;\n cursor: pointer;\n color: var(--body-text-color, #333);\n font-size: var(--text-md, 14px);\n transition: background-color 0.1s ease;\n}\n\n.hf-search .hf-search-item:hover,\n.hf-search .hf-search-item.hf-search-active {\n background: var(--background-fill-secondary, #f7f7f7);\n}\n\n.hf-search .hf-search-item-id {\n flex: 1;\n min-width: 0;\n overflow: hidden;\n text-overflow: ellipsis;\n white-space: nowrap;\n}\n\n.hf-search .hf-search-item-avatar {\n width: 20px;\n height: 20px;\n border-radius: 3px;\n flex-shrink: 0;\n object-fit: cover;\n background: var(--background-fill-secondary, #f0f0f0);\n}\n\n/* Loading / empty states */\n.hf-search .hf-search-status {\n padding: 12px;\n text-align: center;\n color: var(--body-text-color-subdued, #999);\n font-size: 13px;\n}\n\n/* Scrollbar */\n.hf-search .hf-search-dropdown::-webkit-scrollbar {\n width: 6px;\n}\n\n.hf-search .hf-search-dropdown::-webkit-scrollbar-track {\n background: transparent;\n}\n\n.hf-search .hf-search-dropdown::-webkit-scrollbar-thumb {\n background: var(--border-color-primary, #ddd);\n border-radius: 3px;\n}\n",
12
+ "js_on_load": "(function () {\n \"use strict\";\n\n var root = element.querySelector(\".hf-search\");\n var dataScript = element.querySelector(\"script.hf-search-data\");\n var inputEl = root.querySelector(\".hf-search-input\");\n var clearEl = root.querySelector(\".hf-search-clear\");\n var dropdown = root.querySelector(\".hf-search-dropdown\");\n var filtersEl = root.querySelector(\".hf-search-filters\");\n\n var searchType = String(props.search_type || \"all\");\n var submitOnSelect = props.submit_on_select !== false;\n\n var API_URL = \"https://huggingface.co/api/quicksearch\";\n var DEBOUNCE_MS = 300;\n var RESULT_LIMIT = props.result_limit || 5;\n\n var state = {\n query: \"\",\n results: [],\n activeIndex: -1,\n isOpen: false,\n abortController: null,\n debounceTimer: null,\n lastApiData: null,\n userFilters: null,\n // When submit_on_select=false, a dropdown click fills the input but does\n // not immediately set props.value. The selected result object (with full\n // type/url info) is cached here so that a subsequent Enter press can\n // submit the complete structured value rather than a bare string.\n pendingResult: null\n };\n\n // Category configuration\n // API returns different id fields per category:\n // models/datasets/spaces \u2192 \"id\", users \u2192 \"user\", orgs \u2192 \"name\"\n var CATEGORIES = {\n models: { label: \"Models\", type: \"model\", idKey: \"id\" },\n datasets: { label: \"Datasets\", type: \"dataset\", idKey: \"id\" },\n spaces: { label: \"Spaces\", type: \"space\", idKey: \"id\" },\n users: { label: \"Users\", type: \"user\", idKey: \"user\" },\n orgs: { label: \"Organizations\", type: \"org\", idKey: \"name\" }\n };\n\n // Determine which categories to show\n var allowedTypes = searchType === \"all\"\n ? null\n : searchType.split(\",\").map(function (s) { return s.trim(); });\n\n function isTypeAllowed(type) {\n if (allowedTypes && allowedTypes.indexOf(type) === -1) return false;\n if (state.userFilters && state.userFilters.indexOf(type) === -1) return false;\n return true;\n }\n\n // --- Debounce ---\n function debounce(fn, ms) {\n return function () {\n var args = arguments;\n var ctx = this;\n if (state.debounceTimer) clearTimeout(state.debounceTimer);\n state.debounceTimer = setTimeout(function () {\n state.debounceTimer = null;\n fn.apply(ctx, args);\n }, ms);\n };\n }\n\n // --- API search ---\n function doSearch(query) {\n if (!query) {\n state.results = [];\n closeDropdown();\n return;\n }\n\n // Cancel any in-flight request\n if (state.abortController) {\n state.abortController.abort();\n }\n state.abortController = new AbortController();\n\n var params = new URLSearchParams();\n params.set(\"q\", query);\n params.set(\"limit\", String(RESULT_LIMIT));\n\n // Filter by type if narrowed to exactly one type\n var activeFilters = state.userFilters || allowedTypes;\n if (activeFilters && activeFilters.length === 1) {\n params.set(\"type\", activeFilters[0]);\n }\n\n var url = API_URL + \"?\" + params.toString();\n\n fetch(url, { signal: state.abortController.signal })\n .then(function (res) {\n if (!res.ok) throw new Error(\"Search failed: \" + res.status + \" \" + res.statusText);\n return res.json();\n })\n .then(function (data) {\n state.abortController = null;\n state.lastApiData = data;\n parseResults(data);\n if (state.results.length > 0) {\n renderDropdown();\n openDropdown();\n } else {\n renderEmpty();\n openDropdown();\n }\n })\n .catch(function (err) {\n if (err.name === \"AbortError\") return;\n state.abortController = null;\n state.results = [];\n renderError();\n openDropdown();\n });\n }\n\n var debouncedSearch = debounce(doSearch, DEBOUNCE_MS);\n\n // --- Parse API response into flat result list with category grouping ---\n function parseResults(data) {\n var results = [];\n var categoryOrder = [\"models\", \"datasets\", \"spaces\", \"users\", \"orgs\"];\n\n for (var c = 0; c < categoryOrder.length; c++) {\n var catKey = categoryOrder[c];\n var catConfig = CATEGORIES[catKey];\n if (!isTypeAllowed(catConfig.type)) continue;\n\n // The quicksearch API may return \"users\" results under \"users\"\n // and \"orgs\" under \"orgs\", or combine them. Handle both structures.\n var items = data[catKey];\n if (!items || !items.length) continue;\n\n for (var i = 0; i < items.length; i++) {\n var item = items[i];\n var id = item[catConfig.idKey] || item._id || \"\";\n if (!id) continue;\n // Avatar URLs may be relative (e.g. \"/avatars/...\")\n var avatar = item.avatarUrl || null;\n if (avatar && avatar.charAt(0) === \"/\") {\n avatar = \"https://huggingface.co\" + avatar;\n }\n\n results.push({\n id: id,\n category: catKey,\n type: catConfig.type,\n label: catConfig.label,\n avatarUrl: avatar\n });\n }\n }\n\n state.results = results;\n state.activeIndex = -1;\n }\n\n // --- Render dropdown ---\n function renderDropdown() {\n dropdown.innerHTML = \"\";\n var currentCategory = null;\n\n for (var i = 0; i < state.results.length; i++) {\n var result = state.results[i];\n\n // Insert category header when category changes\n if (result.category !== currentCategory) {\n currentCategory = result.category;\n var header = document.createElement(\"div\");\n header.className = \"hf-search-category\";\n var dot = document.createElement(\"span\");\n dot.className = \"hf-search-category-dot hf-type-\" + result.type;\n header.appendChild(dot);\n header.appendChild(document.createTextNode(result.label));\n dropdown.appendChild(header);\n }\n\n var item = document.createElement(\"div\");\n item.className = \"hf-search-item\";\n item.setAttribute(\"data-index\", String(i));\n item.setAttribute(\"data-id\", result.id);\n\n if (result.avatarUrl) {\n var avatar = document.createElement(\"img\");\n avatar.className = \"hf-search-item-avatar\";\n avatar.src = result.avatarUrl;\n avatar.alt = \"\";\n avatar.loading = \"lazy\";\n item.appendChild(avatar);\n }\n\n var idSpan = document.createElement(\"span\");\n idSpan.className = \"hf-search-item-id\";\n idSpan.textContent = result.id;\n item.appendChild(idSpan);\n\n dropdown.appendChild(item);\n }\n }\n\n function renderEmpty() {\n dropdown.innerHTML = \"\";\n var msg = document.createElement(\"div\");\n msg.className = \"hf-search-status\";\n msg.textContent = \"No results found\";\n dropdown.appendChild(msg);\n }\n\n function renderError() {\n dropdown.innerHTML = \"\";\n var msg = document.createElement(\"div\");\n msg.className = \"hf-search-status\";\n msg.textContent = \"Search failed \u2014 please try again\";\n dropdown.appendChild(msg);\n }\n\n // --- Dropdown visibility ---\n function openDropdown() {\n state.isOpen = true;\n dropdown.classList.add(\"hf-search-open\");\n }\n\n function closeDropdown() {\n state.isOpen = false;\n state.activeIndex = -1;\n dropdown.classList.remove(\"hf-search-open\");\n }\n\n // --- Keyboard navigation ---\n function updateActiveItem() {\n var items = dropdown.querySelectorAll(\".hf-search-item\");\n for (var i = 0; i < items.length; i++) {\n if (i === state.activeIndex) {\n items[i].classList.add(\"hf-search-active\");\n // Scroll into view if needed\n items[i].scrollIntoView({ block: \"nearest\" });\n } else {\n items[i].classList.remove(\"hf-search-active\");\n }\n }\n }\n\n // --- URL construction ---\n function buildUrl(id, type) {\n var base = \"https://huggingface.co\";\n switch (type) {\n case \"dataset\": return base + \"/datasets/\" + id;\n case \"space\": return base + \"/spaces/\" + id;\n default: return base + \"/\" + id;\n }\n }\n\n // --- Selection ---\n // Accepts a result object {id, type, ...} from dropdown or a plain string\n // from manual input.\n //\n // forceSubmit: when true, always commit (set props.value) regardless of\n // submitOnSelect. Used for explicit Enter-key confirmations.\n function selectResult(resultOrId, forceSubmit) {\n var id, type, url;\n if (resultOrId && typeof resultOrId === \"object\") {\n id = resultOrId.id;\n type = resultOrId.type;\n url = buildUrl(id, type);\n } else {\n id = String(resultOrId);\n // Infer type when only one type is allowed\n type = (allowedTypes && allowedTypes.length === 1) ? allowedTypes[0] : null;\n url = type ? buildUrl(id, type) : null;\n }\n\n inputEl.value = id;\n closeDropdown();\n\n if (submitOnSelect || forceSubmit) {\n // Setting props.value causes Gradio to automatically dispatch\n // the \"change\" event \u2014 no explicit trigger(\"change\") needed.\n props.value = JSON.stringify({ id: id, type: type, url: url });\n state.pendingResult = null;\n } else {\n // submit_on_select=false: fill the input without committing.\n // Cache the full result so that a subsequent Enter press can\n // submit the structured value (preserving type/url).\n state.pendingResult = { id: id, type: type, url: url };\n }\n }\n\n // --- Filter chips (pre-rendered in HTML template) ---\n // Read existing chip elements from the DOM and initialize state.\n var chipEls = filtersEl.querySelectorAll(\".hf-search-filter-chip\");\n\n if (chipEls.length > 0) {\n state.userFilters = [];\n for (var ci2 = 0; ci2 < chipEls.length; ci2++) {\n state.userFilters.push(chipEls[ci2].getAttribute(\"data-type\"));\n }\n\n // Use event delegation so handlers survive DOM re-renders.\n filtersEl.addEventListener(\"click\", function (e) {\n var target = e.target;\n while (target && target !== filtersEl) {\n if (target.classList && target.classList.contains(\"hf-search-filter-chip\")) {\n var type = target.getAttribute(\"data-type\");\n var idx = state.userFilters.indexOf(type);\n if (idx !== -1) {\n if (state.userFilters.length <= 1) {\n // Sole active chip \u2192 reset: make all chips active\n state.userFilters = [];\n var resetChips = filtersEl.querySelectorAll(\".hf-search-filter-chip\");\n for (var ri = 0; ri < resetChips.length; ri++) {\n state.userFilters.push(resetChips[ri].getAttribute(\"data-type\"));\n }\n } else {\n // Multiple active \u2192 exclusive select: keep only the clicked chip\n state.userFilters = [type];\n }\n } else {\n // Inactive chip \u2192 additive: add to current selection\n state.userFilters.push(type);\n }\n syncChipVisuals();\n\n // Re-filter cached results if available\n if (state.lastApiData && state.query) {\n parseResults(state.lastApiData);\n if (state.results.length > 0) {\n renderDropdown();\n openDropdown();\n } else {\n renderEmpty();\n openDropdown();\n }\n }\n return;\n }\n target = target.parentElement;\n }\n });\n }\n\n // --- Event listeners ---\n\n // Input typing\n inputEl.addEventListener(\"input\", function () {\n state.query = inputEl.value.trim();\n state.pendingResult = null;\n if (state.query.length > 0) {\n debouncedSearch(state.query);\n } else {\n state.results = [];\n closeDropdown();\n }\n });\n\n // Focus opens dropdown if there are results\n inputEl.addEventListener(\"focus\", function () {\n if (state.results.length > 0 && state.query.length > 0) {\n renderDropdown();\n openDropdown();\n }\n });\n\n // Keyboard navigation\n inputEl.addEventListener(\"keydown\", function (e) {\n if (!state.isOpen) {\n if (e.key === \"Enter\" && inputEl.value.trim()) {\n // Enter with closed dropdown is always an explicit commit.\n // If the user previously clicked a dropdown item without\n // submitting (submit_on_select=false), reuse the cached\n // structured result so type/url are preserved.\n var pending = state.pendingResult;\n if (pending && pending.id === inputEl.value.trim()) {\n props.value = JSON.stringify(pending);\n state.pendingResult = null;\n } else {\n selectResult(inputEl.value.trim(), true);\n }\n e.preventDefault();\n }\n return;\n }\n\n switch (e.key) {\n case \"ArrowDown\":\n e.preventDefault();\n state.activeIndex = Math.min(state.activeIndex + 1, state.results.length - 1);\n updateActiveItem();\n break;\n case \"ArrowUp\":\n e.preventDefault();\n state.activeIndex = Math.max(state.activeIndex - 1, -1);\n updateActiveItem();\n break;\n case \"Enter\":\n // Enter with an open dropdown is always an explicit commit.\n e.preventDefault();\n if (state.activeIndex >= 0 && state.activeIndex < state.results.length) {\n selectResult(state.results[state.activeIndex], true);\n } else if (inputEl.value.trim()) {\n selectResult(inputEl.value.trim(), true);\n }\n break;\n case \"Escape\":\n e.preventDefault();\n closeDropdown();\n break;\n }\n });\n\n // Click on dropdown item (event delegation)\n dropdown.addEventListener(\"mousedown\", function (e) {\n // Prevent input blur which would close dropdown before click registers\n e.preventDefault();\n\n var target = e.target;\n while (target && target !== dropdown) {\n if (target.classList && target.classList.contains(\"hf-search-item\")) {\n var idx = parseInt(target.getAttribute(\"data-index\"), 10);\n if (!isNaN(idx) && state.results[idx]) {\n selectResult(state.results[idx]);\n }\n return;\n }\n target = target.parentElement;\n }\n });\n\n // Clear button\n clearEl.addEventListener(\"mousedown\", function (e) {\n e.preventDefault();\n inputEl.value = \"\";\n state.query = \"\";\n state.results = [];\n state.pendingResult = null;\n closeDropdown();\n inputEl.focus();\n\n // Setting props.value causes Gradio to automatically dispatch \"change\".\n props.value = \"\";\n });\n\n // Click outside to close\n document.addEventListener(\"mousedown\", function (e) {\n if (!root.contains(e.target)) {\n closeDropdown();\n }\n });\n\n // --- Chip visual sync (re-applies JS state after Gradio DOM morph) ---\n function syncChipVisuals() {\n if (!state.userFilters) return;\n var allChips = filtersEl.querySelectorAll(\".hf-search-filter-chip\");\n for (var si = 0; si < allChips.length; si++) {\n var chipType = allChips[si].getAttribute(\"data-type\");\n if (state.userFilters.indexOf(chipType) !== -1) {\n allChips[si].classList.add(\"active\");\n } else {\n allChips[si].classList.remove(\"active\");\n }\n }\n }\n\n // --- Value sync from Python (MutationObserver) ---\n function handleValue() {\n var raw = dataScript.textContent.trim();\n if (!raw || raw === \"null\" || raw === \"undefined\") {\n syncChipVisuals();\n return;\n }\n\n // Value may be JSON (structured) or a plain string (repo ID)\n var displayId = raw;\n try {\n var parsed = JSON.parse(raw);\n if (parsed && typeof parsed === \"object\" && parsed.id) {\n displayId = parsed.id;\n }\n } catch (_e) {\n // Plain string value - use as-is\n }\n\n if (displayId !== inputEl.value && !state.isOpen) {\n inputEl.value = displayId;\n }\n syncChipVisuals();\n }\n\n var observer = new MutationObserver(function () { handleValue(); });\n observer.observe(dataScript, { childList: true, characterData: true, subtree: true });\n\n // Initial value sync\n handleValue();\n})();\n",
13
+ "default_props": {
14
+ "value": "",
15
+ "placeholder": "Search Hugging Face Hub...",
16
+ "search_type": "all",
17
+ "submit_on_select": true,
18
+ "result_limit": 5,
19
+ "filters_html": "<button type=\"button\" class=\"hf-search-filter-chip active\" data-type=\"model\"><span class=\"hf-search-filter-dot hf-type-model\"></span>Models</button><button type=\"button\" class=\"hf-search-filter-chip active\" data-type=\"dataset\"><span class=\"hf-search-filter-dot hf-type-dataset\"></span>Datasets</button><button type=\"button\" class=\"hf-search-filter-chip active\" data-type=\"space\"><span class=\"hf-search-filter-dot hf-type-space\"></span>Spaces</button><button type=\"button\" class=\"hf-search-filter-chip active\" data-type=\"user\"><span class=\"hf-search-filter-dot hf-type-user\"></span>Users</button><button type=\"button\" class=\"hf-search-filter-chip active\" data-type=\"org\"><span class=\"hf-search-filter-dot hf-type-org\"></span>Organizations</button>"
20
+ },
21
+ "python_code": "class HuggingFaceHubSearch(gr.HTML):\n def __init__(\n self,\n value: str | None = None,\n *,\n label: str | None = None,\n placeholder: str = \"Search Hugging Face Hub...\",\n search_type: str | list[str] = \"all\",\n submit_on_select: bool = True,\n result_limit: int = _RESULT_LIMIT_DEFAULT,\n **kwargs: Any, # noqa: ANN401 - must accept arbitrary kwargs for gr.HTML\n ) -> None:\n if (\n isinstance(result_limit, bool)\n or not isinstance(result_limit, int)\n or not (1 <= result_limit <= _RESULT_LIMIT_MAX)\n ):\n msg = f\"result_limit must be an integer between 1 and {_RESULT_LIMIT_MAX}, got {result_limit!r}.\"\n raise ValueError(msg)\n\n search_type_str = _normalize_search_type(search_type)\n filters_html = _build_filters_html(search_type_str)\n\n html_template = (_STATIC_DIR / \"template.html\").read_text(encoding=\"utf-8\")\n css_template = (_STATIC_DIR / \"style.css\").read_text(encoding=\"utf-8\")\n js_on_load = (_STATIC_DIR / \"script.js\").read_text(encoding=\"utf-8\")\n\n super().__init__(\n value=value,\n label=label,\n show_label=label is not None,\n container=label is not None,\n html_template=html_template,\n css_template=css_template,\n js_on_load=js_on_load,\n placeholder=placeholder,\n search_type=search_type_str,\n submit_on_select=submit_on_select,\n result_limit=result_limit,\n filters_html=filters_html,\n **kwargs,\n )\n\n def preprocess(self, value: str | None) -> dict[str, str | None] | None:\n if not value:\n return None\n try:\n parsed = json.loads(value)\n if isinstance(parsed, dict):\n return parsed\n except (json.JSONDecodeError, TypeError):\n pass\n # Plain string (backward compat) \u2014 wrap as minimal dict\n return {\"id\": value, \"type\": None, \"url\": None}\n\n def postprocess(self, value: str | dict | None) -> str | None:\n if value is None:\n return None\n if isinstance(value, dict):\n return value.get(\"id\")\n return str(value)\n\n def api_info(self) -> dict[str, Any]:\n return {\n \"type\": \"object\",\n \"description\": \"Selected Hugging Face Hub entity with its id, type, and URL.\",\n \"properties\": {\n \"id\": {\"type\": \"string\"},\n \"type\": {\"type\": \"string\"},\n \"url\": {\"type\": \"string\"},\n },\n }\n",
22
+ "head": "",
23
+ "repo_url": "https://github.com/hysts/gradio-huggingface-hub-search"
24
+ }
manifest.json CHANGED
@@ -157,5 +157,16 @@
157
  "description": "",
158
  "author": "abidlabs",
159
  "tags": []
 
 
 
 
 
 
 
 
 
 
 
160
  }
161
  ]
 
157
  "description": "",
158
  "author": "abidlabs",
159
  "tags": []
160
+ },
161
+ {
162
+ "id": "hugging-face-hub-search",
163
+ "name": "Hugging Face Hub Search",
164
+ "description": "A live search input for finding models, datasets, spaces, users, and organizations on the Hugging Face Hub",
165
+ "author": "hysts",
166
+ "tags": [
167
+ "search",
168
+ "huggingface"
169
+ ],
170
+ "repo_url": "https://github.com/hysts/gradio-huggingface-hub-search"
171
  }
172
  ]