|
| 1 | +/* |
| 2 | + * Generates Inference API snippets using @huggingface/tasks snippets. |
| 3 | + * |
| 4 | + * If used in test mode ("pnpm test"), it compares the generated snippets with the expected ones. |
| 5 | + * If used in generation mode ("pnpm generate-snippets-fixtures"), it generates the expected snippets. |
| 6 | + * |
| 7 | + * Expected snippets are saved under ./snippets-fixtures and are meant to be versioned on GitHub. |
| 8 | + * Each snippet is saved in a separate file placed under "./{test-name}/{index}.{client}.{language}": |
| 9 | + * - test-name: the name of the test (e.g. "text-to-image", "conversational-llm", etc.) |
| 10 | + * - index: the order of the snippet in the array of snippets (0 if not an array) |
| 11 | + * - client: the client name (e.g. "requests", "huggingface_hub", "openai", etc.). Default to "default" if client is not specified. |
| 12 | + * - language: the language of the snippet (e.g. "sh", "js", "py", etc.) |
| 13 | + * |
| 14 | + * Example: |
| 15 | + * ./packages/tasks-gen/snippets-fixtures/text-to-image/0.huggingface_hub.py |
| 16 | + */ |
| 17 | + |
| 18 | +import { existsSync as pathExists } from "node:fs"; |
| 19 | +import * as fs from "node:fs/promises"; |
| 20 | +import * as path from "node:path/posix"; |
| 21 | + |
| 22 | +import type { InferenceSnippet } from "@huggingface/tasks"; |
| 23 | +import { snippets } from "@huggingface/tasks"; |
| 24 | + |
| 25 | +type LANGUAGE = "sh" | "js" | "py"; |
| 26 | + |
| 27 | +const TEST_CASES: { |
| 28 | + testName: string; |
| 29 | + model: snippets.ModelDataMinimal; |
| 30 | + languages: LANGUAGE[]; |
| 31 | + opts?: Record<string, unknown>; |
| 32 | +}[] = [ |
| 33 | + { |
| 34 | + testName: "conversational-llm-non-stream", |
| 35 | + model: { |
| 36 | + id: "meta-llama/Llama-3.1-8B-Instruct", |
| 37 | + pipeline_tag: "text-generation", |
| 38 | + tags: ["conversational"], |
| 39 | + inference: "", |
| 40 | + }, |
| 41 | + languages: ["sh", "js", "py"], |
| 42 | + opts: { streaming: false }, |
| 43 | + }, |
| 44 | + { |
| 45 | + testName: "conversational-llm-stream", |
| 46 | + model: { |
| 47 | + id: "meta-llama/Llama-3.1-8B-Instruct", |
| 48 | + pipeline_tag: "text-generation", |
| 49 | + tags: ["conversational"], |
| 50 | + inference: "", |
| 51 | + }, |
| 52 | + languages: ["sh", "js", "py"], |
| 53 | + opts: { streaming: true }, |
| 54 | + }, |
| 55 | + { |
| 56 | + testName: "conversational-vlm-non-stream", |
| 57 | + model: { |
| 58 | + id: "meta-llama/Llama-3.2-11B-Vision-Instruct", |
| 59 | + pipeline_tag: "image-text-to-text", |
| 60 | + tags: ["conversational"], |
| 61 | + inference: "", |
| 62 | + }, |
| 63 | + languages: ["sh", "js", "py"], |
| 64 | + opts: { streaming: false }, |
| 65 | + }, |
| 66 | + { |
| 67 | + testName: "conversational-vlm-stream", |
| 68 | + model: { |
| 69 | + id: "meta-llama/Llama-3.2-11B-Vision-Instruct", |
| 70 | + pipeline_tag: "image-text-to-text", |
| 71 | + tags: ["conversational"], |
| 72 | + inference: "", |
| 73 | + }, |
| 74 | + languages: ["sh", "js", "py"], |
| 75 | + opts: { streaming: true }, |
| 76 | + }, |
| 77 | + { |
| 78 | + testName: "text-to-image", |
| 79 | + model: { |
| 80 | + id: "black-forest-labs/FLUX.1-schnell", |
| 81 | + pipeline_tag: "text-to-image", |
| 82 | + tags: [], |
| 83 | + inference: "", |
| 84 | + }, |
| 85 | + languages: ["sh", "js", "py"], |
| 86 | + }, |
| 87 | +] as const; |
| 88 | + |
| 89 | +const GET_SNIPPET_FN = { |
| 90 | + sh: snippets.curl.getCurlInferenceSnippet, |
| 91 | + js: snippets.js.getJsInferenceSnippet, |
| 92 | + py: snippets.python.getPythonInferenceSnippet, |
| 93 | +} as const; |
| 94 | + |
| 95 | +const rootDirFinder = (): string => { |
| 96 | + let currentPath = path.normalize(import.meta.url).replace("file:", ""); |
| 97 | + |
| 98 | + while (currentPath !== "/") { |
| 99 | + if (pathExists(path.join(currentPath, "package.json"))) { |
| 100 | + return currentPath; |
| 101 | + } |
| 102 | + |
| 103 | + currentPath = path.normalize(path.join(currentPath, "..")); |
| 104 | + } |
| 105 | + |
| 106 | + return "/"; |
| 107 | +}; |
| 108 | + |
| 109 | +function getFixtureFolder(testName: string): string { |
| 110 | + return path.join(rootDirFinder(), "snippets-fixtures", testName); |
| 111 | +} |
| 112 | + |
| 113 | +function generateInferenceSnippet( |
| 114 | + model: snippets.ModelDataMinimal, |
| 115 | + language: LANGUAGE, |
| 116 | + opts?: Record<string, unknown> |
| 117 | +): InferenceSnippet[] { |
| 118 | + const generatedSnippets = GET_SNIPPET_FN[language](model, "api_token", opts); |
| 119 | + return Array.isArray(generatedSnippets) ? generatedSnippets : [generatedSnippets]; |
| 120 | +} |
| 121 | + |
| 122 | +async function getExpectedInferenceSnippet(testName: string, language: LANGUAGE): Promise<InferenceSnippet[]> { |
| 123 | + const fixtureFolder = getFixtureFolder(testName); |
| 124 | + const files = await fs.readdir(fixtureFolder); |
| 125 | + |
| 126 | + const expectedSnippets: InferenceSnippet[] = []; |
| 127 | + for (const file of files.filter((file) => file.endsWith("." + language)).sort()) { |
| 128 | + const client = path.basename(file).split(".").slice(1, -1).join("."); // e.g. '0.huggingface.js.js' => "huggingface.js" |
| 129 | + const content = await fs.readFile(path.join(fixtureFolder, file), { encoding: "utf-8" }); |
| 130 | + expectedSnippets.push(client === "default" ? { content } : { client, content }); |
| 131 | + } |
| 132 | + return expectedSnippets; |
| 133 | +} |
| 134 | + |
| 135 | +async function saveExpectedInferenceSnippet(testName: string, language: LANGUAGE, snippets: InferenceSnippet[]) { |
| 136 | + const fixtureFolder = getFixtureFolder(testName); |
| 137 | + await fs.mkdir(fixtureFolder, { recursive: true }); |
| 138 | + |
| 139 | + for (const [index, snippet] of snippets.entries()) { |
| 140 | + const file = path.join(fixtureFolder, `${index}.${snippet.client ?? "default"}.${language}`); |
| 141 | + await fs.writeFile(file, snippet.content); |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +if (import.meta.vitest) { |
| 146 | + // Run test if in test mode |
| 147 | + const { describe, expect, it } = import.meta.vitest; |
| 148 | + |
| 149 | + describe("inference API snippets", () => { |
| 150 | + TEST_CASES.forEach(({ testName, model, languages, opts }) => { |
| 151 | + describe(testName, () => { |
| 152 | + languages.forEach((language) => { |
| 153 | + it(language, async () => { |
| 154 | + const generatedSnippets = generateInferenceSnippet(model, language, opts); |
| 155 | + const expectedSnippets = await getExpectedInferenceSnippet(testName, language); |
| 156 | + expect(generatedSnippets).toEqual(expectedSnippets); |
| 157 | + }); |
| 158 | + }); |
| 159 | + }); |
| 160 | + }); |
| 161 | + }); |
| 162 | +} else { |
| 163 | + // Otherwise, generate the fixtures |
| 164 | + console.log("✨ Re-generating snippets"); |
| 165 | + console.debug(" 🚜 Removing existing fixtures..."); |
| 166 | + await fs.rm(path.join(rootDirFinder(), "snippets-fixtures"), { recursive: true, force: true }); |
| 167 | + |
| 168 | + console.debug(" 🏭 Generating new fixtures..."); |
| 169 | + TEST_CASES.forEach(({ testName, model, languages, opts }) => { |
| 170 | + console.debug(` ${testName} (${languages.join(", ")})`); |
| 171 | + languages.forEach(async (language) => { |
| 172 | + const generatedSnippets = generateInferenceSnippet(model, language, opts); |
| 173 | + await saveExpectedInferenceSnippet(testName, language, generatedSnippets); |
| 174 | + }); |
| 175 | + }); |
| 176 | + console.log("✅ All done!"); |
| 177 | + console.log("👉 Please check the generated fixtures before committing them."); |
| 178 | +} |
0 commit comments