Skip to content

[internal] refacto tests for inference snippets #1287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 39 additions & 28 deletions packages/tasks-gen/scripts/generate-snippets-fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ import * as path from "node:path/posix";
import { snippets } from "@huggingface/inference";
import type { SnippetInferenceProvider, InferenceSnippet, ModelDataMinimal } from "@huggingface/tasks";

type LANGUAGE = "sh" | "js" | "py";
const LANGUAGES = ["sh", "js", "python"] as const;
type Language = (typeof LANGUAGES)[number];
const EXTENSIONS: Record<Language, string> = { sh: "sh", js: "js", python: "py" };

const TEST_CASES: {
testName: string;
model: ModelDataMinimal;
languages: LANGUAGE[];
languages: Language[];
providers: SnippetInferenceProvider[];
opts?: Record<string, unknown>;
}[] = [
Expand All @@ -39,7 +41,7 @@ const TEST_CASES: {
tags: [],
inference: "",
},
languages: ["py"],
languages: ["python"],
providers: ["hf-inference"],
},
{
Expand All @@ -50,7 +52,7 @@ const TEST_CASES: {
tags: ["conversational"],
inference: "",
},
languages: ["sh", "js", "py"],
languages: ["sh", "js", "python"],
providers: ["hf-inference", "together"],
opts: { streaming: false },
},
Expand All @@ -62,7 +64,7 @@ const TEST_CASES: {
tags: ["conversational"],
inference: "",
},
languages: ["sh", "js", "py"],
languages: ["sh", "js", "python"],
providers: ["hf-inference", "together"],
opts: { streaming: true },
},
Expand All @@ -74,7 +76,7 @@ const TEST_CASES: {
tags: ["conversational"],
inference: "",
},
languages: ["sh", "js", "py"],
languages: ["sh", "js", "python"],
providers: ["hf-inference", "fireworks-ai"],
opts: { streaming: false },
},
Expand All @@ -86,7 +88,7 @@ const TEST_CASES: {
tags: ["conversational"],
inference: "",
},
languages: ["sh", "js", "py"],
languages: ["sh", "js", "python"],
providers: ["hf-inference", "fireworks-ai"],
opts: { streaming: true },
},
Expand All @@ -98,7 +100,7 @@ const TEST_CASES: {
tags: [],
inference: "",
},
languages: ["py"],
languages: ["python"],
providers: ["hf-inference"],
},
{
Expand All @@ -109,7 +111,7 @@ const TEST_CASES: {
tags: [],
inference: "",
},
languages: ["py"],
languages: ["python"],
providers: ["hf-inference"],
},
{
Expand All @@ -121,7 +123,7 @@ const TEST_CASES: {
inference: "",
},
providers: ["hf-inference"],
languages: ["py"],
languages: ["python"],
},
{
testName: "text-to-audio-transformers",
Expand All @@ -132,7 +134,7 @@ const TEST_CASES: {
inference: "",
},
providers: ["hf-inference"],
languages: ["py"],
languages: ["python"],
},
{
testName: "text-to-image",
Expand All @@ -143,7 +145,7 @@ const TEST_CASES: {
inference: "",
},
providers: ["hf-inference", "fal-ai"],
languages: ["sh", "js", "py"],
languages: ["sh", "js", "python"],
},
{
testName: "text-to-video",
Expand All @@ -154,7 +156,7 @@ const TEST_CASES: {
inference: "",
},
providers: ["replicate", "fal-ai"],
languages: ["js", "py"],
languages: ["js", "python"],
},
{
testName: "text-classification",
Expand All @@ -165,7 +167,7 @@ const TEST_CASES: {
inference: "",
},
providers: ["hf-inference"],
languages: ["sh", "js", "py"],
languages: ["sh", "js", "python"],
},
{
testName: "basic-snippet--token-classification",
Expand All @@ -176,7 +178,7 @@ const TEST_CASES: {
inference: "",
},
providers: ["hf-inference"],
languages: ["py"],
languages: ["python"],
},
{
testName: "zero-shot-classification",
Expand All @@ -187,7 +189,7 @@ const TEST_CASES: {
inference: "",
},
providers: ["hf-inference"],
languages: ["py"],
languages: ["python"],
},
{
testName: "zero-shot-image-classification",
Expand All @@ -198,14 +200,14 @@ const TEST_CASES: {
inference: "",
},
providers: ["hf-inference"],
languages: ["py"],
languages: ["python"],
},
] as const;

const GET_SNIPPET_FN = {
sh: snippets.curl.getCurlInferenceSnippet,
js: snippets.js.getJsInferenceSnippet,
py: snippets.python.getPythonInferenceSnippet,
python: snippets.python.getPythonInferenceSnippet,
} as const;

const rootDirFinder = (): string => {
Expand All @@ -228,42 +230,51 @@ function getFixtureFolder(testName: string): string {

function generateInferenceSnippet(
model: ModelDataMinimal,
language: LANGUAGE,
language: Language,
provider: SnippetInferenceProvider,
opts?: Record<string, unknown>
): InferenceSnippet[] {
const providerModelId = provider === "hf-inference" ? model.id : `<${provider} alias for ${model.id}>`;
return GET_SNIPPET_FN[language](model, "api_token", provider, providerModelId, opts);
const snippets = GET_SNIPPET_FN[language](model, "api_token", provider, providerModelId, opts) as InferenceSnippet[];
return snippets.sort((snippetA, snippetB) => snippetA.client.localeCompare(snippetB.client));
}

async function getExpectedInferenceSnippet(
testName: string,
language: LANGUAGE,
language: Language,
provider: SnippetInferenceProvider
): Promise<InferenceSnippet[]> {
const fixtureFolder = getFixtureFolder(testName);
const files = await fs.readdir(fixtureFolder);
const languageFolder = path.join(fixtureFolder, language);
const files = await fs.readdir(languageFolder, { recursive: true });

const expectedSnippets: InferenceSnippet[] = [];
for (const file of files.filter((file) => file.endsWith("." + language) && file.includes(`.${provider}.`)).sort()) {
const client = path.basename(file).split(".").slice(1, -2).join("."); // e.g. '0.huggingface.js.replicate.js' => "huggingface.js"
const content = await fs.readFile(path.join(fixtureFolder, file), { encoding: "utf-8" });
for (const file of files.filter((file) => file.includes(`.${provider}.`)).sort()) {
const client = file.split("/")[0]; // e.g. fal_client/1.fal-ai.python => fal_client
const content = await fs.readFile(path.join(languageFolder, file), { encoding: "utf-8" });
expectedSnippets.push({ client, content });
}
return expectedSnippets;
}

async function saveExpectedInferenceSnippet(
testName: string,
language: LANGUAGE,
language: Language,
provider: SnippetInferenceProvider,
snippets: InferenceSnippet[]
) {
const fixtureFolder = getFixtureFolder(testName);
await fs.mkdir(fixtureFolder, { recursive: true });

for (const [index, snippet] of snippets.entries()) {
const file = path.join(fixtureFolder, `${index}.${snippet.client ?? "default"}.${provider}.${language}`);
const indexPerClient = new Map<string, number>();
for (const snippet of snippets) {
const extension = EXTENSIONS[language];
const client = snippet.client;
const index = indexPerClient.get(client) ?? 0;
indexPerClient.set(client, index + 1);

const file = path.join(fixtureFolder, language, snippet.client, `${index}.${provider}.${extension}`);
await fs.mkdir(path.dirname(file), { recursive: true });
await fs.writeFile(file, snippet.content);
}
}
Expand Down