-
Notifications
You must be signed in to change notification settings - Fork 490
[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
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
081dac0
snippets for image-to-video
SBrandeis be2fd4e
fal-client
SBrandeis 402b24b
remove log
SBrandeis 966aef8
format + lint
SBrandeis 5d77a95
fix fetch snippet
SBrandeis f13a010
format+lint
SBrandeis c94dcaa
fix query inputs and remove unused code
hanouticelina ef35975
Merge branch 'main' into inference-i2v-snippets
hanouticelina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/inference/src/snippets/templates/js/fetch/imageToVideo.jinja
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); |
20 changes: 20 additions & 0 deletions
20
packages/inference/src/snippets/templates/js/huggingface.js/imageToVideo.jinja
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
23 changes: 23 additions & 0 deletions
23
packages/inference/src/snippets/templates/python/fal_client/imageToVideo.jinja
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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%} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
packages/inference/src/snippets/templates/python/huggingface_hub/imageToVideo.jinja
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }}", | ||
) |
2 changes: 0 additions & 2 deletions
2
packages/inference/src/snippets/templates/python/requests/imageToImage.jinja
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/inference/src/snippets/templates/python/requests/imageToVideo.jinja
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
packages/tasks-gen/snippets-fixtures/image-to-video/js/fetch/0.fal-ai.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); |
15 changes: 15 additions & 0 deletions
15
packages/tasks-gen/snippets-fixtures/image-to-video/js/huggingface.js/0.fal-ai.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
21 changes: 21 additions & 0 deletions
21
packages/tasks-gen/snippets-fixtures/image-to-video/python/fal_client/0.fal-ai.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
16 changes: 16 additions & 0 deletions
16
packages/tasks-gen/snippets-fixtures/image-to-video/python/huggingface_hub/0.fal-ai.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
) |
22 changes: 22 additions & 0 deletions
22
packages/tasks-gen/snippets-fixtures/image-to-video/python/requests/0.fal-ai.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.