Skip to content

Commit 0a1ab31

Browse files
committed
text-to-image
1 parent bea1f97 commit 0a1ab31

File tree

13 files changed

+115
-15
lines changed

13 files changed

+115
-15
lines changed

packages/inference/src/snippets/python.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,13 @@ const snippetGenerator = (templateName: string, inputPreparationFn?: InputPrepar
143143
params["methodName"] = HFH_INFERENCE_CLIENT_METHODS[model.pipeline_tag];
144144
}
145145

146-
/// Handle import section separately
146+
/// Generate snippet
147147
let snippet = template(params).trim();
148+
if (!snippet) {
149+
return;
150+
}
151+
152+
/// Add import section separately
148153
if (tool === "huggingface_hub") {
149154
const importSection = snippetImportInferenceClient({ ...params });
150155
snippet = `${importSection}\n\n${snippet}`;

packages/inference/src/snippets/templates/python/fal_client/textToImage.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import fal_client
44
result = fal_client.subscribe(
55
"{{ providerModelId }}",
66
arguments={
7-
"prompt": {{ inputs }},
7+
"prompt": {{ inputs.asObj.inputs }},
88
},
99
)
1010
print(result)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# output is a PIL.Image object
22
image = client.text_to_image(
3-
{{ inputs }},
3+
{{ inputs.asObj.inputs }},
44
model="{{ model.id }}",
55
)

packages/inference/src/snippets/templates/python/requests/textToImage.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ def query(payload):
44
return response.content
55

66
image_bytes = query({
7-
"inputs": {{ inputs }},
7+
"inputs": {{ inputs.asObj.inputs }},
88
})
99

1010
# You can access the image with PIL.Image for example

packages/tasks-gen/scripts/generate-snippets-fixtures.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -134,17 +134,17 @@ const TEST_CASES: {
134134
providers: ["hf-inference"],
135135
languages: ["py"],
136136
},
137-
// {
138-
// testName: "text-to-image",
139-
// model: {
140-
// id: "black-forest-labs/FLUX.1-schnell",
141-
// pipeline_tag: "text-to-image",
142-
// tags: [],
143-
// inference: "",
144-
// },
145-
// providers: ["hf-inference", "fal-ai"],
146-
// languages: ["sh", "js", "py"],
147-
// },
137+
{
138+
testName: "text-to-image",
139+
model: {
140+
id: "black-forest-labs/FLUX.1-schnell",
141+
pipeline_tag: "text-to-image",
142+
tags: [],
143+
inference: "",
144+
},
145+
providers: ["hf-inference", "fal-ai"],
146+
languages: ["sh", "js", "py"],
147+
},
148148
// {
149149
// testName: "text-to-video",
150150
// model: {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
curl https://router.huggingface.co/hf-inference/models/black-forest-labs/FLUX.1-schnell \
2+
-X POST \
3+
-d '{"inputs": "Astronaut riding a horse"}' \
4+
-H 'Content-Type: application/json' \
5+
-H 'Authorization: Bearer api_token'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { InferenceClient } from "@huggingface/inference";
2+
3+
const client = new InferenceClient("api_token");
4+
5+
const image = await client.textToImage({
6+
model: "black-forest-labs/FLUX.1-schnell",
7+
inputs: "Astronaut riding a horse",
8+
parameters: { num_inference_steps: 5 },
9+
provider: "fal-ai",
10+
});
11+
/// Use the generated image (it's a Blob)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { InferenceClient } from "@huggingface/inference";
2+
3+
const client = new InferenceClient("api_token");
4+
5+
const image = await client.textToImage({
6+
model: "black-forest-labs/FLUX.1-schnell",
7+
inputs: "Astronaut riding a horse",
8+
parameters: { num_inference_steps: 5 },
9+
provider: "hf-inference",
10+
});
11+
/// Use the generated image (it's a Blob)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from huggingface_hub import InferenceClient
2+
3+
client = InferenceClient(
4+
provider="fal-ai",
5+
api_key="api_token",
6+
)
7+
8+
# output is a PIL.Image object
9+
image = client.text_to_image(
10+
"Astronaut riding a horse",
11+
model="black-forest-labs/FLUX.1-schnell",
12+
)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from huggingface_hub import InferenceClient
2+
3+
client = InferenceClient(
4+
provider="hf-inference",
5+
api_key="api_token",
6+
)
7+
8+
# output is a PIL.Image object
9+
image = client.text_to_image(
10+
"Astronaut riding a horse",
11+
model="black-forest-labs/FLUX.1-schnell",
12+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import fal_client
2+
3+
result = fal_client.subscribe(
4+
"<fal-ai alias for black-forest-labs/FLUX.1-schnell>",
5+
arguments={
6+
"prompt": "Astronaut riding a horse",
7+
},
8+
)
9+
print(result)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
async function query(data) {
2+
const response = await fetch(
3+
"https://router.huggingface.co/hf-inference/models/black-forest-labs/FLUX.1-schnell",
4+
{
5+
headers: {
6+
Authorization: "Bearer api_token",
7+
"Content-Type": "application/json",
8+
},
9+
method: "POST",
10+
body: JSON.stringify(data),
11+
}
12+
);
13+
const result = await response.blob();
14+
return result;
15+
}
16+
query({"inputs": "Astronaut riding a horse"}).then((response) => {
17+
// Use image
18+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import requests
2+
3+
API_URL = "https://router.huggingface.co/hf-inference/models/black-forest-labs/FLUX.1-schnell"
4+
headers = {"Authorization": "Bearer api_token"}
5+
6+
def query(payload):
7+
response = requests.post(API_URL, headers=headers, json=payload)
8+
return response.content
9+
10+
image_bytes = query({
11+
"inputs": "Astronaut riding a horse",
12+
})
13+
14+
# You can access the image with PIL.Image for example
15+
import io
16+
from PIL import Image
17+
image = Image.open(io.BytesIO(image_bytes))

0 commit comments

Comments
 (0)