Buckets:
| import { GRADIO_PREFIX, GRADIO_PRIVATE_PREFIX } from '../../shared/constants.js'; | |
| import { logger } from './logger.js'; | |
| import { getGradioSpaces } from './gradio-discovery.js'; | |
| import { repoExists } from '@huggingface/hub'; | |
| export function isGradioTool(toolName) { | |
| return /^grp?\d+_/.test(toolName); | |
| } | |
| export function createGradioToolName(toolName, index, isPrivate, toolIndex) { | |
| const prefix = isPrivate ? GRADIO_PRIVATE_PREFIX : GRADIO_PREFIX; | |
| const indexStr = (index + 1).toString(); | |
| const maxNameLength = 49 - prefix.length - indexStr.length - 1; | |
| let sanitizedName = toolName | |
| ? toolName | |
| .replace(/[-\s.]+/g, '_') | |
| .toLowerCase() | |
| : 'unknown'; | |
| if (sanitizedName.length > maxNameLength) { | |
| if (toolIndex !== undefined) { | |
| const toolIndexPrefix = `${toolIndex}_`; | |
| const availableForName = maxNameLength - toolIndexPrefix.length; | |
| const keepFromEnd = availableForName - 20 - 1; | |
| sanitizedName = toolIndexPrefix + sanitizedName.substring(0, 20) + '_' + sanitizedName.slice(-keepFromEnd); | |
| } | |
| else { | |
| const keepFromEnd = maxNameLength - 20 - 1; | |
| sanitizedName = sanitizedName.substring(0, 20) + '_' + sanitizedName.slice(-keepFromEnd); | |
| } | |
| } | |
| return `${prefix}${indexStr}_${sanitizedName}`; | |
| } | |
| export function parseGradioSpaceIds(gradioParam) { | |
| const spaces = []; | |
| const trimmed = gradioParam.trim(); | |
| if (trimmed.toLowerCase() === 'none') { | |
| return spaces; | |
| } | |
| const entries = gradioParam | |
| .split(',') | |
| .map((s) => s.trim()) | |
| .filter((s) => s.length > 0); | |
| for (const entry of entries) { | |
| if (entry.toLowerCase() === 'none') { | |
| continue; | |
| } | |
| const slashCount = (entry.match(/\//g) || []).length; | |
| const slashIndex = entry.indexOf('/'); | |
| const isValidFormat = slashCount === 1 && slashIndex > 0 && slashIndex < entry.length - 1; | |
| if (!isValidFormat) { | |
| logger.warn(`Skipping invalid gradio entry "${entry}": must contain exactly one slash with content on both sides (format: username/space-name)`); | |
| continue; | |
| } | |
| spaces.push({ name: entry }); | |
| logger.debug(`Parsed gradio space ID: ${entry}`); | |
| } | |
| return spaces; | |
| } | |
| async function fetchGradioSubdomains(spaceIds, hfToken) { | |
| if (spaceIds.length === 0) { | |
| return []; | |
| } | |
| const spaceNames = spaceIds.map((s) => s.name); | |
| const spaces = await getGradioSpaces(spaceNames, hfToken, { skipSchemas: true }); | |
| const spaceTools = spaces.map((space) => ({ | |
| _id: space._id, | |
| name: space.name, | |
| subdomain: space.subdomain, | |
| emoji: space.emoji, | |
| })); | |
| logger.debug({ | |
| requested: spaceIds.length, | |
| successful: spaceTools.length, | |
| }, 'Fetched Gradio subdomains'); | |
| return spaceTools; | |
| } | |
| export async function parseAndFetchGradioEndpoints(gradioParam, hfToken) { | |
| const parsedSpaces = parseGradioSpaceIds(gradioParam); | |
| if (parsedSpaces.length === 0) { | |
| return []; | |
| } | |
| return fetchGradioSubdomains(parsedSpaces, hfToken); | |
| } | |
| function isFileListingRelevant(params) { | |
| return params.gradioSpaceCount > 0 || params.builtInTools.includes(params.dynamicSpaceToolId); | |
| } | |
| export async function resolveMcpFileListingSource(params) { | |
| const { username, token, gradioSpaceCount, builtInTools, dynamicSpaceToolId } = params; | |
| if (!username || !token || !isFileListingRelevant({ gradioSpaceCount, builtInTools, dynamicSpaceToolId })) { | |
| return null; | |
| } | |
| const datasetId = `${username}/gradio-files`; | |
| if (await repoExists({ | |
| repo: { type: 'dataset', name: datasetId }, | |
| accessToken: token, | |
| })) { | |
| return { kind: 'dataset', id: datasetId }; | |
| } | |
| return null; | |
| } | |
| //# sourceMappingURL=gradio-utils.js.map |
Xet Storage Details
- Size:
- 3.88 kB
- Xet hash:
- 2e24c2c9797295bd885d68107525974cea83b4a24de81d44d497e0238c96b6e1
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.