Skip to content

[inference] add snippets for image-to-video #1678

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 8 commits into from
Aug 12, 2025
Merged
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions packages/inference/src/snippets/getInferenceSnippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ const snippets: Partial<
"image-text-to-text": snippetGenerator("conversational"),
"image-to-image": snippetGenerator("imageToImage", prepareImageToImageInput),
"image-to-text": snippetGenerator("basicImage"),
"image-to-video": snippetGenerator("imageToVideo", prepareImageToImageInput),
"object-detection": snippetGenerator("basicImage"),
"question-answering": snippetGenerator("questionAnswering", prepareQuestionAnsweringInput),
"sentence-similarity": snippetGenerator("basic"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const image = fs.readFileSync("{{inputs.asObj.inputs}}");

async function query(data) {
const response = await fetch(
"{{ fullUrl }}",
{
headers: {
Authorization: "{{ authorizationHeader }}",
"Content-Type": "image/jpeg",
{% if billTo %}
"X-HF-Bill-To": "{{ billTo }}",
{% endif %} },
method: "POST",
body: {
"image_url": `data:image/png;base64,${data.image.encode("base64")}`,
"prompt": data.prompt,
}
}
);
const result = await response.json();
return result;
}

query({
"image": image,
"prompt": "{{inputs.asObj.parameters.prompt}}",
}).then((response) => {
// Use video
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { InferenceClient } from "@huggingface/inference";

const client = new InferenceClient("{{ accessToken }}");

const data = fs.readFileSync("{{inputs.asObj.inputs}}");

const video = await client.imageToVideo({
{% if endpointUrl %}
endpointUrl: "{{ endpointUrl }}",
{% endif %}
provider: "{{provider}}",
model: "{{model.id}}",
inputs: data,
parameters: { prompt: "{{inputs.asObj.parameters.prompt}}", },
}{% if billTo %}, {
billTo: "{{ billTo }}",
}{% endif %});

/// Use the generated video (it's a Blob)
// For example, you can save it to a file or display it in a video element
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{%if provider == "fal-ai" %}
import fal_client
import base64

def on_queue_update(update):
if isinstance(update, fal_client.InProgress):
for log in update.logs:
print(log["message"])

with open("{{inputs.asObj.inputs}}", "rb") as image_file:
image_base_64 = base64.b64encode(image_file.read()).decode('utf-8')

result = fal_client.subscribe(
"{{model.id}}",
arguments={
"image_url": f"data:image/png;base64,{image_base_64}",
"prompt": "{{inputs.asObj.parameters.prompt}}",
},
with_logs=True,
on_queue_update=on_queue_update,
)
print(result)
{%endif%}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ image = client.image_to_image(
input_image,
prompt="{{ inputs.asObj.parameters.prompt }}",
model="{{ model.id }}",
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
with open("{{ inputs.asObj.inputs }}", "rb") as image_file:
input_image = image_file.read()

video = client.image_to_video(
input_image,
prompt="{{ inputs.asObj.parameters.prompt }}",
model="{{ model.id }}",
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
with open("{{inputs.asObj.inputs}}", "rb") as image_file:
image_base_64 = base64.b64encode(image_file.read()).decode('utf-8')

def query(payload):
with open(payload["inputs"], "rb") as f:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

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

video_bytes = query({
{{ inputs.asJsonString }}
})
11 changes: 11 additions & 0 deletions packages/tasks-gen/scripts/generate-snippets-fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,17 @@ const TEST_CASES: {
},
providers: ["fal-ai", "replicate", "hf-inference"],
},
{
testName: "image-to-video",
task: "image-to-video",
model: {
id: "Wan-AI/Wan2.2-I2V-A14B",
pipeline_tag: "image-to-video",
tags: [],
inference: "",
},
providers: ["fal-ai"],
},
{
testName: "tabular",
task: "tabular-classification",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
"Authorization": f"Bearer {os.environ['HF_TOKEN']}",
}

with open("cat.png", "rb") as image_file:
image_base_64 = base64.b64encode(image_file.read()).decode('utf-8')

def query(payload):
with open(payload["inputs"], "rb") as f:
img = f.read()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
"Authorization": f"Bearer {os.environ['HF_TOKEN']}",
}

with open("cat.png", "rb") as image_file:
image_base_64 = base64.b64encode(image_file.read()).decode('utf-8')

def query(payload):
with open(payload["inputs"], "rb") as f:
img = f.read()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
"Authorization": f"Bearer {os.environ['HF_TOKEN']}",
}

with open("cat.png", "rb") as image_file:
image_base_64 = base64.b64encode(image_file.read()).decode('utf-8')

def query(payload):
with open(payload["inputs"], "rb") as f:
img = f.read()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const image = fs.readFileSync("cat.png");

async function query(data) {
const response = await fetch(
"https://router.huggingface.co/fal-ai/<fal-ai alias for Wan-AI/Wan2.2-I2V-A14B>?_subdomain=queue",
{
headers: {
Authorization: `Bearer ${process.env.HF_TOKEN}`,
"Content-Type": "image/jpeg",
},
method: "POST",
body: {
"image_url": `data:image/png;base64,${data.image.encode("base64")}`,
"prompt": data.prompt,
}
}
);
const result = await response.json();
return result;
}

query({
"image": image,
"prompt": "The cat starts to dance",
}).then((response) => {
// Use video
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { InferenceClient } from "@huggingface/inference";

const client = new InferenceClient(process.env.HF_TOKEN);

const data = fs.readFileSync("cat.png");

const video = await client.imageToVideo({
provider: "fal-ai",
model: "Wan-AI/Wan2.2-I2V-A14B",
inputs: data,
parameters: { prompt: "The cat starts to dance", },
});

/// Use the generated video (it's a Blob)
// For example, you can save it to a file or display it in a video element
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import fal_client
import base64

def on_queue_update(update):
if isinstance(update, fal_client.InProgress):
for log in update.logs:
print(log["message"])

with open("cat.png", "rb") as image_file:
image_base_64 = base64.b64encode(image_file.read()).decode('utf-8')

result = fal_client.subscribe(
"Wan-AI/Wan2.2-I2V-A14B",
arguments={
"image_url": f"data:image/png;base64,{image_base_64}",
"prompt": "The cat starts to dance",
},
with_logs=True,
on_queue_update=on_queue_update,
)
print(result)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import os
from huggingface_hub import InferenceClient

client = InferenceClient(
provider="fal-ai",
api_key=os.environ["HF_TOKEN"],
)

with open("cat.png", "rb") as image_file:
input_image = image_file.read()

video = client.image_to_video(
input_image,
prompt="The cat starts to dance",
model="Wan-AI/Wan2.2-I2V-A14B",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import os
import base64
import requests

API_URL = "https://router.huggingface.co/fal-ai/<fal-ai alias for Wan-AI/Wan2.2-I2V-A14B>?_subdomain=queue"
headers = {
"Authorization": f"Bearer {os.environ['HF_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

video_bytes = query({
"inputs": "cat.png",
"parameters": {
"prompt": "The cat starts to dance"
}
})