Skip to content

Commit e824d26

Browse files
authored
[Inference] Overrides snippets inputs (#1613)
So we can call ```js snippets.getInferenceSnippets(model, "auto", undefined, { inputs: `"A dog with a baseball cap"` }); ``` to get the following sample ```python # output is a PIL.Image object image = client.text_to_image( "A dog with a baseball cap", model="black-forest-labs/FLUX.1-dev", ) ``` instead of the default input in the snippet: ```python image = client.text_to_image( "Astronaut riding a horse", model="black-forest-labs/FLUX.1-dev", ) ``` it's already possible to do it for `text-generation` with `opts.mesages` needed for (internal huggingface-internal/moon-landing#14421)
1 parent a014b62 commit e824d26

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

packages/inference/src/snippets/getInferenceSnippets.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export type InferenceSnippetOptions = {
2020
accessToken?: string;
2121
directRequest?: boolean; // to bypass HF routing and call the provider directly
2222
endpointUrl?: string; // to call a local endpoint directly
23+
inputs?: Record<string, unknown>; // overrides the default snippet's inputs
2324
} & Record<string, unknown>;
2425

2526
const PYTHON_CLIENTS = ["huggingface_hub", "fal_client", "requests", "openai"] as const;
@@ -167,7 +168,11 @@ const snippetGenerator = (templateName: string, inputPreparationFn?: InputPrepar
167168
const accessTokenOrPlaceholder = opts?.accessToken ?? placeholder;
168169

169170
/// Prepare inputs + make request
170-
const inputs = inputPreparationFn ? inputPreparationFn(model, opts) : { inputs: getModelInputSnippet(model) };
171+
const inputs = opts?.inputs
172+
? { inputs: opts.inputs }
173+
: inputPreparationFn
174+
? inputPreparationFn(model, opts)
175+
: { inputs: getModelInputSnippet(model) };
171176
const request = makeRequestOptionsFromResolvedModel(
172177
providerModelId,
173178
providerHelper,

0 commit comments

Comments
 (0)