diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f9111a4258..fe07e409fc 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -105,7 +105,7 @@ jobs: run: | sleep 3 pnpm i --filter root --filter inference... --filter hub... --filter tasks-gen --frozen-lockfile - pnpm --filter inference --filter hub --filter tasks publish --force --no-git-checks --registry http://localhost:4874/ + pnpm --filter inference --filter hub --filter tasks --filter jinja publish --force --no-git-checks --registry http://localhost:4874/ - name: E2E test - test yarn install working-directory: e2e/ts @@ -136,7 +136,7 @@ jobs: deno-version: vx.x.x - name: E2E test - deno import from npm working-directory: e2e/deno - run: deno run --allow-net --allow-env=HF_TOKEN index.ts + run: deno run --allow-read --allow-net --allow-env=HF_TOKEN index.ts env: NPM_CONFIG_REGISTRY: http://localhost:4874/ HF_TOKEN: ${{ secrets.HF_TOKEN }} diff --git a/.vscode/settings.json b/.vscode/settings.json index 9d7878528a..e5c0fb1c32 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -17,5 +17,5 @@ "search.exclude": { "**/dist": true }, - "typescript.tsdk": "node_modules/typescript/lib" + "typescript.tsdk": "node_modules/typescript/lib", } diff --git a/packages/inference/package.json b/packages/inference/package.json index 466a8093ef..dfe93e8d64 100644 --- a/packages/inference/package.json +++ b/packages/inference/package.json @@ -33,9 +33,16 @@ "main": "./dist/index.cjs", "module": "./dist/index.js", "exports": { - "types": "./dist/src/index.d.ts", - "require": "./dist/index.cjs", - "import": "./dist/index.js" + ".": { + "types": "./dist/src/index.d.ts", + "require": "./dist/index.cjs", + "import": "./dist/index.js" + } + }, + "browser": { + "./src/snippets/index.js": false, + "./dist/index.js": "./dist/browser/index.js", + "./dist/index.mjs": "./dist/browser/index.mjs" }, "type": "module", "scripts": { @@ -52,7 +59,8 @@ "check": "tsc" }, "dependencies": { - "@huggingface/tasks": "workspace:^" + "@huggingface/tasks": "workspace:^", + "@huggingface/jinja": "workspace:^" }, "devDependencies": { "@types/node": "18.13.0" diff --git a/packages/inference/pnpm-lock.yaml b/packages/inference/pnpm-lock.yaml index 5bb56da2d1..082c8ed752 100644 --- a/packages/inference/pnpm-lock.yaml +++ b/packages/inference/pnpm-lock.yaml @@ -5,6 +5,9 @@ settings: excludeLinksFromLockfile: false dependencies: + '@huggingface/jinja': + specifier: workspace:^ + version: link:../jinja '@huggingface/tasks': specifier: workspace:^ version: link:../tasks diff --git a/packages/inference/src/index.ts b/packages/inference/src/index.ts index b21526a5ea..aa16be8e63 100644 --- a/packages/inference/src/index.ts +++ b/packages/inference/src/index.ts @@ -2,6 +2,6 @@ export { InferenceClient, InferenceClientEndpoint, HfInference } from "./Inferen export { InferenceOutputError } from "./lib/InferenceOutputError"; export * from "./types"; export * from "./tasks"; - import * as snippets from "./snippets/index.js"; + export { snippets }; diff --git a/packages/inference/src/lib/makeRequestOptions.ts b/packages/inference/src/lib/makeRequestOptions.ts index a6472d99db..061fce5637 100644 --- a/packages/inference/src/lib/makeRequestOptions.ts +++ b/packages/inference/src/lib/makeRequestOptions.ts @@ -56,13 +56,15 @@ export async function makeRequestOptions( /** In most cases (unless we pass a endpointUrl) we know the task */ task?: InferenceTask; chatCompletion?: boolean; + /* Used internally to generate inference snippets (in which case model mapping is done separately) */ + skipModelIdResolution?: boolean; } ): Promise<{ url: string; info: RequestInit }> { const { accessToken, endpointUrl, provider: maybeProvider, model: maybeModel, ...remainingArgs } = args; const provider = maybeProvider ?? "hf-inference"; const providerConfig = providerConfigs[provider]; - const { includeCredentials, task, chatCompletion, signal } = options ?? {}; + const { includeCredentials, task, chatCompletion, signal, skipModelIdResolution } = options ?? {}; if (endpointUrl && provider !== "hf-inference") { throw new Error(`Cannot use endpointUrl with a third-party provider.`); @@ -81,15 +83,17 @@ export async function makeRequestOptions( } // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const hfModel = maybeModel ?? (await loadDefaultModel(task!)); - const model = providerConfig.clientSideRoutingOnly - ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - removeProviderPrefix(maybeModel!, provider) - : // For closed-models API providers, one needs to pass the model ID directly (e.g. "gpt-3.5-turbo") - await getProviderModelId({ model: hfModel, provider }, args, { - task, - chatCompletion, - fetch: options?.fetch, - }); + const model = skipModelIdResolution + ? hfModel + : providerConfig.clientSideRoutingOnly + ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + removeProviderPrefix(maybeModel!, provider) + : // For closed-models API providers, one needs to pass the model ID directly (e.g. "gpt-3.5-turbo") + await getProviderModelId({ model: hfModel, provider }, args, { + task, + chatCompletion, + fetch: options?.fetch, + }); const authMethod = (() => { if (providerConfig.clientSideRoutingOnly) { diff --git a/packages/inference/src/snippets/python.ts b/packages/inference/src/snippets/python.ts index ef3df2564f..f685f8b9ae 100644 --- a/packages/inference/src/snippets/python.ts +++ b/packages/inference/src/snippets/python.ts @@ -1,58 +1,137 @@ -import { openAIbaseUrl, type SnippetInferenceProvider } from "@huggingface/tasks"; import type { PipelineType, WidgetType } from "@huggingface/tasks/src/pipelines.js"; import type { ChatCompletionInputMessage, GenerationParameters } from "@huggingface/tasks/src/tasks/index.js"; import { + openAIbaseUrl, type InferenceSnippet, type ModelDataMinimal, getModelInputSnippet, stringifyGenerationConfig, stringifyMessages, } from "@huggingface/tasks"; +import type { InferenceProvider } from "../types"; +import { Template } from "@huggingface/jinja"; +import fs from "fs"; +import path from "path"; +import { existsSync as pathExists } from "node:fs"; + +const TOOLS = ["huggingface_hub", "requests", "fal_client", "openai"]; + +type InputPreparationFn = (model: ModelDataMinimal, opts?: Record) => string | object; +interface TemplateParams { + accessToken?: string; + baseUrl?: string; + inputs?: string | object; + model?: ModelDataMinimal; + provider?: InferenceProvider; + providerModelId?: string; + methodName?: string; // specific to snippetBasic + importBase64?: boolean; // specific to snippetImportRequests +} + +// Helpers to find + load templates + +const rootDirFinder = (): string => { + let currentPath = + typeof import.meta !== "undefined" && import.meta.url + ? path.normalize(new URL(import.meta.url).pathname) /// for ESM + : __dirname; /// for CJS + + while (currentPath !== "/") { + if (pathExists(path.join(currentPath, "package.json"))) { + return currentPath; + } + + currentPath = path.normalize(path.join(currentPath, "..")); + } + + return "/"; +}; + +const templatePath = (tool: string, templateName: string): string => + path.join(rootDirFinder(), "src", "snippets", "templates", "python", tool, `${templateName}.jinja`); +const hasTemplate = (tool: string, templateName: string): boolean => pathExists(templatePath(tool, templateName)); + +const loadTemplate = (tool: string, templateName: string): ((data: TemplateParams) => string) => { + const template = fs.readFileSync(templatePath(tool, templateName), "utf8"); + return (data: TemplateParams) => new Template(template).render({ ...data }); +}; + +const snippetImportInferenceClient = loadTemplate("huggingface_hub", "importInferenceClient"); +const snippetImportRequests = loadTemplate("requests", "importRequests"); + +// Needed for huggingface_hub basic snippets const HFH_INFERENCE_CLIENT_METHODS: Partial> = { "audio-classification": "audio_classification", "audio-to-audio": "audio_to_audio", "automatic-speech-recognition": "automatic_speech_recognition", - "text-to-speech": "text_to_speech", + "document-question-answering": "document_question_answering", + "feature-extraction": "feature_extraction", + "fill-mask": "fill_mask", "image-classification": "image_classification", "image-segmentation": "image_segmentation", "image-to-image": "image_to_image", "image-to-text": "image_to_text", "object-detection": "object_detection", - "text-to-image": "text_to_image", - "text-to-video": "text_to_video", - "zero-shot-image-classification": "zero_shot_image_classification", - "document-question-answering": "document_question_answering", - "visual-question-answering": "visual_question_answering", - "feature-extraction": "feature_extraction", - "fill-mask": "fill_mask", "question-answering": "question_answering", "sentence-similarity": "sentence_similarity", summarization: "summarization", "table-question-answering": "table_question_answering", + "tabular-classification": "tabular_classification", + "tabular-regression": "tabular_regression", "text-classification": "text_classification", "text-generation": "text_generation", + "text-to-image": "text_to_image", + "text-to-speech": "text_to_speech", + "text-to-video": "text_to_video", "token-classification": "token_classification", translation: "translation", + "visual-question-answering": "visual_question_answering", "zero-shot-classification": "zero_shot_classification", - "tabular-classification": "tabular_classification", - "tabular-regression": "tabular_regression", + "zero-shot-image-classification": "zero_shot_image_classification", }; -const snippetImportInferenceClient = (accessToken: string, provider: SnippetInferenceProvider): string => - `\ -from huggingface_hub import InferenceClient - -client = InferenceClient( - provider="${provider}", - api_key="${accessToken || "{API_TOKEN}"}", -)`; +// Snippet generators +const snippetGenerator = (templateName: string, inputPreparationFn?: InputPreparationFn) => { + return ( + model: ModelDataMinimal, + accessToken: string, + provider: InferenceProvider, + providerModelId?: string, + opts?: Record + ): InferenceSnippet[] => { + const params: TemplateParams = { + accessToken, + baseUrl: templateName.includes("conversational") ? openAIbaseUrl(provider) : undefined, + inputs: inputPreparationFn ? inputPreparationFn(model, opts) : getModelInputSnippet(model), + model, + provider, + providerModelId: providerModelId ?? model.id, + }; + + // Iterate over tools => check if a snippet exists => generate + return TOOLS.map((tool) => { + if (!hasTemplate(tool, templateName)) { + return; + } + const template = loadTemplate(tool, templateName); + if (tool === "huggingface_hub" && templateName === "basic") { + if (!(model.pipeline_tag && model.pipeline_tag in HFH_INFERENCE_CLIENT_METHODS)) { + return; + } + params["methodName"] = HFH_INFERENCE_CLIENT_METHODS[model.pipeline_tag]; + } + return { + client: tool, + content: template(params).trim(), + }; + }).filter((snippet) => snippet !== undefined && snippet.content) as InferenceSnippet[]; + }; +}; -const snippetConversational = ( +// Input preparation functions (required for a few tasks) +const prepareConversationalInput = ( model: ModelDataMinimal, - accessToken: string, - provider: SnippetInferenceProvider, - providerModelId?: string, opts?: { streaming?: boolean; messages?: ChatCompletionInputMessage[]; @@ -60,421 +139,53 @@ const snippetConversational = ( max_tokens?: GenerationParameters["max_tokens"]; top_p?: GenerationParameters["top_p"]; } -): InferenceSnippet[] => { - const streaming = opts?.streaming ?? true; +): object => { const exampleMessages = getModelInputSnippet(model) as ChatCompletionInputMessage[]; const messages = opts?.messages ?? exampleMessages; - const messagesStr = stringifyMessages(messages, { attributeKeyQuotes: true }); - const config = { - ...(opts?.temperature ? { temperature: opts.temperature } : undefined), + ...(opts?.temperature ? { temperature: opts?.temperature } : undefined), max_tokens: opts?.max_tokens ?? 500, - ...(opts?.top_p ? { top_p: opts.top_p } : undefined), + ...(opts?.top_p ? { top_p: opts?.top_p } : undefined), }; - const configStr = stringifyGenerationConfig(config, { - indent: "\n\t", - attributeValueConnector: "=", - }); - - if (streaming) { - return [ - { - client: "huggingface_hub", - content: `\ -${snippetImportInferenceClient(accessToken, provider)} - -messages = ${messagesStr} - -stream = client.chat.completions.create( - model="${model.id}", - messages=messages, - ${configStr} - stream=True, -) - -for chunk in stream: - print(chunk.choices[0].delta.content, end="")`, - }, - { - client: "openai", - content: `\ -from openai import OpenAI - -client = OpenAI( - base_url="${openAIbaseUrl(provider)}", - api_key="${accessToken || "{API_TOKEN}"}" -) - -messages = ${messagesStr} - -stream = client.chat.completions.create( - model="${providerModelId ?? model.id}", - messages=messages, - ${configStr} - stream=True -) - -for chunk in stream: - print(chunk.choices[0].delta.content, end="")`, - }, - ]; - } else { - return [ - { - client: "huggingface_hub", - content: `\ -${snippetImportInferenceClient(accessToken, provider)} - -messages = ${messagesStr} - -completion = client.chat.completions.create( - model="${model.id}", - messages=messages, - ${configStr} -) - -print(completion.choices[0].message)`, - }, - { - client: "openai", - content: `\ -from openai import OpenAI - -client = OpenAI( - base_url="${openAIbaseUrl(provider)}", - api_key="${accessToken || "{API_TOKEN}"}" -) - -messages = ${messagesStr} - -completion = client.chat.completions.create( - model="${providerModelId ?? model.id}", - messages=messages, - ${configStr} -) - -print(completion.choices[0].message)`, - }, - ]; - } -}; - -const snippetZeroShotClassification = (model: ModelDataMinimal): InferenceSnippet[] => { - return [ - { - client: "requests", - content: `\ -def query(payload): - response = requests.post(API_URL, headers=headers, json=payload) - return response.json() - -output = query({ - "inputs": ${getModelInputSnippet(model)}, - "parameters": {"candidate_labels": ["refund", "legal", "faq"]}, -})`, - }, - ]; -}; - -const snippetZeroShotImageClassification = (model: ModelDataMinimal): InferenceSnippet[] => { - return [ - { - client: "requests", - content: `def query(data): - with open(data["image_path"], "rb") as f: - img = f.read() - payload={ - "parameters": data["parameters"], - "inputs": base64.b64encode(img).decode("utf-8") - } - response = requests.post(API_URL, headers=headers, json=payload) - return response.json() - -output = query({ - "image_path": ${getModelInputSnippet(model)}, - "parameters": {"candidate_labels": ["cat", "dog", "llama"]}, -})`, - }, - ]; -}; -const snippetBasic = ( - model: ModelDataMinimal, - accessToken: string, - provider: SnippetInferenceProvider -): InferenceSnippet[] => { - return [ - ...(model.pipeline_tag && model.pipeline_tag in HFH_INFERENCE_CLIENT_METHODS - ? [ - { - client: "huggingface_hub", - content: `\ -${snippetImportInferenceClient(accessToken, provider)} - -result = client.${HFH_INFERENCE_CLIENT_METHODS[model.pipeline_tag]}( - inputs=${getModelInputSnippet(model)}, - model="${model.id}", -) - -print(result) -`, - }, - ] - : []), - { - client: "requests", - content: `\ -def query(payload): - response = requests.post(API_URL, headers=headers, json=payload) - return response.json() - -output = query({ - "inputs": ${getModelInputSnippet(model)}, -})`, - }, - ]; -}; - -const snippetFile = (model: ModelDataMinimal): InferenceSnippet[] => { - return [ - { - client: "requests", - content: `\ -def query(filename): - with open(filename, "rb") as f: - data = f.read() - response = requests.post(API_URL, headers=headers, data=data) - return response.json() - -output = query(${getModelInputSnippet(model)})`, - }, - ]; -}; - -const snippetTextToImage = ( - model: ModelDataMinimal, - accessToken: string, - provider: SnippetInferenceProvider, - providerModelId?: string -): InferenceSnippet[] => { - return [ - { - client: "huggingface_hub", - content: `\ -${snippetImportInferenceClient(accessToken, provider)} - -# output is a PIL.Image object -image = client.text_to_image( - ${getModelInputSnippet(model)}, - model="${model.id}", -)`, - }, - ...(provider === "fal-ai" - ? [ - { - client: "fal-client", - content: `\ -import fal_client - -result = fal_client.subscribe( - "${providerModelId ?? model.id}", - arguments={ - "prompt": ${getModelInputSnippet(model)}, - }, -) -print(result) -`, - }, - ] - : []), - ...(provider === "hf-inference" - ? [ - { - client: "requests", - content: `\ -def query(payload): - response = requests.post(API_URL, headers=headers, json=payload) - return response.content - -image_bytes = query({ - "inputs": ${getModelInputSnippet(model)}, -}) - -# You can access the image with PIL.Image for example -import io -from PIL import Image -image = Image.open(io.BytesIO(image_bytes))`, - }, - ] - : []), - ]; -}; - -const snippetTextToVideo = ( - model: ModelDataMinimal, - accessToken: string, - provider: SnippetInferenceProvider -): InferenceSnippet[] => { - return ["fal-ai", "replicate"].includes(provider) - ? [ - { - client: "huggingface_hub", - content: `\ -${snippetImportInferenceClient(accessToken, provider)} - -video = client.text_to_video( - ${getModelInputSnippet(model)}, - model="${model.id}", -)`, - }, - ] - : []; -}; - -const snippetTabular = (model: ModelDataMinimal): InferenceSnippet[] => { - return [ - { - client: "requests", - content: `\ -def query(payload): - response = requests.post(API_URL, headers=headers, json=payload) - return response.content - -response = query({ - "inputs": {"data": ${getModelInputSnippet(model)}}, -})`, - }, - ]; -}; - -const snippetTextToAudio = (model: ModelDataMinimal): InferenceSnippet[] => { - // Transformers TTS pipeline and api-inference-community (AIC) pipeline outputs are diverged - // with the latest update to inference-api (IA). - // Transformers IA returns a byte object (wav file), whereas AIC returns wav and sampling_rate. - if (model.library_name === "transformers") { - return [ - { - client: "requests", - content: `\ -def query(payload): - response = requests.post(API_URL, headers=headers, json=payload) - return response.content - -audio_bytes = query({ - "inputs": ${getModelInputSnippet(model)}, -}) -# You can access the audio with IPython.display for example -from IPython.display import Audio -Audio(audio_bytes)`, - }, - ]; - } else { - return [ - { - client: "requests", - content: `def query(payload): - response = requests.post(API_URL, headers=headers, json=payload) - return response.json() - -audio, sampling_rate = query({ - "inputs": ${getModelInputSnippet(model)}, -}) -# You can access the audio with IPython.display for example -from IPython.display import Audio -Audio(audio, rate=sampling_rate)`, - }, - ]; - } -}; - -const snippetAutomaticSpeechRecognition = ( - model: ModelDataMinimal, - accessToken: string, - provider: SnippetInferenceProvider -): InferenceSnippet[] => { - return [ - { - client: "huggingface_hub", - content: `${snippetImportInferenceClient(accessToken, provider)} -output = client.automatic_speech_recognition(${getModelInputSnippet(model)}, model="${model.id}")`, - }, - snippetFile(model)[0], - ]; + return { + messagesStr: stringifyMessages(messages, { attributeKeyQuotes: true }), + configStr: stringifyGenerationConfig(config, { + indent: "\n\t", + attributeValueConnector: "=", + }), + }; }; -const snippetDocumentQuestionAnswering = ( - model: ModelDataMinimal, - accessToken: string, - provider: SnippetInferenceProvider -): InferenceSnippet[] => { +const prepareDocumentQuestionAnsweringInput = (model: ModelDataMinimal): object => { const inputsAsStr = getModelInputSnippet(model) as string; const inputsAsObj = JSON.parse(inputsAsStr); + return { asObj: inputsAsObj, asStr: inputsAsStr }; +}; - return [ - { - client: "huggingface_hub", - content: `${snippetImportInferenceClient(accessToken, provider)} -output = client.document_question_answering( - "${inputsAsObj.image}", - question="${inputsAsObj.question}", - model="${model.id}", -)`, - }, - { - client: "requests", - content: `def query(payload): - with open(payload["image"], "rb") as f: - img = f.read() - payload["image"] = base64.b64encode(img).decode("utf-8") - response = requests.post(API_URL, headers=headers, json=payload) - return response.json() - -output = query({ - "inputs": ${inputsAsStr}, -})`, - }, - ]; +const prepareImageToImageInput = (model: ModelDataMinimal): object => { + return JSON.parse(getModelInputSnippet(model) as string); }; -const snippetImageToImage = ( +const snippetConversational = ( model: ModelDataMinimal, accessToken: string, - provider: SnippetInferenceProvider + provider: InferenceProvider, + providerModelId?: string, + opts?: { + streaming?: boolean; + messages?: ChatCompletionInputMessage[]; + temperature?: GenerationParameters["temperature"]; + max_tokens?: GenerationParameters["max_tokens"]; + top_p?: GenerationParameters["top_p"]; + } ): InferenceSnippet[] => { - const inputsAsStr = getModelInputSnippet(model) as string; - const inputsAsObj = JSON.parse(inputsAsStr); - - return [ - { - client: "huggingface_hub", - content: `${snippetImportInferenceClient(accessToken, provider)} -# output is a PIL.Image object -image = client.image_to_image( - "${inputsAsObj.image}", - prompt="${inputsAsObj.prompt}", - model="${model.id}", -)`, - }, - { - client: "requests", - content: `def query(payload): - with open(payload["inputs"], "rb") as f: - img = f.read() - payload["inputs"] = base64.b64encode(img).decode("utf-8") - response = requests.post(API_URL, headers=headers, json=payload) - return response.content - -image_bytes = query({ - "inputs": "${inputsAsObj.image}", - "parameters": {"prompt": "${inputsAsObj.prompt}"}, -}) - -# You can access the image with PIL.Image for example -import io -from PIL import Image -image = Image.open(io.BytesIO(image_bytes))`, - }, - ]; + const streaming = opts?.streaming ?? true; + const templateName = streaming ? "conversationalStream" : "conversational"; + return snippetGenerator(templateName, prepareConversationalInput)(model, accessToken, provider, providerModelId, { + ...opts, + streaming, + }); }; const pythonSnippets: Partial< @@ -483,81 +194,77 @@ const pythonSnippets: Partial< ( model: ModelDataMinimal, accessToken: string, - provider: SnippetInferenceProvider, + provider: InferenceProvider, providerModelId?: string, opts?: Record ) => InferenceSnippet[] > > = { - // Same order as in tasks/src/pipelines.ts - "text-classification": snippetBasic, - "token-classification": snippetBasic, - "table-question-answering": snippetBasic, - "question-answering": snippetBasic, - "zero-shot-classification": snippetZeroShotClassification, - translation: snippetBasic, - summarization: snippetBasic, - "feature-extraction": snippetBasic, - "text-generation": snippetBasic, - "text2text-generation": snippetBasic, + "audio-classification": snippetGenerator("basicFile"), + "audio-to-audio": snippetGenerator("basicFile"), + "automatic-speech-recognition": snippetGenerator("automaticSpeechRecognition"), + "document-question-answering": snippetGenerator("documentQuestionAnswering", prepareDocumentQuestionAnsweringInput), + "feature-extraction": snippetGenerator("basic"), + "fill-mask": snippetGenerator("basic"), + "image-classification": snippetGenerator("basicFile"), + "image-segmentation": snippetGenerator("basicFile"), "image-text-to-text": snippetConversational, - "fill-mask": snippetBasic, - "sentence-similarity": snippetBasic, - "automatic-speech-recognition": snippetAutomaticSpeechRecognition, - "text-to-image": snippetTextToImage, - "text-to-video": snippetTextToVideo, - "text-to-speech": snippetTextToAudio, - "text-to-audio": snippetTextToAudio, - "audio-to-audio": snippetFile, - "audio-classification": snippetFile, - "image-classification": snippetFile, - "tabular-regression": snippetTabular, - "tabular-classification": snippetTabular, - "object-detection": snippetFile, - "image-segmentation": snippetFile, - "document-question-answering": snippetDocumentQuestionAnswering, - "image-to-text": snippetFile, - "image-to-image": snippetImageToImage, - "zero-shot-image-classification": snippetZeroShotImageClassification, + "image-to-image": snippetGenerator("imageToImage", prepareImageToImageInput), + "image-to-text": snippetGenerator("basicFile"), + "object-detection": snippetGenerator("basicFile"), + "question-answering": snippetGenerator("basic"), + "sentence-similarity": snippetGenerator("basic"), + summarization: snippetGenerator("basic"), + "tabular-classification": snippetGenerator("tabular"), + "tabular-regression": snippetGenerator("tabular"), + "table-question-answering": snippetGenerator("basic"), + "text-classification": snippetGenerator("basic"), + "text-generation": snippetGenerator("basic"), + "text-to-audio": snippetGenerator("textToAudio"), + "text-to-image": snippetGenerator("textToImage"), + "text-to-speech": snippetGenerator("textToAudio"), + "text-to-video": snippetGenerator("textToVideo"), + "text2text-generation": snippetGenerator("basic"), + "token-classification": snippetGenerator("basic"), + translation: snippetGenerator("basic"), + "zero-shot-classification": snippetGenerator("zeroShotClassification"), + "zero-shot-image-classification": snippetGenerator("zeroShotImageClassification"), }; - export function getPythonInferenceSnippet( model: ModelDataMinimal, accessToken: string, - provider: SnippetInferenceProvider, + provider: InferenceProvider, providerModelId?: string, opts?: Record ): InferenceSnippet[] { - if (model.tags.includes("conversational")) { - // Conversational model detected, so we display a code snippet that features the Messages API - return snippetConversational(model, accessToken, provider, providerModelId, opts); - } else { - const snippets = - model.pipeline_tag && model.pipeline_tag in pythonSnippets - ? pythonSnippets[model.pipeline_tag]?.(model, accessToken, provider, providerModelId) ?? [] - : []; + const snippets = model.tags.includes("conversational") + ? snippetConversational(model, accessToken, provider, providerModelId, opts) + : model.pipeline_tag && model.pipeline_tag in pythonSnippets + ? pythonSnippets[model.pipeline_tag]?.(model, accessToken, provider, providerModelId) ?? [] + : []; - return snippets.map((snippet) => { - return { - ...snippet, - content: addImportsToSnippet(snippet.content, model, accessToken), - }; - }); - } + return snippets.map((snippet) => addImportsToSnippet(snippet, model, accessToken, provider)); } -const addImportsToSnippet = (snippet: string, model: ModelDataMinimal, accessToken: string): string => { - if (snippet.includes("requests")) { - snippet = `import requests - -API_URL = "https://router.huggingface.co/hf-inference/models/${model.id}" -headers = {"Authorization": ${accessToken ? `"Bearer ${accessToken}"` : `f"Bearer {API_TOKEN}"`}} - -${snippet}`; - } - if (snippet.includes("base64")) { - snippet = `import base64 -${snippet}`; +const addImportsToSnippet = ( + snippet: InferenceSnippet, + model: ModelDataMinimal, + accessToken: string, + provider: InferenceProvider +): InferenceSnippet => { + let importSection: string | undefined = undefined; + if (snippet.client === "huggingface_hub") { + importSection = snippetImportInferenceClient({ accessToken, provider }); + } else if (snippet.content.includes("requests")) { + importSection = snippetImportRequests({ + accessToken, + model: model, + provider, + importBase64: snippet.content.includes("base64"), + }); } - return snippet; + return { + ...snippet, + content: importSection ? `${importSection}\n\n${snippet.content}` : snippet.content, + }; }; diff --git a/packages/inference/src/snippets/templates/python/fal_client/textToImage.jinja b/packages/inference/src/snippets/templates/python/fal_client/textToImage.jinja new file mode 100644 index 0000000000..db0c94dd09 --- /dev/null +++ b/packages/inference/src/snippets/templates/python/fal_client/textToImage.jinja @@ -0,0 +1,11 @@ +{% if provider == "fal-ai" %} +import fal_client + +result = fal_client.subscribe( + "{{ providerModelId }}", + arguments={ + "prompt": {{ inputs }}, + }, +) +print(result) +{% endif %} \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/huggingface_hub/automaticSpeechRecognition.jinja b/packages/inference/src/snippets/templates/python/huggingface_hub/automaticSpeechRecognition.jinja new file mode 100644 index 0000000000..2a4f0f7769 --- /dev/null +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/automaticSpeechRecognition.jinja @@ -0,0 +1 @@ +output = client.automatic_speech_recognition({{ inputs }}, model="{{ model.id }}") \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/huggingface_hub/basic.jinja b/packages/inference/src/snippets/templates/python/huggingface_hub/basic.jinja new file mode 100644 index 0000000000..f2d22d6778 --- /dev/null +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/basic.jinja @@ -0,0 +1,6 @@ +result = client.{{ methodName }}( + inputs={{ inputs }}, + model="{{ model.id }}", +) + +print(result) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/huggingface_hub/conversational.jinja b/packages/inference/src/snippets/templates/python/huggingface_hub/conversational.jinja new file mode 100644 index 0000000000..a3ee06e8a5 --- /dev/null +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/conversational.jinja @@ -0,0 +1,9 @@ +messages = {{ inputs.messagesStr }} + +completion = client.chat.completions.create( + model="{{ model.id }}", + messages=messages, + {{ inputs.configStr }} +) + +print(completion.choices[0].message) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/huggingface_hub/conversationalStream.jinja b/packages/inference/src/snippets/templates/python/huggingface_hub/conversationalStream.jinja new file mode 100644 index 0000000000..013e513c8a --- /dev/null +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/conversationalStream.jinja @@ -0,0 +1,11 @@ +messages = {{ inputs.messagesStr }} + +stream = client.chat.completions.create( + model="{{ model.id }}", + messages=messages, + {{ inputs.configStr }} + stream=True, +) + +for chunk in stream: + print(chunk.choices[0].delta.content, end="") \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/huggingface_hub/documentQuestionAnswering.jinja b/packages/inference/src/snippets/templates/python/huggingface_hub/documentQuestionAnswering.jinja new file mode 100644 index 0000000000..7643cfed0d --- /dev/null +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/documentQuestionAnswering.jinja @@ -0,0 +1,5 @@ +output = client.document_question_answering( + "{{ inputs.asObj.image }}", + question="{{ inputs.asObj.question }}", + model="{{ model.id }}", +) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/huggingface_hub/imageToImage.jinja b/packages/inference/src/snippets/templates/python/huggingface_hub/imageToImage.jinja new file mode 100644 index 0000000000..001d807daf --- /dev/null +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/imageToImage.jinja @@ -0,0 +1,6 @@ +# output is a PIL.Image object +image = client.image_to_image( + "{{ inputs.image }}", + prompt="{{ inputs.prompt }}", + model="{{ model.id }}", +) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/huggingface_hub/importInferenceClient.jinja b/packages/inference/src/snippets/templates/python/huggingface_hub/importInferenceClient.jinja new file mode 100644 index 0000000000..9a6514f267 --- /dev/null +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/importInferenceClient.jinja @@ -0,0 +1,6 @@ +from huggingface_hub import InferenceClient + +client = InferenceClient( + provider="{{ provider }}", + api_key="{{ accessToken }}", +) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/huggingface_hub/textToImage.jinja b/packages/inference/src/snippets/templates/python/huggingface_hub/textToImage.jinja new file mode 100644 index 0000000000..5379d68b11 --- /dev/null +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/textToImage.jinja @@ -0,0 +1,5 @@ +# output is a PIL.Image object +image = client.text_to_image( + {{ inputs }}, + model="{{ model.id }}", +) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/huggingface_hub/textToVideo.jinja b/packages/inference/src/snippets/templates/python/huggingface_hub/textToVideo.jinja new file mode 100644 index 0000000000..f1d5e8d8a8 --- /dev/null +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/textToVideo.jinja @@ -0,0 +1,4 @@ +video = client.text_to_video( + {{ inputs }}, + model="{{ model.id }}", +) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/openai/conversational.jinja b/packages/inference/src/snippets/templates/python/openai/conversational.jinja new file mode 100644 index 0000000000..4d827f1788 --- /dev/null +++ b/packages/inference/src/snippets/templates/python/openai/conversational.jinja @@ -0,0 +1,16 @@ +from openai import OpenAI + +client = OpenAI( + base_url="{{ baseUrl }}", + api_key="{{ accessToken }}" +) + +messages = {{ inputs.messagesStr }} + +completion = client.chat.completions.create( + model="{{ providerModelId }}", + messages=messages, + {{ inputs.configStr }} +) + +print(completion.choices[0].message) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/openai/conversationalStream.jinja b/packages/inference/src/snippets/templates/python/openai/conversationalStream.jinja new file mode 100644 index 0000000000..8c71bd745a --- /dev/null +++ b/packages/inference/src/snippets/templates/python/openai/conversationalStream.jinja @@ -0,0 +1,18 @@ +from openai import OpenAI + +client = OpenAI( + base_url="{{ baseUrl }}", + api_key="{{ accessToken }}" +) + +messages = {{ inputs.messagesStr }} + +stream = client.chat.completions.create( + model="{{ providerModelId }}", + messages=messages, + {{ inputs.configStr }} + stream=True +) + +for chunk in stream: + print(chunk.choices[0].delta.content, end="") \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/automaticSpeechRecognition.jinja b/packages/inference/src/snippets/templates/python/requests/automaticSpeechRecognition.jinja new file mode 100644 index 0000000000..e73c2ce79a --- /dev/null +++ b/packages/inference/src/snippets/templates/python/requests/automaticSpeechRecognition.jinja @@ -0,0 +1,7 @@ +def query(filename): + with open(filename, "rb") as f: + data = f.read() + response = requests.post(API_URL, headers=headers, data=data) + return response.json() + +output = query({{ inputs }}) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/basic.jinja b/packages/inference/src/snippets/templates/python/requests/basic.jinja new file mode 100644 index 0000000000..489218820d --- /dev/null +++ b/packages/inference/src/snippets/templates/python/requests/basic.jinja @@ -0,0 +1,7 @@ +def query(payload): + response = requests.post(API_URL, headers=headers, json=payload) + return response.json() + +output = query({ + "inputs": {{ inputs }}, +}) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/basicFile.jinja b/packages/inference/src/snippets/templates/python/requests/basicFile.jinja new file mode 100644 index 0000000000..e73c2ce79a --- /dev/null +++ b/packages/inference/src/snippets/templates/python/requests/basicFile.jinja @@ -0,0 +1,7 @@ +def query(filename): + with open(filename, "rb") as f: + data = f.read() + response = requests.post(API_URL, headers=headers, data=data) + return response.json() + +output = query({{ inputs }}) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/documentQuestionAnswering.jinja b/packages/inference/src/snippets/templates/python/requests/documentQuestionAnswering.jinja new file mode 100644 index 0000000000..76072952f5 --- /dev/null +++ b/packages/inference/src/snippets/templates/python/requests/documentQuestionAnswering.jinja @@ -0,0 +1,10 @@ +def query(payload): + with open(payload["image"], "rb") as f: + img = f.read() + payload["image"] = base64.b64encode(img).decode("utf-8") + response = requests.post(API_URL, headers=headers, json=payload) + return response.json() + +output = query({ + "inputs": {{ inputs.asStr }}, +}) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/imageToImage.jinja b/packages/inference/src/snippets/templates/python/requests/imageToImage.jinja new file mode 100644 index 0000000000..4c05cc307a --- /dev/null +++ b/packages/inference/src/snippets/templates/python/requests/imageToImage.jinja @@ -0,0 +1,16 @@ +def query(payload): + with open(payload["inputs"], "rb") as f: + img = f.read() + payload["inputs"] = base64.b64encode(img).decode("utf-8") + response = requests.post(API_URL, headers=headers, json=payload) + return response.content + +image_bytes = query({ + "inputs": "{{ inputs.image }}", + "parameters": {"prompt": "{{ inputs.prompt }}"}, +}) + +# You can access the image with PIL.Image for example +import io +from PIL import Image +image = Image.open(io.BytesIO(image_bytes)) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/importRequests.jinja b/packages/inference/src/snippets/templates/python/requests/importRequests.jinja new file mode 100644 index 0000000000..f0486a32d4 --- /dev/null +++ b/packages/inference/src/snippets/templates/python/requests/importRequests.jinja @@ -0,0 +1,7 @@ +{% if importBase64 %} +import base64 +{% endif %} +import requests + +API_URL = "https://router.huggingface.co/{{ provider }}/models/{{ model.id }}" +headers = {"Authorization": "Bearer {{ accessToken }}"} \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/tabular.jinja b/packages/inference/src/snippets/templates/python/requests/tabular.jinja new file mode 100644 index 0000000000..76914d4c5e --- /dev/null +++ b/packages/inference/src/snippets/templates/python/requests/tabular.jinja @@ -0,0 +1,9 @@ +def query(payload): + response = requests.post(API_URL, headers=headers, json=payload) + return response.content + +response = query({ + "inputs": { + "data": {{ inputs }} + }, +}) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/textToAudio.jinja b/packages/inference/src/snippets/templates/python/requests/textToAudio.jinja new file mode 100644 index 0000000000..ecb2cf23ad --- /dev/null +++ b/packages/inference/src/snippets/templates/python/requests/textToAudio.jinja @@ -0,0 +1,23 @@ +{% if model.library_name == "transformers" %} +def query(payload): + response = requests.post(API_URL, headers=headers, json=payload) + return response.content + +audio_bytes = query({ + "inputs": {{ inputs }}, +}) +# You can access the audio with IPython.display for example +from IPython.display import Audio +Audio(audio_bytes) +{% else %} +def query(payload): + response = requests.post(API_URL, headers=headers, json=payload) + return response.json() + +audio, sampling_rate = query({ + "inputs": {{ inputs }}, +}) +# You can access the audio with IPython.display for example +from IPython.display import Audio +Audio(audio, rate=sampling_rate) +{% endif %} \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/textToImage.jinja b/packages/inference/src/snippets/templates/python/requests/textToImage.jinja new file mode 100644 index 0000000000..1c30ac461c --- /dev/null +++ b/packages/inference/src/snippets/templates/python/requests/textToImage.jinja @@ -0,0 +1,14 @@ +{% if provider == "hf-inference" %} +def query(payload): + response = requests.post(API_URL, headers=headers, json=payload) + return response.content + +image_bytes = query({ + "inputs": {{ inputs }}, +}) + +# You can access the image with PIL.Image for example +import io +from PIL import Image +image = Image.open(io.BytesIO(image_bytes)) +{% endif %} \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/zeroShotClassification.jinja b/packages/inference/src/snippets/templates/python/requests/zeroShotClassification.jinja new file mode 100644 index 0000000000..664ec00b5f --- /dev/null +++ b/packages/inference/src/snippets/templates/python/requests/zeroShotClassification.jinja @@ -0,0 +1,8 @@ +def query(payload): + response = requests.post(API_URL, headers=headers, json=payload) + return response.json() + +output = query({ + "inputs": {{ inputs }}, + "parameters": {"candidate_labels": ["refund", "legal", "faq"]}, +}) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/zeroShotImageClassification.jinja b/packages/inference/src/snippets/templates/python/requests/zeroShotImageClassification.jinja new file mode 100644 index 0000000000..769aac400e --- /dev/null +++ b/packages/inference/src/snippets/templates/python/requests/zeroShotImageClassification.jinja @@ -0,0 +1,14 @@ +def query(data): + with open(data["image_path"], "rb") as f: + img = f.read() + payload={ + "parameters": data["parameters"], + "inputs": base64.b64encode(img).decode("utf-8") + } + response = requests.post(API_URL, headers=headers, json=payload) + return response.json() + +output = query({ + "image_path": {{ inputs }}, + "parameters": {"candidate_labels": ["cat", "dog", "llama"]}, +}) \ No newline at end of file diff --git a/packages/inference/tsup.config.ts b/packages/inference/tsup.config.ts new file mode 100644 index 0000000000..6be4e128a6 --- /dev/null +++ b/packages/inference/tsup.config.ts @@ -0,0 +1,23 @@ +import type { Options } from "tsup"; + +const baseConfig: Options = { + entry: ["./index.ts"], + format: ["cjs", "esm"], + outDir: "dist", + clean: true, +}; + +const nodeConfig: Options = { + ...baseConfig, + platform: "node", +}; + +const browserConfig: Options = { + ...baseConfig, + platform: "browser", + target: "es2018", + splitting: true, + outDir: "dist/browser", +}; + +export default [nodeConfig, browserConfig]; diff --git a/packages/tasks-gen/scripts/generate-snippets-fixtures.ts b/packages/tasks-gen/scripts/generate-snippets-fixtures.ts index b22d7551e4..c9051a2c57 100644 --- a/packages/tasks-gen/scripts/generate-snippets-fixtures.ts +++ b/packages/tasks-gen/scripts/generate-snippets-fixtures.ts @@ -112,6 +112,28 @@ const TEST_CASES: { languages: ["py"], providers: ["hf-inference"], }, + { + testName: "tabular", + model: { + id: "templates/tabular-classification", + pipeline_tag: "tabular-classification", + tags: [], + inference: "", + }, + providers: ["hf-inference"], + languages: ["py"], + }, + { + testName: "text-to-audio-transformers", + model: { + id: "facebook/musicgen-small", + pipeline_tag: "text-to-audio", + tags: ["transformers"], + inference: "", + }, + providers: ["hf-inference"], + languages: ["py"], + }, { testName: "text-to-image", model: { @@ -145,6 +167,39 @@ const TEST_CASES: { providers: ["hf-inference"], languages: ["sh", "js", "py"], }, + { + testName: "basic-snippet--token-classification", + model: { + id: "FacebookAI/xlm-roberta-large-finetuned-conll03-english", + pipeline_tag: "token-classification", + tags: [], + inference: "", + }, + providers: ["hf-inference"], + languages: ["py"], + }, + { + testName: "zero-shot-classification", + model: { + id: "facebook/bart-large-mnli", + pipeline_tag: "zero-shot-classification", + tags: [], + inference: "", + }, + providers: ["hf-inference"], + languages: ["py"], + }, + { + testName: "zero-shot-image-classification", + model: { + id: "openai/clip-vit-large-patch14", + pipeline_tag: "zero-shot-image-classification", + tags: [], + inference: "", + }, + providers: ["hf-inference"], + languages: ["py"], + }, ] as const; const GET_SNIPPET_FN = { diff --git a/packages/tasks-gen/snippets-fixtures/automatic-speech-recognition/0.huggingface_hub.hf-inference.py b/packages/tasks-gen/snippets-fixtures/automatic-speech-recognition/0.huggingface_hub.hf-inference.py index 9a945fcf65..fa9955a05b 100644 --- a/packages/tasks-gen/snippets-fixtures/automatic-speech-recognition/0.huggingface_hub.hf-inference.py +++ b/packages/tasks-gen/snippets-fixtures/automatic-speech-recognition/0.huggingface_hub.hf-inference.py @@ -4,4 +4,5 @@ provider="hf-inference", api_key="api_token", ) + output = client.automatic_speech_recognition("sample1.flac", model="openai/whisper-large-v3-turbo") \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/automatic-speech-recognition/1.requests.hf-inference.py b/packages/tasks-gen/snippets-fixtures/automatic-speech-recognition/1.requests.hf-inference.py index 4acaa0ed5b..665b620769 100644 --- a/packages/tasks-gen/snippets-fixtures/automatic-speech-recognition/1.requests.hf-inference.py +++ b/packages/tasks-gen/snippets-fixtures/automatic-speech-recognition/1.requests.hf-inference.py @@ -4,9 +4,9 @@ headers = {"Authorization": "Bearer api_token"} def query(filename): - with open(filename, "rb") as f: - data = f.read() - response = requests.post(API_URL, headers=headers, data=data) - return response.json() + with open(filename, "rb") as f: + data = f.read() + response = requests.post(API_URL, headers=headers, data=data) + return response.json() output = query("sample1.flac") \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/basic-snippet--token-classification/0.huggingface_hub.hf-inference.py b/packages/tasks-gen/snippets-fixtures/basic-snippet--token-classification/0.huggingface_hub.hf-inference.py new file mode 100644 index 0000000000..60eb648eba --- /dev/null +++ b/packages/tasks-gen/snippets-fixtures/basic-snippet--token-classification/0.huggingface_hub.hf-inference.py @@ -0,0 +1,13 @@ +from huggingface_hub import InferenceClient + +client = InferenceClient( + provider="hf-inference", + api_key="api_token", +) + +result = client.token_classification( + inputs="My name is Sarah Jessica Parker but you can call me Jessica", + model="FacebookAI/xlm-roberta-large-finetuned-conll03-english", +) + +print(result) \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/basic-snippet--token-classification/1.requests.hf-inference.py b/packages/tasks-gen/snippets-fixtures/basic-snippet--token-classification/1.requests.hf-inference.py new file mode 100644 index 0000000000..52a5ca4217 --- /dev/null +++ b/packages/tasks-gen/snippets-fixtures/basic-snippet--token-classification/1.requests.hf-inference.py @@ -0,0 +1,12 @@ +import requests + +API_URL = "https://router.huggingface.co/hf-inference/models/FacebookAI/xlm-roberta-large-finetuned-conll03-english" +headers = {"Authorization": "Bearer api_token"} + +def query(payload): + response = requests.post(API_URL, headers=headers, json=payload) + return response.json() + +output = query({ + "inputs": "My name is Sarah Jessica Parker but you can call me Jessica", +}) \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.hf-inference.py b/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.hf-inference.py index 44e29d2a0b..4e53fde9ac 100644 --- a/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.hf-inference.py +++ b/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.hf-inference.py @@ -13,9 +13,9 @@ ] completion = client.chat.completions.create( - model="meta-llama/Llama-3.1-8B-Instruct", - messages=messages, - max_tokens=500, + model="meta-llama/Llama-3.1-8B-Instruct", + messages=messages, + max_tokens=500, ) print(completion.choices[0].message) \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.together.py b/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.together.py index ee348d6987..0e5231e4dc 100644 --- a/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.together.py +++ b/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.together.py @@ -13,9 +13,9 @@ ] completion = client.chat.completions.create( - model="meta-llama/Llama-3.1-8B-Instruct", - messages=messages, - max_tokens=500, + model="meta-llama/Llama-3.1-8B-Instruct", + messages=messages, + max_tokens=500, ) print(completion.choices[0].message) \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/1.openai.hf-inference.py b/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/1.openai.hf-inference.py index 4d557b289e..bed410d47b 100644 --- a/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/1.openai.hf-inference.py +++ b/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/1.openai.hf-inference.py @@ -1,8 +1,8 @@ from openai import OpenAI client = OpenAI( - base_url="https://router.huggingface.co/hf-inference/v1", - api_key="api_token" + base_url="https://router.huggingface.co/hf-inference/v1", + api_key="api_token" ) messages = [ @@ -13,9 +13,9 @@ ] completion = client.chat.completions.create( - model="meta-llama/Llama-3.1-8B-Instruct", - messages=messages, - max_tokens=500, + model="meta-llama/Llama-3.1-8B-Instruct", + messages=messages, + max_tokens=500, ) print(completion.choices[0].message) \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/1.openai.together.py b/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/1.openai.together.py index d4fb0f865c..ea0f8683bc 100644 --- a/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/1.openai.together.py +++ b/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/1.openai.together.py @@ -1,8 +1,8 @@ from openai import OpenAI client = OpenAI( - base_url="https://router.huggingface.co/together", - api_key="api_token" + base_url="https://router.huggingface.co/together", + api_key="api_token" ) messages = [ @@ -13,9 +13,9 @@ ] completion = client.chat.completions.create( - model="", - messages=messages, - max_tokens=500, + model="", + messages=messages, + max_tokens=500, ) print(completion.choices[0].message) \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.hf-inference.py b/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.hf-inference.py index 0f1fd0dbdc..a99e90b97c 100644 --- a/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.hf-inference.py +++ b/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.hf-inference.py @@ -13,10 +13,10 @@ ] stream = client.chat.completions.create( - model="meta-llama/Llama-3.1-8B-Instruct", - messages=messages, - max_tokens=500, - stream=True, + model="meta-llama/Llama-3.1-8B-Instruct", + messages=messages, + max_tokens=500, + stream=True, ) for chunk in stream: diff --git a/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.together.py b/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.together.py index 31d00d979c..d4bb30cda6 100644 --- a/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.together.py +++ b/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.together.py @@ -13,10 +13,10 @@ ] stream = client.chat.completions.create( - model="meta-llama/Llama-3.1-8B-Instruct", - messages=messages, - max_tokens=500, - stream=True, + model="meta-llama/Llama-3.1-8B-Instruct", + messages=messages, + max_tokens=500, + stream=True, ) for chunk in stream: diff --git a/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.py b/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.py index 12167b83bc..1476285a3b 100644 --- a/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.py +++ b/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.py @@ -1,8 +1,8 @@ from openai import OpenAI client = OpenAI( - base_url="https://router.huggingface.co/hf-inference/v1", - api_key="api_token" + base_url="https://router.huggingface.co/hf-inference/v1", + api_key="api_token" ) messages = [ @@ -13,11 +13,11 @@ ] stream = client.chat.completions.create( - model="meta-llama/Llama-3.1-8B-Instruct", - messages=messages, - max_tokens=500, - stream=True + model="meta-llama/Llama-3.1-8B-Instruct", + messages=messages, + max_tokens=500, + stream=True ) for chunk in stream: - print(chunk.choices[0].delta.content, end="") \ No newline at end of file + print(chunk.choices[0].delta.content, end="") \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/1.openai.together.py b/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/1.openai.together.py index 67d0bde845..e04dfcb7ed 100644 --- a/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/1.openai.together.py +++ b/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/1.openai.together.py @@ -1,8 +1,8 @@ from openai import OpenAI client = OpenAI( - base_url="https://router.huggingface.co/together", - api_key="api_token" + base_url="https://router.huggingface.co/together", + api_key="api_token" ) messages = [ @@ -13,11 +13,11 @@ ] stream = client.chat.completions.create( - model="", - messages=messages, - max_tokens=500, - stream=True + model="", + messages=messages, + max_tokens=500, + stream=True ) for chunk in stream: - print(chunk.choices[0].delta.content, end="") \ No newline at end of file + print(chunk.choices[0].delta.content, end="") \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.fireworks-ai.py b/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.fireworks-ai.py index 41f26d30a7..27fbac0fba 100644 --- a/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.fireworks-ai.py +++ b/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.fireworks-ai.py @@ -24,9 +24,9 @@ ] completion = client.chat.completions.create( - model="meta-llama/Llama-3.2-11B-Vision-Instruct", - messages=messages, - max_tokens=500, + model="meta-llama/Llama-3.2-11B-Vision-Instruct", + messages=messages, + max_tokens=500, ) print(completion.choices[0].message) \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.hf-inference.py b/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.hf-inference.py index 507809756d..02201bfa11 100644 --- a/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.hf-inference.py +++ b/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.hf-inference.py @@ -24,9 +24,9 @@ ] completion = client.chat.completions.create( - model="meta-llama/Llama-3.2-11B-Vision-Instruct", - messages=messages, - max_tokens=500, + model="meta-llama/Llama-3.2-11B-Vision-Instruct", + messages=messages, + max_tokens=500, ) print(completion.choices[0].message) \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/1.openai.fireworks-ai.py b/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/1.openai.fireworks-ai.py index 19a126c63d..25490a6a9c 100644 --- a/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/1.openai.fireworks-ai.py +++ b/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/1.openai.fireworks-ai.py @@ -1,8 +1,8 @@ from openai import OpenAI client = OpenAI( - base_url="https://router.huggingface.co/fireworks-ai", - api_key="api_token" + base_url="https://router.huggingface.co/fireworks-ai", + api_key="api_token" ) messages = [ @@ -24,9 +24,9 @@ ] completion = client.chat.completions.create( - model="", - messages=messages, - max_tokens=500, + model="", + messages=messages, + max_tokens=500, ) print(completion.choices[0].message) \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/1.openai.hf-inference.py b/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/1.openai.hf-inference.py index 517466190f..dab0422b2d 100644 --- a/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/1.openai.hf-inference.py +++ b/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/1.openai.hf-inference.py @@ -1,8 +1,8 @@ from openai import OpenAI client = OpenAI( - base_url="https://router.huggingface.co/hf-inference/v1", - api_key="api_token" + base_url="https://router.huggingface.co/hf-inference/v1", + api_key="api_token" ) messages = [ @@ -24,9 +24,9 @@ ] completion = client.chat.completions.create( - model="meta-llama/Llama-3.2-11B-Vision-Instruct", - messages=messages, - max_tokens=500, + model="meta-llama/Llama-3.2-11B-Vision-Instruct", + messages=messages, + max_tokens=500, ) print(completion.choices[0].message) \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.fireworks-ai.py b/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.fireworks-ai.py index ad86364e4d..6395669c4c 100644 --- a/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.fireworks-ai.py +++ b/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.fireworks-ai.py @@ -24,10 +24,10 @@ ] stream = client.chat.completions.create( - model="meta-llama/Llama-3.2-11B-Vision-Instruct", - messages=messages, - max_tokens=500, - stream=True, + model="meta-llama/Llama-3.2-11B-Vision-Instruct", + messages=messages, + max_tokens=500, + stream=True, ) for chunk in stream: diff --git a/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.hf-inference.py b/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.hf-inference.py index 7d58f8020a..dc31c62ae2 100644 --- a/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.hf-inference.py +++ b/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.hf-inference.py @@ -24,10 +24,10 @@ ] stream = client.chat.completions.create( - model="meta-llama/Llama-3.2-11B-Vision-Instruct", - messages=messages, - max_tokens=500, - stream=True, + model="meta-llama/Llama-3.2-11B-Vision-Instruct", + messages=messages, + max_tokens=500, + stream=True, ) for chunk in stream: diff --git a/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.py b/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.py index 33acb547ad..81bede83e9 100644 --- a/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.py +++ b/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.py @@ -1,8 +1,8 @@ from openai import OpenAI client = OpenAI( - base_url="https://router.huggingface.co/fireworks-ai", - api_key="api_token" + base_url="https://router.huggingface.co/fireworks-ai", + api_key="api_token" ) messages = [ @@ -24,11 +24,11 @@ ] stream = client.chat.completions.create( - model="", - messages=messages, - max_tokens=500, - stream=True + model="", + messages=messages, + max_tokens=500, + stream=True ) for chunk in stream: - print(chunk.choices[0].delta.content, end="") \ No newline at end of file + print(chunk.choices[0].delta.content, end="") \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.py b/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.py index 6c788816ee..8ceb00bfe3 100644 --- a/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.py +++ b/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.py @@ -1,8 +1,8 @@ from openai import OpenAI client = OpenAI( - base_url="https://router.huggingface.co/hf-inference/v1", - api_key="api_token" + base_url="https://router.huggingface.co/hf-inference/v1", + api_key="api_token" ) messages = [ @@ -24,11 +24,11 @@ ] stream = client.chat.completions.create( - model="meta-llama/Llama-3.2-11B-Vision-Instruct", - messages=messages, - max_tokens=500, - stream=True + model="meta-llama/Llama-3.2-11B-Vision-Instruct", + messages=messages, + max_tokens=500, + stream=True ) for chunk in stream: - print(chunk.choices[0].delta.content, end="") \ No newline at end of file + print(chunk.choices[0].delta.content, end="") \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/document-question-answering/0.huggingface_hub.hf-inference.py b/packages/tasks-gen/snippets-fixtures/document-question-answering/0.huggingface_hub.hf-inference.py index 46e437c63a..738921cc13 100644 --- a/packages/tasks-gen/snippets-fixtures/document-question-answering/0.huggingface_hub.hf-inference.py +++ b/packages/tasks-gen/snippets-fixtures/document-question-answering/0.huggingface_hub.hf-inference.py @@ -4,8 +4,9 @@ provider="hf-inference", api_key="api_token", ) + output = client.document_question_answering( "cat.png", - question="What is in this image?", - model="impira/layoutlm-invoices", + question="What is in this image?", + model="impira/layoutlm-invoices", ) \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/document-question-answering/1.requests.hf-inference.py b/packages/tasks-gen/snippets-fixtures/document-question-answering/1.requests.hf-inference.py index 51fed678d8..d8ab49b354 100644 --- a/packages/tasks-gen/snippets-fixtures/document-question-answering/1.requests.hf-inference.py +++ b/packages/tasks-gen/snippets-fixtures/document-question-answering/1.requests.hf-inference.py @@ -5,11 +5,11 @@ headers = {"Authorization": "Bearer api_token"} def query(payload): - with open(payload["image"], "rb") as f: - img = f.read() - payload["image"] = base64.b64encode(img).decode("utf-8") - response = requests.post(API_URL, headers=headers, json=payload) - return response.json() + with open(payload["image"], "rb") as f: + img = f.read() + payload["image"] = base64.b64encode(img).decode("utf-8") + response = requests.post(API_URL, headers=headers, json=payload) + return response.json() output = query({ "inputs": { diff --git a/packages/tasks-gen/snippets-fixtures/image-to-image/0.huggingface_hub.hf-inference.py b/packages/tasks-gen/snippets-fixtures/image-to-image/0.huggingface_hub.hf-inference.py index c21949966a..dc89746110 100644 --- a/packages/tasks-gen/snippets-fixtures/image-to-image/0.huggingface_hub.hf-inference.py +++ b/packages/tasks-gen/snippets-fixtures/image-to-image/0.huggingface_hub.hf-inference.py @@ -4,6 +4,7 @@ provider="hf-inference", api_key="api_token", ) + # output is a PIL.Image object image = client.image_to_image( "cat.png", diff --git a/packages/tasks-gen/snippets-fixtures/image-to-image/1.requests.hf-inference.py b/packages/tasks-gen/snippets-fixtures/image-to-image/1.requests.hf-inference.py index 2d16ba05f5..d411492847 100644 --- a/packages/tasks-gen/snippets-fixtures/image-to-image/1.requests.hf-inference.py +++ b/packages/tasks-gen/snippets-fixtures/image-to-image/1.requests.hf-inference.py @@ -5,15 +5,15 @@ headers = {"Authorization": "Bearer api_token"} def query(payload): - with open(payload["inputs"], "rb") as f: - img = f.read() - payload["inputs"] = base64.b64encode(img).decode("utf-8") - response = requests.post(API_URL, headers=headers, json=payload) - return response.content + with open(payload["inputs"], "rb") as f: + img = f.read() + payload["inputs"] = base64.b64encode(img).decode("utf-8") + response = requests.post(API_URL, headers=headers, json=payload) + return response.content image_bytes = query({ - "inputs": "cat.png", - "parameters": {"prompt": "Turn the cat into a tiger."}, + "inputs": "cat.png", + "parameters": {"prompt": "Turn the cat into a tiger."}, }) # You can access the image with PIL.Image for example diff --git a/packages/tasks-gen/snippets-fixtures/tabular/0.requests.hf-inference.py b/packages/tasks-gen/snippets-fixtures/tabular/0.requests.hf-inference.py new file mode 100644 index 0000000000..e2714639b4 --- /dev/null +++ b/packages/tasks-gen/snippets-fixtures/tabular/0.requests.hf-inference.py @@ -0,0 +1,14 @@ +import requests + +API_URL = "https://router.huggingface.co/hf-inference/models/templates/tabular-classification" +headers = {"Authorization": "Bearer api_token"} + +def query(payload): + response = requests.post(API_URL, headers=headers, json=payload) + return response.content + +response = query({ + "inputs": { + "data": '{"Height":[11.52,12.48],"Length1":[23.2,24.0],"Length2":[25.4,26.3],"Species": ["Bream","Bream"]}' + }, +}) \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/text-classification/0.huggingface_hub.hf-inference.py b/packages/tasks-gen/snippets-fixtures/text-classification/0.huggingface_hub.hf-inference.py index d6b9b9aa45..3150e44217 100644 --- a/packages/tasks-gen/snippets-fixtures/text-classification/0.huggingface_hub.hf-inference.py +++ b/packages/tasks-gen/snippets-fixtures/text-classification/0.huggingface_hub.hf-inference.py @@ -6,8 +6,8 @@ ) result = client.text_classification( - inputs="I like you. I love you", - model="distilbert/distilbert-base-uncased-finetuned-sst-2-english", + inputs="I like you. I love you", + model="distilbert/distilbert-base-uncased-finetuned-sst-2-english", ) -print(result) +print(result) \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/text-classification/1.requests.hf-inference.py b/packages/tasks-gen/snippets-fixtures/text-classification/1.requests.hf-inference.py index 281fca0df2..cb900c6b72 100644 --- a/packages/tasks-gen/snippets-fixtures/text-classification/1.requests.hf-inference.py +++ b/packages/tasks-gen/snippets-fixtures/text-classification/1.requests.hf-inference.py @@ -4,9 +4,9 @@ headers = {"Authorization": "Bearer api_token"} def query(payload): - response = requests.post(API_URL, headers=headers, json=payload) - return response.json() - + response = requests.post(API_URL, headers=headers, json=payload) + return response.json() + output = query({ - "inputs": "I like you. I love you", + "inputs": "I like you. I love you", }) \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/text-to-audio-transformers/0.requests.hf-inference.py b/packages/tasks-gen/snippets-fixtures/text-to-audio-transformers/0.requests.hf-inference.py new file mode 100644 index 0000000000..ad492aee35 --- /dev/null +++ b/packages/tasks-gen/snippets-fixtures/text-to-audio-transformers/0.requests.hf-inference.py @@ -0,0 +1,15 @@ +import requests + +API_URL = "https://router.huggingface.co/hf-inference/models/facebook/musicgen-small" +headers = {"Authorization": "Bearer api_token"} + +def query(payload): + response = requests.post(API_URL, headers=headers, json=payload) + return response.json() + +audio, sampling_rate = query({ + "inputs": "liquid drum and bass, atmospheric synths, airy sounds", +}) +# You can access the audio with IPython.display for example +from IPython.display import Audio +Audio(audio, rate=sampling_rate) \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/text-to-image/0.huggingface_hub.fal-ai.py b/packages/tasks-gen/snippets-fixtures/text-to-image/0.huggingface_hub.fal-ai.py index 8f8a6b1021..f70ff34f3a 100644 --- a/packages/tasks-gen/snippets-fixtures/text-to-image/0.huggingface_hub.fal-ai.py +++ b/packages/tasks-gen/snippets-fixtures/text-to-image/0.huggingface_hub.fal-ai.py @@ -7,6 +7,6 @@ # output is a PIL.Image object image = client.text_to_image( - "Astronaut riding a horse", - model="black-forest-labs/FLUX.1-schnell", + "Astronaut riding a horse", + model="black-forest-labs/FLUX.1-schnell", ) \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/text-to-image/0.huggingface_hub.hf-inference.py b/packages/tasks-gen/snippets-fixtures/text-to-image/0.huggingface_hub.hf-inference.py index b4c48cec43..84ce49c29c 100644 --- a/packages/tasks-gen/snippets-fixtures/text-to-image/0.huggingface_hub.hf-inference.py +++ b/packages/tasks-gen/snippets-fixtures/text-to-image/0.huggingface_hub.hf-inference.py @@ -7,6 +7,6 @@ # output is a PIL.Image object image = client.text_to_image( - "Astronaut riding a horse", - model="black-forest-labs/FLUX.1-schnell", + "Astronaut riding a horse", + model="black-forest-labs/FLUX.1-schnell", ) \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/text-to-image/1.fal-client.fal-ai.py b/packages/tasks-gen/snippets-fixtures/text-to-image/1.fal-client.fal-ai.py deleted file mode 100644 index 36220f1057..0000000000 --- a/packages/tasks-gen/snippets-fixtures/text-to-image/1.fal-client.fal-ai.py +++ /dev/null @@ -1,9 +0,0 @@ -import fal_client - -result = fal_client.subscribe( - "", - arguments={ - "prompt": "Astronaut riding a horse", - }, -) -print(result) diff --git a/packages/tasks-gen/snippets-fixtures/text-to-image/1.fal_client.fal-ai.py b/packages/tasks-gen/snippets-fixtures/text-to-image/1.fal_client.fal-ai.py new file mode 100644 index 0000000000..461dbb2271 --- /dev/null +++ b/packages/tasks-gen/snippets-fixtures/text-to-image/1.fal_client.fal-ai.py @@ -0,0 +1,9 @@ +import fal_client + +result = fal_client.subscribe( + "", + arguments={ + "prompt": "Astronaut riding a horse", + }, +) +print(result) \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/text-to-image/1.requests.hf-inference.py b/packages/tasks-gen/snippets-fixtures/text-to-image/1.requests.hf-inference.py index a8e1ad9de0..568c788dc7 100644 --- a/packages/tasks-gen/snippets-fixtures/text-to-image/1.requests.hf-inference.py +++ b/packages/tasks-gen/snippets-fixtures/text-to-image/1.requests.hf-inference.py @@ -4,11 +4,11 @@ headers = {"Authorization": "Bearer api_token"} def query(payload): - response = requests.post(API_URL, headers=headers, json=payload) - return response.content + response = requests.post(API_URL, headers=headers, json=payload) + return response.content image_bytes = query({ - "inputs": "Astronaut riding a horse", + "inputs": "Astronaut riding a horse", }) # You can access the image with PIL.Image for example diff --git a/packages/tasks-gen/snippets-fixtures/text-to-video/0.huggingface_hub.fal-ai.py b/packages/tasks-gen/snippets-fixtures/text-to-video/0.huggingface_hub.fal-ai.py index fc96fd381b..240c59184d 100644 --- a/packages/tasks-gen/snippets-fixtures/text-to-video/0.huggingface_hub.fal-ai.py +++ b/packages/tasks-gen/snippets-fixtures/text-to-video/0.huggingface_hub.fal-ai.py @@ -6,6 +6,6 @@ ) video = client.text_to_video( - "A young man walking on the street", - model="tencent/HunyuanVideo", + "A young man walking on the street", + model="tencent/HunyuanVideo", ) \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/text-to-video/0.huggingface_hub.replicate.py b/packages/tasks-gen/snippets-fixtures/text-to-video/0.huggingface_hub.replicate.py index 208c583119..6299fe51d5 100644 --- a/packages/tasks-gen/snippets-fixtures/text-to-video/0.huggingface_hub.replicate.py +++ b/packages/tasks-gen/snippets-fixtures/text-to-video/0.huggingface_hub.replicate.py @@ -6,6 +6,6 @@ ) video = client.text_to_video( - "A young man walking on the street", - model="tencent/HunyuanVideo", + "A young man walking on the street", + model="tencent/HunyuanVideo", ) \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/zero-shot-classification/0.requests.hf-inference.py b/packages/tasks-gen/snippets-fixtures/zero-shot-classification/0.requests.hf-inference.py new file mode 100644 index 0000000000..3054fe8de8 --- /dev/null +++ b/packages/tasks-gen/snippets-fixtures/zero-shot-classification/0.requests.hf-inference.py @@ -0,0 +1,13 @@ +import requests + +API_URL = "https://router.huggingface.co/hf-inference/models/facebook/bart-large-mnli" +headers = {"Authorization": "Bearer api_token"} + +def query(payload): + response = requests.post(API_URL, headers=headers, json=payload) + return response.json() + +output = query({ + "inputs": "Hi, I recently bought a device from your company but it is not working as advertised and I would like to get reimbursed!", + "parameters": {"candidate_labels": ["refund", "legal", "faq"]}, +}) \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/zero-shot-image-classification/0.requests.hf-inference.py b/packages/tasks-gen/snippets-fixtures/zero-shot-image-classification/0.requests.hf-inference.py new file mode 100644 index 0000000000..a6cfd80da1 --- /dev/null +++ b/packages/tasks-gen/snippets-fixtures/zero-shot-image-classification/0.requests.hf-inference.py @@ -0,0 +1,20 @@ +import base64 +import requests + +API_URL = "https://router.huggingface.co/hf-inference/models/openai/clip-vit-large-patch14" +headers = {"Authorization": "Bearer api_token"} + +def query(data): + with open(data["image_path"], "rb") as f: + img = f.read() + payload={ + "parameters": data["parameters"], + "inputs": base64.b64encode(img).decode("utf-8") + } + response = requests.post(API_URL, headers=headers, json=payload) + return response.json() + +output = query({ + "image_path": "cats.jpg", + "parameters": {"candidate_labels": ["cat", "dog", "llama"]}, +}) \ No newline at end of file