From ea685417b2ce07fc9cd35982e960f4b21a63c441 Mon Sep 17 00:00:00 2001 From: Wauplin Date: Wed, 5 Mar 2025 17:29:01 +0100 Subject: [PATCH 01/26] tmp commit --- .../0.huggingface_hub.hf-inference.py | 7 ++++ .../1.requests.hf-inference.py | 12 ++++++ .../0.curl.hf-inference.sh | 14 +++++++ .../0.curl.together.sh | 14 +++++++ .../0.huggingface.js.hf-inference.js | 17 ++++++++ .../0.huggingface.js.together.js | 17 ++++++++ .../0.huggingface_hub.hf-inference.py | 21 ++++++++++ .../0.huggingface_hub.together.py | 21 ++++++++++ .../1.openai.hf-inference.js | 19 +++++++++ .../1.openai.hf-inference.py | 21 ++++++++++ .../1.openai.together.js | 19 +++++++++ .../1.openai.together.py | 21 ++++++++++ .../0.curl.hf-inference.sh | 14 +++++++ .../0.curl.together.sh | 14 +++++++ .../0.huggingface.js.hf-inference.js | 25 ++++++++++++ .../0.huggingface.js.together.js | 25 ++++++++++++ .../0.huggingface_hub.hf-inference.py | 23 +++++++++++ .../0.huggingface_hub.together.py | 23 +++++++++++ .../1.openai.hf-inference.js | 28 +++++++++++++ .../1.openai.hf-inference.py | 23 +++++++++++ .../1.openai.together.js | 28 +++++++++++++ .../1.openai.together.py | 23 +++++++++++ .../0.curl.fireworks-ai.sh | 25 ++++++++++++ .../0.curl.hf-inference.sh | 25 ++++++++++++ .../0.huggingface.js.fireworks-ai.js | 28 +++++++++++++ .../0.huggingface.js.hf-inference.js | 28 +++++++++++++ .../0.huggingface_hub.fireworks-ai.py | 32 +++++++++++++++ .../0.huggingface_hub.hf-inference.py | 32 +++++++++++++++ .../1.openai.fireworks-ai.js | 30 ++++++++++++++ .../1.openai.fireworks-ai.py | 32 +++++++++++++++ .../1.openai.hf-inference.js | 30 ++++++++++++++ .../1.openai.hf-inference.py | 32 +++++++++++++++ .../0.curl.fireworks-ai.sh | 25 ++++++++++++ .../0.curl.hf-inference.sh | 25 ++++++++++++ .../0.huggingface.js.fireworks-ai.js | 36 +++++++++++++++++ .../0.huggingface.js.hf-inference.js | 36 +++++++++++++++++ .../0.huggingface_hub.fireworks-ai.py | 34 ++++++++++++++++ .../0.huggingface_hub.hf-inference.py | 34 ++++++++++++++++ .../1.openai.fireworks-ai.js | 39 +++++++++++++++++++ .../1.openai.fireworks-ai.py | 34 ++++++++++++++++ .../1.openai.hf-inference.js | 39 +++++++++++++++++++ .../1.openai.hf-inference.py | 34 ++++++++++++++++ .../0.huggingface_hub.hf-inference.py | 11 ++++++ .../1.requests.hf-inference.py | 19 +++++++++ .../0.huggingface_hub.hf-inference.py | 12 ++++++ .../image-to-image/1.requests.hf-inference.py | 22 +++++++++++ .../0.curl.hf-inference.sh | 5 +++ .../0.huggingface.js.hf-inference.js | 11 ++++++ .../0.huggingface_hub.hf-inference.py | 13 +++++++ .../1.fetch.hf-inference.js | 19 +++++++++ .../1.requests.hf-inference.py | 12 ++++++ .../text-to-image/0.curl.hf-inference.sh | 5 +++ .../text-to-image/0.huggingface.js.fal-ai.js | 11 ++++++ .../0.huggingface.js.hf-inference.js | 11 ++++++ .../text-to-image/0.huggingface_hub.fal-ai.py | 12 ++++++ .../0.huggingface_hub.hf-inference.py | 12 ++++++ .../text-to-image/1.fal-client.fal-ai.py | 9 +++++ .../text-to-image/1.fetch.hf-inference.js | 18 +++++++++ .../text-to-image/1.requests.hf-inference.py | 17 ++++++++ .../text-to-video/0.huggingface.js.fal-ai.js | 11 ++++++ .../0.huggingface.js.replicate.js | 11 ++++++ .../text-to-video/0.huggingface_hub.fal-ai.py | 11 ++++++ .../0.huggingface_hub.replicate.py | 11 ++++++ 63 files changed, 1322 insertions(+) create mode 100644 packages/inference/snippets-fixtures/automatic-speech-recognition/0.huggingface_hub.hf-inference.py create mode 100644 packages/inference/snippets-fixtures/automatic-speech-recognition/1.requests.hf-inference.py create mode 100644 packages/inference/snippets-fixtures/conversational-llm-non-stream/0.curl.hf-inference.sh create mode 100644 packages/inference/snippets-fixtures/conversational-llm-non-stream/0.curl.together.sh create mode 100644 packages/inference/snippets-fixtures/conversational-llm-non-stream/0.huggingface.js.hf-inference.js create mode 100644 packages/inference/snippets-fixtures/conversational-llm-non-stream/0.huggingface.js.together.js create mode 100644 packages/inference/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.hf-inference.py create mode 100644 packages/inference/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.together.py create mode 100644 packages/inference/snippets-fixtures/conversational-llm-non-stream/1.openai.hf-inference.js create mode 100644 packages/inference/snippets-fixtures/conversational-llm-non-stream/1.openai.hf-inference.py create mode 100644 packages/inference/snippets-fixtures/conversational-llm-non-stream/1.openai.together.js create mode 100644 packages/inference/snippets-fixtures/conversational-llm-non-stream/1.openai.together.py create mode 100644 packages/inference/snippets-fixtures/conversational-llm-stream/0.curl.hf-inference.sh create mode 100644 packages/inference/snippets-fixtures/conversational-llm-stream/0.curl.together.sh create mode 100644 packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface.js.hf-inference.js create mode 100644 packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface.js.together.js create mode 100644 packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.hf-inference.py create mode 100644 packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.together.py create mode 100644 packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.js create mode 100644 packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.py create mode 100644 packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.together.js create mode 100644 packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.together.py create mode 100644 packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.curl.fireworks-ai.sh create mode 100644 packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.curl.hf-inference.sh create mode 100644 packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.huggingface.js.fireworks-ai.js create mode 100644 packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.huggingface.js.hf-inference.js create mode 100644 packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.fireworks-ai.py create mode 100644 packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.hf-inference.py create mode 100644 packages/inference/snippets-fixtures/conversational-vlm-non-stream/1.openai.fireworks-ai.js create mode 100644 packages/inference/snippets-fixtures/conversational-vlm-non-stream/1.openai.fireworks-ai.py create mode 100644 packages/inference/snippets-fixtures/conversational-vlm-non-stream/1.openai.hf-inference.js create mode 100644 packages/inference/snippets-fixtures/conversational-vlm-non-stream/1.openai.hf-inference.py create mode 100644 packages/inference/snippets-fixtures/conversational-vlm-stream/0.curl.fireworks-ai.sh create mode 100644 packages/inference/snippets-fixtures/conversational-vlm-stream/0.curl.hf-inference.sh create mode 100644 packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface.js.fireworks-ai.js create mode 100644 packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface.js.hf-inference.js create mode 100644 packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.fireworks-ai.py create mode 100644 packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.hf-inference.py create mode 100644 packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.js create mode 100644 packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.py create mode 100644 packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.js create mode 100644 packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.py create mode 100644 packages/inference/snippets-fixtures/document-question-answering/0.huggingface_hub.hf-inference.py create mode 100644 packages/inference/snippets-fixtures/document-question-answering/1.requests.hf-inference.py create mode 100644 packages/inference/snippets-fixtures/image-to-image/0.huggingface_hub.hf-inference.py create mode 100644 packages/inference/snippets-fixtures/image-to-image/1.requests.hf-inference.py create mode 100644 packages/inference/snippets-fixtures/text-classification/0.curl.hf-inference.sh create mode 100644 packages/inference/snippets-fixtures/text-classification/0.huggingface.js.hf-inference.js create mode 100644 packages/inference/snippets-fixtures/text-classification/0.huggingface_hub.hf-inference.py create mode 100644 packages/inference/snippets-fixtures/text-classification/1.fetch.hf-inference.js create mode 100644 packages/inference/snippets-fixtures/text-classification/1.requests.hf-inference.py create mode 100644 packages/inference/snippets-fixtures/text-to-image/0.curl.hf-inference.sh create mode 100644 packages/inference/snippets-fixtures/text-to-image/0.huggingface.js.fal-ai.js create mode 100644 packages/inference/snippets-fixtures/text-to-image/0.huggingface.js.hf-inference.js create mode 100644 packages/inference/snippets-fixtures/text-to-image/0.huggingface_hub.fal-ai.py create mode 100644 packages/inference/snippets-fixtures/text-to-image/0.huggingface_hub.hf-inference.py create mode 100644 packages/inference/snippets-fixtures/text-to-image/1.fal-client.fal-ai.py create mode 100644 packages/inference/snippets-fixtures/text-to-image/1.fetch.hf-inference.js create mode 100644 packages/inference/snippets-fixtures/text-to-image/1.requests.hf-inference.py create mode 100644 packages/inference/snippets-fixtures/text-to-video/0.huggingface.js.fal-ai.js create mode 100644 packages/inference/snippets-fixtures/text-to-video/0.huggingface.js.replicate.js create mode 100644 packages/inference/snippets-fixtures/text-to-video/0.huggingface_hub.fal-ai.py create mode 100644 packages/inference/snippets-fixtures/text-to-video/0.huggingface_hub.replicate.py diff --git a/packages/inference/snippets-fixtures/automatic-speech-recognition/0.huggingface_hub.hf-inference.py b/packages/inference/snippets-fixtures/automatic-speech-recognition/0.huggingface_hub.hf-inference.py new file mode 100644 index 0000000000..9a945fcf65 --- /dev/null +++ b/packages/inference/snippets-fixtures/automatic-speech-recognition/0.huggingface_hub.hf-inference.py @@ -0,0 +1,7 @@ +from huggingface_hub import InferenceClient + +client = InferenceClient( + 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/inference/snippets-fixtures/automatic-speech-recognition/1.requests.hf-inference.py b/packages/inference/snippets-fixtures/automatic-speech-recognition/1.requests.hf-inference.py new file mode 100644 index 0000000000..4acaa0ed5b --- /dev/null +++ b/packages/inference/snippets-fixtures/automatic-speech-recognition/1.requests.hf-inference.py @@ -0,0 +1,12 @@ +import requests + +API_URL = "https://router.huggingface.co/hf-inference/models/openai/whisper-large-v3-turbo" +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() + +output = query("sample1.flac") \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/conversational-llm-non-stream/0.curl.hf-inference.sh b/packages/inference/snippets-fixtures/conversational-llm-non-stream/0.curl.hf-inference.sh new file mode 100644 index 0000000000..fc9cf73015 --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-llm-non-stream/0.curl.hf-inference.sh @@ -0,0 +1,14 @@ +curl 'https://router.huggingface.co/hf-inference/models/meta-llama/Llama-3.1-8B-Instruct/v1/chat/completions' \ +-H 'Authorization: Bearer api_token' \ +-H 'Content-Type: application/json' \ +--data '{ + "model": "meta-llama/Llama-3.1-8B-Instruct", + "messages": [ + { + "role": "user", + "content": "What is the capital of France?" + } + ], + "max_tokens": 500, + "stream": false +}' \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/conversational-llm-non-stream/0.curl.together.sh b/packages/inference/snippets-fixtures/conversational-llm-non-stream/0.curl.together.sh new file mode 100644 index 0000000000..12a3dd7fd9 --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-llm-non-stream/0.curl.together.sh @@ -0,0 +1,14 @@ +curl 'https://router.huggingface.co/together/v1/chat/completions' \ +-H 'Authorization: Bearer api_token' \ +-H 'Content-Type: application/json' \ +--data '{ + "model": "", + "messages": [ + { + "role": "user", + "content": "What is the capital of France?" + } + ], + "max_tokens": 500, + "stream": false +}' \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/conversational-llm-non-stream/0.huggingface.js.hf-inference.js b/packages/inference/snippets-fixtures/conversational-llm-non-stream/0.huggingface.js.hf-inference.js new file mode 100644 index 0000000000..15361c3e45 --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-llm-non-stream/0.huggingface.js.hf-inference.js @@ -0,0 +1,17 @@ +import { HfInference } from "@huggingface/inference"; + +const client = new HfInference("api_token"); + +const chatCompletion = await client.chatCompletion({ + model: "meta-llama/Llama-3.1-8B-Instruct", + messages: [ + { + role: "user", + content: "What is the capital of France?" + } + ], + provider: "hf-inference", + max_tokens: 500, +}); + +console.log(chatCompletion.choices[0].message); diff --git a/packages/inference/snippets-fixtures/conversational-llm-non-stream/0.huggingface.js.together.js b/packages/inference/snippets-fixtures/conversational-llm-non-stream/0.huggingface.js.together.js new file mode 100644 index 0000000000..9d64919ed9 --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-llm-non-stream/0.huggingface.js.together.js @@ -0,0 +1,17 @@ +import { HfInference } from "@huggingface/inference"; + +const client = new HfInference("api_token"); + +const chatCompletion = await client.chatCompletion({ + model: "meta-llama/Llama-3.1-8B-Instruct", + messages: [ + { + role: "user", + content: "What is the capital of France?" + } + ], + provider: "together", + max_tokens: 500, +}); + +console.log(chatCompletion.choices[0].message); diff --git a/packages/inference/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.hf-inference.py b/packages/inference/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.hf-inference.py new file mode 100644 index 0000000000..44e29d2a0b --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.hf-inference.py @@ -0,0 +1,21 @@ +from huggingface_hub import InferenceClient + +client = InferenceClient( + provider="hf-inference", + api_key="api_token", +) + +messages = [ + { + "role": "user", + "content": "What is the capital of France?" + } +] + +completion = client.chat.completions.create( + 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/inference/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.together.py b/packages/inference/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.together.py new file mode 100644 index 0000000000..ee348d6987 --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.together.py @@ -0,0 +1,21 @@ +from huggingface_hub import InferenceClient + +client = InferenceClient( + provider="together", + api_key="api_token", +) + +messages = [ + { + "role": "user", + "content": "What is the capital of France?" + } +] + +completion = client.chat.completions.create( + 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/inference/snippets-fixtures/conversational-llm-non-stream/1.openai.hf-inference.js b/packages/inference/snippets-fixtures/conversational-llm-non-stream/1.openai.hf-inference.js new file mode 100644 index 0000000000..3f825bcf80 --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-llm-non-stream/1.openai.hf-inference.js @@ -0,0 +1,19 @@ +import { OpenAI } from "openai"; + +const client = new OpenAI({ + baseURL: "https://router.huggingface.co/hf-inference/v1", + apiKey: "api_token" +}); + +const chatCompletion = await client.chat.completions.create({ + model: "meta-llama/Llama-3.1-8B-Instruct", + messages: [ + { + role: "user", + content: "What is the capital of France?" + } + ], + max_tokens: 500, +}); + +console.log(chatCompletion.choices[0].message); diff --git a/packages/inference/snippets-fixtures/conversational-llm-non-stream/1.openai.hf-inference.py b/packages/inference/snippets-fixtures/conversational-llm-non-stream/1.openai.hf-inference.py new file mode 100644 index 0000000000..4d557b289e --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-llm-non-stream/1.openai.hf-inference.py @@ -0,0 +1,21 @@ +from openai import OpenAI + +client = OpenAI( + base_url="https://router.huggingface.co/hf-inference/v1", + api_key="api_token" +) + +messages = [ + { + "role": "user", + "content": "What is the capital of France?" + } +] + +completion = client.chat.completions.create( + 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/inference/snippets-fixtures/conversational-llm-non-stream/1.openai.together.js b/packages/inference/snippets-fixtures/conversational-llm-non-stream/1.openai.together.js new file mode 100644 index 0000000000..816b6dd513 --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-llm-non-stream/1.openai.together.js @@ -0,0 +1,19 @@ +import { OpenAI } from "openai"; + +const client = new OpenAI({ + baseURL: "https://router.huggingface.co/together", + apiKey: "api_token" +}); + +const chatCompletion = await client.chat.completions.create({ + model: "", + messages: [ + { + role: "user", + content: "What is the capital of France?" + } + ], + max_tokens: 500, +}); + +console.log(chatCompletion.choices[0].message); diff --git a/packages/inference/snippets-fixtures/conversational-llm-non-stream/1.openai.together.py b/packages/inference/snippets-fixtures/conversational-llm-non-stream/1.openai.together.py new file mode 100644 index 0000000000..d4fb0f865c --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-llm-non-stream/1.openai.together.py @@ -0,0 +1,21 @@ +from openai import OpenAI + +client = OpenAI( + base_url="https://router.huggingface.co/together", + api_key="api_token" +) + +messages = [ + { + "role": "user", + "content": "What is the capital of France?" + } +] + +completion = client.chat.completions.create( + model="", + messages=messages, + max_tokens=500, +) + +print(completion.choices[0].message) \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/conversational-llm-stream/0.curl.hf-inference.sh b/packages/inference/snippets-fixtures/conversational-llm-stream/0.curl.hf-inference.sh new file mode 100644 index 0000000000..0d96e5c312 --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-llm-stream/0.curl.hf-inference.sh @@ -0,0 +1,14 @@ +curl 'https://router.huggingface.co/hf-inference/models/meta-llama/Llama-3.1-8B-Instruct/v1/chat/completions' \ +-H 'Authorization: Bearer api_token' \ +-H 'Content-Type: application/json' \ +--data '{ + "model": "meta-llama/Llama-3.1-8B-Instruct", + "messages": [ + { + "role": "user", + "content": "What is the capital of France?" + } + ], + "max_tokens": 500, + "stream": true +}' \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/conversational-llm-stream/0.curl.together.sh b/packages/inference/snippets-fixtures/conversational-llm-stream/0.curl.together.sh new file mode 100644 index 0000000000..e0e22e352d --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-llm-stream/0.curl.together.sh @@ -0,0 +1,14 @@ +curl 'https://router.huggingface.co/together/v1/chat/completions' \ +-H 'Authorization: Bearer api_token' \ +-H 'Content-Type: application/json' \ +--data '{ + "model": "", + "messages": [ + { + "role": "user", + "content": "What is the capital of France?" + } + ], + "max_tokens": 500, + "stream": true +}' \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface.js.hf-inference.js b/packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface.js.hf-inference.js new file mode 100644 index 0000000000..4bdd0c143e --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface.js.hf-inference.js @@ -0,0 +1,25 @@ +import { HfInference } from "@huggingface/inference"; + +const client = new HfInference("api_token"); + +let out = ""; + +const stream = client.chatCompletionStream({ + model: "meta-llama/Llama-3.1-8B-Instruct", + messages: [ + { + role: "user", + content: "What is the capital of France?" + } + ], + provider: "hf-inference", + max_tokens: 500, +}); + +for await (const chunk of stream) { + if (chunk.choices && chunk.choices.length > 0) { + const newContent = chunk.choices[0].delta.content; + out += newContent; + console.log(newContent); + } +} \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface.js.together.js b/packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface.js.together.js new file mode 100644 index 0000000000..5822b2ce57 --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface.js.together.js @@ -0,0 +1,25 @@ +import { HfInference } from "@huggingface/inference"; + +const client = new HfInference("api_token"); + +let out = ""; + +const stream = client.chatCompletionStream({ + model: "meta-llama/Llama-3.1-8B-Instruct", + messages: [ + { + role: "user", + content: "What is the capital of France?" + } + ], + provider: "together", + max_tokens: 500, +}); + +for await (const chunk of stream) { + if (chunk.choices && chunk.choices.length > 0) { + const newContent = chunk.choices[0].delta.content; + out += newContent; + console.log(newContent); + } +} \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.hf-inference.py b/packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.hf-inference.py new file mode 100644 index 0000000000..0f1fd0dbdc --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.hf-inference.py @@ -0,0 +1,23 @@ +from huggingface_hub import InferenceClient + +client = InferenceClient( + provider="hf-inference", + api_key="api_token", +) + +messages = [ + { + "role": "user", + "content": "What is the capital of France?" + } +] + +stream = client.chat.completions.create( + 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 diff --git a/packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.together.py b/packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.together.py new file mode 100644 index 0000000000..31d00d979c --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.together.py @@ -0,0 +1,23 @@ +from huggingface_hub import InferenceClient + +client = InferenceClient( + provider="together", + api_key="api_token", +) + +messages = [ + { + "role": "user", + "content": "What is the capital of France?" + } +] + +stream = client.chat.completions.create( + 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 diff --git a/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.js b/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.js new file mode 100644 index 0000000000..568cbf72a5 --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.js @@ -0,0 +1,28 @@ +import { OpenAI } from "openai"; + +const client = new OpenAI({ + baseURL: "https://router.huggingface.co/hf-inference/v1", + apiKey: "api_token" +}); + +let out = ""; + +const stream = await client.chat.completions.create({ + model: "meta-llama/Llama-3.1-8B-Instruct", + messages: [ + { + role: "user", + content: "What is the capital of France?" + } + ], + max_tokens: 500, + stream: true, +}); + +for await (const chunk of stream) { + if (chunk.choices && chunk.choices.length > 0) { + const newContent = chunk.choices[0].delta.content; + out += newContent; + console.log(newContent); + } +} \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.py b/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.py new file mode 100644 index 0000000000..12167b83bc --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.py @@ -0,0 +1,23 @@ +from openai import OpenAI + +client = OpenAI( + base_url="https://router.huggingface.co/hf-inference/v1", + api_key="api_token" +) + +messages = [ + { + "role": "user", + "content": "What is the capital of France?" + } +] + +stream = client.chat.completions.create( + 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 diff --git a/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.together.js b/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.together.js new file mode 100644 index 0000000000..ef0f928e13 --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.together.js @@ -0,0 +1,28 @@ +import { OpenAI } from "openai"; + +const client = new OpenAI({ + baseURL: "https://router.huggingface.co/together", + apiKey: "api_token" +}); + +let out = ""; + +const stream = await client.chat.completions.create({ + model: "", + messages: [ + { + role: "user", + content: "What is the capital of France?" + } + ], + max_tokens: 500, + stream: true, +}); + +for await (const chunk of stream) { + if (chunk.choices && chunk.choices.length > 0) { + const newContent = chunk.choices[0].delta.content; + out += newContent; + console.log(newContent); + } +} \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.together.py b/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.together.py new file mode 100644 index 0000000000..67d0bde845 --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.together.py @@ -0,0 +1,23 @@ +from openai import OpenAI + +client = OpenAI( + base_url="https://router.huggingface.co/together", + api_key="api_token" +) + +messages = [ + { + "role": "user", + "content": "What is the capital of France?" + } +] + +stream = client.chat.completions.create( + 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 diff --git a/packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.curl.fireworks-ai.sh b/packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.curl.fireworks-ai.sh new file mode 100644 index 0000000000..fa8775048d --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.curl.fireworks-ai.sh @@ -0,0 +1,25 @@ +curl 'https://router.huggingface.co/fireworks-ai/v1/chat/completions' \ +-H 'Authorization: Bearer api_token' \ +-H 'Content-Type: application/json' \ +--data '{ + "model": "", + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "Describe this image in one sentence." + }, + { + "type": "image_url", + "image_url": { + "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" + } + } + ] + } + ], + "max_tokens": 500, + "stream": false +}' \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.curl.hf-inference.sh b/packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.curl.hf-inference.sh new file mode 100644 index 0000000000..680a12557c --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.curl.hf-inference.sh @@ -0,0 +1,25 @@ +curl 'https://router.huggingface.co/hf-inference/models/meta-llama/Llama-3.2-11B-Vision-Instruct/v1/chat/completions' \ +-H 'Authorization: Bearer api_token' \ +-H 'Content-Type: application/json' \ +--data '{ + "model": "meta-llama/Llama-3.2-11B-Vision-Instruct", + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "Describe this image in one sentence." + }, + { + "type": "image_url", + "image_url": { + "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" + } + } + ] + } + ], + "max_tokens": 500, + "stream": false +}' \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.huggingface.js.fireworks-ai.js b/packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.huggingface.js.fireworks-ai.js new file mode 100644 index 0000000000..88e8d44506 --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.huggingface.js.fireworks-ai.js @@ -0,0 +1,28 @@ +import { HfInference } from "@huggingface/inference"; + +const client = new HfInference("api_token"); + +const chatCompletion = await client.chatCompletion({ + model: "meta-llama/Llama-3.2-11B-Vision-Instruct", + messages: [ + { + role: "user", + content: [ + { + type: "text", + text: "Describe this image in one sentence." + }, + { + type: "image_url", + image_url: { + url: "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" + } + } + ] + } + ], + provider: "fireworks-ai", + max_tokens: 500, +}); + +console.log(chatCompletion.choices[0].message); diff --git a/packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.huggingface.js.hf-inference.js b/packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.huggingface.js.hf-inference.js new file mode 100644 index 0000000000..2a69d4d82c --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.huggingface.js.hf-inference.js @@ -0,0 +1,28 @@ +import { HfInference } from "@huggingface/inference"; + +const client = new HfInference("api_token"); + +const chatCompletion = await client.chatCompletion({ + model: "meta-llama/Llama-3.2-11B-Vision-Instruct", + messages: [ + { + role: "user", + content: [ + { + type: "text", + text: "Describe this image in one sentence." + }, + { + type: "image_url", + image_url: { + url: "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" + } + } + ] + } + ], + provider: "hf-inference", + max_tokens: 500, +}); + +console.log(chatCompletion.choices[0].message); diff --git a/packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.fireworks-ai.py b/packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.fireworks-ai.py new file mode 100644 index 0000000000..41f26d30a7 --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.fireworks-ai.py @@ -0,0 +1,32 @@ +from huggingface_hub import InferenceClient + +client = InferenceClient( + provider="fireworks-ai", + api_key="api_token", +) + +messages = [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "Describe this image in one sentence." + }, + { + "type": "image_url", + "image_url": { + "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" + } + } + ] + } +] + +completion = client.chat.completions.create( + 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/inference/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.hf-inference.py b/packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.hf-inference.py new file mode 100644 index 0000000000..507809756d --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.hf-inference.py @@ -0,0 +1,32 @@ +from huggingface_hub import InferenceClient + +client = InferenceClient( + provider="hf-inference", + api_key="api_token", +) + +messages = [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "Describe this image in one sentence." + }, + { + "type": "image_url", + "image_url": { + "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" + } + } + ] + } +] + +completion = client.chat.completions.create( + 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/inference/snippets-fixtures/conversational-vlm-non-stream/1.openai.fireworks-ai.js b/packages/inference/snippets-fixtures/conversational-vlm-non-stream/1.openai.fireworks-ai.js new file mode 100644 index 0000000000..28b031e754 --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-vlm-non-stream/1.openai.fireworks-ai.js @@ -0,0 +1,30 @@ +import { OpenAI } from "openai"; + +const client = new OpenAI({ + baseURL: "https://router.huggingface.co/fireworks-ai", + apiKey: "api_token" +}); + +const chatCompletion = await client.chat.completions.create({ + model: "", + messages: [ + { + role: "user", + content: [ + { + type: "text", + text: "Describe this image in one sentence." + }, + { + type: "image_url", + image_url: { + url: "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" + } + } + ] + } + ], + max_tokens: 500, +}); + +console.log(chatCompletion.choices[0].message); diff --git a/packages/inference/snippets-fixtures/conversational-vlm-non-stream/1.openai.fireworks-ai.py b/packages/inference/snippets-fixtures/conversational-vlm-non-stream/1.openai.fireworks-ai.py new file mode 100644 index 0000000000..19a126c63d --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-vlm-non-stream/1.openai.fireworks-ai.py @@ -0,0 +1,32 @@ +from openai import OpenAI + +client = OpenAI( + base_url="https://router.huggingface.co/fireworks-ai", + api_key="api_token" +) + +messages = [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "Describe this image in one sentence." + }, + { + "type": "image_url", + "image_url": { + "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" + } + } + ] + } +] + +completion = client.chat.completions.create( + model="", + messages=messages, + max_tokens=500, +) + +print(completion.choices[0].message) \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/conversational-vlm-non-stream/1.openai.hf-inference.js b/packages/inference/snippets-fixtures/conversational-vlm-non-stream/1.openai.hf-inference.js new file mode 100644 index 0000000000..00d7095eeb --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-vlm-non-stream/1.openai.hf-inference.js @@ -0,0 +1,30 @@ +import { OpenAI } from "openai"; + +const client = new OpenAI({ + baseURL: "https://router.huggingface.co/hf-inference/v1", + apiKey: "api_token" +}); + +const chatCompletion = await client.chat.completions.create({ + model: "meta-llama/Llama-3.2-11B-Vision-Instruct", + messages: [ + { + role: "user", + content: [ + { + type: "text", + text: "Describe this image in one sentence." + }, + { + type: "image_url", + image_url: { + url: "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" + } + } + ] + } + ], + max_tokens: 500, +}); + +console.log(chatCompletion.choices[0].message); diff --git a/packages/inference/snippets-fixtures/conversational-vlm-non-stream/1.openai.hf-inference.py b/packages/inference/snippets-fixtures/conversational-vlm-non-stream/1.openai.hf-inference.py new file mode 100644 index 0000000000..517466190f --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-vlm-non-stream/1.openai.hf-inference.py @@ -0,0 +1,32 @@ +from openai import OpenAI + +client = OpenAI( + base_url="https://router.huggingface.co/hf-inference/v1", + api_key="api_token" +) + +messages = [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "Describe this image in one sentence." + }, + { + "type": "image_url", + "image_url": { + "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" + } + } + ] + } +] + +completion = client.chat.completions.create( + 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/inference/snippets-fixtures/conversational-vlm-stream/0.curl.fireworks-ai.sh b/packages/inference/snippets-fixtures/conversational-vlm-stream/0.curl.fireworks-ai.sh new file mode 100644 index 0000000000..9cd88a26c4 --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-vlm-stream/0.curl.fireworks-ai.sh @@ -0,0 +1,25 @@ +curl 'https://router.huggingface.co/fireworks-ai/v1/chat/completions' \ +-H 'Authorization: Bearer api_token' \ +-H 'Content-Type: application/json' \ +--data '{ + "model": "", + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "Describe this image in one sentence." + }, + { + "type": "image_url", + "image_url": { + "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" + } + } + ] + } + ], + "max_tokens": 500, + "stream": true +}' \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/conversational-vlm-stream/0.curl.hf-inference.sh b/packages/inference/snippets-fixtures/conversational-vlm-stream/0.curl.hf-inference.sh new file mode 100644 index 0000000000..cf7c484801 --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-vlm-stream/0.curl.hf-inference.sh @@ -0,0 +1,25 @@ +curl 'https://router.huggingface.co/hf-inference/models/meta-llama/Llama-3.2-11B-Vision-Instruct/v1/chat/completions' \ +-H 'Authorization: Bearer api_token' \ +-H 'Content-Type: application/json' \ +--data '{ + "model": "meta-llama/Llama-3.2-11B-Vision-Instruct", + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "Describe this image in one sentence." + }, + { + "type": "image_url", + "image_url": { + "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" + } + } + ] + } + ], + "max_tokens": 500, + "stream": true +}' \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface.js.fireworks-ai.js b/packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface.js.fireworks-ai.js new file mode 100644 index 0000000000..2407499efe --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface.js.fireworks-ai.js @@ -0,0 +1,36 @@ +import { HfInference } from "@huggingface/inference"; + +const client = new HfInference("api_token"); + +let out = ""; + +const stream = client.chatCompletionStream({ + model: "meta-llama/Llama-3.2-11B-Vision-Instruct", + messages: [ + { + role: "user", + content: [ + { + type: "text", + text: "Describe this image in one sentence." + }, + { + type: "image_url", + image_url: { + url: "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" + } + } + ] + } + ], + provider: "fireworks-ai", + max_tokens: 500, +}); + +for await (const chunk of stream) { + if (chunk.choices && chunk.choices.length > 0) { + const newContent = chunk.choices[0].delta.content; + out += newContent; + console.log(newContent); + } +} \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface.js.hf-inference.js b/packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface.js.hf-inference.js new file mode 100644 index 0000000000..45886e7709 --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface.js.hf-inference.js @@ -0,0 +1,36 @@ +import { HfInference } from "@huggingface/inference"; + +const client = new HfInference("api_token"); + +let out = ""; + +const stream = client.chatCompletionStream({ + model: "meta-llama/Llama-3.2-11B-Vision-Instruct", + messages: [ + { + role: "user", + content: [ + { + type: "text", + text: "Describe this image in one sentence." + }, + { + type: "image_url", + image_url: { + url: "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" + } + } + ] + } + ], + provider: "hf-inference", + max_tokens: 500, +}); + +for await (const chunk of stream) { + if (chunk.choices && chunk.choices.length > 0) { + const newContent = chunk.choices[0].delta.content; + out += newContent; + console.log(newContent); + } +} \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.fireworks-ai.py b/packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.fireworks-ai.py new file mode 100644 index 0000000000..ad86364e4d --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.fireworks-ai.py @@ -0,0 +1,34 @@ +from huggingface_hub import InferenceClient + +client = InferenceClient( + provider="fireworks-ai", + api_key="api_token", +) + +messages = [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "Describe this image in one sentence." + }, + { + "type": "image_url", + "image_url": { + "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" + } + } + ] + } +] + +stream = client.chat.completions.create( + 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 diff --git a/packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.hf-inference.py b/packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.hf-inference.py new file mode 100644 index 0000000000..7d58f8020a --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.hf-inference.py @@ -0,0 +1,34 @@ +from huggingface_hub import InferenceClient + +client = InferenceClient( + provider="hf-inference", + api_key="api_token", +) + +messages = [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "Describe this image in one sentence." + }, + { + "type": "image_url", + "image_url": { + "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" + } + } + ] + } +] + +stream = client.chat.completions.create( + 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 diff --git a/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.js b/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.js new file mode 100644 index 0000000000..53e2830b6a --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.js @@ -0,0 +1,39 @@ +import { OpenAI } from "openai"; + +const client = new OpenAI({ + baseURL: "https://router.huggingface.co/fireworks-ai", + apiKey: "api_token" +}); + +let out = ""; + +const stream = await client.chat.completions.create({ + model: "", + messages: [ + { + role: "user", + content: [ + { + type: "text", + text: "Describe this image in one sentence." + }, + { + type: "image_url", + image_url: { + url: "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" + } + } + ] + } + ], + max_tokens: 500, + stream: true, +}); + +for await (const chunk of stream) { + if (chunk.choices && chunk.choices.length > 0) { + const newContent = chunk.choices[0].delta.content; + out += newContent; + console.log(newContent); + } +} \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.py b/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.py new file mode 100644 index 0000000000..33acb547ad --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.py @@ -0,0 +1,34 @@ +from openai import OpenAI + +client = OpenAI( + base_url="https://router.huggingface.co/fireworks-ai", + api_key="api_token" +) + +messages = [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "Describe this image in one sentence." + }, + { + "type": "image_url", + "image_url": { + "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" + } + } + ] + } +] + +stream = client.chat.completions.create( + 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 diff --git a/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.js b/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.js new file mode 100644 index 0000000000..093c104780 --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.js @@ -0,0 +1,39 @@ +import { OpenAI } from "openai"; + +const client = new OpenAI({ + baseURL: "https://router.huggingface.co/hf-inference/v1", + apiKey: "api_token" +}); + +let out = ""; + +const stream = await client.chat.completions.create({ + model: "meta-llama/Llama-3.2-11B-Vision-Instruct", + messages: [ + { + role: "user", + content: [ + { + type: "text", + text: "Describe this image in one sentence." + }, + { + type: "image_url", + image_url: { + url: "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" + } + } + ] + } + ], + max_tokens: 500, + stream: true, +}); + +for await (const chunk of stream) { + if (chunk.choices && chunk.choices.length > 0) { + const newContent = chunk.choices[0].delta.content; + out += newContent; + console.log(newContent); + } +} \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.py b/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.py new file mode 100644 index 0000000000..6c788816ee --- /dev/null +++ b/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.py @@ -0,0 +1,34 @@ +from openai import OpenAI + +client = OpenAI( + base_url="https://router.huggingface.co/hf-inference/v1", + api_key="api_token" +) + +messages = [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "Describe this image in one sentence." + }, + { + "type": "image_url", + "image_url": { + "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" + } + } + ] + } +] + +stream = client.chat.completions.create( + 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 diff --git a/packages/inference/snippets-fixtures/document-question-answering/0.huggingface_hub.hf-inference.py b/packages/inference/snippets-fixtures/document-question-answering/0.huggingface_hub.hf-inference.py new file mode 100644 index 0000000000..46e437c63a --- /dev/null +++ b/packages/inference/snippets-fixtures/document-question-answering/0.huggingface_hub.hf-inference.py @@ -0,0 +1,11 @@ +from huggingface_hub import InferenceClient + +client = InferenceClient( + provider="hf-inference", + api_key="api_token", +) +output = client.document_question_answering( + "cat.png", + question="What is in this image?", + model="impira/layoutlm-invoices", +) \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/document-question-answering/1.requests.hf-inference.py b/packages/inference/snippets-fixtures/document-question-answering/1.requests.hf-inference.py new file mode 100644 index 0000000000..51fed678d8 --- /dev/null +++ b/packages/inference/snippets-fixtures/document-question-answering/1.requests.hf-inference.py @@ -0,0 +1,19 @@ +import base64 +import requests + +API_URL = "https://router.huggingface.co/hf-inference/models/impira/layoutlm-invoices" +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() + +output = query({ + "inputs": { + "image": "cat.png", + "question": "What is in this image?" + }, +}) \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/image-to-image/0.huggingface_hub.hf-inference.py b/packages/inference/snippets-fixtures/image-to-image/0.huggingface_hub.hf-inference.py new file mode 100644 index 0000000000..c21949966a --- /dev/null +++ b/packages/inference/snippets-fixtures/image-to-image/0.huggingface_hub.hf-inference.py @@ -0,0 +1,12 @@ +from huggingface_hub import InferenceClient + +client = InferenceClient( + provider="hf-inference", + api_key="api_token", +) +# output is a PIL.Image object +image = client.image_to_image( + "cat.png", + prompt="Turn the cat into a tiger.", + model="stabilityai/stable-diffusion-xl-refiner-1.0", +) \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/image-to-image/1.requests.hf-inference.py b/packages/inference/snippets-fixtures/image-to-image/1.requests.hf-inference.py new file mode 100644 index 0000000000..2d16ba05f5 --- /dev/null +++ b/packages/inference/snippets-fixtures/image-to-image/1.requests.hf-inference.py @@ -0,0 +1,22 @@ +import base64 +import requests + +API_URL = "https://router.huggingface.co/hf-inference/models/stabilityai/stable-diffusion-xl-refiner-1.0" +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 + +image_bytes = query({ + "inputs": "cat.png", + "parameters": {"prompt": "Turn the cat into a tiger."}, +}) + +# 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/snippets-fixtures/text-classification/0.curl.hf-inference.sh b/packages/inference/snippets-fixtures/text-classification/0.curl.hf-inference.sh new file mode 100644 index 0000000000..5661a427b2 --- /dev/null +++ b/packages/inference/snippets-fixtures/text-classification/0.curl.hf-inference.sh @@ -0,0 +1,5 @@ +curl https://router.huggingface.co/hf-inference/models/distilbert/distilbert-base-uncased-finetuned-sst-2-english \ + -X POST \ + -d '{"inputs": "I like you. I love you"}' \ + -H 'Content-Type: application/json' \ + -H 'Authorization: Bearer api_token' \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/text-classification/0.huggingface.js.hf-inference.js b/packages/inference/snippets-fixtures/text-classification/0.huggingface.js.hf-inference.js new file mode 100644 index 0000000000..5d7bfb7307 --- /dev/null +++ b/packages/inference/snippets-fixtures/text-classification/0.huggingface.js.hf-inference.js @@ -0,0 +1,11 @@ +import { HfInference } from "@huggingface/inference"; + +const client = new HfInference("api_token"); + +const output = await client.textClassification({ + model: "distilbert/distilbert-base-uncased-finetuned-sst-2-english", + inputs: "I like you. I love you", + provider: "hf-inference", +}); + +console.log(output); diff --git a/packages/inference/snippets-fixtures/text-classification/0.huggingface_hub.hf-inference.py b/packages/inference/snippets-fixtures/text-classification/0.huggingface_hub.hf-inference.py new file mode 100644 index 0000000000..d6b9b9aa45 --- /dev/null +++ b/packages/inference/snippets-fixtures/text-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.text_classification( + inputs="I like you. I love you", + model="distilbert/distilbert-base-uncased-finetuned-sst-2-english", +) + +print(result) diff --git a/packages/inference/snippets-fixtures/text-classification/1.fetch.hf-inference.js b/packages/inference/snippets-fixtures/text-classification/1.fetch.hf-inference.js new file mode 100644 index 0000000000..c6e9a43b21 --- /dev/null +++ b/packages/inference/snippets-fixtures/text-classification/1.fetch.hf-inference.js @@ -0,0 +1,19 @@ +async function query(data) { + const response = await fetch( + "https://router.huggingface.co/hf-inference/models/distilbert/distilbert-base-uncased-finetuned-sst-2-english", + { + headers: { + Authorization: "Bearer api_token", + "Content-Type": "application/json", + }, + method: "POST", + body: JSON.stringify(data), + } + ); + const result = await response.json(); + return result; +} + +query({"inputs": "I like you. I love you"}).then((response) => { + console.log(JSON.stringify(response)); +}); \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/text-classification/1.requests.hf-inference.py b/packages/inference/snippets-fixtures/text-classification/1.requests.hf-inference.py new file mode 100644 index 0000000000..281fca0df2 --- /dev/null +++ b/packages/inference/snippets-fixtures/text-classification/1.requests.hf-inference.py @@ -0,0 +1,12 @@ +import requests + +API_URL = "https://router.huggingface.co/hf-inference/models/distilbert/distilbert-base-uncased-finetuned-sst-2-english" +headers = {"Authorization": "Bearer api_token"} + +def query(payload): + response = requests.post(API_URL, headers=headers, json=payload) + return response.json() + +output = query({ + "inputs": "I like you. I love you", +}) \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/text-to-image/0.curl.hf-inference.sh b/packages/inference/snippets-fixtures/text-to-image/0.curl.hf-inference.sh new file mode 100644 index 0000000000..b89f831606 --- /dev/null +++ b/packages/inference/snippets-fixtures/text-to-image/0.curl.hf-inference.sh @@ -0,0 +1,5 @@ +curl https://router.huggingface.co/hf-inference/models/black-forest-labs/FLUX.1-schnell \ + -X POST \ + -d '{"inputs": "Astronaut riding a horse"}' \ + -H 'Content-Type: application/json' \ + -H 'Authorization: Bearer api_token' \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/text-to-image/0.huggingface.js.fal-ai.js b/packages/inference/snippets-fixtures/text-to-image/0.huggingface.js.fal-ai.js new file mode 100644 index 0000000000..1eff0bedbe --- /dev/null +++ b/packages/inference/snippets-fixtures/text-to-image/0.huggingface.js.fal-ai.js @@ -0,0 +1,11 @@ +import { HfInference } from "@huggingface/inference"; + +const client = new HfInference("api_token"); + +const image = await client.textToImage({ + model: "black-forest-labs/FLUX.1-schnell", + inputs: "Astronaut riding a horse", + parameters: { num_inference_steps: 5 }, + provider: "fal-ai", +}); +/// Use the generated image (it's a Blob) diff --git a/packages/inference/snippets-fixtures/text-to-image/0.huggingface.js.hf-inference.js b/packages/inference/snippets-fixtures/text-to-image/0.huggingface.js.hf-inference.js new file mode 100644 index 0000000000..f506de747a --- /dev/null +++ b/packages/inference/snippets-fixtures/text-to-image/0.huggingface.js.hf-inference.js @@ -0,0 +1,11 @@ +import { HfInference } from "@huggingface/inference"; + +const client = new HfInference("api_token"); + +const image = await client.textToImage({ + model: "black-forest-labs/FLUX.1-schnell", + inputs: "Astronaut riding a horse", + parameters: { num_inference_steps: 5 }, + provider: "hf-inference", +}); +/// Use the generated image (it's a Blob) diff --git a/packages/inference/snippets-fixtures/text-to-image/0.huggingface_hub.fal-ai.py b/packages/inference/snippets-fixtures/text-to-image/0.huggingface_hub.fal-ai.py new file mode 100644 index 0000000000..8f8a6b1021 --- /dev/null +++ b/packages/inference/snippets-fixtures/text-to-image/0.huggingface_hub.fal-ai.py @@ -0,0 +1,12 @@ +from huggingface_hub import InferenceClient + +client = InferenceClient( + provider="fal-ai", + api_key="api_token", +) + +# output is a PIL.Image object +image = client.text_to_image( + "Astronaut riding a horse", + model="black-forest-labs/FLUX.1-schnell", +) \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/text-to-image/0.huggingface_hub.hf-inference.py b/packages/inference/snippets-fixtures/text-to-image/0.huggingface_hub.hf-inference.py new file mode 100644 index 0000000000..b4c48cec43 --- /dev/null +++ b/packages/inference/snippets-fixtures/text-to-image/0.huggingface_hub.hf-inference.py @@ -0,0 +1,12 @@ +from huggingface_hub import InferenceClient + +client = InferenceClient( + provider="hf-inference", + api_key="api_token", +) + +# output is a PIL.Image object +image = client.text_to_image( + "Astronaut riding a horse", + model="black-forest-labs/FLUX.1-schnell", +) \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/text-to-image/1.fal-client.fal-ai.py b/packages/inference/snippets-fixtures/text-to-image/1.fal-client.fal-ai.py new file mode 100644 index 0000000000..36220f1057 --- /dev/null +++ b/packages/inference/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) diff --git a/packages/inference/snippets-fixtures/text-to-image/1.fetch.hf-inference.js b/packages/inference/snippets-fixtures/text-to-image/1.fetch.hf-inference.js new file mode 100644 index 0000000000..b344bae816 --- /dev/null +++ b/packages/inference/snippets-fixtures/text-to-image/1.fetch.hf-inference.js @@ -0,0 +1,18 @@ +async function query(data) { + const response = await fetch( + "https://router.huggingface.co/hf-inference/models/black-forest-labs/FLUX.1-schnell", + { + headers: { + Authorization: "Bearer api_token", + "Content-Type": "application/json", + }, + method: "POST", + body: JSON.stringify(data), + } + ); + const result = await response.blob(); + return result; +} +query({"inputs": "Astronaut riding a horse"}).then((response) => { + // Use image +}); \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/text-to-image/1.requests.hf-inference.py b/packages/inference/snippets-fixtures/text-to-image/1.requests.hf-inference.py new file mode 100644 index 0000000000..a8e1ad9de0 --- /dev/null +++ b/packages/inference/snippets-fixtures/text-to-image/1.requests.hf-inference.py @@ -0,0 +1,17 @@ +import requests + +API_URL = "https://router.huggingface.co/hf-inference/models/black-forest-labs/FLUX.1-schnell" +headers = {"Authorization": "Bearer api_token"} + +def query(payload): + response = requests.post(API_URL, headers=headers, json=payload) + return response.content + +image_bytes = query({ + "inputs": "Astronaut riding a horse", +}) + +# 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/snippets-fixtures/text-to-video/0.huggingface.js.fal-ai.js b/packages/inference/snippets-fixtures/text-to-video/0.huggingface.js.fal-ai.js new file mode 100644 index 0000000000..1176dc2716 --- /dev/null +++ b/packages/inference/snippets-fixtures/text-to-video/0.huggingface.js.fal-ai.js @@ -0,0 +1,11 @@ +import { HfInference } from "@huggingface/inference"; + +const client = new HfInference("api_token"); + +const video = await client.textToVideo({ + model: "tencent/HunyuanVideo", + provider: "fal-ai", + inputs: "A young man walking on the street", + parameters: { num_inference_steps: 5 }, +}); +// Use the generated video (it's a Blob) diff --git a/packages/inference/snippets-fixtures/text-to-video/0.huggingface.js.replicate.js b/packages/inference/snippets-fixtures/text-to-video/0.huggingface.js.replicate.js new file mode 100644 index 0000000000..fa0c617b3b --- /dev/null +++ b/packages/inference/snippets-fixtures/text-to-video/0.huggingface.js.replicate.js @@ -0,0 +1,11 @@ +import { HfInference } from "@huggingface/inference"; + +const client = new HfInference("api_token"); + +const video = await client.textToVideo({ + model: "tencent/HunyuanVideo", + provider: "replicate", + inputs: "A young man walking on the street", + parameters: { num_inference_steps: 5 }, +}); +// Use the generated video (it's a Blob) diff --git a/packages/inference/snippets-fixtures/text-to-video/0.huggingface_hub.fal-ai.py b/packages/inference/snippets-fixtures/text-to-video/0.huggingface_hub.fal-ai.py new file mode 100644 index 0000000000..fc96fd381b --- /dev/null +++ b/packages/inference/snippets-fixtures/text-to-video/0.huggingface_hub.fal-ai.py @@ -0,0 +1,11 @@ +from huggingface_hub import InferenceClient + +client = InferenceClient( + provider="fal-ai", + api_key="api_token", +) + +video = client.text_to_video( + "A young man walking on the street", + model="tencent/HunyuanVideo", +) \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/text-to-video/0.huggingface_hub.replicate.py b/packages/inference/snippets-fixtures/text-to-video/0.huggingface_hub.replicate.py new file mode 100644 index 0000000000..208c583119 --- /dev/null +++ b/packages/inference/snippets-fixtures/text-to-video/0.huggingface_hub.replicate.py @@ -0,0 +1,11 @@ +from huggingface_hub import InferenceClient + +client = InferenceClient( + provider="replicate", + api_key="api_token", +) + +video = client.text_to_video( + "A young man walking on the street", + model="tencent/HunyuanVideo", +) \ No newline at end of file From 00060429d4447c2dd393e7ebfb67145681ab6302 Mon Sep 17 00:00:00 2001 From: Wauplin Date: Wed, 5 Mar 2025 17:40:22 +0100 Subject: [PATCH 02/26] nope --- .../conversational-llm-stream/0.huggingface_hub.hf-inference.py | 2 +- .../conversational-llm-stream/0.huggingface_hub.together.py | 2 +- .../conversational-vlm-stream/0.huggingface_hub.fireworks-ai.py | 2 +- .../conversational-vlm-stream/0.huggingface_hub.hf-inference.py | 2 +- packages/inference/src/snippets/python.ts | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.hf-inference.py b/packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.hf-inference.py index 0f1fd0dbdc..2056520eeb 100644 --- a/packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.hf-inference.py +++ b/packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.hf-inference.py @@ -14,7 +14,7 @@ stream = client.chat.completions.create( model="meta-llama/Llama-3.1-8B-Instruct", - messages=messages, + messages=messages, max_tokens=500, stream=True, ) diff --git a/packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.together.py b/packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.together.py index 31d00d979c..4026d437d9 100644 --- a/packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.together.py +++ b/packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.together.py @@ -14,7 +14,7 @@ stream = client.chat.completions.create( model="meta-llama/Llama-3.1-8B-Instruct", - messages=messages, + messages=messages, max_tokens=500, stream=True, ) diff --git a/packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.fireworks-ai.py b/packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.fireworks-ai.py index ad86364e4d..19ca984c7a 100644 --- a/packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.fireworks-ai.py +++ b/packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.fireworks-ai.py @@ -25,7 +25,7 @@ stream = client.chat.completions.create( model="meta-llama/Llama-3.2-11B-Vision-Instruct", - messages=messages, + messages=messages, max_tokens=500, stream=True, ) diff --git a/packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.hf-inference.py b/packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.hf-inference.py index 7d58f8020a..bcb774f072 100644 --- a/packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.hf-inference.py +++ b/packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.hf-inference.py @@ -25,7 +25,7 @@ stream = client.chat.completions.create( model="meta-llama/Llama-3.2-11B-Vision-Instruct", - messages=messages, + messages=messages, max_tokens=500, stream=True, ) diff --git a/packages/inference/src/snippets/python.ts b/packages/inference/src/snippets/python.ts index ef3df2564f..d947dc196f 100644 --- a/packages/inference/src/snippets/python.ts +++ b/packages/inference/src/snippets/python.ts @@ -87,7 +87,7 @@ messages = ${messagesStr} stream = client.chat.completions.create( model="${model.id}", - messages=messages, + messages=messages, ${configStr} stream=True, ) From 8ae6d93521a105aea1c96e2eab0ddda18beb7734 Mon Sep 17 00:00:00 2001 From: Wauplin Date: Wed, 5 Mar 2025 18:17:11 +0100 Subject: [PATCH 03/26] draft --- .../inference/src/lib/makeRequestOptions.ts | 24 ++++++----- packages/inference/src/snippets/python.ts | 42 +++++++++++++------ 2 files changed, 43 insertions(+), 23 deletions(-) diff --git a/packages/inference/src/lib/makeRequestOptions.ts b/packages/inference/src/lib/makeRequestOptions.ts index a6472d99db..6d0394481d 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 d947dc196f..ffdb378392 100644 --- a/packages/inference/src/snippets/python.ts +++ b/packages/inference/src/snippets/python.ts @@ -1,13 +1,15 @@ -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 { makeRequestOptions } from "../lib/makeRequestOptions"; +import type { InferenceProvider } from "../types"; const HFH_INFERENCE_CLIENT_METHODS: Partial> = { "audio-classification": "audio_classification", @@ -39,7 +41,7 @@ const HFH_INFERENCE_CLIENT_METHODS: Partial> = { "tabular-regression": "tabular_regression", }; -const snippetImportInferenceClient = (accessToken: string, provider: SnippetInferenceProvider): string => +const snippetImportInferenceClient = (accessToken: string, provider: InferenceProvider): string => `\ from huggingface_hub import InferenceClient @@ -51,7 +53,7 @@ client = InferenceClient( const snippetConversational = ( model: ModelDataMinimal, accessToken: string, - provider: SnippetInferenceProvider, + provider: InferenceProvider, providerModelId?: string, opts?: { streaming?: boolean; @@ -76,6 +78,20 @@ const snippetConversational = ( attributeValueConnector: "=", }); + makeRequestOptions( + { + messages, + model: providerModelId, + accessToken, + provider, + }, + { + task: "text-generation", + chatCompletion: true, + __skipModelIdResolution: true, + } + ).then((res) => console.log(res)); + if (streaming) { return [ { @@ -165,8 +181,8 @@ const snippetZeroShotClassification = (model: ModelDataMinimal): InferenceSnippe client: "requests", content: `\ 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": ${getModelInputSnippet(model)}, @@ -201,7 +217,7 @@ output = query({ const snippetBasic = ( model: ModelDataMinimal, accessToken: string, - provider: SnippetInferenceProvider + provider: InferenceProvider ): InferenceSnippet[] => { return [ ...(model.pipeline_tag && model.pipeline_tag in HFH_INFERENCE_CLIENT_METHODS @@ -254,7 +270,7 @@ output = query(${getModelInputSnippet(model)})`, const snippetTextToImage = ( model: ModelDataMinimal, accessToken: string, - provider: SnippetInferenceProvider, + provider: InferenceProvider, providerModelId?: string ): InferenceSnippet[] => { return [ @@ -313,7 +329,7 @@ image = Image.open(io.BytesIO(image_bytes))`, const snippetTextToVideo = ( model: ModelDataMinimal, accessToken: string, - provider: SnippetInferenceProvider + provider: InferenceProvider ): InferenceSnippet[] => { return ["fal-ai", "replicate"].includes(provider) ? [ @@ -390,7 +406,7 @@ Audio(audio, rate=sampling_rate)`, const snippetAutomaticSpeechRecognition = ( model: ModelDataMinimal, accessToken: string, - provider: SnippetInferenceProvider + provider: InferenceProvider ): InferenceSnippet[] => { return [ { @@ -405,7 +421,7 @@ output = client.automatic_speech_recognition(${getModelInputSnippet(model)}, mod const snippetDocumentQuestionAnswering = ( model: ModelDataMinimal, accessToken: string, - provider: SnippetInferenceProvider + provider: InferenceProvider ): InferenceSnippet[] => { const inputsAsStr = getModelInputSnippet(model) as string; const inputsAsObj = JSON.parse(inputsAsStr); @@ -439,7 +455,7 @@ output = query({ const snippetImageToImage = ( model: ModelDataMinimal, accessToken: string, - provider: SnippetInferenceProvider + provider: InferenceProvider ): InferenceSnippet[] => { const inputsAsStr = getModelInputSnippet(model) as string; const inputsAsObj = JSON.parse(inputsAsStr); @@ -483,7 +499,7 @@ const pythonSnippets: Partial< ( model: ModelDataMinimal, accessToken: string, - provider: SnippetInferenceProvider, + provider: InferenceProvider, providerModelId?: string, opts?: Record ) => InferenceSnippet[] @@ -524,7 +540,7 @@ const pythonSnippets: Partial< export function getPythonInferenceSnippet( model: ModelDataMinimal, accessToken: string, - provider: SnippetInferenceProvider, + provider: InferenceProvider, providerModelId?: string, opts?: Record ): InferenceSnippet[] { From b86faf5f26c939278daa0165f83e315198e5ebad Mon Sep 17 00:00:00 2001 From: Wauplin Date: Thu, 6 Mar 2025 12:40:47 +0100 Subject: [PATCH 04/26] fixing some tab -> whitespaces issues --- .../0.huggingface_hub.hf-inference.py | 6 +++--- .../0.huggingface_hub.together.py | 6 +++--- .../1.openai.hf-inference.py | 10 +++++----- .../1.openai.together.py | 10 +++++----- .../0.huggingface_hub.hf-inference.py | 2 +- .../0.huggingface_hub.together.py | 2 +- .../1.openai.hf-inference.py | 14 +++++++------- .../conversational-llm-stream/1.openai.together.py | 14 +++++++------- .../0.huggingface_hub.fireworks-ai.py | 6 +++--- .../0.huggingface_hub.hf-inference.py | 6 +++--- .../1.openai.fireworks-ai.py | 10 +++++----- .../1.openai.hf-inference.py | 10 +++++----- .../0.huggingface_hub.fireworks-ai.py | 2 +- .../0.huggingface_hub.hf-inference.py | 2 +- .../1.openai.fireworks-ai.py | 14 +++++++------- .../1.openai.hf-inference.py | 14 +++++++------- 16 files changed, 64 insertions(+), 64 deletions(-) diff --git a/packages/inference/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.hf-inference.py b/packages/inference/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.hf-inference.py index 44e29d2a0b..4e53fde9ac 100644 --- a/packages/inference/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.hf-inference.py +++ b/packages/inference/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/inference/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.together.py b/packages/inference/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.together.py index ee348d6987..0e5231e4dc 100644 --- a/packages/inference/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.together.py +++ b/packages/inference/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/inference/snippets-fixtures/conversational-llm-non-stream/1.openai.hf-inference.py b/packages/inference/snippets-fixtures/conversational-llm-non-stream/1.openai.hf-inference.py index 4d557b289e..bed410d47b 100644 --- a/packages/inference/snippets-fixtures/conversational-llm-non-stream/1.openai.hf-inference.py +++ b/packages/inference/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/inference/snippets-fixtures/conversational-llm-non-stream/1.openai.together.py b/packages/inference/snippets-fixtures/conversational-llm-non-stream/1.openai.together.py index d4fb0f865c..ea0f8683bc 100644 --- a/packages/inference/snippets-fixtures/conversational-llm-non-stream/1.openai.together.py +++ b/packages/inference/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/inference/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.hf-inference.py b/packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.hf-inference.py index 2056520eeb..941601839e 100644 --- a/packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.hf-inference.py +++ b/packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.hf-inference.py @@ -13,7 +13,7 @@ ] stream = client.chat.completions.create( - model="meta-llama/Llama-3.1-8B-Instruct", + model="meta-llama/Llama-3.1-8B-Instruct", messages=messages, max_tokens=500, stream=True, diff --git a/packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.together.py b/packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.together.py index 4026d437d9..317eef428c 100644 --- a/packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.together.py +++ b/packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.together.py @@ -13,7 +13,7 @@ ] stream = client.chat.completions.create( - model="meta-llama/Llama-3.1-8B-Instruct", + model="meta-llama/Llama-3.1-8B-Instruct", messages=messages, max_tokens=500, stream=True, diff --git a/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.py b/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.py index 12167b83bc..1c77a5a3a0 100644 --- a/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.py +++ b/packages/inference/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="", + 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="") diff --git a/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.together.py b/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.together.py index 67d0bde845..29765655bc 100644 --- a/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.together.py +++ b/packages/inference/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="", + 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="") diff --git a/packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.fireworks-ai.py b/packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.fireworks-ai.py index 41f26d30a7..27fbac0fba 100644 --- a/packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.fireworks-ai.py +++ b/packages/inference/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/inference/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.hf-inference.py b/packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.hf-inference.py index 507809756d..02201bfa11 100644 --- a/packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.hf-inference.py +++ b/packages/inference/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/inference/snippets-fixtures/conversational-vlm-non-stream/1.openai.fireworks-ai.py b/packages/inference/snippets-fixtures/conversational-vlm-non-stream/1.openai.fireworks-ai.py index 19a126c63d..25490a6a9c 100644 --- a/packages/inference/snippets-fixtures/conversational-vlm-non-stream/1.openai.fireworks-ai.py +++ b/packages/inference/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/inference/snippets-fixtures/conversational-vlm-non-stream/1.openai.hf-inference.py b/packages/inference/snippets-fixtures/conversational-vlm-non-stream/1.openai.hf-inference.py index 517466190f..dab0422b2d 100644 --- a/packages/inference/snippets-fixtures/conversational-vlm-non-stream/1.openai.hf-inference.py +++ b/packages/inference/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/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.fireworks-ai.py b/packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.fireworks-ai.py index 19ca984c7a..6cdec6118d 100644 --- a/packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.fireworks-ai.py +++ b/packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.fireworks-ai.py @@ -24,7 +24,7 @@ ] stream = client.chat.completions.create( - model="meta-llama/Llama-3.2-11B-Vision-Instruct", + model="meta-llama/Llama-3.2-11B-Vision-Instruct", messages=messages, max_tokens=500, stream=True, diff --git a/packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.hf-inference.py b/packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.hf-inference.py index bcb774f072..d5e2b64dae 100644 --- a/packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.hf-inference.py +++ b/packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.hf-inference.py @@ -24,7 +24,7 @@ ] stream = client.chat.completions.create( - model="meta-llama/Llama-3.2-11B-Vision-Instruct", + model="meta-llama/Llama-3.2-11B-Vision-Instruct", messages=messages, max_tokens=500, stream=True, diff --git a/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.py b/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.py index 33acb547ad..40c29b5fd9 100644 --- a/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.py +++ b/packages/inference/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="", + 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="") diff --git a/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.py b/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.py index 6c788816ee..9a484124e4 100644 --- a/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.py +++ b/packages/inference/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="", + 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="") From f121009562412581236b081a5f6cd44aa93ef60d Mon Sep 17 00:00:00 2001 From: Wauplin Date: Thu, 6 Mar 2025 13:01:24 +0100 Subject: [PATCH 05/26] first move towards templating --- .vscode/settings.json | 5 +- packages/inference/package.json | 7 +- packages/inference/pnpm-lock.yaml | 45 +++ packages/inference/scripts/tmp_gen.ts | 275 ++++++++++++++++++ .../0.requests.hf-inference.py | 13 + .../0.requests.hf-inference.py | 21 ++ packages/inference/src/snippets/python.ts | 208 ++++++------- .../python/huggingface_hub/conversational.hbs | 11 + .../huggingface_hub/conversationalStream.hbs | 13 + .../huggingface_hub/importInferenceClient.hbs | 6 + .../python/openai/conversational.hbs | 16 + .../python/openai/conversationalStream.hbs | 18 ++ .../requests/zeroShotClassification.hbs | 8 + .../requests/zeroShotImageClassification.hbs | 14 + 14 files changed, 543 insertions(+), 117 deletions(-) create mode 100644 packages/inference/scripts/tmp_gen.ts create mode 100644 packages/inference/snippets-fixtures/zero-shot-classification/0.requests.hf-inference.py create mode 100644 packages/inference/snippets-fixtures/zero-shot-image-classification/0.requests.hf-inference.py create mode 100644 packages/inference/src/snippets/templates/python/huggingface_hub/conversational.hbs create mode 100644 packages/inference/src/snippets/templates/python/huggingface_hub/conversationalStream.hbs create mode 100644 packages/inference/src/snippets/templates/python/huggingface_hub/importInferenceClient.hbs create mode 100644 packages/inference/src/snippets/templates/python/openai/conversational.hbs create mode 100644 packages/inference/src/snippets/templates/python/openai/conversationalStream.hbs create mode 100644 packages/inference/src/snippets/templates/python/requests/zeroShotClassification.hbs create mode 100644 packages/inference/src/snippets/templates/python/requests/zeroShotImageClassification.hbs diff --git a/.vscode/settings.json b/.vscode/settings.json index 9d7878528a..a9c6ca92b4 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -17,5 +17,8 @@ "search.exclude": { "**/dist": true }, - "typescript.tsdk": "node_modules/typescript/lib" + "typescript.tsdk": "node_modules/typescript/lib", + "[handlebars]": { + "editor.defaultFormatter": "mfeckies.handlebars-formatter" + } } diff --git a/packages/inference/package.json b/packages/inference/package.json index 31fafb2031..5e2a1d6186 100644 --- a/packages/inference/package.json +++ b/packages/inference/package.json @@ -49,12 +49,15 @@ "prepublishOnly": "pnpm run build", "test": "vitest run --config vitest.config.mts", "test:browser": "vitest run --browser.name=chrome --browser.headless --config vitest.config.mts", - "check": "tsc" + "check": "tsc", + "generate": "tsup src/index.ts --format cjs,esm --clean && tsx scripts/tmp_gen.ts" }, "dependencies": { - "@huggingface/tasks": "workspace:^" + "@huggingface/tasks": "workspace:^", + "handlebars": "^4.7.8" }, "devDependencies": { + "@types/handlebars": "^4.1.0", "@types/node": "18.13.0" }, "resolutions": {} diff --git a/packages/inference/pnpm-lock.yaml b/packages/inference/pnpm-lock.yaml index 5bb56da2d1..954979285d 100644 --- a/packages/inference/pnpm-lock.yaml +++ b/packages/inference/pnpm-lock.yaml @@ -8,14 +8,59 @@ dependencies: '@huggingface/tasks': specifier: workspace:^ version: link:../tasks + handlebars: + specifier: ^4.7.8 + version: 4.7.8 devDependencies: + '@types/handlebars': + specifier: ^4.1.0 + version: 4.1.0 '@types/node': specifier: 18.13.0 version: 18.13.0 packages: + /@types/handlebars@4.1.0: + resolution: {integrity: sha512-gq9YweFKNNB1uFK71eRqsd4niVkXrxHugqWFQkeLRJvGjnxsLr16bYtcsG4tOFwmYi0Bax+wCkbf1reUfdl4kA==} + deprecated: This is a stub types definition. handlebars provides its own type definitions, so you do not need this installed. + dependencies: + handlebars: 4.7.8 + dev: true + /@types/node@18.13.0: resolution: {integrity: sha512-gC3TazRzGoOnoKAhUx+Q0t8S9Tzs74z7m0ipwGpSqQrleP14hKxP4/JUeEQcD3W1/aIpnWl8pHowI7WokuZpXg==} dev: true + + /handlebars@4.7.8: + resolution: {integrity: sha512-vafaFqs8MZkRrSX7sFVUdo3ap/eNiLnb4IakshzvP56X5Nr1iGKAIqdX6tMlm6HcNRIkr6AxO5jFEoJzzpT8aQ==} + engines: {node: '>=0.4.7'} + hasBin: true + dependencies: + minimist: 1.2.8 + neo-async: 2.6.2 + source-map: 0.6.1 + wordwrap: 1.0.0 + optionalDependencies: + uglify-js: 3.19.3 + + /minimist@1.2.8: + resolution: {integrity: sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==} + + /neo-async@2.6.2: + resolution: {integrity: sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==} + + /source-map@0.6.1: + resolution: {integrity: sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==} + engines: {node: '>=0.10.0'} + + /uglify-js@3.19.3: + resolution: {integrity: sha512-v3Xu+yuwBXisp6QYTcH4UbH+xYJXqnq2m/LtQVWKWzYc1iehYnLixoQDN9FH6/j9/oybfd6W9Ghwkl8+UMKTKQ==} + engines: {node: '>=0.8.0'} + hasBin: true + requiresBuild: true + optional: true + + /wordwrap@1.0.0: + resolution: {integrity: sha512-gvVzJFlPycKc5dZN4yPkP8w7Dc37BtP1yczEneOb4uq34pXZcvrtRTmWV8W+Ume+XCxKgbjM+nevkyFPMybd4Q==} diff --git a/packages/inference/scripts/tmp_gen.ts b/packages/inference/scripts/tmp_gen.ts new file mode 100644 index 0000000000..b81ae86b37 --- /dev/null +++ b/packages/inference/scripts/tmp_gen.ts @@ -0,0 +1,275 @@ +/* + * Generates Inference API snippets using @huggingface/tasks snippets. + * + * If used in test mode ("pnpm test"), it compares the generated snippets with the expected ones. + * If used in generation mode ("pnpm generate-snippets-fixtures"), it generates the expected snippets. + * + * Expected snippets are saved under ./snippets-fixtures and are meant to be versioned on GitHub. + * Each snippet is saved in a separate file placed under "./{test-name}/{index}.{client}.{language}": + * - test-name: the name of the test (e.g. "text-to-image", "conversational-llm", etc.) + * - index: the order of the snippet in the array of snippets (0 if not an array) + * - client: the client name (e.g. "requests", "huggingface_hub", "openai", etc.). Default to "default" if client is not specified. + * - language: the language of the snippet (e.g. "sh", "js", "py", etc.) + * + * Example: + * ./packages/tasks-gen/snippets-fixtures/text-to-image/0.huggingface_hub.py + */ + +import { existsSync as pathExists } from "node:fs"; +import * as fs from "node:fs/promises"; +import * as path from "node:path/posix"; + +import { snippets } from "@huggingface/inference"; +import type { SnippetInferenceProvider, InferenceSnippet, ModelDataMinimal } from "@huggingface/tasks"; + +type LANGUAGE = "sh" | "js" | "py"; + +const TEST_CASES: { + testName: string; + model: ModelDataMinimal; + languages: LANGUAGE[]; + providers: SnippetInferenceProvider[]; + opts?: Record; +}[] = [ + { + testName: "automatic-speech-recognition", + model: { + id: "openai/whisper-large-v3-turbo", + pipeline_tag: "automatic-speech-recognition", + tags: [], + inference: "", + }, + languages: ["py"], + providers: ["hf-inference"], + }, + { + testName: "conversational-llm-non-stream", + model: { + id: "meta-llama/Llama-3.1-8B-Instruct", + pipeline_tag: "text-generation", + tags: ["conversational"], + inference: "", + }, + languages: ["sh", "js", "py"], + providers: ["hf-inference", "together"], + opts: { streaming: false }, + }, + { + testName: "conversational-llm-stream", + model: { + id: "meta-llama/Llama-3.1-8B-Instruct", + pipeline_tag: "text-generation", + tags: ["conversational"], + inference: "", + }, + languages: ["sh", "js", "py"], + providers: ["hf-inference", "together"], + opts: { streaming: true }, + }, + { + testName: "conversational-vlm-non-stream", + model: { + id: "meta-llama/Llama-3.2-11B-Vision-Instruct", + pipeline_tag: "image-text-to-text", + tags: ["conversational"], + inference: "", + }, + languages: ["sh", "js", "py"], + providers: ["hf-inference", "fireworks-ai"], + opts: { streaming: false }, + }, + { + testName: "conversational-vlm-stream", + model: { + id: "meta-llama/Llama-3.2-11B-Vision-Instruct", + pipeline_tag: "image-text-to-text", + tags: ["conversational"], + inference: "", + }, + languages: ["sh", "js", "py"], + providers: ["hf-inference", "fireworks-ai"], + opts: { streaming: true }, + }, + { + testName: "document-question-answering", + model: { + id: "impira/layoutlm-invoices", + pipeline_tag: "document-question-answering", + tags: [], + inference: "", + }, + languages: ["py"], + providers: ["hf-inference"], + }, + { + testName: "image-to-image", + model: { + id: "stabilityai/stable-diffusion-xl-refiner-1.0", + pipeline_tag: "image-to-image", + tags: [], + inference: "", + }, + languages: ["py"], + providers: ["hf-inference"], + }, + { + testName: "text-to-image", + model: { + id: "black-forest-labs/FLUX.1-schnell", + pipeline_tag: "text-to-image", + tags: [], + inference: "", + }, + providers: ["hf-inference", "fal-ai"], + languages: ["sh", "js", "py"], + }, + { + testName: "text-to-video", + model: { + id: "tencent/HunyuanVideo", + pipeline_tag: "text-to-video", + tags: [], + inference: "", + }, + providers: ["replicate", "fal-ai"], + languages: ["js", "py"], + }, + { + testName: "text-classification", + model: { + id: "distilbert/distilbert-base-uncased-finetuned-sst-2-english", + pipeline_tag: "text-classification", + tags: [], + inference: "", + }, + providers: ["hf-inference"], + languages: ["sh", "js", "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 = { + sh: snippets.curl.getCurlInferenceSnippet, + js: snippets.js.getJsInferenceSnippet, + py: snippets.python.getPythonInferenceSnippet, +} as const; + +const rootDirFinder = (): string => { + let currentPath = path.normalize(import.meta.url).replace("file:", ""); + + while (currentPath !== "/") { + if (pathExists(path.join(currentPath, "package.json"))) { + return currentPath; + } + + currentPath = path.normalize(path.join(currentPath, "..")); + } + + return "/"; +}; + +function getFixtureFolder(testName: string): string { + return path.join(rootDirFinder(), "snippets-fixtures", testName); +} + +function generateInferenceSnippet( + model: ModelDataMinimal, + language: LANGUAGE, + provider: SnippetInferenceProvider, + opts?: Record +): InferenceSnippet[] { + const providerModelId = provider === "hf-inference" ? model.id : `<${provider} alias for ${model.id}>`; + return GET_SNIPPET_FN[language](model, "api_token", provider, providerModelId, opts); +} + +async function getExpectedInferenceSnippet( + testName: string, + language: LANGUAGE, + provider: SnippetInferenceProvider +): Promise { + const fixtureFolder = getFixtureFolder(testName); + const files = await fs.readdir(fixtureFolder); + + const expectedSnippets: InferenceSnippet[] = []; + for (const file of files.filter((file) => file.endsWith("." + language) && file.includes(`.${provider}.`)).sort()) { + const client = path.basename(file).split(".").slice(1, -2).join("."); // e.g. '0.huggingface.js.replicate.js' => "huggingface.js" + const content = await fs.readFile(path.join(fixtureFolder, file), { encoding: "utf-8" }); + expectedSnippets.push({ client, content }); + } + return expectedSnippets; +} + +async function saveExpectedInferenceSnippet( + testName: string, + language: LANGUAGE, + provider: SnippetInferenceProvider, + snippets: InferenceSnippet[] +) { + const fixtureFolder = getFixtureFolder(testName); + await fs.mkdir(fixtureFolder, { recursive: true }); + + for (const [index, snippet] of snippets.entries()) { + const file = path.join(fixtureFolder, `${index}.${snippet.client ?? "default"}.${provider}.${language}`); + await fs.writeFile(file, snippet.content); + } +} + +if (import.meta.vitest) { + // Run test if in test mode + const { describe, expect, it } = import.meta.vitest; + + describe("inference API snippets", () => { + TEST_CASES.forEach(({ testName, model, languages, providers, opts }) => { + describe(testName, () => { + languages.forEach((language) => { + providers.forEach((provider) => { + it(language, async () => { + const generatedSnippets = generateInferenceSnippet(model, language, provider, opts); + const expectedSnippets = await getExpectedInferenceSnippet(testName, language, provider); + expect(generatedSnippets).toEqual(expectedSnippets); + }); + }); + }); + }); + }); + }); +} else { + // Otherwise, generate the fixtures + console.log("✨ Re-generating snippets"); + console.debug(" 🚜 Removing existing fixtures..."); + await fs.rm(path.join(rootDirFinder(), "snippets-fixtures"), { recursive: true, force: true }); + + console.debug(" 🏭 Generating new fixtures..."); + TEST_CASES.forEach(({ testName, model, languages, providers, opts }) => { + console.debug(` ${testName} (${languages.join(", ")}) (${providers.join(", ")})`); + languages.forEach(async (language) => { + providers.forEach(async (provider) => { + const generatedSnippets = generateInferenceSnippet(model, language, provider, opts); + await saveExpectedInferenceSnippet(testName, language, provider, generatedSnippets); + }); + }); + }); + console.log("✅ All done!"); + console.log("👉 Please check the generated fixtures before committing them."); +} diff --git a/packages/inference/snippets-fixtures/zero-shot-classification/0.requests.hf-inference.py b/packages/inference/snippets-fixtures/zero-shot-classification/0.requests.hf-inference.py new file mode 100644 index 0000000000..3054fe8de8 --- /dev/null +++ b/packages/inference/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/inference/snippets-fixtures/zero-shot-image-classification/0.requests.hf-inference.py b/packages/inference/snippets-fixtures/zero-shot-image-classification/0.requests.hf-inference.py new file mode 100644 index 0000000000..07004ed2cf --- /dev/null +++ b/packages/inference/snippets-fixtures/zero-shot-image-classification/0.requests.hf-inference.py @@ -0,0 +1,21 @@ +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 diff --git a/packages/inference/src/snippets/python.ts b/packages/inference/src/snippets/python.ts index ffdb378392..0f56fd188c 100644 --- a/packages/inference/src/snippets/python.ts +++ b/packages/inference/src/snippets/python.ts @@ -8,8 +8,50 @@ import { stringifyGenerationConfig, stringifyMessages, } from "@huggingface/tasks"; -import { makeRequestOptions } from "../lib/makeRequestOptions"; import type { InferenceProvider } from "../types"; +import fs from "fs"; +import Handlebars from "handlebars"; +import path from "path"; +import { existsSync as pathExists } from "node:fs"; + +interface TemplateParams { + accessToken?: string; + provider?: InferenceProvider; + modelId?: string; + importInferenceClient?: string; + messagesStr?: string; + configStr?: string; + baseUrl?: string; + inputs?: string | ChatCompletionInputMessage[]; +} + +const rootDirFinder = (): string => { + let currentPath = path.normalize(import.meta.url).replace("file:", ""); + + while (currentPath !== "/") { + if (pathExists(path.join(currentPath, "package.json"))) { + return currentPath; + } + + currentPath = path.normalize(path.join(currentPath, "..")); + } + + return "/"; +}; + +const loadTemplate = (language: string, tool: string, templateName: string): ((data: TemplateParams) => string) => { + const templatePath = path.join( + rootDirFinder(), + "src", + "snippets", + "templates", + language, + tool, + `${templateName}.hbs` + ); + const template = fs.readFileSync(templatePath, "utf8"); + return Handlebars.compile(template); +}; const HFH_INFERENCE_CLIENT_METHODS: Partial> = { "audio-classification": "audio_classification", @@ -41,14 +83,13 @@ const HFH_INFERENCE_CLIENT_METHODS: Partial> = { "tabular-regression": "tabular_regression", }; -const snippetImportInferenceClient = (accessToken: string, provider: InferenceProvider): string => - `\ -from huggingface_hub import InferenceClient - -client = InferenceClient( - provider="${provider}", - api_key="${accessToken || "{API_TOKEN}"}", -)`; +const snippetImportInferenceClient = loadTemplate("python", "huggingface_hub", "importInferenceClient"); +const snippetOpenAIConversational = loadTemplate("python", "openai", "conversational"); +const snippetOpenAIConversationalStream = loadTemplate("python", "openai", "conversationalStream"); +const snippetInferenceClientConversational = loadTemplate("python", "huggingface_hub", "conversational"); +const snippetInferenceClientConversationalStream = loadTemplate("python", "huggingface_hub", "conversationalStream"); +const snippetRequestZeroShotClassification = loadTemplate("python", "requests", "zeroShotClassification"); +const snippetRequestZeroShotImageClassification = loadTemplate("python", "requests", "zeroShotImageClassification"); const snippetConversational = ( model: ModelDataMinimal, @@ -78,98 +119,54 @@ const snippetConversational = ( attributeValueConnector: "=", }); - makeRequestOptions( - { - messages, - model: providerModelId, - accessToken, - provider, - }, - { - task: "text-generation", - chatCompletion: true, - __skipModelIdResolution: true, - } - ).then((res) => console.log(res)); - 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="")`, + content: snippetInferenceClientConversationalStream({ + accessToken, + provider, + modelId: model.id, + importInferenceClient: snippetImportInferenceClient({ accessToken, provider }), + messagesStr, + configStr, + }), }, { 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="")`, + content: snippetOpenAIConversationalStream({ + accessToken, + provider, + modelId: providerModelId ?? model.id, + importInferenceClient: snippetImportInferenceClient({ accessToken, provider }), + messagesStr, + configStr, + }), }, ]; } 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)`, + content: snippetInferenceClientConversational({ + accessToken, + provider, + modelId: model.id, + importInferenceClient: snippetImportInferenceClient({ accessToken, provider }), + messagesStr, + configStr, + }), }, { 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)`, + content: snippetOpenAIConversational({ + accessToken, + provider, + modelId: providerModelId ?? model.id, + messagesStr, + configStr, + baseUrl: openAIbaseUrl(provider), + }), }, ]; } @@ -179,15 +176,9 @@ const snippetZeroShotClassification = (model: ModelDataMinimal): InferenceSnippe 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"]}, -})`, + content: snippetRequestZeroShotClassification({ + inputs: getModelInputSnippet(model), + }), }, ]; }; @@ -196,20 +187,9 @@ const snippetZeroShotImageClassification = (model: ModelDataMinimal): InferenceS 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"]}, -})`, + content: snippetRequestZeroShotImageClassification({ + inputs: getModelInputSnippet(model), + }), }, ]; }; @@ -225,7 +205,7 @@ const snippetBasic = ( { client: "huggingface_hub", content: `\ -${snippetImportInferenceClient(accessToken, provider)} +${snippetImportInferenceClient({ accessToken, provider })} result = client.${HFH_INFERENCE_CLIENT_METHODS[model.pipeline_tag]}( inputs=${getModelInputSnippet(model)}, @@ -277,7 +257,7 @@ const snippetTextToImage = ( { client: "huggingface_hub", content: `\ -${snippetImportInferenceClient(accessToken, provider)} +${snippetImportInferenceClient({ accessToken, provider })} # output is a PIL.Image object image = client.text_to_image( @@ -336,7 +316,7 @@ const snippetTextToVideo = ( { client: "huggingface_hub", content: `\ -${snippetImportInferenceClient(accessToken, provider)} +${snippetImportInferenceClient({ accessToken, provider })} video = client.text_to_video( ${getModelInputSnippet(model)}, @@ -411,7 +391,7 @@ const snippetAutomaticSpeechRecognition = ( return [ { client: "huggingface_hub", - content: `${snippetImportInferenceClient(accessToken, provider)} + content: `${snippetImportInferenceClient({ accessToken, provider })} output = client.automatic_speech_recognition(${getModelInputSnippet(model)}, model="${model.id}")`, }, snippetFile(model)[0], @@ -429,7 +409,7 @@ const snippetDocumentQuestionAnswering = ( return [ { client: "huggingface_hub", - content: `${snippetImportInferenceClient(accessToken, provider)} + content: `${snippetImportInferenceClient({ accessToken, provider })} output = client.document_question_answering( "${inputsAsObj.image}", question="${inputsAsObj.question}", @@ -463,7 +443,7 @@ const snippetImageToImage = ( return [ { client: "huggingface_hub", - content: `${snippetImportInferenceClient(accessToken, provider)} + content: `${snippetImportInferenceClient({ accessToken, provider })} # output is a PIL.Image object image = client.image_to_image( "${inputsAsObj.image}", diff --git a/packages/inference/src/snippets/templates/python/huggingface_hub/conversational.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/conversational.hbs new file mode 100644 index 0000000000..bb27b6a6e9 --- /dev/null +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/conversational.hbs @@ -0,0 +1,11 @@ +{{{importInferenceClient}}} + +messages = {{{messagesStr}}} + +completion = client.chat.completions.create( + model="{{{modelId}}}", + messages=messages, + {{{configStr}}} +) + +print(completion.choices[0].message) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/huggingface_hub/conversationalStream.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/conversationalStream.hbs new file mode 100644 index 0000000000..dd6484f008 --- /dev/null +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/conversationalStream.hbs @@ -0,0 +1,13 @@ +{{{importInferenceClient}}} + +messages = {{{messagesStr}}} + +stream = client.chat.completions.create( + model="{{{modelId}}}", + messages=messages, + {{{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/importInferenceClient.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/importInferenceClient.hbs new file mode 100644 index 0000000000..a5308a08c6 --- /dev/null +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/importInferenceClient.hbs @@ -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/openai/conversational.hbs b/packages/inference/src/snippets/templates/python/openai/conversational.hbs new file mode 100644 index 0000000000..0580ab51f8 --- /dev/null +++ b/packages/inference/src/snippets/templates/python/openai/conversational.hbs @@ -0,0 +1,16 @@ +from openai import OpenAI + +client = OpenAI( + base_url="{{{baseUrl}}}", + api_key="{{{accessToken}}}" +) + +messages = {{{messagesStr}}} + +completion = client.chat.completions.create( + model="{{{modelId}}}", + messages=messages, + {{{configStr}}} +) + +print(completion.choices[0].message) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/openai/conversationalStream.hbs b/packages/inference/src/snippets/templates/python/openai/conversationalStream.hbs new file mode 100644 index 0000000000..2381f51d50 --- /dev/null +++ b/packages/inference/src/snippets/templates/python/openai/conversationalStream.hbs @@ -0,0 +1,18 @@ +from openai import OpenAI + +client = OpenAI( + base_url="{{{baseUrl}}}", + api_key="{{{accessToken}}}" +) + +messages = {{{messagesStr}}} + +stream = client.chat.completions.create( + model="{{{modelId}}}", + messages=messages, + {{{configStr}}} + stream=True +) + +for chunk in stream: + print(chunk.choices[0].delta.content, end="") diff --git a/packages/inference/src/snippets/templates/python/requests/zeroShotClassification.hbs b/packages/inference/src/snippets/templates/python/requests/zeroShotClassification.hbs new file mode 100644 index 0000000000..b55285c980 --- /dev/null +++ b/packages/inference/src/snippets/templates/python/requests/zeroShotClassification.hbs @@ -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.hbs b/packages/inference/src/snippets/templates/python/requests/zeroShotImageClassification.hbs new file mode 100644 index 0000000000..b572fe42cf --- /dev/null +++ b/packages/inference/src/snippets/templates/python/requests/zeroShotImageClassification.hbs @@ -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 From c2231ad891fd5c867ce67b625be66cf686b10e27 Mon Sep 17 00:00:00 2001 From: Wauplin Date: Thu, 6 Mar 2025 13:25:50 +0100 Subject: [PATCH 06/26] always more --- packages/inference/scripts/tmp_gen.ts | 11 +++ .../0.huggingface_hub.hf-inference.py | 13 +++ .../1.requests.hf-inference.py | 12 +++ .../0.huggingface_hub.hf-inference.py | 2 +- .../text-to-image/1.fal-client.fal-ai.py | 2 +- .../0.requests.hf-inference.py | 1 - packages/inference/src/snippets/python.ts | 89 ++++++++----------- .../templates/python/fal_ai/textToImage.hbs | 9 ++ .../python/huggingface_hub/basic.hbs | 8 ++ .../python/huggingface_hub/textToImage.hbs | 7 ++ .../templates/python/requests/basic.hbs | 7 ++ .../templates/python/requests/basicFile.hbs | 7 ++ 12 files changed, 113 insertions(+), 55 deletions(-) create mode 100644 packages/inference/snippets-fixtures/basic-snippet--token-classification/0.huggingface_hub.hf-inference.py create mode 100644 packages/inference/snippets-fixtures/basic-snippet--token-classification/1.requests.hf-inference.py create mode 100644 packages/inference/src/snippets/templates/python/fal_ai/textToImage.hbs create mode 100644 packages/inference/src/snippets/templates/python/huggingface_hub/basic.hbs create mode 100644 packages/inference/src/snippets/templates/python/huggingface_hub/textToImage.hbs create mode 100644 packages/inference/src/snippets/templates/python/requests/basic.hbs create mode 100644 packages/inference/src/snippets/templates/python/requests/basicFile.hbs diff --git a/packages/inference/scripts/tmp_gen.ts b/packages/inference/scripts/tmp_gen.ts index b81ae86b37..3b7dd1e87b 100644 --- a/packages/inference/scripts/tmp_gen.ts +++ b/packages/inference/scripts/tmp_gen.ts @@ -145,6 +145,17 @@ 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: { diff --git a/packages/inference/snippets-fixtures/basic-snippet--token-classification/0.huggingface_hub.hf-inference.py b/packages/inference/snippets-fixtures/basic-snippet--token-classification/0.huggingface_hub.hf-inference.py new file mode 100644 index 0000000000..852ab032de --- /dev/null +++ b/packages/inference/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/inference/snippets-fixtures/basic-snippet--token-classification/1.requests.hf-inference.py b/packages/inference/snippets-fixtures/basic-snippet--token-classification/1.requests.hf-inference.py new file mode 100644 index 0000000000..7ee7c608e7 --- /dev/null +++ b/packages/inference/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/inference/snippets-fixtures/text-classification/0.huggingface_hub.hf-inference.py b/packages/inference/snippets-fixtures/text-classification/0.huggingface_hub.hf-inference.py index d6b9b9aa45..10c9d7a359 100644 --- a/packages/inference/snippets-fixtures/text-classification/0.huggingface_hub.hf-inference.py +++ b/packages/inference/snippets-fixtures/text-classification/0.huggingface_hub.hf-inference.py @@ -10,4 +10,4 @@ model="distilbert/distilbert-base-uncased-finetuned-sst-2-english", ) -print(result) +print(result) \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/text-to-image/1.fal-client.fal-ai.py b/packages/inference/snippets-fixtures/text-to-image/1.fal-client.fal-ai.py index 36220f1057..177c07bb8f 100644 --- a/packages/inference/snippets-fixtures/text-to-image/1.fal-client.fal-ai.py +++ b/packages/inference/snippets-fixtures/text-to-image/1.fal-client.fal-ai.py @@ -6,4 +6,4 @@ "prompt": "Astronaut riding a horse", }, ) -print(result) +print(result) \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/zero-shot-image-classification/0.requests.hf-inference.py b/packages/inference/snippets-fixtures/zero-shot-image-classification/0.requests.hf-inference.py index 07004ed2cf..f25c4eab01 100644 --- a/packages/inference/snippets-fixtures/zero-shot-image-classification/0.requests.hf-inference.py +++ b/packages/inference/snippets-fixtures/zero-shot-image-classification/0.requests.hf-inference.py @@ -1,5 +1,4 @@ import base64 - import requests API_URL = "https://router.huggingface.co/hf-inference/models/openai/clip-vit-large-patch14" diff --git a/packages/inference/src/snippets/python.ts b/packages/inference/src/snippets/python.ts index 0f56fd188c..3a958e93e6 100644 --- a/packages/inference/src/snippets/python.ts +++ b/packages/inference/src/snippets/python.ts @@ -16,13 +16,14 @@ import { existsSync as pathExists } from "node:fs"; interface TemplateParams { accessToken?: string; - provider?: InferenceProvider; - modelId?: string; - importInferenceClient?: string; - messagesStr?: string; - configStr?: string; baseUrl?: string; + configStr?: string; + importInferenceClient?: string; inputs?: string | ChatCompletionInputMessage[]; + messagesStr?: string; + methodName?: string; + modelId?: string; + provider?: InferenceProvider; } const rootDirFinder = (): string => { @@ -84,10 +85,18 @@ const HFH_INFERENCE_CLIENT_METHODS: Partial> = { }; const snippetImportInferenceClient = loadTemplate("python", "huggingface_hub", "importInferenceClient"); -const snippetOpenAIConversational = loadTemplate("python", "openai", "conversational"); -const snippetOpenAIConversationalStream = loadTemplate("python", "openai", "conversationalStream"); +const snippetInferenceClientBasic = loadTemplate("python", "huggingface_hub", "basic"); +const snippetInferenceClientTextToImage = loadTemplate("python", "huggingface_hub", "textToImage"); const snippetInferenceClientConversational = loadTemplate("python", "huggingface_hub", "conversational"); const snippetInferenceClientConversationalStream = loadTemplate("python", "huggingface_hub", "conversationalStream"); + +const snippetFalAITextToImage = loadTemplate("python", "fal_ai", "textToImage"); + +const snippetOpenAIConversational = loadTemplate("python", "openai", "conversational"); +const snippetOpenAIConversationalStream = loadTemplate("python", "openai", "conversationalStream"); + +const snippetRequestsBasic = loadTemplate("python", "requests", "basic"); +const snippetRequestsBasicFile = loadTemplate("python", "requests", "basicFile"); const snippetRequestZeroShotClassification = loadTemplate("python", "requests", "zeroShotClassification"); const snippetRequestZeroShotImageClassification = loadTemplate("python", "requests", "zeroShotImageClassification"); @@ -204,29 +213,20 @@ const snippetBasic = ( ? [ { client: "huggingface_hub", - content: `\ -${snippetImportInferenceClient({ accessToken, provider })} - -result = client.${HFH_INFERENCE_CLIENT_METHODS[model.pipeline_tag]}( - inputs=${getModelInputSnippet(model)}, - model="${model.id}", -) - -print(result) -`, + content: snippetInferenceClientBasic({ + inputs: getModelInputSnippet(model), + modelId: model.id, + importInferenceClient: snippetImportInferenceClient({ accessToken, provider }), + methodName: HFH_INFERENCE_CLIENT_METHODS[model.pipeline_tag], + }), }, ] : []), { client: "requests", - content: `\ -def query(payload): - response = requests.post(API_URL, headers=headers, json=payload) - return response.json() - -output = query({ - "inputs": ${getModelInputSnippet(model)}, -})`, + content: snippetRequestsBasic({ + inputs: getModelInputSnippet(model), + }), }, ]; }; @@ -235,14 +235,9 @@ 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)})`, + content: snippetRequestsBasicFile({ + inputs: getModelInputSnippet(model), + }), }, ]; }; @@ -256,30 +251,20 @@ const snippetTextToImage = ( return [ { client: "huggingface_hub", - content: `\ -${snippetImportInferenceClient({ accessToken, provider })} - -# output is a PIL.Image object -image = client.text_to_image( - ${getModelInputSnippet(model)}, - model="${model.id}", -)`, + content: snippetInferenceClientTextToImage({ + importInferenceClient: snippetImportInferenceClient({ accessToken, provider }), + inputs: getModelInputSnippet(model), + modelId: 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) -`, + content: snippetFalAITextToImage({ + modelId: providerModelId ?? model.id, + inputs: getModelInputSnippet(model), + }), }, ] : []), diff --git a/packages/inference/src/snippets/templates/python/fal_ai/textToImage.hbs b/packages/inference/src/snippets/templates/python/fal_ai/textToImage.hbs new file mode 100644 index 0000000000..aa18c60ff4 --- /dev/null +++ b/packages/inference/src/snippets/templates/python/fal_ai/textToImage.hbs @@ -0,0 +1,9 @@ +import fal_client + +result = fal_client.subscribe( + "{{{modelId}}}", + arguments={ + "prompt": {{{inputs}}}, + }, +) +print(result) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/huggingface_hub/basic.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/basic.hbs new file mode 100644 index 0000000000..bc76066a0f --- /dev/null +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/basic.hbs @@ -0,0 +1,8 @@ +{{{importInferenceClient}}} + +result = client.{{{methodName}}}( + inputs={{{inputs}}}, + model="{{{modelId}}}", +) + +print(result) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/huggingface_hub/textToImage.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/textToImage.hbs new file mode 100644 index 0000000000..2d2182459f --- /dev/null +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/textToImage.hbs @@ -0,0 +1,7 @@ +{{{importInferenceClient}}} + +# output is a PIL.Image object +image = client.text_to_image( + {{{inputs}}}, + model="{{{modelId}}}", +) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/basic.hbs b/packages/inference/src/snippets/templates/python/requests/basic.hbs new file mode 100644 index 0000000000..7b596b74bf --- /dev/null +++ b/packages/inference/src/snippets/templates/python/requests/basic.hbs @@ -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.hbs b/packages/inference/src/snippets/templates/python/requests/basicFile.hbs new file mode 100644 index 0000000000..456b237b60 --- /dev/null +++ b/packages/inference/src/snippets/templates/python/requests/basicFile.hbs @@ -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 From 7090adc276738ef2953054eab7a86408118f3086 Mon Sep 17 00:00:00 2001 From: Wauplin Date: Thu, 6 Mar 2025 15:34:33 +0100 Subject: [PATCH 07/26] all python snippets as templates + some examples --- packages/inference/scripts/tmp_gen.ts | 22 ++ .../0.huggingface_hub.hf-inference.py | 1 + .../0.huggingface_hub.hf-inference.py | 1 + .../0.huggingface_hub.hf-inference.py | 1 + .../tabular/0.requests.hf-inference.py | 14 ++ .../0.requests.hf-inference.py | 15 ++ packages/inference/src/snippets/python.ts | 205 +++++++----------- .../automaticSpeechRecognition.hbs | 1 + .../python/huggingface_hub/basic.hbs | 2 - .../python/huggingface_hub/conversational.hbs | 2 - .../huggingface_hub/conversationalStream.hbs | 2 - .../documentQuestionAnswering.hbs | 5 + .../python/huggingface_hub/imageToImage.hbs | 6 + .../python/huggingface_hub/textToImage.hbs | 2 - .../python/huggingface_hub/textToVideo.hbs | 4 + .../requests/documentQuestionAnswering.hbs | 10 + .../python/requests/imageToImage.hbs | 16 ++ .../python/requests/importRequests.hbs | 7 + .../templates/python/requests/tabular.hbs | 9 + .../python/requests/textToAudioOther.hbs | 10 + .../requests/textToAudioTransformers.hbs | 10 + .../templates/python/requests/textToImage.hbs | 12 + 22 files changed, 226 insertions(+), 131 deletions(-) create mode 100644 packages/inference/snippets-fixtures/tabular/0.requests.hf-inference.py create mode 100644 packages/inference/snippets-fixtures/text-to-audio-transformers/0.requests.hf-inference.py create mode 100644 packages/inference/src/snippets/templates/python/huggingface_hub/automaticSpeechRecognition.hbs create mode 100644 packages/inference/src/snippets/templates/python/huggingface_hub/documentQuestionAnswering.hbs create mode 100644 packages/inference/src/snippets/templates/python/huggingface_hub/imageToImage.hbs create mode 100644 packages/inference/src/snippets/templates/python/huggingface_hub/textToVideo.hbs create mode 100644 packages/inference/src/snippets/templates/python/requests/documentQuestionAnswering.hbs create mode 100644 packages/inference/src/snippets/templates/python/requests/imageToImage.hbs create mode 100644 packages/inference/src/snippets/templates/python/requests/importRequests.hbs create mode 100644 packages/inference/src/snippets/templates/python/requests/tabular.hbs create mode 100644 packages/inference/src/snippets/templates/python/requests/textToAudioOther.hbs create mode 100644 packages/inference/src/snippets/templates/python/requests/textToAudioTransformers.hbs create mode 100644 packages/inference/src/snippets/templates/python/requests/textToImage.hbs diff --git a/packages/inference/scripts/tmp_gen.ts b/packages/inference/scripts/tmp_gen.ts index 3b7dd1e87b..c9051a2c57 100644 --- a/packages/inference/scripts/tmp_gen.ts +++ b/packages/inference/scripts/tmp_gen.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: { diff --git a/packages/inference/snippets-fixtures/automatic-speech-recognition/0.huggingface_hub.hf-inference.py b/packages/inference/snippets-fixtures/automatic-speech-recognition/0.huggingface_hub.hf-inference.py index 9a945fcf65..fa9955a05b 100644 --- a/packages/inference/snippets-fixtures/automatic-speech-recognition/0.huggingface_hub.hf-inference.py +++ b/packages/inference/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/inference/snippets-fixtures/document-question-answering/0.huggingface_hub.hf-inference.py b/packages/inference/snippets-fixtures/document-question-answering/0.huggingface_hub.hf-inference.py index 46e437c63a..6f46b93654 100644 --- a/packages/inference/snippets-fixtures/document-question-answering/0.huggingface_hub.hf-inference.py +++ b/packages/inference/snippets-fixtures/document-question-answering/0.huggingface_hub.hf-inference.py @@ -4,6 +4,7 @@ provider="hf-inference", api_key="api_token", ) + output = client.document_question_answering( "cat.png", question="What is in this image?", diff --git a/packages/inference/snippets-fixtures/image-to-image/0.huggingface_hub.hf-inference.py b/packages/inference/snippets-fixtures/image-to-image/0.huggingface_hub.hf-inference.py index c21949966a..dc89746110 100644 --- a/packages/inference/snippets-fixtures/image-to-image/0.huggingface_hub.hf-inference.py +++ b/packages/inference/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/inference/snippets-fixtures/tabular/0.requests.hf-inference.py b/packages/inference/snippets-fixtures/tabular/0.requests.hf-inference.py new file mode 100644 index 0000000000..cdf1ec4db8 --- /dev/null +++ b/packages/inference/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/inference/snippets-fixtures/text-to-audio-transformers/0.requests.hf-inference.py b/packages/inference/snippets-fixtures/text-to-audio-transformers/0.requests.hf-inference.py new file mode 100644 index 0000000000..40add8d37e --- /dev/null +++ b/packages/inference/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/inference/src/snippets/python.ts b/packages/inference/src/snippets/python.ts index 3a958e93e6..4b5b8c42f0 100644 --- a/packages/inference/src/snippets/python.ts +++ b/packages/inference/src/snippets/python.ts @@ -24,6 +24,8 @@ interface TemplateParams { methodName?: string; modelId?: string; provider?: InferenceProvider; + question?: string; + importBase64?: boolean; } const rootDirFinder = (): string => { @@ -85,18 +87,37 @@ const HFH_INFERENCE_CLIENT_METHODS: Partial> = { }; const snippetImportInferenceClient = loadTemplate("python", "huggingface_hub", "importInferenceClient"); +const snippetInferenceClientAutomaticSpeechRecognition = loadTemplate( + "python", + "huggingface_hub", + "automaticSpeechRecognition" +); const snippetInferenceClientBasic = loadTemplate("python", "huggingface_hub", "basic"); +const snippetInferenceClientImageToImage = loadTemplate("python", "huggingface_hub", "imageToImage"); const snippetInferenceClientTextToImage = loadTemplate("python", "huggingface_hub", "textToImage"); +const snippetInferenceClientTextToVideo = loadTemplate("python", "huggingface_hub", "textToVideo"); const snippetInferenceClientConversational = loadTemplate("python", "huggingface_hub", "conversational"); const snippetInferenceClientConversationalStream = loadTemplate("python", "huggingface_hub", "conversationalStream"); +const snippetInferenceClientDocumentQuestionAnswering = loadTemplate( + "python", + "huggingface_hub", + "documentQuestionAnswering" +); const snippetFalAITextToImage = loadTemplate("python", "fal_ai", "textToImage"); const snippetOpenAIConversational = loadTemplate("python", "openai", "conversational"); const snippetOpenAIConversationalStream = loadTemplate("python", "openai", "conversationalStream"); +const snippetImportRequests = loadTemplate("python", "requests", "importRequests"); const snippetRequestsBasic = loadTemplate("python", "requests", "basic"); const snippetRequestsBasicFile = loadTemplate("python", "requests", "basicFile"); +const snippetRequestsDocumentQuestionAnswering = loadTemplate("python", "requests", "documentQuestionAnswering"); +const snippetRequestsImageToImage = loadTemplate("python", "requests", "imageToImage"); +const snippetRequestsTabular = loadTemplate("python", "requests", "tabular"); +const snippetRequestsTextToImage = loadTemplate("python", "requests", "textToImage"); +const snippetRequestsTextToAudioOther = loadTemplate("python", "requests", "textToAudioOther"); +const snippetRequestsTextToAudioTransformers = loadTemplate("python", "requests", "textToAudioTransformers"); const snippetRequestZeroShotClassification = loadTemplate("python", "requests", "zeroShotClassification"); const snippetRequestZeroShotImageClassification = loadTemplate("python", "requests", "zeroShotImageClassification"); @@ -136,7 +157,6 @@ const snippetConversational = ( accessToken, provider, modelId: model.id, - importInferenceClient: snippetImportInferenceClient({ accessToken, provider }), messagesStr, configStr, }), @@ -147,7 +167,6 @@ const snippetConversational = ( accessToken, provider, modelId: providerModelId ?? model.id, - importInferenceClient: snippetImportInferenceClient({ accessToken, provider }), messagesStr, configStr, }), @@ -161,7 +180,6 @@ const snippetConversational = ( accessToken, provider, modelId: model.id, - importInferenceClient: snippetImportInferenceClient({ accessToken, provider }), messagesStr, configStr, }), @@ -216,7 +234,6 @@ const snippetBasic = ( content: snippetInferenceClientBasic({ inputs: getModelInputSnippet(model), modelId: model.id, - importInferenceClient: snippetImportInferenceClient({ accessToken, provider }), methodName: HFH_INFERENCE_CLIENT_METHODS[model.pipeline_tag], }), }, @@ -252,7 +269,6 @@ const snippetTextToImage = ( { client: "huggingface_hub", content: snippetInferenceClientTextToImage({ - importInferenceClient: snippetImportInferenceClient({ accessToken, provider }), inputs: getModelInputSnippet(model), modelId: model.id, }), @@ -272,19 +288,9 @@ const snippetTextToImage = ( ? [ { 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))`, + content: snippetRequestsTextToImage({ + inputs: getModelInputSnippet(model), + }), }, ] : []), @@ -300,13 +306,10 @@ const snippetTextToVideo = ( ? [ { client: "huggingface_hub", - content: `\ -${snippetImportInferenceClient({ accessToken, provider })} - -video = client.text_to_video( - ${getModelInputSnippet(model)}, - model="${model.id}", -)`, + content: snippetInferenceClientTextToVideo({ + inputs: getModelInputSnippet(model), + modelId: model.id, + }), }, ] : []; @@ -316,14 +319,9 @@ 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)}}, -})`, + content: snippetRequestsTabular({ + inputs: getModelInputSnippet(model), + }), }, ]; }; @@ -336,33 +334,18 @@ const snippetTextToAudio = (model: ModelDataMinimal): InferenceSnippet[] => { 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)`, + content: snippetRequestsTextToAudioTransformers({ + inputs: getModelInputSnippet(model), + }), }, ]; } 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)`, + content: snippetRequestsTextToAudioOther({ + inputs: getModelInputSnippet(model), + }), }, ]; } @@ -376,8 +359,10 @@ const snippetAutomaticSpeechRecognition = ( return [ { client: "huggingface_hub", - content: `${snippetImportInferenceClient({ accessToken, provider })} -output = client.automatic_speech_recognition(${getModelInputSnippet(model)}, model="${model.id}")`, + content: snippetInferenceClientAutomaticSpeechRecognition({ + inputs: getModelInputSnippet(model), + modelId: model.id, + }), }, snippetFile(model)[0], ]; @@ -394,25 +379,17 @@ const snippetDocumentQuestionAnswering = ( return [ { client: "huggingface_hub", - content: `${snippetImportInferenceClient({ accessToken, provider })} -output = client.document_question_answering( - "${inputsAsObj.image}", - question="${inputsAsObj.question}", - model="${model.id}", -)`, + content: snippetInferenceClientDocumentQuestionAnswering({ + inputs: inputsAsObj.image, + question: inputsAsObj.question, + modelId: 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}, -})`, + content: snippetRequestsDocumentQuestionAnswering({ + inputs: inputsAsStr, + }), }, ]; }; @@ -428,32 +405,16 @@ const snippetImageToImage = ( 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}", -)`, + content: snippetInferenceClientImageToImage({ + inputs: inputsAsObj, + modelId: 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))`, + content: snippetRequestsImageToImage({ + inputs: inputsAsObj, + }), }, ]; }; @@ -509,36 +470,34 @@ export function getPythonInferenceSnippet( 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, + modelId: model.id, + 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/huggingface_hub/automaticSpeechRecognition.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/automaticSpeechRecognition.hbs new file mode 100644 index 0000000000..d90b932590 --- /dev/null +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/automaticSpeechRecognition.hbs @@ -0,0 +1 @@ +output = client.automatic_speech_recognition({{{inputs}}}, model="{{{modelId}}}") \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/huggingface_hub/basic.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/basic.hbs index bc76066a0f..497adb2ddf 100644 --- a/packages/inference/src/snippets/templates/python/huggingface_hub/basic.hbs +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/basic.hbs @@ -1,5 +1,3 @@ -{{{importInferenceClient}}} - result = client.{{{methodName}}}( inputs={{{inputs}}}, model="{{{modelId}}}", diff --git a/packages/inference/src/snippets/templates/python/huggingface_hub/conversational.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/conversational.hbs index bb27b6a6e9..14f894abc3 100644 --- a/packages/inference/src/snippets/templates/python/huggingface_hub/conversational.hbs +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/conversational.hbs @@ -1,5 +1,3 @@ -{{{importInferenceClient}}} - messages = {{{messagesStr}}} completion = client.chat.completions.create( diff --git a/packages/inference/src/snippets/templates/python/huggingface_hub/conversationalStream.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/conversationalStream.hbs index dd6484f008..7772ebc02a 100644 --- a/packages/inference/src/snippets/templates/python/huggingface_hub/conversationalStream.hbs +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/conversationalStream.hbs @@ -1,5 +1,3 @@ -{{{importInferenceClient}}} - messages = {{{messagesStr}}} stream = client.chat.completions.create( diff --git a/packages/inference/src/snippets/templates/python/huggingface_hub/documentQuestionAnswering.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/documentQuestionAnswering.hbs new file mode 100644 index 0000000000..68be145de3 --- /dev/null +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/documentQuestionAnswering.hbs @@ -0,0 +1,5 @@ +output = client.document_question_answering( + "{{{inputs}}}", + question="{{{question}}}", + model="{{{modelId}}}", +) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/huggingface_hub/imageToImage.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/imageToImage.hbs new file mode 100644 index 0000000000..b1f8e4ffa1 --- /dev/null +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/imageToImage.hbs @@ -0,0 +1,6 @@ +# output is a PIL.Image object +image = client.image_to_image( + "{{{inputs.image}}}", + prompt="{{{inputs.prompt}}}", + model="{{{modelId}}}", +) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/huggingface_hub/textToImage.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/textToImage.hbs index 2d2182459f..aaf1970724 100644 --- a/packages/inference/src/snippets/templates/python/huggingface_hub/textToImage.hbs +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/textToImage.hbs @@ -1,5 +1,3 @@ -{{{importInferenceClient}}} - # output is a PIL.Image object image = client.text_to_image( {{{inputs}}}, diff --git a/packages/inference/src/snippets/templates/python/huggingface_hub/textToVideo.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/textToVideo.hbs new file mode 100644 index 0000000000..fa8771ce43 --- /dev/null +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/textToVideo.hbs @@ -0,0 +1,4 @@ +video = client.text_to_video( + {{{inputs}}}, + model="{{{modelId}}}", +) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/documentQuestionAnswering.hbs b/packages/inference/src/snippets/templates/python/requests/documentQuestionAnswering.hbs new file mode 100644 index 0000000000..2693cd2784 --- /dev/null +++ b/packages/inference/src/snippets/templates/python/requests/documentQuestionAnswering.hbs @@ -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}}}, +}) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/imageToImage.hbs b/packages/inference/src/snippets/templates/python/requests/imageToImage.hbs new file mode 100644 index 0000000000..84a49d2fad --- /dev/null +++ b/packages/inference/src/snippets/templates/python/requests/imageToImage.hbs @@ -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.hbs b/packages/inference/src/snippets/templates/python/requests/importRequests.hbs new file mode 100644 index 0000000000..4dc29c6b2e --- /dev/null +++ b/packages/inference/src/snippets/templates/python/requests/importRequests.hbs @@ -0,0 +1,7 @@ +{{#if importBase64}} +import base64 +{{/if}} +import requests + +API_URL = "https://router.huggingface.co/{{{provider}}}/models/{{{modelId}}}" +headers = {"Authorization": "Bearer {{{accessToken}}}"} \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/tabular.hbs b/packages/inference/src/snippets/templates/python/requests/tabular.hbs new file mode 100644 index 0000000000..9656877996 --- /dev/null +++ b/packages/inference/src/snippets/templates/python/requests/tabular.hbs @@ -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/textToAudioOther.hbs b/packages/inference/src/snippets/templates/python/requests/textToAudioOther.hbs new file mode 100644 index 0000000000..8694e6ff47 --- /dev/null +++ b/packages/inference/src/snippets/templates/python/requests/textToAudioOther.hbs @@ -0,0 +1,10 @@ +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) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/textToAudioTransformers.hbs b/packages/inference/src/snippets/templates/python/requests/textToAudioTransformers.hbs new file mode 100644 index 0000000000..e098c43e6c --- /dev/null +++ b/packages/inference/src/snippets/templates/python/requests/textToAudioTransformers.hbs @@ -0,0 +1,10 @@ +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) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/textToImage.hbs b/packages/inference/src/snippets/templates/python/requests/textToImage.hbs new file mode 100644 index 0000000000..e853c77df4 --- /dev/null +++ b/packages/inference/src/snippets/templates/python/requests/textToImage.hbs @@ -0,0 +1,12 @@ +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)) \ No newline at end of file From 134fa2d4310c41199927cad0fa8404a63decaec5 Mon Sep 17 00:00:00 2001 From: Wauplin Date: Thu, 6 Mar 2025 16:14:43 +0100 Subject: [PATCH 08/26] base url for openai snippets --- .../1.openai.hf-inference.py | 2 +- .../1.openai.together.py | 2 +- .../1.openai.fireworks-ai.py | 2 +- .../1.openai.hf-inference.py | 2 +- packages/inference/src/snippets/python.ts | 25 ++++--------------- 5 files changed, 9 insertions(+), 24 deletions(-) diff --git a/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.py b/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.py index 1c77a5a3a0..1668a4bb88 100644 --- a/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.py +++ b/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.py @@ -1,7 +1,7 @@ from openai import OpenAI client = OpenAI( - base_url="", + base_url="https://router.huggingface.co/hf-inference/v1", api_key="api_token" ) diff --git a/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.together.py b/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.together.py index 29765655bc..84681fa62e 100644 --- a/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.together.py +++ b/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.together.py @@ -1,7 +1,7 @@ from openai import OpenAI client = OpenAI( - base_url="", + base_url="https://router.huggingface.co/together", api_key="api_token" ) diff --git a/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.py b/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.py index 40c29b5fd9..a49d598d8f 100644 --- a/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.py +++ b/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.py @@ -1,7 +1,7 @@ from openai import OpenAI client = OpenAI( - base_url="", + base_url="https://router.huggingface.co/fireworks-ai", api_key="api_token" ) diff --git a/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.py b/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.py index 9a484124e4..47874aaff1 100644 --- a/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.py +++ b/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.py @@ -1,7 +1,7 @@ from openai import OpenAI client = OpenAI( - base_url="", + base_url="https://router.huggingface.co/hf-inference/v1", api_key="api_token" ) diff --git a/packages/inference/src/snippets/python.ts b/packages/inference/src/snippets/python.ts index 4b5b8c42f0..e71af66adf 100644 --- a/packages/inference/src/snippets/python.ts +++ b/packages/inference/src/snippets/python.ts @@ -169,6 +169,7 @@ const snippetConversational = ( modelId: providerModelId ?? model.id, messagesStr, configStr, + baseUrl: openAIbaseUrl(provider), }), }, ]; @@ -221,11 +222,7 @@ const snippetZeroShotImageClassification = (model: ModelDataMinimal): InferenceS ]; }; -const snippetBasic = ( - model: ModelDataMinimal, - accessToken: string, - provider: InferenceProvider -): InferenceSnippet[] => { +const snippetBasic = (model: ModelDataMinimal): InferenceSnippet[] => { return [ ...(model.pipeline_tag && model.pipeline_tag in HFH_INFERENCE_CLIENT_METHODS ? [ @@ -351,11 +348,7 @@ const snippetTextToAudio = (model: ModelDataMinimal): InferenceSnippet[] => { } }; -const snippetAutomaticSpeechRecognition = ( - model: ModelDataMinimal, - accessToken: string, - provider: InferenceProvider -): InferenceSnippet[] => { +const snippetAutomaticSpeechRecognition = (model: ModelDataMinimal): InferenceSnippet[] => { return [ { client: "huggingface_hub", @@ -368,11 +361,7 @@ const snippetAutomaticSpeechRecognition = ( ]; }; -const snippetDocumentQuestionAnswering = ( - model: ModelDataMinimal, - accessToken: string, - provider: InferenceProvider -): InferenceSnippet[] => { +const snippetDocumentQuestionAnswering = (model: ModelDataMinimal): InferenceSnippet[] => { const inputsAsStr = getModelInputSnippet(model) as string; const inputsAsObj = JSON.parse(inputsAsStr); @@ -394,11 +383,7 @@ const snippetDocumentQuestionAnswering = ( ]; }; -const snippetImageToImage = ( - model: ModelDataMinimal, - accessToken: string, - provider: InferenceProvider -): InferenceSnippet[] => { +const snippetImageToImage = (model: ModelDataMinimal): InferenceSnippet[] => { const inputsAsStr = getModelInputSnippet(model) as string; const inputsAsObj = JSON.parse(inputsAsStr); From d07032e1c81feb3f62812b894f826e82d44b4353 Mon Sep 17 00:00:00 2001 From: Wauplin Date: Thu, 6 Mar 2025 16:59:30 +0100 Subject: [PATCH 09/26] start refacto --- packages/inference/src/snippets/python.ts | 171 ++++++++---------- .../python/huggingface_hub/conversational.hbs | 4 +- .../huggingface_hub/conversationalStream.hbs | 4 +- .../documentQuestionAnswering.hbs | 4 +- .../python/openai/conversational.hbs | 6 +- .../python/openai/conversationalStream.hbs | 6 +- 6 files changed, 83 insertions(+), 112 deletions(-) diff --git a/packages/inference/src/snippets/python.ts b/packages/inference/src/snippets/python.ts index e71af66adf..f5c5d3fdc5 100644 --- a/packages/inference/src/snippets/python.ts +++ b/packages/inference/src/snippets/python.ts @@ -17,17 +17,25 @@ import { existsSync as pathExists } from "node:fs"; interface TemplateParams { accessToken?: string; baseUrl?: string; - configStr?: string; - importInferenceClient?: string; - inputs?: string | ChatCompletionInputMessage[]; - messagesStr?: string; - methodName?: string; + inputs?: string | object; modelId?: string; provider?: InferenceProvider; - question?: string; - importBase64?: boolean; + providerModelId?: string; + methodName?: string; // specific to snippetBasic + importBase64?: boolean; // specific to snippetImportRequests } +interface SnippetTemplateParams { + accessToken: string; + baseUrl: string; + inputs: string | object; + modelId: string; + provider: InferenceProvider; + providerModelId: string; +} + +const TOOLS = ["huggingface_hub", "requests", "fal_ai", "openai"]; + const rootDirFinder = (): string => { let currentPath = path.normalize(import.meta.url).replace("file:", ""); @@ -42,16 +50,19 @@ const rootDirFinder = (): string => { return "/"; }; -const loadTemplate = (language: string, tool: string, templateName: string): ((data: TemplateParams) => string) => { +const loadTemplate = (tool: string, templateName: string): ((data: TemplateParams) => string) | undefined => { const templatePath = path.join( rootDirFinder(), "src", "snippets", "templates", - language, + "python", tool, `${templateName}.hbs` ); + if (!pathExists(templatePath)) { + return; + } const template = fs.readFileSync(templatePath, "utf8"); return Handlebars.compile(template); }; @@ -86,40 +97,40 @@ const HFH_INFERENCE_CLIENT_METHODS: Partial> = { "tabular-regression": "tabular_regression", }; -const snippetImportInferenceClient = loadTemplate("python", "huggingface_hub", "importInferenceClient"); -const snippetInferenceClientAutomaticSpeechRecognition = loadTemplate( - "python", - "huggingface_hub", - "automaticSpeechRecognition" -); -const snippetInferenceClientBasic = loadTemplate("python", "huggingface_hub", "basic"); -const snippetInferenceClientImageToImage = loadTemplate("python", "huggingface_hub", "imageToImage"); -const snippetInferenceClientTextToImage = loadTemplate("python", "huggingface_hub", "textToImage"); -const snippetInferenceClientTextToVideo = loadTemplate("python", "huggingface_hub", "textToVideo"); -const snippetInferenceClientConversational = loadTemplate("python", "huggingface_hub", "conversational"); -const snippetInferenceClientConversationalStream = loadTemplate("python", "huggingface_hub", "conversationalStream"); -const snippetInferenceClientDocumentQuestionAnswering = loadTemplate( - "python", - "huggingface_hub", - "documentQuestionAnswering" -); - -const snippetFalAITextToImage = loadTemplate("python", "fal_ai", "textToImage"); - -const snippetOpenAIConversational = loadTemplate("python", "openai", "conversational"); -const snippetOpenAIConversationalStream = loadTemplate("python", "openai", "conversationalStream"); - -const snippetImportRequests = loadTemplate("python", "requests", "importRequests"); -const snippetRequestsBasic = loadTemplate("python", "requests", "basic"); -const snippetRequestsBasicFile = loadTemplate("python", "requests", "basicFile"); -const snippetRequestsDocumentQuestionAnswering = loadTemplate("python", "requests", "documentQuestionAnswering"); -const snippetRequestsImageToImage = loadTemplate("python", "requests", "imageToImage"); -const snippetRequestsTabular = loadTemplate("python", "requests", "tabular"); -const snippetRequestsTextToImage = loadTemplate("python", "requests", "textToImage"); -const snippetRequestsTextToAudioOther = loadTemplate("python", "requests", "textToAudioOther"); -const snippetRequestsTextToAudioTransformers = loadTemplate("python", "requests", "textToAudioTransformers"); -const snippetRequestZeroShotClassification = loadTemplate("python", "requests", "zeroShotClassification"); -const snippetRequestZeroShotImageClassification = loadTemplate("python", "requests", "zeroShotImageClassification"); +const snippetImportInferenceClient = loadTemplate("huggingface_hub", "importInferenceClient"); +const snippetInferenceClientAutomaticSpeechRecognition = loadTemplate("huggingface_hub", "automaticSpeechRecognition"); +const snippetInferenceClientBasic = loadTemplate("huggingface_hub", "basic"); +const snippetInferenceClientImageToImage = loadTemplate("huggingface_hub", "imageToImage"); +const snippetInferenceClientTextToImage = loadTemplate("huggingface_hub", "textToImage"); +const snippetInferenceClientTextToVideo = loadTemplate("huggingface_hub", "textToVideo"); +const snippetInferenceClientDocumentQuestionAnswering = loadTemplate("huggingface_hub", "documentQuestionAnswering"); + +const snippetFalAITextToImage = loadTemplate("fal_ai", "textToImage"); + +const snippetImportRequests = loadTemplate("requests", "importRequests"); +const snippetRequestsBasic = loadTemplate("requests", "basic"); +const snippetRequestsBasicFile = loadTemplate("requests", "basicFile"); +const snippetRequestsDocumentQuestionAnswering = loadTemplate("requests", "documentQuestionAnswering"); +const snippetRequestsImageToImage = loadTemplate("requests", "imageToImage"); +const snippetRequestsTabular = loadTemplate("requests", "tabular"); +const snippetRequestsTextToImage = loadTemplate("requests", "textToImage"); +const snippetRequestsTextToAudioOther = loadTemplate("requests", "textToAudioOther"); +const snippetRequestsTextToAudioTransformers = loadTemplate("requests", "textToAudioTransformers"); +const snippetRequestZeroShotClassification = loadTemplate("requests", "zeroShotClassification"); +const snippetRequestZeroShotImageClassification = loadTemplate("requests", "zeroShotImageClassification"); + +const generateSnippets = (templateName: string, params: SnippetTemplateParams): InferenceSnippet[] => { + return TOOLS.map((tool) => { + const template = loadTemplate(tool, templateName); + if (!template) { + return; + } + return { + client: tool, + content: template(params), + }; + }).filter((snippet) => snippet !== undefined) as InferenceSnippet[]; +}; const snippetConversational = ( model: ModelDataMinimal, @@ -137,67 +148,28 @@ const snippetConversational = ( const streaming = opts?.streaming ?? true; 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), max_tokens: opts?.max_tokens ?? 500, ...(opts?.top_p ? { top_p: opts.top_p } : undefined), }; - const configStr = stringifyGenerationConfig(config, { - indent: "\n\t", - attributeValueConnector: "=", - }); - if (streaming) { - return [ - { - client: "huggingface_hub", - content: snippetInferenceClientConversationalStream({ - accessToken, - provider, - modelId: model.id, - messagesStr, - configStr, - }), - }, - { - client: "openai", - content: snippetOpenAIConversationalStream({ - accessToken, - provider, - modelId: providerModelId ?? model.id, - messagesStr, - configStr, - baseUrl: openAIbaseUrl(provider), - }), - }, - ]; - } else { - return [ - { - client: "huggingface_hub", - content: snippetInferenceClientConversational({ - accessToken, - provider, - modelId: model.id, - messagesStr, - configStr, - }), - }, - { - client: "openai", - content: snippetOpenAIConversational({ - accessToken, - provider, - modelId: providerModelId ?? model.id, - messagesStr, - configStr, - baseUrl: openAIbaseUrl(provider), - }), - }, - ]; - } + const params: SnippetTemplateParams = { + accessToken, + baseUrl: openAIbaseUrl(provider), + inputs: { + messagesStr: stringifyMessages(messages, { attributeKeyQuotes: true }), + configStr: stringifyGenerationConfig(config, { + indent: "\n\t", + attributeValueConnector: "=", + }), + }, + modelId: model.id, + provider, + providerModelId: providerModelId ?? model.id, + }; + const templateName = streaming ? "conversationalStream" : "conversational"; + return generateSnippets(templateName, params); }; const snippetZeroShotClassification = (model: ModelDataMinimal): InferenceSnippet[] => { @@ -369,8 +341,7 @@ const snippetDocumentQuestionAnswering = (model: ModelDataMinimal): InferenceSni { client: "huggingface_hub", content: snippetInferenceClientDocumentQuestionAnswering({ - inputs: inputsAsObj.image, - question: inputsAsObj.question, + inputs: inputsAsObj, modelId: model.id, }), }, diff --git a/packages/inference/src/snippets/templates/python/huggingface_hub/conversational.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/conversational.hbs index 14f894abc3..f086035fa5 100644 --- a/packages/inference/src/snippets/templates/python/huggingface_hub/conversational.hbs +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/conversational.hbs @@ -1,9 +1,9 @@ -messages = {{{messagesStr}}} +messages = {{{inputs.messagesStr}}} completion = client.chat.completions.create( model="{{{modelId}}}", messages=messages, - {{{configStr}}} + {{{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.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/conversationalStream.hbs index 7772ebc02a..22d628bb30 100644 --- a/packages/inference/src/snippets/templates/python/huggingface_hub/conversationalStream.hbs +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/conversationalStream.hbs @@ -1,9 +1,9 @@ -messages = {{{messagesStr}}} +messages = {{{inputs.messagesStr}}} stream = client.chat.completions.create( model="{{{modelId}}}", messages=messages, - {{{configStr}}} + {{{inputs.configStr}}} stream=True, ) diff --git a/packages/inference/src/snippets/templates/python/huggingface_hub/documentQuestionAnswering.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/documentQuestionAnswering.hbs index 68be145de3..1c2ca6cdc0 100644 --- a/packages/inference/src/snippets/templates/python/huggingface_hub/documentQuestionAnswering.hbs +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/documentQuestionAnswering.hbs @@ -1,5 +1,5 @@ output = client.document_question_answering( - "{{{inputs}}}", - question="{{{question}}}", + "{{{inputs.image}}}", + question="{{{inputs.question}}}", model="{{{modelId}}}", ) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/openai/conversational.hbs b/packages/inference/src/snippets/templates/python/openai/conversational.hbs index 0580ab51f8..d2a0d70e40 100644 --- a/packages/inference/src/snippets/templates/python/openai/conversational.hbs +++ b/packages/inference/src/snippets/templates/python/openai/conversational.hbs @@ -5,12 +5,12 @@ client = OpenAI( api_key="{{{accessToken}}}" ) -messages = {{{messagesStr}}} +messages = {{{inputs.messagesStr}}} completion = client.chat.completions.create( - model="{{{modelId}}}", + model="{{{providerModelId}}}", messages=messages, - {{{configStr}}} + {{{inputs.configStr}}} ) print(completion.choices[0].message) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/openai/conversationalStream.hbs b/packages/inference/src/snippets/templates/python/openai/conversationalStream.hbs index 2381f51d50..d6a4eb1072 100644 --- a/packages/inference/src/snippets/templates/python/openai/conversationalStream.hbs +++ b/packages/inference/src/snippets/templates/python/openai/conversationalStream.hbs @@ -5,12 +5,12 @@ client = OpenAI( api_key="{{{accessToken}}}" ) -messages = {{{messagesStr}}} +messages = {{{inputs.messagesStr}}} stream = client.chat.completions.create( - model="{{{modelId}}}", + model="{{{providerModelId}}}", messages=messages, - {{{configStr}}} + {{{inputs.configStr}}} stream=True ) From 2b953765523b144ed58fff42b8f602f92f8b4808 Mon Sep 17 00:00:00 2001 From: Wauplin Date: Thu, 6 Mar 2025 18:04:25 +0100 Subject: [PATCH 10/26] closer to a good version --- .../0.requests.hf-inference.py | 2 +- ...lient.fal-ai.py => 1.fal_client.fal-ai.py} | 2 +- .../text-to-image/1.requests.hf-inference.py | 2 +- packages/inference/src/snippets/python.ts | 344 ++++++++---------- .../{fal_ai => fal_client}/textToImage.hbs | 6 +- .../automaticSpeechRecognition.hbs | 2 +- .../python/huggingface_hub/basic.hbs | 2 +- .../python/huggingface_hub/conversational.hbs | 2 +- .../huggingface_hub/conversationalStream.hbs | 2 +- .../documentQuestionAnswering.hbs | 6 +- .../python/huggingface_hub/imageToImage.hbs | 2 +- .../python/huggingface_hub/textToImage.hbs | 2 +- .../python/huggingface_hub/textToVideo.hbs | 2 +- .../requests/automaticSpeechRecognition.hbs | 7 + .../requests/documentQuestionAnswering.hbs | 2 +- .../templates/python/requests/textToAudio.hbs | 23 ++ .../python/requests/textToAudioOther.hbs | 10 - .../requests/textToAudioTransformers.hbs | 10 - .../templates/python/requests/textToImage.hbs | 4 +- 19 files changed, 202 insertions(+), 230 deletions(-) rename packages/inference/snippets-fixtures/text-to-image/{1.fal-client.fal-ai.py => 1.fal_client.fal-ai.py} (92%) rename packages/inference/src/snippets/templates/python/{fal_ai => fal_client}/textToImage.hbs (53%) create mode 100644 packages/inference/src/snippets/templates/python/requests/automaticSpeechRecognition.hbs create mode 100644 packages/inference/src/snippets/templates/python/requests/textToAudio.hbs delete mode 100644 packages/inference/src/snippets/templates/python/requests/textToAudioOther.hbs delete mode 100644 packages/inference/src/snippets/templates/python/requests/textToAudioTransformers.hbs diff --git a/packages/inference/snippets-fixtures/text-to-audio-transformers/0.requests.hf-inference.py b/packages/inference/snippets-fixtures/text-to-audio-transformers/0.requests.hf-inference.py index 40add8d37e..abeccceee0 100644 --- a/packages/inference/snippets-fixtures/text-to-audio-transformers/0.requests.hf-inference.py +++ b/packages/inference/snippets-fixtures/text-to-audio-transformers/0.requests.hf-inference.py @@ -12,4 +12,4 @@ def query(payload): }) # 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 +Audio(audio, rate=sampling_rate) diff --git a/packages/inference/snippets-fixtures/text-to-image/1.fal-client.fal-ai.py b/packages/inference/snippets-fixtures/text-to-image/1.fal_client.fal-ai.py similarity index 92% rename from packages/inference/snippets-fixtures/text-to-image/1.fal-client.fal-ai.py rename to packages/inference/snippets-fixtures/text-to-image/1.fal_client.fal-ai.py index 177c07bb8f..36220f1057 100644 --- a/packages/inference/snippets-fixtures/text-to-image/1.fal-client.fal-ai.py +++ b/packages/inference/snippets-fixtures/text-to-image/1.fal_client.fal-ai.py @@ -6,4 +6,4 @@ "prompt": "Astronaut riding a horse", }, ) -print(result) \ No newline at end of file +print(result) diff --git a/packages/inference/snippets-fixtures/text-to-image/1.requests.hf-inference.py b/packages/inference/snippets-fixtures/text-to-image/1.requests.hf-inference.py index a8e1ad9de0..83f8c0175f 100644 --- a/packages/inference/snippets-fixtures/text-to-image/1.requests.hf-inference.py +++ b/packages/inference/snippets-fixtures/text-to-image/1.requests.hf-inference.py @@ -14,4 +14,4 @@ def query(payload): # 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 +image = Image.open(io.BytesIO(image_bytes)) diff --git a/packages/inference/src/snippets/python.ts b/packages/inference/src/snippets/python.ts index f5c5d3fdc5..2f7613eb14 100644 --- a/packages/inference/src/snippets/python.ts +++ b/packages/inference/src/snippets/python.ts @@ -27,14 +27,18 @@ interface TemplateParams { interface SnippetTemplateParams { accessToken: string; - baseUrl: string; + baseUrl?: string; inputs: string | object; - modelId: string; + model: ModelDataMinimal; provider: InferenceProvider; providerModelId: string; } -const TOOLS = ["huggingface_hub", "requests", "fal_ai", "openai"]; +const TOOLS = ["huggingface_hub", "requests", "fal_client", "openai"]; + +Handlebars.registerHelper("equals", function (value1, value2) { + return value1 === value2; +}); const rootDirFinder = (): string => { let currentPath = path.normalize(import.meta.url).replace("file:", ""); @@ -98,26 +102,7 @@ const HFH_INFERENCE_CLIENT_METHODS: Partial> = { }; const snippetImportInferenceClient = loadTemplate("huggingface_hub", "importInferenceClient"); -const snippetInferenceClientAutomaticSpeechRecognition = loadTemplate("huggingface_hub", "automaticSpeechRecognition"); -const snippetInferenceClientBasic = loadTemplate("huggingface_hub", "basic"); -const snippetInferenceClientImageToImage = loadTemplate("huggingface_hub", "imageToImage"); -const snippetInferenceClientTextToImage = loadTemplate("huggingface_hub", "textToImage"); -const snippetInferenceClientTextToVideo = loadTemplate("huggingface_hub", "textToVideo"); -const snippetInferenceClientDocumentQuestionAnswering = loadTemplate("huggingface_hub", "documentQuestionAnswering"); - -const snippetFalAITextToImage = loadTemplate("fal_ai", "textToImage"); - const snippetImportRequests = loadTemplate("requests", "importRequests"); -const snippetRequestsBasic = loadTemplate("requests", "basic"); -const snippetRequestsBasicFile = loadTemplate("requests", "basicFile"); -const snippetRequestsDocumentQuestionAnswering = loadTemplate("requests", "documentQuestionAnswering"); -const snippetRequestsImageToImage = loadTemplate("requests", "imageToImage"); -const snippetRequestsTabular = loadTemplate("requests", "tabular"); -const snippetRequestsTextToImage = loadTemplate("requests", "textToImage"); -const snippetRequestsTextToAudioOther = loadTemplate("requests", "textToAudioOther"); -const snippetRequestsTextToAudioTransformers = loadTemplate("requests", "textToAudioTransformers"); -const snippetRequestZeroShotClassification = loadTemplate("requests", "zeroShotClassification"); -const snippetRequestZeroShotImageClassification = loadTemplate("requests", "zeroShotImageClassification"); const generateSnippets = (templateName: string, params: SnippetTemplateParams): InferenceSnippet[] => { return TOOLS.map((tool) => { @@ -125,11 +110,20 @@ const generateSnippets = (templateName: string, params: SnippetTemplateParams): if (!template) { return; } + if (tool === "huggingface_hub" && templateName === "basic") { + if (!(params.model.pipeline_tag && params.model.pipeline_tag in HFH_INFERENCE_CLIENT_METHODS)) { + return; + } + return { + client: tool, + content: template({ ...params, methodName: HFH_INFERENCE_CLIENT_METHODS[params.model.pipeline_tag] }), + }; + } return { client: tool, content: template(params), }; - }).filter((snippet) => snippet !== undefined) as InferenceSnippet[]; + }).filter((snippet) => snippet !== undefined && snippet.content.trim()) as InferenceSnippet[]; }; const snippetConversational = ( @@ -164,7 +158,7 @@ const snippetConversational = ( attributeValueConnector: "=", }), }, - modelId: model.id, + model, provider, providerModelId: providerModelId ?? model.id, }; @@ -172,60 +166,64 @@ const snippetConversational = ( return generateSnippets(templateName, params); }; -const snippetZeroShotClassification = (model: ModelDataMinimal): InferenceSnippet[] => { - return [ - { - client: "requests", - content: snippetRequestZeroShotClassification({ - inputs: getModelInputSnippet(model), - }), - }, - ]; +const snippetZeroShotClassification = ( + model: ModelDataMinimal, + accessToken: string, + provider: InferenceProvider, + providerModelId?: string +): InferenceSnippet[] => { + return generateSnippets("zeroShotClassification", { + accessToken: accessToken, + inputs: getModelInputSnippet(model), + model: model, + provider: provider, + providerModelId: providerModelId ?? model.id, + }); }; -const snippetZeroShotImageClassification = (model: ModelDataMinimal): InferenceSnippet[] => { - return [ - { - client: "requests", - content: snippetRequestZeroShotImageClassification({ - inputs: getModelInputSnippet(model), - }), - }, - ]; +const snippetZeroShotImageClassification = ( + model: ModelDataMinimal, + accessToken: string, + provider: InferenceProvider, + providerModelId?: string +): InferenceSnippet[] => { + return generateSnippets("zeroShotImageClassification", { + accessToken: accessToken, + inputs: getModelInputSnippet(model), + model: model, + provider: provider, + providerModelId: providerModelId ?? model.id, + }); }; -const snippetBasic = (model: ModelDataMinimal): InferenceSnippet[] => { - return [ - ...(model.pipeline_tag && model.pipeline_tag in HFH_INFERENCE_CLIENT_METHODS - ? [ - { - client: "huggingface_hub", - content: snippetInferenceClientBasic({ - inputs: getModelInputSnippet(model), - modelId: model.id, - methodName: HFH_INFERENCE_CLIENT_METHODS[model.pipeline_tag], - }), - }, - ] - : []), - { - client: "requests", - content: snippetRequestsBasic({ - inputs: getModelInputSnippet(model), - }), - }, - ]; +const snippetBasic = ( + model: ModelDataMinimal, + accessToken: string, + provider: InferenceProvider, + providerModelId?: string +): InferenceSnippet[] => { + return generateSnippets("basic", { + accessToken: accessToken, + inputs: getModelInputSnippet(model), + model: model, + provider: provider, + providerModelId: providerModelId ?? model.id, + }); }; -const snippetFile = (model: ModelDataMinimal): InferenceSnippet[] => { - return [ - { - client: "requests", - content: snippetRequestsBasicFile({ - inputs: getModelInputSnippet(model), - }), - }, - ]; +const snippetFile = ( + model: ModelDataMinimal, + accessToken: string, + provider: InferenceProvider, + providerModelId?: string +): InferenceSnippet[] => { + return generateSnippets("basicFile", { + accessToken: accessToken, + inputs: getModelInputSnippet(model), + model: model, + provider: provider, + providerModelId: providerModelId ?? model.id, + }); }; const snippetTextToImage = ( @@ -234,145 +232,105 @@ const snippetTextToImage = ( provider: InferenceProvider, providerModelId?: string ): InferenceSnippet[] => { - return [ - { - client: "huggingface_hub", - content: snippetInferenceClientTextToImage({ - inputs: getModelInputSnippet(model), - modelId: model.id, - }), - }, - ...(provider === "fal-ai" - ? [ - { - client: "fal-client", - content: snippetFalAITextToImage({ - modelId: providerModelId ?? model.id, - inputs: getModelInputSnippet(model), - }), - }, - ] - : []), - ...(provider === "hf-inference" - ? [ - { - client: "requests", - content: snippetRequestsTextToImage({ - inputs: getModelInputSnippet(model), - }), - }, - ] - : []), - ]; + return generateSnippets("textToImage", { + accessToken: accessToken, + inputs: getModelInputSnippet(model), + model: model, + provider: provider, + providerModelId: providerModelId ?? model.id, + }); }; const snippetTextToVideo = ( model: ModelDataMinimal, accessToken: string, - provider: InferenceProvider + provider: InferenceProvider, + providerModelId?: string ): InferenceSnippet[] => { - return ["fal-ai", "replicate"].includes(provider) - ? [ - { - client: "huggingface_hub", - content: snippetInferenceClientTextToVideo({ - inputs: getModelInputSnippet(model), - modelId: model.id, - }), - }, - ] - : []; + return generateSnippets("textToVideo", { + accessToken: accessToken, + inputs: getModelInputSnippet(model), + model: model, + provider: provider, + providerModelId: providerModelId ?? model.id, + }); }; -const snippetTabular = (model: ModelDataMinimal): InferenceSnippet[] => { - return [ - { - client: "requests", - content: snippetRequestsTabular({ - inputs: getModelInputSnippet(model), - }), - }, - ]; +const snippetTabular = ( + model: ModelDataMinimal, + accessToken: string, + provider: InferenceProvider, + providerModelId?: string +): InferenceSnippet[] => { + return generateSnippets("tabular", { + accessToken: accessToken, + inputs: getModelInputSnippet(model), + model: model, + provider: provider, + providerModelId: providerModelId ?? model.id, + }); }; -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: snippetRequestsTextToAudioTransformers({ - inputs: getModelInputSnippet(model), - }), - }, - ]; - } else { - return [ - { - client: "requests", - content: snippetRequestsTextToAudioOther({ - inputs: getModelInputSnippet(model), - }), - }, - ]; - } +const snippetTextToAudio = ( + model: ModelDataMinimal, + accessToken: string, + provider: InferenceProvider, + providerModelId?: string +): InferenceSnippet[] => { + return generateSnippets("textToAudio", { + accessToken: accessToken, + inputs: getModelInputSnippet(model), + model: model, + provider: provider, + providerModelId: providerModelId ?? model.id, + }); }; -const snippetAutomaticSpeechRecognition = (model: ModelDataMinimal): InferenceSnippet[] => { - return [ - { - client: "huggingface_hub", - content: snippetInferenceClientAutomaticSpeechRecognition({ - inputs: getModelInputSnippet(model), - modelId: model.id, - }), - }, - snippetFile(model)[0], - ]; +const snippetAutomaticSpeechRecognition = ( + model: ModelDataMinimal, + accessToken: string, + provider: InferenceProvider, + providerModelId?: string +): InferenceSnippet[] => { + return generateSnippets("automaticSpeechRecognition", { + accessToken: accessToken, + inputs: getModelInputSnippet(model), + model: model, + provider: provider, + providerModelId: providerModelId ?? model.id, + }); }; -const snippetDocumentQuestionAnswering = (model: ModelDataMinimal): InferenceSnippet[] => { +const snippetDocumentQuestionAnswering = ( + model: ModelDataMinimal, + accessToken: string, + provider: InferenceProvider, + providerModelId?: string +): InferenceSnippet[] => { const inputsAsStr = getModelInputSnippet(model) as string; const inputsAsObj = JSON.parse(inputsAsStr); - - return [ - { - client: "huggingface_hub", - content: snippetInferenceClientDocumentQuestionAnswering({ - inputs: inputsAsObj, - modelId: model.id, - }), - }, - { - client: "requests", - content: snippetRequestsDocumentQuestionAnswering({ - inputs: inputsAsStr, - }), - }, - ]; + return generateSnippets("documentQuestionAnswering", { + accessToken: accessToken, + inputs: { asObj: inputsAsObj, asStr: inputsAsStr }, + model: model, + provider: provider, + providerModelId: providerModelId ?? model.id, + }); }; -const snippetImageToImage = (model: ModelDataMinimal): InferenceSnippet[] => { - const inputsAsStr = getModelInputSnippet(model) as string; - const inputsAsObj = JSON.parse(inputsAsStr); - - return [ - { - client: "huggingface_hub", - content: snippetInferenceClientImageToImage({ - inputs: inputsAsObj, - modelId: model.id, - }), - }, - { - client: "requests", - content: snippetRequestsImageToImage({ - inputs: inputsAsObj, - }), - }, - ]; +const snippetImageToImage = ( + model: ModelDataMinimal, + accessToken: string, + provider: InferenceProvider, + providerModelId?: string +): InferenceSnippet[] => { + return generateSnippets("imageToImage", { + accessToken: accessToken, + inputs: JSON.parse(getModelInputSnippet(model) as string), + model: model, + provider: provider, + providerModelId: providerModelId ?? model.id, + }); }; const pythonSnippets: Partial< diff --git a/packages/inference/src/snippets/templates/python/fal_ai/textToImage.hbs b/packages/inference/src/snippets/templates/python/fal_client/textToImage.hbs similarity index 53% rename from packages/inference/src/snippets/templates/python/fal_ai/textToImage.hbs rename to packages/inference/src/snippets/templates/python/fal_client/textToImage.hbs index aa18c60ff4..137d2d54d1 100644 --- a/packages/inference/src/snippets/templates/python/fal_ai/textToImage.hbs +++ b/packages/inference/src/snippets/templates/python/fal_client/textToImage.hbs @@ -1,9 +1,11 @@ +{{#if (equals provider "fal-ai")}} import fal_client result = fal_client.subscribe( - "{{{modelId}}}", + "{{{providerModelId}}}", arguments={ "prompt": {{{inputs}}}, }, ) -print(result) \ No newline at end of file +print(result) +{{/if}} \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/huggingface_hub/automaticSpeechRecognition.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/automaticSpeechRecognition.hbs index d90b932590..1bc5cae1d0 100644 --- a/packages/inference/src/snippets/templates/python/huggingface_hub/automaticSpeechRecognition.hbs +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/automaticSpeechRecognition.hbs @@ -1 +1 @@ -output = client.automatic_speech_recognition({{{inputs}}}, model="{{{modelId}}}") \ No newline at end of file +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.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/basic.hbs index 497adb2ddf..4c84144e62 100644 --- a/packages/inference/src/snippets/templates/python/huggingface_hub/basic.hbs +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/basic.hbs @@ -1,6 +1,6 @@ result = client.{{{methodName}}}( inputs={{{inputs}}}, - model="{{{modelId}}}", + model="{{{model.id}}}", ) print(result) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/huggingface_hub/conversational.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/conversational.hbs index f086035fa5..aa69fda701 100644 --- a/packages/inference/src/snippets/templates/python/huggingface_hub/conversational.hbs +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/conversational.hbs @@ -1,7 +1,7 @@ messages = {{{inputs.messagesStr}}} completion = client.chat.completions.create( - model="{{{modelId}}}", + model="{{{model.id}}}", messages=messages, {{{inputs.configStr}}} ) diff --git a/packages/inference/src/snippets/templates/python/huggingface_hub/conversationalStream.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/conversationalStream.hbs index 22d628bb30..b9f4b48ca9 100644 --- a/packages/inference/src/snippets/templates/python/huggingface_hub/conversationalStream.hbs +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/conversationalStream.hbs @@ -1,7 +1,7 @@ messages = {{{inputs.messagesStr}}} stream = client.chat.completions.create( - model="{{{modelId}}}", + model="{{{model.id}}}", messages=messages, {{{inputs.configStr}}} stream=True, diff --git a/packages/inference/src/snippets/templates/python/huggingface_hub/documentQuestionAnswering.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/documentQuestionAnswering.hbs index 1c2ca6cdc0..ed5bae39be 100644 --- a/packages/inference/src/snippets/templates/python/huggingface_hub/documentQuestionAnswering.hbs +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/documentQuestionAnswering.hbs @@ -1,5 +1,5 @@ output = client.document_question_answering( - "{{{inputs.image}}}", - question="{{{inputs.question}}}", - model="{{{modelId}}}", + "{{{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.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/imageToImage.hbs index b1f8e4ffa1..1ca023b89f 100644 --- a/packages/inference/src/snippets/templates/python/huggingface_hub/imageToImage.hbs +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/imageToImage.hbs @@ -2,5 +2,5 @@ image = client.image_to_image( "{{{inputs.image}}}", prompt="{{{inputs.prompt}}}", - model="{{{modelId}}}", + model="{{{model.id}}}", ) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/huggingface_hub/textToImage.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/textToImage.hbs index aaf1970724..eed729a3a1 100644 --- a/packages/inference/src/snippets/templates/python/huggingface_hub/textToImage.hbs +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/textToImage.hbs @@ -1,5 +1,5 @@ # output is a PIL.Image object image = client.text_to_image( {{{inputs}}}, - model="{{{modelId}}}", + model="{{{model.id}}}", ) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/huggingface_hub/textToVideo.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/textToVideo.hbs index fa8771ce43..dc78306b07 100644 --- a/packages/inference/src/snippets/templates/python/huggingface_hub/textToVideo.hbs +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/textToVideo.hbs @@ -1,4 +1,4 @@ video = client.text_to_video( {{{inputs}}}, - model="{{{modelId}}}", + model="{{{model.id}}}", ) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/automaticSpeechRecognition.hbs b/packages/inference/src/snippets/templates/python/requests/automaticSpeechRecognition.hbs new file mode 100644 index 0000000000..456b237b60 --- /dev/null +++ b/packages/inference/src/snippets/templates/python/requests/automaticSpeechRecognition.hbs @@ -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.hbs b/packages/inference/src/snippets/templates/python/requests/documentQuestionAnswering.hbs index 2693cd2784..9bc82c6640 100644 --- a/packages/inference/src/snippets/templates/python/requests/documentQuestionAnswering.hbs +++ b/packages/inference/src/snippets/templates/python/requests/documentQuestionAnswering.hbs @@ -6,5 +6,5 @@ def query(payload): return response.json() output = query({ - "inputs": {{{inputs}}}, + "inputs": {{{inputs.asStr}}}, }) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/textToAudio.hbs b/packages/inference/src/snippets/templates/python/requests/textToAudio.hbs new file mode 100644 index 0000000000..9503fd3630 --- /dev/null +++ b/packages/inference/src/snippets/templates/python/requests/textToAudio.hbs @@ -0,0 +1,23 @@ +{{#if (equals 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) +{{/if}} \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/textToAudioOther.hbs b/packages/inference/src/snippets/templates/python/requests/textToAudioOther.hbs deleted file mode 100644 index 8694e6ff47..0000000000 --- a/packages/inference/src/snippets/templates/python/requests/textToAudioOther.hbs +++ /dev/null @@ -1,10 +0,0 @@ -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) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/textToAudioTransformers.hbs b/packages/inference/src/snippets/templates/python/requests/textToAudioTransformers.hbs deleted file mode 100644 index e098c43e6c..0000000000 --- a/packages/inference/src/snippets/templates/python/requests/textToAudioTransformers.hbs +++ /dev/null @@ -1,10 +0,0 @@ -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) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/textToImage.hbs b/packages/inference/src/snippets/templates/python/requests/textToImage.hbs index e853c77df4..9d54f0cd77 100644 --- a/packages/inference/src/snippets/templates/python/requests/textToImage.hbs +++ b/packages/inference/src/snippets/templates/python/requests/textToImage.hbs @@ -1,3 +1,4 @@ +{{#if (equals provider "hf-inference")}} def query(payload): response = requests.post(API_URL, headers=headers, json=payload) return response.content @@ -9,4 +10,5 @@ image_bytes = query({ # 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 +image = Image.open(io.BytesIO(image_bytes)) +{{/if}} \ No newline at end of file From 0766e08cbe33e6b3d0b32d237597fd7c923cec61 Mon Sep 17 00:00:00 2001 From: Wauplin Date: Thu, 6 Mar 2025 18:26:39 +0100 Subject: [PATCH 11/26] minimal code + trim all snippets --- .../1.openai.hf-inference.py | 2 +- .../1.openai.together.py | 2 +- .../1.openai.fireworks-ai.py | 2 +- .../1.openai.hf-inference.py | 2 +- .../0.requests.hf-inference.py | 2 +- .../text-to-image/1.fal_client.fal-ai.py | 2 +- .../text-to-image/1.requests.hf-inference.py | 2 +- packages/inference/src/snippets/python.ts | 350 +++++------------- .../python/requests/importRequests.hbs | 2 +- 9 files changed, 108 insertions(+), 258 deletions(-) diff --git a/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.py b/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.py index 1668a4bb88..1476285a3b 100644 --- a/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.py +++ b/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.py @@ -20,4 +20,4 @@ ) for chunk in stream: - print(chunk.choices[0].delta.content, end="") + print(chunk.choices[0].delta.content, end="") \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.together.py b/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.together.py index 84681fa62e..e04dfcb7ed 100644 --- a/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.together.py +++ b/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.together.py @@ -20,4 +20,4 @@ ) for chunk in stream: - print(chunk.choices[0].delta.content, end="") + print(chunk.choices[0].delta.content, end="") \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.py b/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.py index a49d598d8f..81bede83e9 100644 --- a/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.py +++ b/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.py @@ -31,4 +31,4 @@ ) for chunk in stream: - print(chunk.choices[0].delta.content, end="") + print(chunk.choices[0].delta.content, end="") \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.py b/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.py index 47874aaff1..8ceb00bfe3 100644 --- a/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.py +++ b/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.py @@ -31,4 +31,4 @@ ) for chunk in stream: - print(chunk.choices[0].delta.content, end="") + print(chunk.choices[0].delta.content, end="") \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/text-to-audio-transformers/0.requests.hf-inference.py b/packages/inference/snippets-fixtures/text-to-audio-transformers/0.requests.hf-inference.py index abeccceee0..40add8d37e 100644 --- a/packages/inference/snippets-fixtures/text-to-audio-transformers/0.requests.hf-inference.py +++ b/packages/inference/snippets-fixtures/text-to-audio-transformers/0.requests.hf-inference.py @@ -12,4 +12,4 @@ def query(payload): }) # You can access the audio with IPython.display for example from IPython.display import Audio -Audio(audio, rate=sampling_rate) +Audio(audio, rate=sampling_rate) \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/text-to-image/1.fal_client.fal-ai.py b/packages/inference/snippets-fixtures/text-to-image/1.fal_client.fal-ai.py index 36220f1057..177c07bb8f 100644 --- a/packages/inference/snippets-fixtures/text-to-image/1.fal_client.fal-ai.py +++ b/packages/inference/snippets-fixtures/text-to-image/1.fal_client.fal-ai.py @@ -6,4 +6,4 @@ "prompt": "Astronaut riding a horse", }, ) -print(result) +print(result) \ No newline at end of file diff --git a/packages/inference/snippets-fixtures/text-to-image/1.requests.hf-inference.py b/packages/inference/snippets-fixtures/text-to-image/1.requests.hf-inference.py index 83f8c0175f..a8e1ad9de0 100644 --- a/packages/inference/snippets-fixtures/text-to-image/1.requests.hf-inference.py +++ b/packages/inference/snippets-fixtures/text-to-image/1.requests.hf-inference.py @@ -14,4 +14,4 @@ def query(payload): # You can access the image with PIL.Image for example import io from PIL import Image -image = Image.open(io.BytesIO(image_bytes)) +image = Image.open(io.BytesIO(image_bytes)) \ No newline at end of file diff --git a/packages/inference/src/snippets/python.ts b/packages/inference/src/snippets/python.ts index 2f7613eb14..07c00f4d4a 100644 --- a/packages/inference/src/snippets/python.ts +++ b/packages/inference/src/snippets/python.ts @@ -14,28 +14,20 @@ import Handlebars from "handlebars"; 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; - modelId?: string; + model?: ModelDataMinimal; provider?: InferenceProvider; providerModelId?: string; methodName?: string; // specific to snippetBasic importBase64?: boolean; // specific to snippetImportRequests } -interface SnippetTemplateParams { - accessToken: string; - baseUrl?: string; - inputs: string | object; - model: ModelDataMinimal; - provider: InferenceProvider; - providerModelId: string; -} - -const TOOLS = ["huggingface_hub", "requests", "fal_client", "openai"]; - Handlebars.registerHelper("equals", function (value1, value2) { return value1 === value2; }); @@ -54,20 +46,12 @@ const rootDirFinder = (): string => { return "/"; }; -const loadTemplate = (tool: string, templateName: string): ((data: TemplateParams) => string) | undefined => { - const templatePath = path.join( - rootDirFinder(), - "src", - "snippets", - "templates", - "python", - tool, - `${templateName}.hbs` - ); - if (!pathExists(templatePath)) { - return; - } - const template = fs.readFileSync(templatePath, "utf8"); +const templatePath = (tool: string, templateName: string): string => + path.join(rootDirFinder(), "src", "snippets", "templates", "python", tool, `${templateName}.hbs`); +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 Handlebars.compile(template); }; @@ -104,33 +88,46 @@ const HFH_INFERENCE_CLIENT_METHODS: Partial> = { const snippetImportInferenceClient = loadTemplate("huggingface_hub", "importInferenceClient"); const snippetImportRequests = loadTemplate("requests", "importRequests"); -const generateSnippets = (templateName: string, params: SnippetTemplateParams): InferenceSnippet[] => { - return TOOLS.map((tool) => { - const template = loadTemplate(tool, templateName); - if (!template) { - return; - } - if (tool === "huggingface_hub" && templateName === "basic") { - if (!(params.model.pipeline_tag && params.model.pipeline_tag in HFH_INFERENCE_CLIENT_METHODS)) { +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, methodName: HFH_INFERENCE_CLIENT_METHODS[params.model.pipeline_tag] }), + content: template(params).trim(), }; - } - return { - client: tool, - content: template(params), - }; - }).filter((snippet) => snippet !== undefined && snippet.content.trim()) as InferenceSnippet[]; + }).filter((snippet) => snippet !== undefined && snippet.content) as InferenceSnippet[]; + }; }; -const snippetConversational = ( +// Specialized input preparation functions +const prepareConversationalInput = ( model: ModelDataMinimal, - accessToken: string, - provider: InferenceProvider, - providerModelId?: string, opts?: { streaming?: boolean; messages?: ChatCompletionInputMessage[]; @@ -138,198 +135,52 @@ 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 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 params: SnippetTemplateParams = { - accessToken, - baseUrl: openAIbaseUrl(provider), - inputs: { - messagesStr: stringifyMessages(messages, { attributeKeyQuotes: true }), - configStr: stringifyGenerationConfig(config, { - indent: "\n\t", - attributeValueConnector: "=", - }), - }, - model, - provider, - providerModelId: providerModelId ?? model.id, + return { + messagesStr: stringifyMessages(messages, { attributeKeyQuotes: true }), + configStr: stringifyGenerationConfig(config, { + indent: "\n\t", + attributeValueConnector: "=", + }), }; - const templateName = streaming ? "conversationalStream" : "conversational"; - return generateSnippets(templateName, params); -}; - -const snippetZeroShotClassification = ( - model: ModelDataMinimal, - accessToken: string, - provider: InferenceProvider, - providerModelId?: string -): InferenceSnippet[] => { - return generateSnippets("zeroShotClassification", { - accessToken: accessToken, - inputs: getModelInputSnippet(model), - model: model, - provider: provider, - providerModelId: providerModelId ?? model.id, - }); -}; - -const snippetZeroShotImageClassification = ( - model: ModelDataMinimal, - accessToken: string, - provider: InferenceProvider, - providerModelId?: string -): InferenceSnippet[] => { - return generateSnippets("zeroShotImageClassification", { - accessToken: accessToken, - inputs: getModelInputSnippet(model), - model: model, - provider: provider, - providerModelId: providerModelId ?? model.id, - }); -}; - -const snippetBasic = ( - model: ModelDataMinimal, - accessToken: string, - provider: InferenceProvider, - providerModelId?: string -): InferenceSnippet[] => { - return generateSnippets("basic", { - accessToken: accessToken, - inputs: getModelInputSnippet(model), - model: model, - provider: provider, - providerModelId: providerModelId ?? model.id, - }); -}; - -const snippetFile = ( - model: ModelDataMinimal, - accessToken: string, - provider: InferenceProvider, - providerModelId?: string -): InferenceSnippet[] => { - return generateSnippets("basicFile", { - accessToken: accessToken, - inputs: getModelInputSnippet(model), - model: model, - provider: provider, - providerModelId: providerModelId ?? model.id, - }); }; -const snippetTextToImage = ( - model: ModelDataMinimal, - accessToken: string, - provider: InferenceProvider, - providerModelId?: string -): InferenceSnippet[] => { - return generateSnippets("textToImage", { - accessToken: accessToken, - inputs: getModelInputSnippet(model), - model: model, - provider: provider, - providerModelId: providerModelId ?? model.id, - }); -}; - -const snippetTextToVideo = ( - model: ModelDataMinimal, - accessToken: string, - provider: InferenceProvider, - providerModelId?: string -): InferenceSnippet[] => { - return generateSnippets("textToVideo", { - accessToken: accessToken, - inputs: getModelInputSnippet(model), - model: model, - provider: provider, - providerModelId: providerModelId ?? model.id, - }); -}; - -const snippetTabular = ( - model: ModelDataMinimal, - accessToken: string, - provider: InferenceProvider, - providerModelId?: string -): InferenceSnippet[] => { - return generateSnippets("tabular", { - accessToken: accessToken, - inputs: getModelInputSnippet(model), - model: model, - provider: provider, - providerModelId: providerModelId ?? model.id, - }); -}; - -const snippetTextToAudio = ( - model: ModelDataMinimal, - accessToken: string, - provider: InferenceProvider, - providerModelId?: string -): InferenceSnippet[] => { - return generateSnippets("textToAudio", { - accessToken: accessToken, - inputs: getModelInputSnippet(model), - model: model, - provider: provider, - providerModelId: providerModelId ?? model.id, - }); -}; - -const snippetAutomaticSpeechRecognition = ( - model: ModelDataMinimal, - accessToken: string, - provider: InferenceProvider, - providerModelId?: string -): InferenceSnippet[] => { - return generateSnippets("automaticSpeechRecognition", { - accessToken: accessToken, - inputs: getModelInputSnippet(model), - model: model, - provider: provider, - providerModelId: providerModelId ?? model.id, - }); -}; - -const snippetDocumentQuestionAnswering = ( - model: ModelDataMinimal, - accessToken: string, - provider: InferenceProvider, - providerModelId?: string -): InferenceSnippet[] => { +const prepareDocumentQuestionAnsweringInput = (model: ModelDataMinimal): object => { const inputsAsStr = getModelInputSnippet(model) as string; const inputsAsObj = JSON.parse(inputsAsStr); - return generateSnippets("documentQuestionAnswering", { - accessToken: accessToken, - inputs: { asObj: inputsAsObj, asStr: inputsAsStr }, - model: model, - provider: provider, - providerModelId: providerModelId ?? model.id, - }); + return { asObj: inputsAsObj, asStr: inputsAsStr }; }; -const snippetImageToImage = ( +const prepareImageToImageInput = (model: ModelDataMinimal): object => { + return JSON.parse(getModelInputSnippet(model) as string); +}; + +const snippetConversational = ( model: ModelDataMinimal, accessToken: string, provider: InferenceProvider, - providerModelId?: string + providerModelId?: string, + opts?: { + streaming?: boolean; + messages?: ChatCompletionInputMessage[]; + temperature?: GenerationParameters["temperature"]; + max_tokens?: GenerationParameters["max_tokens"]; + top_p?: GenerationParameters["top_p"]; + } ): InferenceSnippet[] => { - return generateSnippets("imageToImage", { - accessToken: accessToken, - inputs: JSON.parse(getModelInputSnippet(model) as string), - model: model, - provider: provider, - providerModelId: providerModelId ?? model.id, + const streaming = opts?.streaming ?? true; + const templateName = streaming ? "conversationalStream" : "conversational"; + return snippetGenerator(templateName, prepareConversationalInput)(model, accessToken, provider, providerModelId, { + ...opts, + streaming, }); }; @@ -345,36 +196,35 @@ const pythonSnippets: Partial< ) => 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, + "text-classification": snippetGenerator("basic"), + "token-classification": snippetGenerator("basic"), + "table-question-answering": snippetGenerator("basic"), + "question-answering": snippetGenerator("basic"), + "zero-shot-classification": snippetGenerator("zeroShotClassification"), + translation: snippetGenerator("basic"), + summarization: snippetGenerator("basic"), + "feature-extraction": snippetGenerator("basic"), + "text-generation": snippetGenerator("basic"), + "text2text-generation": snippetGenerator("basic"), "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, + "fill-mask": snippetGenerator("basic"), + "sentence-similarity": snippetGenerator("basic"), + "automatic-speech-recognition": snippetGenerator("automaticSpeechRecognition"), + "text-to-image": snippetGenerator("textToImage"), + "text-to-video": snippetGenerator("textToVideo"), + "text-to-speech": snippetGenerator("textToAudio"), + "text-to-audio": snippetGenerator("textToAudio"), + "audio-to-audio": snippetGenerator("basicFile"), + "audio-classification": snippetGenerator("basicFile"), + "image-classification": snippetGenerator("basicFile"), + "tabular-regression": snippetGenerator("tabular"), + "tabular-classification": snippetGenerator("tabular"), + "object-detection": snippetGenerator("basicFile"), + "image-segmentation": snippetGenerator("basicFile"), + "document-question-answering": snippetGenerator("documentQuestionAnswering", prepareDocumentQuestionAnsweringInput), + "image-to-text": snippetGenerator("basicFile"), + "image-to-image": snippetGenerator("imageToImage", prepareImageToImageInput), + "zero-shot-image-classification": snippetGenerator("zeroShotImageClassification"), }; export function getPythonInferenceSnippet( @@ -405,7 +255,7 @@ const addImportsToSnippet = ( } else if (snippet.content.includes("requests")) { importSection = snippetImportRequests({ accessToken, - modelId: model.id, + model: model, provider, importBase64: snippet.content.includes("base64"), }); diff --git a/packages/inference/src/snippets/templates/python/requests/importRequests.hbs b/packages/inference/src/snippets/templates/python/requests/importRequests.hbs index 4dc29c6b2e..aa8bb8c63f 100644 --- a/packages/inference/src/snippets/templates/python/requests/importRequests.hbs +++ b/packages/inference/src/snippets/templates/python/requests/importRequests.hbs @@ -3,5 +3,5 @@ import base64 {{/if}} import requests -API_URL = "https://router.huggingface.co/{{{provider}}}/models/{{{modelId}}}" +API_URL = "https://router.huggingface.co/{{{provider}}}/models/{{{model.id}}}" headers = {"Authorization": "Bearer {{{accessToken}}}"} \ No newline at end of file From 29ebae9da4ec1a6b1027f0ccf4d5cf2270ddb364 Mon Sep 17 00:00:00 2001 From: Wauplin Date: Thu, 6 Mar 2025 18:28:48 +0100 Subject: [PATCH 12/26] some comments --- packages/inference/src/snippets/python.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/inference/src/snippets/python.ts b/packages/inference/src/snippets/python.ts index 07c00f4d4a..78bf0f8f83 100644 --- a/packages/inference/src/snippets/python.ts +++ b/packages/inference/src/snippets/python.ts @@ -32,6 +32,8 @@ Handlebars.registerHelper("equals", function (value1, value2) { return value1 === value2; }); +// Helpers to find + load templates + const rootDirFinder = (): string => { let currentPath = path.normalize(import.meta.url).replace("file:", ""); @@ -55,6 +57,11 @@ const loadTemplate = (tool: string, templateName: string): ((data: TemplateParam return Handlebars.compile(template); }; +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", @@ -85,9 +92,7 @@ const HFH_INFERENCE_CLIENT_METHODS: Partial> = { "tabular-regression": "tabular_regression", }; -const snippetImportInferenceClient = loadTemplate("huggingface_hub", "importInferenceClient"); -const snippetImportRequests = loadTemplate("requests", "importRequests"); - +// Snippet generators const snippetGenerator = (templateName: string, inputPreparationFn?: InputPreparationFn) => { return ( model: ModelDataMinimal, @@ -125,7 +130,7 @@ const snippetGenerator = (templateName: string, inputPreparationFn?: InputPrepar }; }; -// Specialized input preparation functions +// Input preparation functions (required for a few tasks) const prepareConversationalInput = ( model: ModelDataMinimal, opts?: { From 95a2a7119668d66df4f7f013f6b235bd55033206 Mon Sep 17 00:00:00 2001 From: Wauplin Date: Thu, 6 Mar 2025 18:29:39 +0100 Subject: [PATCH 13/26] sort --- packages/inference/src/snippets/python.ts | 47 +++++++++++------------ 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/packages/inference/src/snippets/python.ts b/packages/inference/src/snippets/python.ts index 78bf0f8f83..df71dcdc89 100644 --- a/packages/inference/src/snippets/python.ts +++ b/packages/inference/src/snippets/python.ts @@ -201,37 +201,36 @@ const pythonSnippets: Partial< ) => InferenceSnippet[] > > = { - "text-classification": snippetGenerator("basic"), - "token-classification": snippetGenerator("basic"), - "table-question-answering": snippetGenerator("basic"), - "question-answering": snippetGenerator("basic"), - "zero-shot-classification": snippetGenerator("zeroShotClassification"), - translation: snippetGenerator("basic"), - summarization: snippetGenerator("basic"), + "audio-classification": snippetGenerator("basicFile"), + "audio-to-audio": snippetGenerator("basicFile"), + "automatic-speech-recognition": snippetGenerator("automaticSpeechRecognition"), + "document-question-answering": snippetGenerator("documentQuestionAnswering", prepareDocumentQuestionAnsweringInput), "feature-extraction": snippetGenerator("basic"), - "text-generation": snippetGenerator("basic"), - "text2text-generation": snippetGenerator("basic"), - "image-text-to-text": snippetConversational, "fill-mask": snippetGenerator("basic"), - "sentence-similarity": snippetGenerator("basic"), - "automatic-speech-recognition": snippetGenerator("automaticSpeechRecognition"), - "text-to-image": snippetGenerator("textToImage"), - "text-to-video": snippetGenerator("textToVideo"), - "text-to-speech": snippetGenerator("textToAudio"), - "text-to-audio": snippetGenerator("textToAudio"), - "audio-to-audio": snippetGenerator("basicFile"), - "audio-classification": snippetGenerator("basicFile"), "image-classification": snippetGenerator("basicFile"), - "tabular-regression": snippetGenerator("tabular"), - "tabular-classification": snippetGenerator("tabular"), - "object-detection": snippetGenerator("basicFile"), "image-segmentation": snippetGenerator("basicFile"), - "document-question-answering": snippetGenerator("documentQuestionAnswering", prepareDocumentQuestionAnsweringInput), - "image-to-text": snippetGenerator("basicFile"), + "image-text-to-text": snippetConversational, "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, From f529ba62e3647b103a5912341442f1d419b2c134 Mon Sep 17 00:00:00 2001 From: Wauplin Date: Thu, 6 Mar 2025 18:33:34 +0100 Subject: [PATCH 14/26] sort --- packages/inference/src/snippets/python.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/inference/src/snippets/python.ts b/packages/inference/src/snippets/python.ts index df71dcdc89..a7f14033b1 100644 --- a/packages/inference/src/snippets/python.ts +++ b/packages/inference/src/snippets/python.ts @@ -66,30 +66,30 @@ 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", }; // Snippet generators From b7b6869be9c3b195db35b7d64317cbf0ae1e38e2 Mon Sep 17 00:00:00 2001 From: Wauplin Date: Thu, 6 Mar 2025 18:39:25 +0100 Subject: [PATCH 15/26] delete before coming back --- .../0.huggingface_hub.hf-inference.py | 7 ---- .../1.requests.hf-inference.py | 12 ------ .../0.curl.hf-inference.sh | 14 ------- .../0.curl.together.sh | 14 ------- .../0.huggingface.js.hf-inference.js | 17 -------- .../0.huggingface.js.together.js | 17 -------- .../0.huggingface_hub.hf-inference.py | 21 ---------- .../0.huggingface_hub.together.py | 21 ---------- .../1.openai.hf-inference.js | 19 --------- .../1.openai.hf-inference.py | 21 ---------- .../1.openai.together.js | 19 --------- .../1.openai.together.py | 21 ---------- .../0.curl.hf-inference.sh | 14 ------- .../0.curl.together.sh | 14 ------- .../0.huggingface.js.hf-inference.js | 25 ------------ .../0.huggingface.js.together.js | 25 ------------ .../0.huggingface_hub.hf-inference.py | 23 ----------- .../0.huggingface_hub.together.py | 23 ----------- .../1.openai.hf-inference.js | 28 ------------- .../1.openai.hf-inference.py | 23 ----------- .../1.openai.together.js | 28 ------------- .../1.openai.together.py | 23 ----------- .../0.curl.fireworks-ai.sh | 25 ------------ .../0.curl.hf-inference.sh | 25 ------------ .../0.huggingface.js.fireworks-ai.js | 28 ------------- .../0.huggingface.js.hf-inference.js | 28 ------------- .../0.huggingface_hub.fireworks-ai.py | 32 --------------- .../0.huggingface_hub.hf-inference.py | 32 --------------- .../1.openai.fireworks-ai.js | 30 -------------- .../1.openai.fireworks-ai.py | 32 --------------- .../1.openai.hf-inference.js | 30 -------------- .../1.openai.hf-inference.py | 32 --------------- .../0.curl.fireworks-ai.sh | 25 ------------ .../0.curl.hf-inference.sh | 25 ------------ .../0.huggingface.js.fireworks-ai.js | 36 ----------------- .../0.huggingface.js.hf-inference.js | 36 ----------------- .../0.huggingface_hub.fireworks-ai.py | 34 ---------------- .../0.huggingface_hub.hf-inference.py | 34 ---------------- .../1.openai.fireworks-ai.js | 39 ------------------- .../1.openai.fireworks-ai.py | 34 ---------------- .../1.openai.hf-inference.js | 39 ------------------- .../1.openai.hf-inference.py | 34 ---------------- .../0.huggingface_hub.hf-inference.py | 11 ------ .../1.requests.hf-inference.py | 19 --------- .../0.huggingface_hub.hf-inference.py | 12 ------ .../image-to-image/1.requests.hf-inference.py | 22 ----------- .../0.curl.hf-inference.sh | 5 --- .../0.huggingface.js.hf-inference.js | 11 ------ .../0.huggingface_hub.hf-inference.py | 13 ------- .../1.fetch.hf-inference.js | 19 --------- .../1.requests.hf-inference.py | 12 ------ .../text-to-image/0.curl.hf-inference.sh | 5 --- .../text-to-image/0.huggingface.js.fal-ai.js | 11 ------ .../0.huggingface.js.hf-inference.js | 11 ------ .../text-to-image/0.huggingface_hub.fal-ai.py | 12 ------ .../0.huggingface_hub.hf-inference.py | 12 ------ .../text-to-image/1.fal-client.fal-ai.py | 9 ----- .../text-to-image/1.fetch.hf-inference.js | 18 --------- .../text-to-image/1.requests.hf-inference.py | 17 -------- .../text-to-video/0.huggingface.js.fal-ai.js | 11 ------ .../0.huggingface.js.replicate.js | 11 ------ .../text-to-video/0.huggingface_hub.fal-ai.py | 11 ------ .../0.huggingface_hub.replicate.py | 11 ------ 63 files changed, 1322 deletions(-) delete mode 100644 packages/tasks-gen/snippets-fixtures/automatic-speech-recognition/0.huggingface_hub.hf-inference.py delete mode 100644 packages/tasks-gen/snippets-fixtures/automatic-speech-recognition/1.requests.hf-inference.py delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.curl.hf-inference.sh delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.curl.together.sh delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.huggingface.js.hf-inference.js delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.huggingface.js.together.js delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.hf-inference.py delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.together.py delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/1.openai.hf-inference.js delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/1.openai.hf-inference.py delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/1.openai.together.js delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/1.openai.together.py delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.curl.hf-inference.sh delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.curl.together.sh delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.huggingface.js.hf-inference.js delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.huggingface.js.together.js delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.hf-inference.py delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.together.py delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.js delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.py delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-llm-stream/1.openai.together.js delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-llm-stream/1.openai.together.py delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.curl.fireworks-ai.sh delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.curl.hf-inference.sh delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.huggingface.js.fireworks-ai.js delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.huggingface.js.hf-inference.js delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.fireworks-ai.py delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.hf-inference.py delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/1.openai.fireworks-ai.js delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/1.openai.fireworks-ai.py delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/1.openai.hf-inference.js delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/1.openai.hf-inference.py delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.curl.fireworks-ai.sh delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.curl.hf-inference.sh delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.huggingface.js.fireworks-ai.js delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.huggingface.js.hf-inference.js delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.fireworks-ai.py delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.hf-inference.py delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.js delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.py delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.js delete mode 100644 packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.py delete mode 100644 packages/tasks-gen/snippets-fixtures/document-question-answering/0.huggingface_hub.hf-inference.py delete mode 100644 packages/tasks-gen/snippets-fixtures/document-question-answering/1.requests.hf-inference.py delete mode 100644 packages/tasks-gen/snippets-fixtures/image-to-image/0.huggingface_hub.hf-inference.py delete mode 100644 packages/tasks-gen/snippets-fixtures/image-to-image/1.requests.hf-inference.py delete mode 100644 packages/tasks-gen/snippets-fixtures/text-classification/0.curl.hf-inference.sh delete mode 100644 packages/tasks-gen/snippets-fixtures/text-classification/0.huggingface.js.hf-inference.js delete mode 100644 packages/tasks-gen/snippets-fixtures/text-classification/0.huggingface_hub.hf-inference.py delete mode 100644 packages/tasks-gen/snippets-fixtures/text-classification/1.fetch.hf-inference.js delete mode 100644 packages/tasks-gen/snippets-fixtures/text-classification/1.requests.hf-inference.py delete mode 100644 packages/tasks-gen/snippets-fixtures/text-to-image/0.curl.hf-inference.sh delete mode 100644 packages/tasks-gen/snippets-fixtures/text-to-image/0.huggingface.js.fal-ai.js delete mode 100644 packages/tasks-gen/snippets-fixtures/text-to-image/0.huggingface.js.hf-inference.js delete mode 100644 packages/tasks-gen/snippets-fixtures/text-to-image/0.huggingface_hub.fal-ai.py delete mode 100644 packages/tasks-gen/snippets-fixtures/text-to-image/0.huggingface_hub.hf-inference.py delete mode 100644 packages/tasks-gen/snippets-fixtures/text-to-image/1.fal-client.fal-ai.py delete mode 100644 packages/tasks-gen/snippets-fixtures/text-to-image/1.fetch.hf-inference.js delete mode 100644 packages/tasks-gen/snippets-fixtures/text-to-image/1.requests.hf-inference.py delete mode 100644 packages/tasks-gen/snippets-fixtures/text-to-video/0.huggingface.js.fal-ai.js delete mode 100644 packages/tasks-gen/snippets-fixtures/text-to-video/0.huggingface.js.replicate.js delete mode 100644 packages/tasks-gen/snippets-fixtures/text-to-video/0.huggingface_hub.fal-ai.py delete mode 100644 packages/tasks-gen/snippets-fixtures/text-to-video/0.huggingface_hub.replicate.py 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 deleted file mode 100644 index 9a945fcf65..0000000000 --- a/packages/tasks-gen/snippets-fixtures/automatic-speech-recognition/0.huggingface_hub.hf-inference.py +++ /dev/null @@ -1,7 +0,0 @@ -from huggingface_hub import InferenceClient - -client = InferenceClient( - 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 deleted file mode 100644 index 4acaa0ed5b..0000000000 --- a/packages/tasks-gen/snippets-fixtures/automatic-speech-recognition/1.requests.hf-inference.py +++ /dev/null @@ -1,12 +0,0 @@ -import requests - -API_URL = "https://router.huggingface.co/hf-inference/models/openai/whisper-large-v3-turbo" -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() - -output = query("sample1.flac") \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.curl.hf-inference.sh b/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.curl.hf-inference.sh deleted file mode 100644 index fc9cf73015..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.curl.hf-inference.sh +++ /dev/null @@ -1,14 +0,0 @@ -curl 'https://router.huggingface.co/hf-inference/models/meta-llama/Llama-3.1-8B-Instruct/v1/chat/completions' \ --H 'Authorization: Bearer api_token' \ --H 'Content-Type: application/json' \ ---data '{ - "model": "meta-llama/Llama-3.1-8B-Instruct", - "messages": [ - { - "role": "user", - "content": "What is the capital of France?" - } - ], - "max_tokens": 500, - "stream": false -}' \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.curl.together.sh b/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.curl.together.sh deleted file mode 100644 index 12a3dd7fd9..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.curl.together.sh +++ /dev/null @@ -1,14 +0,0 @@ -curl 'https://router.huggingface.co/together/v1/chat/completions' \ --H 'Authorization: Bearer api_token' \ --H 'Content-Type: application/json' \ ---data '{ - "model": "", - "messages": [ - { - "role": "user", - "content": "What is the capital of France?" - } - ], - "max_tokens": 500, - "stream": false -}' \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.huggingface.js.hf-inference.js b/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.huggingface.js.hf-inference.js deleted file mode 100644 index 15361c3e45..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.huggingface.js.hf-inference.js +++ /dev/null @@ -1,17 +0,0 @@ -import { HfInference } from "@huggingface/inference"; - -const client = new HfInference("api_token"); - -const chatCompletion = await client.chatCompletion({ - model: "meta-llama/Llama-3.1-8B-Instruct", - messages: [ - { - role: "user", - content: "What is the capital of France?" - } - ], - provider: "hf-inference", - max_tokens: 500, -}); - -console.log(chatCompletion.choices[0].message); diff --git a/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.huggingface.js.together.js b/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.huggingface.js.together.js deleted file mode 100644 index 9d64919ed9..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.huggingface.js.together.js +++ /dev/null @@ -1,17 +0,0 @@ -import { HfInference } from "@huggingface/inference"; - -const client = new HfInference("api_token"); - -const chatCompletion = await client.chatCompletion({ - model: "meta-llama/Llama-3.1-8B-Instruct", - messages: [ - { - role: "user", - content: "What is the capital of France?" - } - ], - provider: "together", - max_tokens: 500, -}); - -console.log(chatCompletion.choices[0].message); 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 deleted file mode 100644 index 44e29d2a0b..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.hf-inference.py +++ /dev/null @@ -1,21 +0,0 @@ -from huggingface_hub import InferenceClient - -client = InferenceClient( - provider="hf-inference", - api_key="api_token", -) - -messages = [ - { - "role": "user", - "content": "What is the capital of France?" - } -] - -completion = client.chat.completions.create( - 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 deleted file mode 100644 index ee348d6987..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.together.py +++ /dev/null @@ -1,21 +0,0 @@ -from huggingface_hub import InferenceClient - -client = InferenceClient( - provider="together", - api_key="api_token", -) - -messages = [ - { - "role": "user", - "content": "What is the capital of France?" - } -] - -completion = client.chat.completions.create( - 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.js b/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/1.openai.hf-inference.js deleted file mode 100644 index 3f825bcf80..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/1.openai.hf-inference.js +++ /dev/null @@ -1,19 +0,0 @@ -import { OpenAI } from "openai"; - -const client = new OpenAI({ - baseURL: "https://router.huggingface.co/hf-inference/v1", - apiKey: "api_token" -}); - -const chatCompletion = await client.chat.completions.create({ - model: "meta-llama/Llama-3.1-8B-Instruct", - messages: [ - { - role: "user", - content: "What is the capital of France?" - } - ], - max_tokens: 500, -}); - -console.log(chatCompletion.choices[0].message); 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 deleted file mode 100644 index 4d557b289e..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/1.openai.hf-inference.py +++ /dev/null @@ -1,21 +0,0 @@ -from openai import OpenAI - -client = OpenAI( - base_url="https://router.huggingface.co/hf-inference/v1", - api_key="api_token" -) - -messages = [ - { - "role": "user", - "content": "What is the capital of France?" - } -] - -completion = client.chat.completions.create( - 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.js b/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/1.openai.together.js deleted file mode 100644 index 816b6dd513..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/1.openai.together.js +++ /dev/null @@ -1,19 +0,0 @@ -import { OpenAI } from "openai"; - -const client = new OpenAI({ - baseURL: "https://router.huggingface.co/together", - apiKey: "api_token" -}); - -const chatCompletion = await client.chat.completions.create({ - model: "", - messages: [ - { - role: "user", - content: "What is the capital of France?" - } - ], - max_tokens: 500, -}); - -console.log(chatCompletion.choices[0].message); 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 deleted file mode 100644 index d4fb0f865c..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/1.openai.together.py +++ /dev/null @@ -1,21 +0,0 @@ -from openai import OpenAI - -client = OpenAI( - base_url="https://router.huggingface.co/together", - api_key="api_token" -) - -messages = [ - { - "role": "user", - "content": "What is the capital of France?" - } -] - -completion = client.chat.completions.create( - 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.curl.hf-inference.sh b/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.curl.hf-inference.sh deleted file mode 100644 index 0d96e5c312..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.curl.hf-inference.sh +++ /dev/null @@ -1,14 +0,0 @@ -curl 'https://router.huggingface.co/hf-inference/models/meta-llama/Llama-3.1-8B-Instruct/v1/chat/completions' \ --H 'Authorization: Bearer api_token' \ --H 'Content-Type: application/json' \ ---data '{ - "model": "meta-llama/Llama-3.1-8B-Instruct", - "messages": [ - { - "role": "user", - "content": "What is the capital of France?" - } - ], - "max_tokens": 500, - "stream": true -}' \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.curl.together.sh b/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.curl.together.sh deleted file mode 100644 index e0e22e352d..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.curl.together.sh +++ /dev/null @@ -1,14 +0,0 @@ -curl 'https://router.huggingface.co/together/v1/chat/completions' \ --H 'Authorization: Bearer api_token' \ --H 'Content-Type: application/json' \ ---data '{ - "model": "", - "messages": [ - { - "role": "user", - "content": "What is the capital of France?" - } - ], - "max_tokens": 500, - "stream": true -}' \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.huggingface.js.hf-inference.js b/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.huggingface.js.hf-inference.js deleted file mode 100644 index 4bdd0c143e..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.huggingface.js.hf-inference.js +++ /dev/null @@ -1,25 +0,0 @@ -import { HfInference } from "@huggingface/inference"; - -const client = new HfInference("api_token"); - -let out = ""; - -const stream = client.chatCompletionStream({ - model: "meta-llama/Llama-3.1-8B-Instruct", - messages: [ - { - role: "user", - content: "What is the capital of France?" - } - ], - provider: "hf-inference", - max_tokens: 500, -}); - -for await (const chunk of stream) { - if (chunk.choices && chunk.choices.length > 0) { - const newContent = chunk.choices[0].delta.content; - out += newContent; - console.log(newContent); - } -} \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.huggingface.js.together.js b/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.huggingface.js.together.js deleted file mode 100644 index 5822b2ce57..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.huggingface.js.together.js +++ /dev/null @@ -1,25 +0,0 @@ -import { HfInference } from "@huggingface/inference"; - -const client = new HfInference("api_token"); - -let out = ""; - -const stream = client.chatCompletionStream({ - model: "meta-llama/Llama-3.1-8B-Instruct", - messages: [ - { - role: "user", - content: "What is the capital of France?" - } - ], - provider: "together", - max_tokens: 500, -}); - -for await (const chunk of stream) { - if (chunk.choices && chunk.choices.length > 0) { - const newContent = chunk.choices[0].delta.content; - out += newContent; - console.log(newContent); - } -} \ 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 deleted file mode 100644 index 0f1fd0dbdc..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.hf-inference.py +++ /dev/null @@ -1,23 +0,0 @@ -from huggingface_hub import InferenceClient - -client = InferenceClient( - provider="hf-inference", - api_key="api_token", -) - -messages = [ - { - "role": "user", - "content": "What is the capital of France?" - } -] - -stream = client.chat.completions.create( - 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 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 deleted file mode 100644 index 31d00d979c..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.together.py +++ /dev/null @@ -1,23 +0,0 @@ -from huggingface_hub import InferenceClient - -client = InferenceClient( - provider="together", - api_key="api_token", -) - -messages = [ - { - "role": "user", - "content": "What is the capital of France?" - } -] - -stream = client.chat.completions.create( - 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 diff --git a/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.js b/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.js deleted file mode 100644 index 568cbf72a5..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.js +++ /dev/null @@ -1,28 +0,0 @@ -import { OpenAI } from "openai"; - -const client = new OpenAI({ - baseURL: "https://router.huggingface.co/hf-inference/v1", - apiKey: "api_token" -}); - -let out = ""; - -const stream = await client.chat.completions.create({ - model: "meta-llama/Llama-3.1-8B-Instruct", - messages: [ - { - role: "user", - content: "What is the capital of France?" - } - ], - max_tokens: 500, - stream: true, -}); - -for await (const chunk of stream) { - if (chunk.choices && chunk.choices.length > 0) { - const newContent = chunk.choices[0].delta.content; - out += newContent; - console.log(newContent); - } -} \ No newline at end of file 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 deleted file mode 100644 index 12167b83bc..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.py +++ /dev/null @@ -1,23 +0,0 @@ -from openai import OpenAI - -client = OpenAI( - base_url="https://router.huggingface.co/hf-inference/v1", - api_key="api_token" -) - -messages = [ - { - "role": "user", - "content": "What is the capital of France?" - } -] - -stream = client.chat.completions.create( - 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 diff --git a/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/1.openai.together.js b/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/1.openai.together.js deleted file mode 100644 index ef0f928e13..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/1.openai.together.js +++ /dev/null @@ -1,28 +0,0 @@ -import { OpenAI } from "openai"; - -const client = new OpenAI({ - baseURL: "https://router.huggingface.co/together", - apiKey: "api_token" -}); - -let out = ""; - -const stream = await client.chat.completions.create({ - model: "", - messages: [ - { - role: "user", - content: "What is the capital of France?" - } - ], - max_tokens: 500, - stream: true, -}); - -for await (const chunk of stream) { - if (chunk.choices && chunk.choices.length > 0) { - const newContent = chunk.choices[0].delta.content; - out += newContent; - console.log(newContent); - } -} \ 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 deleted file mode 100644 index 67d0bde845..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/1.openai.together.py +++ /dev/null @@ -1,23 +0,0 @@ -from openai import OpenAI - -client = OpenAI( - base_url="https://router.huggingface.co/together", - api_key="api_token" -) - -messages = [ - { - "role": "user", - "content": "What is the capital of France?" - } -] - -stream = client.chat.completions.create( - 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 diff --git a/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.curl.fireworks-ai.sh b/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.curl.fireworks-ai.sh deleted file mode 100644 index fa8775048d..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.curl.fireworks-ai.sh +++ /dev/null @@ -1,25 +0,0 @@ -curl 'https://router.huggingface.co/fireworks-ai/v1/chat/completions' \ --H 'Authorization: Bearer api_token' \ --H 'Content-Type: application/json' \ ---data '{ - "model": "", - "messages": [ - { - "role": "user", - "content": [ - { - "type": "text", - "text": "Describe this image in one sentence." - }, - { - "type": "image_url", - "image_url": { - "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" - } - } - ] - } - ], - "max_tokens": 500, - "stream": false -}' \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.curl.hf-inference.sh b/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.curl.hf-inference.sh deleted file mode 100644 index 680a12557c..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.curl.hf-inference.sh +++ /dev/null @@ -1,25 +0,0 @@ -curl 'https://router.huggingface.co/hf-inference/models/meta-llama/Llama-3.2-11B-Vision-Instruct/v1/chat/completions' \ --H 'Authorization: Bearer api_token' \ --H 'Content-Type: application/json' \ ---data '{ - "model": "meta-llama/Llama-3.2-11B-Vision-Instruct", - "messages": [ - { - "role": "user", - "content": [ - { - "type": "text", - "text": "Describe this image in one sentence." - }, - { - "type": "image_url", - "image_url": { - "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" - } - } - ] - } - ], - "max_tokens": 500, - "stream": false -}' \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.huggingface.js.fireworks-ai.js b/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.huggingface.js.fireworks-ai.js deleted file mode 100644 index 88e8d44506..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.huggingface.js.fireworks-ai.js +++ /dev/null @@ -1,28 +0,0 @@ -import { HfInference } from "@huggingface/inference"; - -const client = new HfInference("api_token"); - -const chatCompletion = await client.chatCompletion({ - model: "meta-llama/Llama-3.2-11B-Vision-Instruct", - messages: [ - { - role: "user", - content: [ - { - type: "text", - text: "Describe this image in one sentence." - }, - { - type: "image_url", - image_url: { - url: "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" - } - } - ] - } - ], - provider: "fireworks-ai", - max_tokens: 500, -}); - -console.log(chatCompletion.choices[0].message); diff --git a/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.huggingface.js.hf-inference.js b/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.huggingface.js.hf-inference.js deleted file mode 100644 index 2a69d4d82c..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.huggingface.js.hf-inference.js +++ /dev/null @@ -1,28 +0,0 @@ -import { HfInference } from "@huggingface/inference"; - -const client = new HfInference("api_token"); - -const chatCompletion = await client.chatCompletion({ - model: "meta-llama/Llama-3.2-11B-Vision-Instruct", - messages: [ - { - role: "user", - content: [ - { - type: "text", - text: "Describe this image in one sentence." - }, - { - type: "image_url", - image_url: { - url: "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" - } - } - ] - } - ], - provider: "hf-inference", - max_tokens: 500, -}); - -console.log(chatCompletion.choices[0].message); 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 deleted file mode 100644 index 41f26d30a7..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.fireworks-ai.py +++ /dev/null @@ -1,32 +0,0 @@ -from huggingface_hub import InferenceClient - -client = InferenceClient( - provider="fireworks-ai", - api_key="api_token", -) - -messages = [ - { - "role": "user", - "content": [ - { - "type": "text", - "text": "Describe this image in one sentence." - }, - { - "type": "image_url", - "image_url": { - "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" - } - } - ] - } -] - -completion = client.chat.completions.create( - 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 deleted file mode 100644 index 507809756d..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.hf-inference.py +++ /dev/null @@ -1,32 +0,0 @@ -from huggingface_hub import InferenceClient - -client = InferenceClient( - provider="hf-inference", - api_key="api_token", -) - -messages = [ - { - "role": "user", - "content": [ - { - "type": "text", - "text": "Describe this image in one sentence." - }, - { - "type": "image_url", - "image_url": { - "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" - } - } - ] - } -] - -completion = client.chat.completions.create( - 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.js b/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/1.openai.fireworks-ai.js deleted file mode 100644 index 28b031e754..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/1.openai.fireworks-ai.js +++ /dev/null @@ -1,30 +0,0 @@ -import { OpenAI } from "openai"; - -const client = new OpenAI({ - baseURL: "https://router.huggingface.co/fireworks-ai", - apiKey: "api_token" -}); - -const chatCompletion = await client.chat.completions.create({ - model: "", - messages: [ - { - role: "user", - content: [ - { - type: "text", - text: "Describe this image in one sentence." - }, - { - type: "image_url", - image_url: { - url: "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" - } - } - ] - } - ], - max_tokens: 500, -}); - -console.log(chatCompletion.choices[0].message); 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 deleted file mode 100644 index 19a126c63d..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/1.openai.fireworks-ai.py +++ /dev/null @@ -1,32 +0,0 @@ -from openai import OpenAI - -client = OpenAI( - base_url="https://router.huggingface.co/fireworks-ai", - api_key="api_token" -) - -messages = [ - { - "role": "user", - "content": [ - { - "type": "text", - "text": "Describe this image in one sentence." - }, - { - "type": "image_url", - "image_url": { - "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" - } - } - ] - } -] - -completion = client.chat.completions.create( - 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.js b/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/1.openai.hf-inference.js deleted file mode 100644 index 00d7095eeb..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/1.openai.hf-inference.js +++ /dev/null @@ -1,30 +0,0 @@ -import { OpenAI } from "openai"; - -const client = new OpenAI({ - baseURL: "https://router.huggingface.co/hf-inference/v1", - apiKey: "api_token" -}); - -const chatCompletion = await client.chat.completions.create({ - model: "meta-llama/Llama-3.2-11B-Vision-Instruct", - messages: [ - { - role: "user", - content: [ - { - type: "text", - text: "Describe this image in one sentence." - }, - { - type: "image_url", - image_url: { - url: "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" - } - } - ] - } - ], - max_tokens: 500, -}); - -console.log(chatCompletion.choices[0].message); 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 deleted file mode 100644 index 517466190f..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/1.openai.hf-inference.py +++ /dev/null @@ -1,32 +0,0 @@ -from openai import OpenAI - -client = OpenAI( - base_url="https://router.huggingface.co/hf-inference/v1", - api_key="api_token" -) - -messages = [ - { - "role": "user", - "content": [ - { - "type": "text", - "text": "Describe this image in one sentence." - }, - { - "type": "image_url", - "image_url": { - "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" - } - } - ] - } -] - -completion = client.chat.completions.create( - 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.curl.fireworks-ai.sh b/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.curl.fireworks-ai.sh deleted file mode 100644 index 9cd88a26c4..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.curl.fireworks-ai.sh +++ /dev/null @@ -1,25 +0,0 @@ -curl 'https://router.huggingface.co/fireworks-ai/v1/chat/completions' \ --H 'Authorization: Bearer api_token' \ --H 'Content-Type: application/json' \ ---data '{ - "model": "", - "messages": [ - { - "role": "user", - "content": [ - { - "type": "text", - "text": "Describe this image in one sentence." - }, - { - "type": "image_url", - "image_url": { - "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" - } - } - ] - } - ], - "max_tokens": 500, - "stream": true -}' \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.curl.hf-inference.sh b/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.curl.hf-inference.sh deleted file mode 100644 index cf7c484801..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.curl.hf-inference.sh +++ /dev/null @@ -1,25 +0,0 @@ -curl 'https://router.huggingface.co/hf-inference/models/meta-llama/Llama-3.2-11B-Vision-Instruct/v1/chat/completions' \ --H 'Authorization: Bearer api_token' \ --H 'Content-Type: application/json' \ ---data '{ - "model": "meta-llama/Llama-3.2-11B-Vision-Instruct", - "messages": [ - { - "role": "user", - "content": [ - { - "type": "text", - "text": "Describe this image in one sentence." - }, - { - "type": "image_url", - "image_url": { - "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" - } - } - ] - } - ], - "max_tokens": 500, - "stream": true -}' \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.huggingface.js.fireworks-ai.js b/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.huggingface.js.fireworks-ai.js deleted file mode 100644 index 2407499efe..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.huggingface.js.fireworks-ai.js +++ /dev/null @@ -1,36 +0,0 @@ -import { HfInference } from "@huggingface/inference"; - -const client = new HfInference("api_token"); - -let out = ""; - -const stream = client.chatCompletionStream({ - model: "meta-llama/Llama-3.2-11B-Vision-Instruct", - messages: [ - { - role: "user", - content: [ - { - type: "text", - text: "Describe this image in one sentence." - }, - { - type: "image_url", - image_url: { - url: "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" - } - } - ] - } - ], - provider: "fireworks-ai", - max_tokens: 500, -}); - -for await (const chunk of stream) { - if (chunk.choices && chunk.choices.length > 0) { - const newContent = chunk.choices[0].delta.content; - out += newContent; - console.log(newContent); - } -} \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.huggingface.js.hf-inference.js b/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.huggingface.js.hf-inference.js deleted file mode 100644 index 45886e7709..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.huggingface.js.hf-inference.js +++ /dev/null @@ -1,36 +0,0 @@ -import { HfInference } from "@huggingface/inference"; - -const client = new HfInference("api_token"); - -let out = ""; - -const stream = client.chatCompletionStream({ - model: "meta-llama/Llama-3.2-11B-Vision-Instruct", - messages: [ - { - role: "user", - content: [ - { - type: "text", - text: "Describe this image in one sentence." - }, - { - type: "image_url", - image_url: { - url: "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" - } - } - ] - } - ], - provider: "hf-inference", - max_tokens: 500, -}); - -for await (const chunk of stream) { - if (chunk.choices && chunk.choices.length > 0) { - const newContent = chunk.choices[0].delta.content; - out += newContent; - console.log(newContent); - } -} \ 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 deleted file mode 100644 index ad86364e4d..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.fireworks-ai.py +++ /dev/null @@ -1,34 +0,0 @@ -from huggingface_hub import InferenceClient - -client = InferenceClient( - provider="fireworks-ai", - api_key="api_token", -) - -messages = [ - { - "role": "user", - "content": [ - { - "type": "text", - "text": "Describe this image in one sentence." - }, - { - "type": "image_url", - "image_url": { - "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" - } - } - ] - } -] - -stream = client.chat.completions.create( - 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 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 deleted file mode 100644 index 7d58f8020a..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.hf-inference.py +++ /dev/null @@ -1,34 +0,0 @@ -from huggingface_hub import InferenceClient - -client = InferenceClient( - provider="hf-inference", - api_key="api_token", -) - -messages = [ - { - "role": "user", - "content": [ - { - "type": "text", - "text": "Describe this image in one sentence." - }, - { - "type": "image_url", - "image_url": { - "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" - } - } - ] - } -] - -stream = client.chat.completions.create( - 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 diff --git a/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.js b/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.js deleted file mode 100644 index 53e2830b6a..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.js +++ /dev/null @@ -1,39 +0,0 @@ -import { OpenAI } from "openai"; - -const client = new OpenAI({ - baseURL: "https://router.huggingface.co/fireworks-ai", - apiKey: "api_token" -}); - -let out = ""; - -const stream = await client.chat.completions.create({ - model: "", - messages: [ - { - role: "user", - content: [ - { - type: "text", - text: "Describe this image in one sentence." - }, - { - type: "image_url", - image_url: { - url: "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" - } - } - ] - } - ], - max_tokens: 500, - stream: true, -}); - -for await (const chunk of stream) { - if (chunk.choices && chunk.choices.length > 0) { - const newContent = chunk.choices[0].delta.content; - out += newContent; - console.log(newContent); - } -} \ No newline at end of file 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 deleted file mode 100644 index 33acb547ad..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.py +++ /dev/null @@ -1,34 +0,0 @@ -from openai import OpenAI - -client = OpenAI( - base_url="https://router.huggingface.co/fireworks-ai", - api_key="api_token" -) - -messages = [ - { - "role": "user", - "content": [ - { - "type": "text", - "text": "Describe this image in one sentence." - }, - { - "type": "image_url", - "image_url": { - "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" - } - } - ] - } -] - -stream = client.chat.completions.create( - 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 diff --git a/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.js b/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.js deleted file mode 100644 index 093c104780..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.js +++ /dev/null @@ -1,39 +0,0 @@ -import { OpenAI } from "openai"; - -const client = new OpenAI({ - baseURL: "https://router.huggingface.co/hf-inference/v1", - apiKey: "api_token" -}); - -let out = ""; - -const stream = await client.chat.completions.create({ - model: "meta-llama/Llama-3.2-11B-Vision-Instruct", - messages: [ - { - role: "user", - content: [ - { - type: "text", - text: "Describe this image in one sentence." - }, - { - type: "image_url", - image_url: { - url: "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" - } - } - ] - } - ], - max_tokens: 500, - stream: true, -}); - -for await (const chunk of stream) { - if (chunk.choices && chunk.choices.length > 0) { - const newContent = chunk.choices[0].delta.content; - out += newContent; - console.log(newContent); - } -} \ 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 deleted file mode 100644 index 6c788816ee..0000000000 --- a/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.py +++ /dev/null @@ -1,34 +0,0 @@ -from openai import OpenAI - -client = OpenAI( - base_url="https://router.huggingface.co/hf-inference/v1", - api_key="api_token" -) - -messages = [ - { - "role": "user", - "content": [ - { - "type": "text", - "text": "Describe this image in one sentence." - }, - { - "type": "image_url", - "image_url": { - "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" - } - } - ] - } -] - -stream = client.chat.completions.create( - 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 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 deleted file mode 100644 index 46e437c63a..0000000000 --- a/packages/tasks-gen/snippets-fixtures/document-question-answering/0.huggingface_hub.hf-inference.py +++ /dev/null @@ -1,11 +0,0 @@ -from huggingface_hub import InferenceClient - -client = InferenceClient( - provider="hf-inference", - api_key="api_token", -) -output = client.document_question_answering( - "cat.png", - 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 deleted file mode 100644 index 51fed678d8..0000000000 --- a/packages/tasks-gen/snippets-fixtures/document-question-answering/1.requests.hf-inference.py +++ /dev/null @@ -1,19 +0,0 @@ -import base64 -import requests - -API_URL = "https://router.huggingface.co/hf-inference/models/impira/layoutlm-invoices" -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() - -output = query({ - "inputs": { - "image": "cat.png", - "question": "What is in this image?" - }, -}) \ No newline at end of file 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 deleted file mode 100644 index c21949966a..0000000000 --- a/packages/tasks-gen/snippets-fixtures/image-to-image/0.huggingface_hub.hf-inference.py +++ /dev/null @@ -1,12 +0,0 @@ -from huggingface_hub import InferenceClient - -client = InferenceClient( - provider="hf-inference", - api_key="api_token", -) -# output is a PIL.Image object -image = client.image_to_image( - "cat.png", - prompt="Turn the cat into a tiger.", - model="stabilityai/stable-diffusion-xl-refiner-1.0", -) \ No newline at end of file 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 deleted file mode 100644 index 2d16ba05f5..0000000000 --- a/packages/tasks-gen/snippets-fixtures/image-to-image/1.requests.hf-inference.py +++ /dev/null @@ -1,22 +0,0 @@ -import base64 -import requests - -API_URL = "https://router.huggingface.co/hf-inference/models/stabilityai/stable-diffusion-xl-refiner-1.0" -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 - -image_bytes = query({ - "inputs": "cat.png", - "parameters": {"prompt": "Turn the cat into a tiger."}, -}) - -# 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/tasks-gen/snippets-fixtures/text-classification/0.curl.hf-inference.sh b/packages/tasks-gen/snippets-fixtures/text-classification/0.curl.hf-inference.sh deleted file mode 100644 index 5661a427b2..0000000000 --- a/packages/tasks-gen/snippets-fixtures/text-classification/0.curl.hf-inference.sh +++ /dev/null @@ -1,5 +0,0 @@ -curl https://router.huggingface.co/hf-inference/models/distilbert/distilbert-base-uncased-finetuned-sst-2-english \ - -X POST \ - -d '{"inputs": "I like you. I love you"}' \ - -H 'Content-Type: application/json' \ - -H 'Authorization: Bearer api_token' \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/text-classification/0.huggingface.js.hf-inference.js b/packages/tasks-gen/snippets-fixtures/text-classification/0.huggingface.js.hf-inference.js deleted file mode 100644 index 5d7bfb7307..0000000000 --- a/packages/tasks-gen/snippets-fixtures/text-classification/0.huggingface.js.hf-inference.js +++ /dev/null @@ -1,11 +0,0 @@ -import { HfInference } from "@huggingface/inference"; - -const client = new HfInference("api_token"); - -const output = await client.textClassification({ - model: "distilbert/distilbert-base-uncased-finetuned-sst-2-english", - inputs: "I like you. I love you", - provider: "hf-inference", -}); - -console.log(output); 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 deleted file mode 100644 index d6b9b9aa45..0000000000 --- a/packages/tasks-gen/snippets-fixtures/text-classification/0.huggingface_hub.hf-inference.py +++ /dev/null @@ -1,13 +0,0 @@ -from huggingface_hub import InferenceClient - -client = InferenceClient( - provider="hf-inference", - api_key="api_token", -) - -result = client.text_classification( - inputs="I like you. I love you", - model="distilbert/distilbert-base-uncased-finetuned-sst-2-english", -) - -print(result) diff --git a/packages/tasks-gen/snippets-fixtures/text-classification/1.fetch.hf-inference.js b/packages/tasks-gen/snippets-fixtures/text-classification/1.fetch.hf-inference.js deleted file mode 100644 index c6e9a43b21..0000000000 --- a/packages/tasks-gen/snippets-fixtures/text-classification/1.fetch.hf-inference.js +++ /dev/null @@ -1,19 +0,0 @@ -async function query(data) { - const response = await fetch( - "https://router.huggingface.co/hf-inference/models/distilbert/distilbert-base-uncased-finetuned-sst-2-english", - { - headers: { - Authorization: "Bearer api_token", - "Content-Type": "application/json", - }, - method: "POST", - body: JSON.stringify(data), - } - ); - const result = await response.json(); - return result; -} - -query({"inputs": "I like you. I love you"}).then((response) => { - console.log(JSON.stringify(response)); -}); \ 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 deleted file mode 100644 index 281fca0df2..0000000000 --- a/packages/tasks-gen/snippets-fixtures/text-classification/1.requests.hf-inference.py +++ /dev/null @@ -1,12 +0,0 @@ -import requests - -API_URL = "https://router.huggingface.co/hf-inference/models/distilbert/distilbert-base-uncased-finetuned-sst-2-english" -headers = {"Authorization": "Bearer api_token"} - -def query(payload): - response = requests.post(API_URL, headers=headers, json=payload) - return response.json() - -output = query({ - "inputs": "I like you. I love you", -}) \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/text-to-image/0.curl.hf-inference.sh b/packages/tasks-gen/snippets-fixtures/text-to-image/0.curl.hf-inference.sh deleted file mode 100644 index b89f831606..0000000000 --- a/packages/tasks-gen/snippets-fixtures/text-to-image/0.curl.hf-inference.sh +++ /dev/null @@ -1,5 +0,0 @@ -curl https://router.huggingface.co/hf-inference/models/black-forest-labs/FLUX.1-schnell \ - -X POST \ - -d '{"inputs": "Astronaut riding a horse"}' \ - -H 'Content-Type: application/json' \ - -H 'Authorization: Bearer api_token' \ No newline at end of file diff --git a/packages/tasks-gen/snippets-fixtures/text-to-image/0.huggingface.js.fal-ai.js b/packages/tasks-gen/snippets-fixtures/text-to-image/0.huggingface.js.fal-ai.js deleted file mode 100644 index 1eff0bedbe..0000000000 --- a/packages/tasks-gen/snippets-fixtures/text-to-image/0.huggingface.js.fal-ai.js +++ /dev/null @@ -1,11 +0,0 @@ -import { HfInference } from "@huggingface/inference"; - -const client = new HfInference("api_token"); - -const image = await client.textToImage({ - model: "black-forest-labs/FLUX.1-schnell", - inputs: "Astronaut riding a horse", - parameters: { num_inference_steps: 5 }, - provider: "fal-ai", -}); -/// Use the generated image (it's a Blob) diff --git a/packages/tasks-gen/snippets-fixtures/text-to-image/0.huggingface.js.hf-inference.js b/packages/tasks-gen/snippets-fixtures/text-to-image/0.huggingface.js.hf-inference.js deleted file mode 100644 index f506de747a..0000000000 --- a/packages/tasks-gen/snippets-fixtures/text-to-image/0.huggingface.js.hf-inference.js +++ /dev/null @@ -1,11 +0,0 @@ -import { HfInference } from "@huggingface/inference"; - -const client = new HfInference("api_token"); - -const image = await client.textToImage({ - model: "black-forest-labs/FLUX.1-schnell", - inputs: "Astronaut riding a horse", - parameters: { num_inference_steps: 5 }, - provider: "hf-inference", -}); -/// Use the generated image (it's a Blob) 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 deleted file mode 100644 index 8f8a6b1021..0000000000 --- a/packages/tasks-gen/snippets-fixtures/text-to-image/0.huggingface_hub.fal-ai.py +++ /dev/null @@ -1,12 +0,0 @@ -from huggingface_hub import InferenceClient - -client = InferenceClient( - provider="fal-ai", - api_key="api_token", -) - -# output is a PIL.Image object -image = client.text_to_image( - "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 deleted file mode 100644 index b4c48cec43..0000000000 --- a/packages/tasks-gen/snippets-fixtures/text-to-image/0.huggingface_hub.hf-inference.py +++ /dev/null @@ -1,12 +0,0 @@ -from huggingface_hub import InferenceClient - -client = InferenceClient( - provider="hf-inference", - api_key="api_token", -) - -# output is a PIL.Image object -image = client.text_to_image( - "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.fetch.hf-inference.js b/packages/tasks-gen/snippets-fixtures/text-to-image/1.fetch.hf-inference.js deleted file mode 100644 index b344bae816..0000000000 --- a/packages/tasks-gen/snippets-fixtures/text-to-image/1.fetch.hf-inference.js +++ /dev/null @@ -1,18 +0,0 @@ -async function query(data) { - const response = await fetch( - "https://router.huggingface.co/hf-inference/models/black-forest-labs/FLUX.1-schnell", - { - headers: { - Authorization: "Bearer api_token", - "Content-Type": "application/json", - }, - method: "POST", - body: JSON.stringify(data), - } - ); - const result = await response.blob(); - return result; -} -query({"inputs": "Astronaut riding a horse"}).then((response) => { - // Use image -}); \ 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 deleted file mode 100644 index a8e1ad9de0..0000000000 --- a/packages/tasks-gen/snippets-fixtures/text-to-image/1.requests.hf-inference.py +++ /dev/null @@ -1,17 +0,0 @@ -import requests - -API_URL = "https://router.huggingface.co/hf-inference/models/black-forest-labs/FLUX.1-schnell" -headers = {"Authorization": "Bearer api_token"} - -def query(payload): - response = requests.post(API_URL, headers=headers, json=payload) - return response.content - -image_bytes = query({ - "inputs": "Astronaut riding a horse", -}) - -# 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/tasks-gen/snippets-fixtures/text-to-video/0.huggingface.js.fal-ai.js b/packages/tasks-gen/snippets-fixtures/text-to-video/0.huggingface.js.fal-ai.js deleted file mode 100644 index 1176dc2716..0000000000 --- a/packages/tasks-gen/snippets-fixtures/text-to-video/0.huggingface.js.fal-ai.js +++ /dev/null @@ -1,11 +0,0 @@ -import { HfInference } from "@huggingface/inference"; - -const client = new HfInference("api_token"); - -const video = await client.textToVideo({ - model: "tencent/HunyuanVideo", - provider: "fal-ai", - inputs: "A young man walking on the street", - parameters: { num_inference_steps: 5 }, -}); -// Use the generated video (it's a Blob) diff --git a/packages/tasks-gen/snippets-fixtures/text-to-video/0.huggingface.js.replicate.js b/packages/tasks-gen/snippets-fixtures/text-to-video/0.huggingface.js.replicate.js deleted file mode 100644 index fa0c617b3b..0000000000 --- a/packages/tasks-gen/snippets-fixtures/text-to-video/0.huggingface.js.replicate.js +++ /dev/null @@ -1,11 +0,0 @@ -import { HfInference } from "@huggingface/inference"; - -const client = new HfInference("api_token"); - -const video = await client.textToVideo({ - model: "tencent/HunyuanVideo", - provider: "replicate", - inputs: "A young man walking on the street", - parameters: { num_inference_steps: 5 }, -}); -// Use the generated video (it's a Blob) 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 deleted file mode 100644 index fc96fd381b..0000000000 --- a/packages/tasks-gen/snippets-fixtures/text-to-video/0.huggingface_hub.fal-ai.py +++ /dev/null @@ -1,11 +0,0 @@ -from huggingface_hub import InferenceClient - -client = InferenceClient( - provider="fal-ai", - api_key="api_token", -) - -video = client.text_to_video( - "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 deleted file mode 100644 index 208c583119..0000000000 --- a/packages/tasks-gen/snippets-fixtures/text-to-video/0.huggingface_hub.replicate.py +++ /dev/null @@ -1,11 +0,0 @@ -from huggingface_hub import InferenceClient - -client = InferenceClient( - provider="replicate", - api_key="api_token", -) - -video = client.text_to_video( - "A young man walking on the street", - model="tencent/HunyuanVideo", -) \ No newline at end of file From f8ecf337447ed60c23b60a53630971bfabdfd5cc Mon Sep 17 00:00:00 2001 From: Wauplin Date: Thu, 6 Mar 2025 18:39:56 +0100 Subject: [PATCH 16/26] moving generated snippets --- .../0.huggingface_hub.hf-inference.py | 0 .../automatic-speech-recognition/1.requests.hf-inference.py | 0 .../0.huggingface_hub.hf-inference.py | 0 .../1.requests.hf-inference.py | 0 .../conversational-llm-non-stream/0.curl.hf-inference.sh | 0 .../conversational-llm-non-stream/0.curl.together.sh | 0 .../0.huggingface.js.hf-inference.js | 0 .../conversational-llm-non-stream/0.huggingface.js.together.js | 0 .../0.huggingface_hub.hf-inference.py | 0 .../conversational-llm-non-stream/0.huggingface_hub.together.py | 0 .../conversational-llm-non-stream/1.openai.hf-inference.js | 0 .../conversational-llm-non-stream/1.openai.hf-inference.py | 0 .../conversational-llm-non-stream/1.openai.together.js | 0 .../conversational-llm-non-stream/1.openai.together.py | 0 .../conversational-llm-stream/0.curl.hf-inference.sh | 0 .../conversational-llm-stream/0.curl.together.sh | 0 .../conversational-llm-stream/0.huggingface.js.hf-inference.js | 0 .../conversational-llm-stream/0.huggingface.js.together.js | 0 .../conversational-llm-stream/0.huggingface_hub.hf-inference.py | 0 .../conversational-llm-stream/0.huggingface_hub.together.py | 0 .../conversational-llm-stream/1.openai.hf-inference.js | 0 .../conversational-llm-stream/1.openai.hf-inference.py | 0 .../conversational-llm-stream/1.openai.together.js | 0 .../conversational-llm-stream/1.openai.together.py | 0 .../conversational-vlm-non-stream/0.curl.fireworks-ai.sh | 0 .../conversational-vlm-non-stream/0.curl.hf-inference.sh | 0 .../0.huggingface.js.fireworks-ai.js | 0 .../0.huggingface.js.hf-inference.js | 0 .../0.huggingface_hub.fireworks-ai.py | 0 .../0.huggingface_hub.hf-inference.py | 0 .../conversational-vlm-non-stream/1.openai.fireworks-ai.js | 0 .../conversational-vlm-non-stream/1.openai.fireworks-ai.py | 0 .../conversational-vlm-non-stream/1.openai.hf-inference.js | 0 .../conversational-vlm-non-stream/1.openai.hf-inference.py | 0 .../conversational-vlm-stream/0.curl.fireworks-ai.sh | 0 .../conversational-vlm-stream/0.curl.hf-inference.sh | 0 .../conversational-vlm-stream/0.huggingface.js.fireworks-ai.js | 0 .../conversational-vlm-stream/0.huggingface.js.hf-inference.js | 0 .../conversational-vlm-stream/0.huggingface_hub.fireworks-ai.py | 0 .../conversational-vlm-stream/0.huggingface_hub.hf-inference.py | 0 .../conversational-vlm-stream/1.openai.fireworks-ai.js | 0 .../conversational-vlm-stream/1.openai.fireworks-ai.py | 0 .../conversational-vlm-stream/1.openai.hf-inference.js | 0 .../conversational-vlm-stream/1.openai.hf-inference.py | 0 .../document-question-answering/0.huggingface_hub.hf-inference.py | 0 .../document-question-answering/1.requests.hf-inference.py | 0 .../image-to-image/0.huggingface_hub.hf-inference.py | 0 .../snippets-fixtures/image-to-image/1.requests.hf-inference.py | 0 .../snippets-fixtures/tabular/0.requests.hf-inference.py | 0 .../snippets-fixtures/text-classification/0.curl.hf-inference.sh | 0 .../text-classification/0.huggingface.js.hf-inference.js | 0 .../text-classification/0.huggingface_hub.hf-inference.py | 0 .../snippets-fixtures/text-classification/1.fetch.hf-inference.js | 0 .../text-classification/1.requests.hf-inference.py | 0 .../text-to-audio-transformers/0.requests.hf-inference.py | 0 .../snippets-fixtures/text-to-image/0.curl.hf-inference.sh | 0 .../snippets-fixtures/text-to-image/0.huggingface.js.fal-ai.js | 0 .../text-to-image/0.huggingface.js.hf-inference.js | 0 .../snippets-fixtures/text-to-image/0.huggingface_hub.fal-ai.py | 0 .../text-to-image/0.huggingface_hub.hf-inference.py | 0 .../snippets-fixtures/text-to-image/1.fal_client.fal-ai.py | 0 .../snippets-fixtures/text-to-image/1.fetch.hf-inference.js | 0 .../snippets-fixtures/text-to-image/1.requests.hf-inference.py | 0 .../snippets-fixtures/text-to-video/0.huggingface.js.fal-ai.js | 0 .../snippets-fixtures/text-to-video/0.huggingface.js.replicate.js | 0 .../snippets-fixtures/text-to-video/0.huggingface_hub.fal-ai.py | 0 .../text-to-video/0.huggingface_hub.replicate.py | 0 .../zero-shot-classification/0.requests.hf-inference.py | 0 .../zero-shot-image-classification/0.requests.hf-inference.py | 0 69 files changed, 0 insertions(+), 0 deletions(-) rename packages/{inference => tasks-gen}/snippets-fixtures/automatic-speech-recognition/0.huggingface_hub.hf-inference.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/automatic-speech-recognition/1.requests.hf-inference.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/basic-snippet--token-classification/0.huggingface_hub.hf-inference.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/basic-snippet--token-classification/1.requests.hf-inference.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-llm-non-stream/0.curl.hf-inference.sh (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-llm-non-stream/0.curl.together.sh (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-llm-non-stream/0.huggingface.js.hf-inference.js (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-llm-non-stream/0.huggingface.js.together.js (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.hf-inference.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.together.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-llm-non-stream/1.openai.hf-inference.js (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-llm-non-stream/1.openai.hf-inference.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-llm-non-stream/1.openai.together.js (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-llm-non-stream/1.openai.together.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-llm-stream/0.curl.hf-inference.sh (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-llm-stream/0.curl.together.sh (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-llm-stream/0.huggingface.js.hf-inference.js (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-llm-stream/0.huggingface.js.together.js (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.hf-inference.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.together.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.js (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-llm-stream/1.openai.together.js (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-llm-stream/1.openai.together.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-vlm-non-stream/0.curl.fireworks-ai.sh (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-vlm-non-stream/0.curl.hf-inference.sh (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-vlm-non-stream/0.huggingface.js.fireworks-ai.js (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-vlm-non-stream/0.huggingface.js.hf-inference.js (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.fireworks-ai.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.hf-inference.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-vlm-non-stream/1.openai.fireworks-ai.js (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-vlm-non-stream/1.openai.fireworks-ai.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-vlm-non-stream/1.openai.hf-inference.js (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-vlm-non-stream/1.openai.hf-inference.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-vlm-stream/0.curl.fireworks-ai.sh (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-vlm-stream/0.curl.hf-inference.sh (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-vlm-stream/0.huggingface.js.fireworks-ai.js (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-vlm-stream/0.huggingface.js.hf-inference.js (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.fireworks-ai.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.hf-inference.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.js (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.js (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/document-question-answering/0.huggingface_hub.hf-inference.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/document-question-answering/1.requests.hf-inference.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/image-to-image/0.huggingface_hub.hf-inference.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/image-to-image/1.requests.hf-inference.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/tabular/0.requests.hf-inference.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/text-classification/0.curl.hf-inference.sh (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/text-classification/0.huggingface.js.hf-inference.js (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/text-classification/0.huggingface_hub.hf-inference.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/text-classification/1.fetch.hf-inference.js (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/text-classification/1.requests.hf-inference.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/text-to-audio-transformers/0.requests.hf-inference.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/text-to-image/0.curl.hf-inference.sh (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/text-to-image/0.huggingface.js.fal-ai.js (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/text-to-image/0.huggingface.js.hf-inference.js (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/text-to-image/0.huggingface_hub.fal-ai.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/text-to-image/0.huggingface_hub.hf-inference.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/text-to-image/1.fal_client.fal-ai.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/text-to-image/1.fetch.hf-inference.js (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/text-to-image/1.requests.hf-inference.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/text-to-video/0.huggingface.js.fal-ai.js (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/text-to-video/0.huggingface.js.replicate.js (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/text-to-video/0.huggingface_hub.fal-ai.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/text-to-video/0.huggingface_hub.replicate.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/zero-shot-classification/0.requests.hf-inference.py (100%) rename packages/{inference => tasks-gen}/snippets-fixtures/zero-shot-image-classification/0.requests.hf-inference.py (100%) diff --git a/packages/inference/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 similarity index 100% rename from packages/inference/snippets-fixtures/automatic-speech-recognition/0.huggingface_hub.hf-inference.py rename to packages/tasks-gen/snippets-fixtures/automatic-speech-recognition/0.huggingface_hub.hf-inference.py diff --git a/packages/inference/snippets-fixtures/automatic-speech-recognition/1.requests.hf-inference.py b/packages/tasks-gen/snippets-fixtures/automatic-speech-recognition/1.requests.hf-inference.py similarity index 100% rename from packages/inference/snippets-fixtures/automatic-speech-recognition/1.requests.hf-inference.py rename to packages/tasks-gen/snippets-fixtures/automatic-speech-recognition/1.requests.hf-inference.py diff --git a/packages/inference/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 similarity index 100% rename from packages/inference/snippets-fixtures/basic-snippet--token-classification/0.huggingface_hub.hf-inference.py rename to packages/tasks-gen/snippets-fixtures/basic-snippet--token-classification/0.huggingface_hub.hf-inference.py diff --git a/packages/inference/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 similarity index 100% rename from packages/inference/snippets-fixtures/basic-snippet--token-classification/1.requests.hf-inference.py rename to packages/tasks-gen/snippets-fixtures/basic-snippet--token-classification/1.requests.hf-inference.py diff --git a/packages/inference/snippets-fixtures/conversational-llm-non-stream/0.curl.hf-inference.sh b/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.curl.hf-inference.sh similarity index 100% rename from packages/inference/snippets-fixtures/conversational-llm-non-stream/0.curl.hf-inference.sh rename to packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.curl.hf-inference.sh diff --git a/packages/inference/snippets-fixtures/conversational-llm-non-stream/0.curl.together.sh b/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.curl.together.sh similarity index 100% rename from packages/inference/snippets-fixtures/conversational-llm-non-stream/0.curl.together.sh rename to packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.curl.together.sh diff --git a/packages/inference/snippets-fixtures/conversational-llm-non-stream/0.huggingface.js.hf-inference.js b/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.huggingface.js.hf-inference.js similarity index 100% rename from packages/inference/snippets-fixtures/conversational-llm-non-stream/0.huggingface.js.hf-inference.js rename to packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.huggingface.js.hf-inference.js diff --git a/packages/inference/snippets-fixtures/conversational-llm-non-stream/0.huggingface.js.together.js b/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.huggingface.js.together.js similarity index 100% rename from packages/inference/snippets-fixtures/conversational-llm-non-stream/0.huggingface.js.together.js rename to packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.huggingface.js.together.js diff --git a/packages/inference/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 similarity index 100% rename from packages/inference/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.hf-inference.py rename to packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.hf-inference.py diff --git a/packages/inference/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 similarity index 100% rename from packages/inference/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.together.py rename to packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/0.huggingface_hub.together.py diff --git a/packages/inference/snippets-fixtures/conversational-llm-non-stream/1.openai.hf-inference.js b/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/1.openai.hf-inference.js similarity index 100% rename from packages/inference/snippets-fixtures/conversational-llm-non-stream/1.openai.hf-inference.js rename to packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/1.openai.hf-inference.js diff --git a/packages/inference/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 similarity index 100% rename from packages/inference/snippets-fixtures/conversational-llm-non-stream/1.openai.hf-inference.py rename to packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/1.openai.hf-inference.py diff --git a/packages/inference/snippets-fixtures/conversational-llm-non-stream/1.openai.together.js b/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/1.openai.together.js similarity index 100% rename from packages/inference/snippets-fixtures/conversational-llm-non-stream/1.openai.together.js rename to packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/1.openai.together.js diff --git a/packages/inference/snippets-fixtures/conversational-llm-non-stream/1.openai.together.py b/packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/1.openai.together.py similarity index 100% rename from packages/inference/snippets-fixtures/conversational-llm-non-stream/1.openai.together.py rename to packages/tasks-gen/snippets-fixtures/conversational-llm-non-stream/1.openai.together.py diff --git a/packages/inference/snippets-fixtures/conversational-llm-stream/0.curl.hf-inference.sh b/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.curl.hf-inference.sh similarity index 100% rename from packages/inference/snippets-fixtures/conversational-llm-stream/0.curl.hf-inference.sh rename to packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.curl.hf-inference.sh diff --git a/packages/inference/snippets-fixtures/conversational-llm-stream/0.curl.together.sh b/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.curl.together.sh similarity index 100% rename from packages/inference/snippets-fixtures/conversational-llm-stream/0.curl.together.sh rename to packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.curl.together.sh diff --git a/packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface.js.hf-inference.js b/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.huggingface.js.hf-inference.js similarity index 100% rename from packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface.js.hf-inference.js rename to packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.huggingface.js.hf-inference.js diff --git a/packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface.js.together.js b/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.huggingface.js.together.js similarity index 100% rename from packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface.js.together.js rename to packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.huggingface.js.together.js diff --git a/packages/inference/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 similarity index 100% rename from packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.hf-inference.py rename to packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.hf-inference.py diff --git a/packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.together.py b/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.together.py similarity index 100% rename from packages/inference/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.together.py rename to packages/tasks-gen/snippets-fixtures/conversational-llm-stream/0.huggingface_hub.together.py diff --git a/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.js b/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.js similarity index 100% rename from packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.js rename to packages/tasks-gen/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.js diff --git a/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.py b/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.py similarity index 100% rename from packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.py rename to packages/tasks-gen/snippets-fixtures/conversational-llm-stream/1.openai.hf-inference.py diff --git a/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.together.js b/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/1.openai.together.js similarity index 100% rename from packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.together.js rename to packages/tasks-gen/snippets-fixtures/conversational-llm-stream/1.openai.together.js diff --git a/packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.together.py b/packages/tasks-gen/snippets-fixtures/conversational-llm-stream/1.openai.together.py similarity index 100% rename from packages/inference/snippets-fixtures/conversational-llm-stream/1.openai.together.py rename to packages/tasks-gen/snippets-fixtures/conversational-llm-stream/1.openai.together.py diff --git a/packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.curl.fireworks-ai.sh b/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.curl.fireworks-ai.sh similarity index 100% rename from packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.curl.fireworks-ai.sh rename to packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.curl.fireworks-ai.sh diff --git a/packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.curl.hf-inference.sh b/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.curl.hf-inference.sh similarity index 100% rename from packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.curl.hf-inference.sh rename to packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.curl.hf-inference.sh diff --git a/packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.huggingface.js.fireworks-ai.js b/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.huggingface.js.fireworks-ai.js similarity index 100% rename from packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.huggingface.js.fireworks-ai.js rename to packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.huggingface.js.fireworks-ai.js diff --git a/packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.huggingface.js.hf-inference.js b/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.huggingface.js.hf-inference.js similarity index 100% rename from packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.huggingface.js.hf-inference.js rename to packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.huggingface.js.hf-inference.js diff --git a/packages/inference/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 similarity index 100% rename from packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.fireworks-ai.py rename to packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.fireworks-ai.py diff --git a/packages/inference/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 similarity index 100% rename from packages/inference/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.hf-inference.py rename to packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/0.huggingface_hub.hf-inference.py diff --git a/packages/inference/snippets-fixtures/conversational-vlm-non-stream/1.openai.fireworks-ai.js b/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/1.openai.fireworks-ai.js similarity index 100% rename from packages/inference/snippets-fixtures/conversational-vlm-non-stream/1.openai.fireworks-ai.js rename to packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/1.openai.fireworks-ai.js diff --git a/packages/inference/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 similarity index 100% rename from packages/inference/snippets-fixtures/conversational-vlm-non-stream/1.openai.fireworks-ai.py rename to packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/1.openai.fireworks-ai.py diff --git a/packages/inference/snippets-fixtures/conversational-vlm-non-stream/1.openai.hf-inference.js b/packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/1.openai.hf-inference.js similarity index 100% rename from packages/inference/snippets-fixtures/conversational-vlm-non-stream/1.openai.hf-inference.js rename to packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/1.openai.hf-inference.js diff --git a/packages/inference/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 similarity index 100% rename from packages/inference/snippets-fixtures/conversational-vlm-non-stream/1.openai.hf-inference.py rename to packages/tasks-gen/snippets-fixtures/conversational-vlm-non-stream/1.openai.hf-inference.py diff --git a/packages/inference/snippets-fixtures/conversational-vlm-stream/0.curl.fireworks-ai.sh b/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.curl.fireworks-ai.sh similarity index 100% rename from packages/inference/snippets-fixtures/conversational-vlm-stream/0.curl.fireworks-ai.sh rename to packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.curl.fireworks-ai.sh diff --git a/packages/inference/snippets-fixtures/conversational-vlm-stream/0.curl.hf-inference.sh b/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.curl.hf-inference.sh similarity index 100% rename from packages/inference/snippets-fixtures/conversational-vlm-stream/0.curl.hf-inference.sh rename to packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.curl.hf-inference.sh diff --git a/packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface.js.fireworks-ai.js b/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.huggingface.js.fireworks-ai.js similarity index 100% rename from packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface.js.fireworks-ai.js rename to packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.huggingface.js.fireworks-ai.js diff --git a/packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface.js.hf-inference.js b/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.huggingface.js.hf-inference.js similarity index 100% rename from packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface.js.hf-inference.js rename to packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.huggingface.js.hf-inference.js diff --git a/packages/inference/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 similarity index 100% rename from packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.fireworks-ai.py rename to packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.fireworks-ai.py diff --git a/packages/inference/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 similarity index 100% rename from packages/inference/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.hf-inference.py rename to packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.hf-inference.py diff --git a/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.js b/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.js similarity index 100% rename from packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.js rename to packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.js diff --git a/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.py b/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.py similarity index 100% rename from packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.py rename to packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/1.openai.fireworks-ai.py diff --git a/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.js b/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.js similarity index 100% rename from packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.js rename to packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.js diff --git a/packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.py b/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.py similarity index 100% rename from packages/inference/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.py rename to packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/1.openai.hf-inference.py diff --git a/packages/inference/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 similarity index 100% rename from packages/inference/snippets-fixtures/document-question-answering/0.huggingface_hub.hf-inference.py rename to packages/tasks-gen/snippets-fixtures/document-question-answering/0.huggingface_hub.hf-inference.py diff --git a/packages/inference/snippets-fixtures/document-question-answering/1.requests.hf-inference.py b/packages/tasks-gen/snippets-fixtures/document-question-answering/1.requests.hf-inference.py similarity index 100% rename from packages/inference/snippets-fixtures/document-question-answering/1.requests.hf-inference.py rename to packages/tasks-gen/snippets-fixtures/document-question-answering/1.requests.hf-inference.py diff --git a/packages/inference/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 similarity index 100% rename from packages/inference/snippets-fixtures/image-to-image/0.huggingface_hub.hf-inference.py rename to packages/tasks-gen/snippets-fixtures/image-to-image/0.huggingface_hub.hf-inference.py diff --git a/packages/inference/snippets-fixtures/image-to-image/1.requests.hf-inference.py b/packages/tasks-gen/snippets-fixtures/image-to-image/1.requests.hf-inference.py similarity index 100% rename from packages/inference/snippets-fixtures/image-to-image/1.requests.hf-inference.py rename to packages/tasks-gen/snippets-fixtures/image-to-image/1.requests.hf-inference.py diff --git a/packages/inference/snippets-fixtures/tabular/0.requests.hf-inference.py b/packages/tasks-gen/snippets-fixtures/tabular/0.requests.hf-inference.py similarity index 100% rename from packages/inference/snippets-fixtures/tabular/0.requests.hf-inference.py rename to packages/tasks-gen/snippets-fixtures/tabular/0.requests.hf-inference.py diff --git a/packages/inference/snippets-fixtures/text-classification/0.curl.hf-inference.sh b/packages/tasks-gen/snippets-fixtures/text-classification/0.curl.hf-inference.sh similarity index 100% rename from packages/inference/snippets-fixtures/text-classification/0.curl.hf-inference.sh rename to packages/tasks-gen/snippets-fixtures/text-classification/0.curl.hf-inference.sh diff --git a/packages/inference/snippets-fixtures/text-classification/0.huggingface.js.hf-inference.js b/packages/tasks-gen/snippets-fixtures/text-classification/0.huggingface.js.hf-inference.js similarity index 100% rename from packages/inference/snippets-fixtures/text-classification/0.huggingface.js.hf-inference.js rename to packages/tasks-gen/snippets-fixtures/text-classification/0.huggingface.js.hf-inference.js diff --git a/packages/inference/snippets-fixtures/text-classification/0.huggingface_hub.hf-inference.py b/packages/tasks-gen/snippets-fixtures/text-classification/0.huggingface_hub.hf-inference.py similarity index 100% rename from packages/inference/snippets-fixtures/text-classification/0.huggingface_hub.hf-inference.py rename to packages/tasks-gen/snippets-fixtures/text-classification/0.huggingface_hub.hf-inference.py diff --git a/packages/inference/snippets-fixtures/text-classification/1.fetch.hf-inference.js b/packages/tasks-gen/snippets-fixtures/text-classification/1.fetch.hf-inference.js similarity index 100% rename from packages/inference/snippets-fixtures/text-classification/1.fetch.hf-inference.js rename to packages/tasks-gen/snippets-fixtures/text-classification/1.fetch.hf-inference.js diff --git a/packages/inference/snippets-fixtures/text-classification/1.requests.hf-inference.py b/packages/tasks-gen/snippets-fixtures/text-classification/1.requests.hf-inference.py similarity index 100% rename from packages/inference/snippets-fixtures/text-classification/1.requests.hf-inference.py rename to packages/tasks-gen/snippets-fixtures/text-classification/1.requests.hf-inference.py diff --git a/packages/inference/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 similarity index 100% rename from packages/inference/snippets-fixtures/text-to-audio-transformers/0.requests.hf-inference.py rename to packages/tasks-gen/snippets-fixtures/text-to-audio-transformers/0.requests.hf-inference.py diff --git a/packages/inference/snippets-fixtures/text-to-image/0.curl.hf-inference.sh b/packages/tasks-gen/snippets-fixtures/text-to-image/0.curl.hf-inference.sh similarity index 100% rename from packages/inference/snippets-fixtures/text-to-image/0.curl.hf-inference.sh rename to packages/tasks-gen/snippets-fixtures/text-to-image/0.curl.hf-inference.sh diff --git a/packages/inference/snippets-fixtures/text-to-image/0.huggingface.js.fal-ai.js b/packages/tasks-gen/snippets-fixtures/text-to-image/0.huggingface.js.fal-ai.js similarity index 100% rename from packages/inference/snippets-fixtures/text-to-image/0.huggingface.js.fal-ai.js rename to packages/tasks-gen/snippets-fixtures/text-to-image/0.huggingface.js.fal-ai.js diff --git a/packages/inference/snippets-fixtures/text-to-image/0.huggingface.js.hf-inference.js b/packages/tasks-gen/snippets-fixtures/text-to-image/0.huggingface.js.hf-inference.js similarity index 100% rename from packages/inference/snippets-fixtures/text-to-image/0.huggingface.js.hf-inference.js rename to packages/tasks-gen/snippets-fixtures/text-to-image/0.huggingface.js.hf-inference.js diff --git a/packages/inference/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 similarity index 100% rename from packages/inference/snippets-fixtures/text-to-image/0.huggingface_hub.fal-ai.py rename to packages/tasks-gen/snippets-fixtures/text-to-image/0.huggingface_hub.fal-ai.py diff --git a/packages/inference/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 similarity index 100% rename from packages/inference/snippets-fixtures/text-to-image/0.huggingface_hub.hf-inference.py rename to packages/tasks-gen/snippets-fixtures/text-to-image/0.huggingface_hub.hf-inference.py diff --git a/packages/inference/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 similarity index 100% rename from packages/inference/snippets-fixtures/text-to-image/1.fal_client.fal-ai.py rename to packages/tasks-gen/snippets-fixtures/text-to-image/1.fal_client.fal-ai.py diff --git a/packages/inference/snippets-fixtures/text-to-image/1.fetch.hf-inference.js b/packages/tasks-gen/snippets-fixtures/text-to-image/1.fetch.hf-inference.js similarity index 100% rename from packages/inference/snippets-fixtures/text-to-image/1.fetch.hf-inference.js rename to packages/tasks-gen/snippets-fixtures/text-to-image/1.fetch.hf-inference.js diff --git a/packages/inference/snippets-fixtures/text-to-image/1.requests.hf-inference.py b/packages/tasks-gen/snippets-fixtures/text-to-image/1.requests.hf-inference.py similarity index 100% rename from packages/inference/snippets-fixtures/text-to-image/1.requests.hf-inference.py rename to packages/tasks-gen/snippets-fixtures/text-to-image/1.requests.hf-inference.py diff --git a/packages/inference/snippets-fixtures/text-to-video/0.huggingface.js.fal-ai.js b/packages/tasks-gen/snippets-fixtures/text-to-video/0.huggingface.js.fal-ai.js similarity index 100% rename from packages/inference/snippets-fixtures/text-to-video/0.huggingface.js.fal-ai.js rename to packages/tasks-gen/snippets-fixtures/text-to-video/0.huggingface.js.fal-ai.js diff --git a/packages/inference/snippets-fixtures/text-to-video/0.huggingface.js.replicate.js b/packages/tasks-gen/snippets-fixtures/text-to-video/0.huggingface.js.replicate.js similarity index 100% rename from packages/inference/snippets-fixtures/text-to-video/0.huggingface.js.replicate.js rename to packages/tasks-gen/snippets-fixtures/text-to-video/0.huggingface.js.replicate.js diff --git a/packages/inference/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 similarity index 100% rename from packages/inference/snippets-fixtures/text-to-video/0.huggingface_hub.fal-ai.py rename to packages/tasks-gen/snippets-fixtures/text-to-video/0.huggingface_hub.fal-ai.py diff --git a/packages/inference/snippets-fixtures/text-to-video/0.huggingface_hub.replicate.py b/packages/tasks-gen/snippets-fixtures/text-to-video/0.huggingface_hub.replicate.py similarity index 100% rename from packages/inference/snippets-fixtures/text-to-video/0.huggingface_hub.replicate.py rename to packages/tasks-gen/snippets-fixtures/text-to-video/0.huggingface_hub.replicate.py diff --git a/packages/inference/snippets-fixtures/zero-shot-classification/0.requests.hf-inference.py b/packages/tasks-gen/snippets-fixtures/zero-shot-classification/0.requests.hf-inference.py similarity index 100% rename from packages/inference/snippets-fixtures/zero-shot-classification/0.requests.hf-inference.py rename to packages/tasks-gen/snippets-fixtures/zero-shot-classification/0.requests.hf-inference.py diff --git a/packages/inference/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 similarity index 100% rename from packages/inference/snippets-fixtures/zero-shot-image-classification/0.requests.hf-inference.py rename to packages/tasks-gen/snippets-fixtures/zero-shot-image-classification/0.requests.hf-inference.py From ddd62b60d0a858dc808b6dd7b3589977681f5a8b Mon Sep 17 00:00:00 2001 From: Wauplin Date: Thu, 6 Mar 2025 18:40:23 +0100 Subject: [PATCH 17/26] remove tmp script --- packages/inference/package.json | 3 +- packages/inference/scripts/tmp_gen.ts | 308 ------------------ .../scripts/generate-snippets-fixtures.ts | 55 ++++ 3 files changed, 56 insertions(+), 310 deletions(-) delete mode 100644 packages/inference/scripts/tmp_gen.ts diff --git a/packages/inference/package.json b/packages/inference/package.json index 5e2a1d6186..8f0a869cf1 100644 --- a/packages/inference/package.json +++ b/packages/inference/package.json @@ -49,8 +49,7 @@ "prepublishOnly": "pnpm run build", "test": "vitest run --config vitest.config.mts", "test:browser": "vitest run --browser.name=chrome --browser.headless --config vitest.config.mts", - "check": "tsc", - "generate": "tsup src/index.ts --format cjs,esm --clean && tsx scripts/tmp_gen.ts" + "check": "tsc" }, "dependencies": { "@huggingface/tasks": "workspace:^", diff --git a/packages/inference/scripts/tmp_gen.ts b/packages/inference/scripts/tmp_gen.ts deleted file mode 100644 index c9051a2c57..0000000000 --- a/packages/inference/scripts/tmp_gen.ts +++ /dev/null @@ -1,308 +0,0 @@ -/* - * Generates Inference API snippets using @huggingface/tasks snippets. - * - * If used in test mode ("pnpm test"), it compares the generated snippets with the expected ones. - * If used in generation mode ("pnpm generate-snippets-fixtures"), it generates the expected snippets. - * - * Expected snippets are saved under ./snippets-fixtures and are meant to be versioned on GitHub. - * Each snippet is saved in a separate file placed under "./{test-name}/{index}.{client}.{language}": - * - test-name: the name of the test (e.g. "text-to-image", "conversational-llm", etc.) - * - index: the order of the snippet in the array of snippets (0 if not an array) - * - client: the client name (e.g. "requests", "huggingface_hub", "openai", etc.). Default to "default" if client is not specified. - * - language: the language of the snippet (e.g. "sh", "js", "py", etc.) - * - * Example: - * ./packages/tasks-gen/snippets-fixtures/text-to-image/0.huggingface_hub.py - */ - -import { existsSync as pathExists } from "node:fs"; -import * as fs from "node:fs/promises"; -import * as path from "node:path/posix"; - -import { snippets } from "@huggingface/inference"; -import type { SnippetInferenceProvider, InferenceSnippet, ModelDataMinimal } from "@huggingface/tasks"; - -type LANGUAGE = "sh" | "js" | "py"; - -const TEST_CASES: { - testName: string; - model: ModelDataMinimal; - languages: LANGUAGE[]; - providers: SnippetInferenceProvider[]; - opts?: Record; -}[] = [ - { - testName: "automatic-speech-recognition", - model: { - id: "openai/whisper-large-v3-turbo", - pipeline_tag: "automatic-speech-recognition", - tags: [], - inference: "", - }, - languages: ["py"], - providers: ["hf-inference"], - }, - { - testName: "conversational-llm-non-stream", - model: { - id: "meta-llama/Llama-3.1-8B-Instruct", - pipeline_tag: "text-generation", - tags: ["conversational"], - inference: "", - }, - languages: ["sh", "js", "py"], - providers: ["hf-inference", "together"], - opts: { streaming: false }, - }, - { - testName: "conversational-llm-stream", - model: { - id: "meta-llama/Llama-3.1-8B-Instruct", - pipeline_tag: "text-generation", - tags: ["conversational"], - inference: "", - }, - languages: ["sh", "js", "py"], - providers: ["hf-inference", "together"], - opts: { streaming: true }, - }, - { - testName: "conversational-vlm-non-stream", - model: { - id: "meta-llama/Llama-3.2-11B-Vision-Instruct", - pipeline_tag: "image-text-to-text", - tags: ["conversational"], - inference: "", - }, - languages: ["sh", "js", "py"], - providers: ["hf-inference", "fireworks-ai"], - opts: { streaming: false }, - }, - { - testName: "conversational-vlm-stream", - model: { - id: "meta-llama/Llama-3.2-11B-Vision-Instruct", - pipeline_tag: "image-text-to-text", - tags: ["conversational"], - inference: "", - }, - languages: ["sh", "js", "py"], - providers: ["hf-inference", "fireworks-ai"], - opts: { streaming: true }, - }, - { - testName: "document-question-answering", - model: { - id: "impira/layoutlm-invoices", - pipeline_tag: "document-question-answering", - tags: [], - inference: "", - }, - languages: ["py"], - providers: ["hf-inference"], - }, - { - testName: "image-to-image", - model: { - id: "stabilityai/stable-diffusion-xl-refiner-1.0", - pipeline_tag: "image-to-image", - tags: [], - inference: "", - }, - 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: { - id: "black-forest-labs/FLUX.1-schnell", - pipeline_tag: "text-to-image", - tags: [], - inference: "", - }, - providers: ["hf-inference", "fal-ai"], - languages: ["sh", "js", "py"], - }, - { - testName: "text-to-video", - model: { - id: "tencent/HunyuanVideo", - pipeline_tag: "text-to-video", - tags: [], - inference: "", - }, - providers: ["replicate", "fal-ai"], - languages: ["js", "py"], - }, - { - testName: "text-classification", - model: { - id: "distilbert/distilbert-base-uncased-finetuned-sst-2-english", - pipeline_tag: "text-classification", - tags: [], - inference: "", - }, - 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 = { - sh: snippets.curl.getCurlInferenceSnippet, - js: snippets.js.getJsInferenceSnippet, - py: snippets.python.getPythonInferenceSnippet, -} as const; - -const rootDirFinder = (): string => { - let currentPath = path.normalize(import.meta.url).replace("file:", ""); - - while (currentPath !== "/") { - if (pathExists(path.join(currentPath, "package.json"))) { - return currentPath; - } - - currentPath = path.normalize(path.join(currentPath, "..")); - } - - return "/"; -}; - -function getFixtureFolder(testName: string): string { - return path.join(rootDirFinder(), "snippets-fixtures", testName); -} - -function generateInferenceSnippet( - model: ModelDataMinimal, - language: LANGUAGE, - provider: SnippetInferenceProvider, - opts?: Record -): InferenceSnippet[] { - const providerModelId = provider === "hf-inference" ? model.id : `<${provider} alias for ${model.id}>`; - return GET_SNIPPET_FN[language](model, "api_token", provider, providerModelId, opts); -} - -async function getExpectedInferenceSnippet( - testName: string, - language: LANGUAGE, - provider: SnippetInferenceProvider -): Promise { - const fixtureFolder = getFixtureFolder(testName); - const files = await fs.readdir(fixtureFolder); - - const expectedSnippets: InferenceSnippet[] = []; - for (const file of files.filter((file) => file.endsWith("." + language) && file.includes(`.${provider}.`)).sort()) { - const client = path.basename(file).split(".").slice(1, -2).join("."); // e.g. '0.huggingface.js.replicate.js' => "huggingface.js" - const content = await fs.readFile(path.join(fixtureFolder, file), { encoding: "utf-8" }); - expectedSnippets.push({ client, content }); - } - return expectedSnippets; -} - -async function saveExpectedInferenceSnippet( - testName: string, - language: LANGUAGE, - provider: SnippetInferenceProvider, - snippets: InferenceSnippet[] -) { - const fixtureFolder = getFixtureFolder(testName); - await fs.mkdir(fixtureFolder, { recursive: true }); - - for (const [index, snippet] of snippets.entries()) { - const file = path.join(fixtureFolder, `${index}.${snippet.client ?? "default"}.${provider}.${language}`); - await fs.writeFile(file, snippet.content); - } -} - -if (import.meta.vitest) { - // Run test if in test mode - const { describe, expect, it } = import.meta.vitest; - - describe("inference API snippets", () => { - TEST_CASES.forEach(({ testName, model, languages, providers, opts }) => { - describe(testName, () => { - languages.forEach((language) => { - providers.forEach((provider) => { - it(language, async () => { - const generatedSnippets = generateInferenceSnippet(model, language, provider, opts); - const expectedSnippets = await getExpectedInferenceSnippet(testName, language, provider); - expect(generatedSnippets).toEqual(expectedSnippets); - }); - }); - }); - }); - }); - }); -} else { - // Otherwise, generate the fixtures - console.log("✨ Re-generating snippets"); - console.debug(" 🚜 Removing existing fixtures..."); - await fs.rm(path.join(rootDirFinder(), "snippets-fixtures"), { recursive: true, force: true }); - - console.debug(" 🏭 Generating new fixtures..."); - TEST_CASES.forEach(({ testName, model, languages, providers, opts }) => { - console.debug(` ${testName} (${languages.join(", ")}) (${providers.join(", ")})`); - languages.forEach(async (language) => { - providers.forEach(async (provider) => { - const generatedSnippets = generateInferenceSnippet(model, language, provider, opts); - await saveExpectedInferenceSnippet(testName, language, provider, generatedSnippets); - }); - }); - }); - console.log("✅ All done!"); - console.log("👉 Please check the generated fixtures before committing them."); -} 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 = { From 7a498c6fe588a0739b6e1ceace1d277b71901f46 Mon Sep 17 00:00:00 2001 From: Wauplin Date: Thu, 6 Mar 2025 18:50:18 +0100 Subject: [PATCH 18/26] tabs -> whitespaces --- .../requests/zeroShotImageClassification.hbs | 20 +++++++++---------- .../0.requests.hf-inference.py | 20 +++++++++---------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/packages/inference/src/snippets/templates/python/requests/zeroShotImageClassification.hbs b/packages/inference/src/snippets/templates/python/requests/zeroShotImageClassification.hbs index b572fe42cf..34aee45b8c 100644 --- a/packages/inference/src/snippets/templates/python/requests/zeroShotImageClassification.hbs +++ b/packages/inference/src/snippets/templates/python/requests/zeroShotImageClassification.hbs @@ -1,14 +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() + 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"]}, + "image_path": {{{inputs}}}, + "parameters": {"candidate_labels": ["cat", "dog", "llama"]}, }) \ 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 index f25c4eab01..a6cfd80da1 100644 --- 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 @@ -5,16 +5,16 @@ 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() + 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"]}, + "image_path": "cats.jpg", + "parameters": {"candidate_labels": ["cat", "dog", "llama"]}, }) \ No newline at end of file From e23a2a3d14f7883da7f29421217d3e6bcb1239ee Mon Sep 17 00:00:00 2001 From: Wauplin Date: Thu, 6 Mar 2025 18:52:36 +0100 Subject: [PATCH 19/26] tabs -> whitespaces --- .../python/requests/automaticSpeechRecognition.hbs | 8 ++++---- .../snippets/templates/python/requests/basic.hbs | 8 ++++---- .../templates/python/requests/basicFile.hbs | 8 ++++---- .../python/requests/documentQuestionAnswering.hbs | 10 +++++----- .../templates/python/requests/imageToImage.hbs | 14 +++++++------- .../snippets/templates/python/requests/tabular.hbs | 10 +++++----- .../templates/python/requests/textToAudio.hbs | 14 +++++++------- .../templates/python/requests/textToImage.hbs | 6 +++--- .../1.requests.hf-inference.py | 8 ++++---- .../1.requests.hf-inference.py | 8 ++++---- .../1.requests.hf-inference.py | 10 +++++----- .../image-to-image/1.requests.hf-inference.py | 14 +++++++------- .../tabular/0.requests.hf-inference.py | 10 +++++----- .../text-classification/1.requests.hf-inference.py | 8 ++++---- .../0.requests.hf-inference.py | 8 ++++---- .../text-to-image/1.requests.hf-inference.py | 6 +++--- 16 files changed, 75 insertions(+), 75 deletions(-) diff --git a/packages/inference/src/snippets/templates/python/requests/automaticSpeechRecognition.hbs b/packages/inference/src/snippets/templates/python/requests/automaticSpeechRecognition.hbs index 456b237b60..543fde00e7 100644 --- a/packages/inference/src/snippets/templates/python/requests/automaticSpeechRecognition.hbs +++ b/packages/inference/src/snippets/templates/python/requests/automaticSpeechRecognition.hbs @@ -1,7 +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() + 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.hbs b/packages/inference/src/snippets/templates/python/requests/basic.hbs index 7b596b74bf..d46487b3c6 100644 --- a/packages/inference/src/snippets/templates/python/requests/basic.hbs +++ b/packages/inference/src/snippets/templates/python/requests/basic.hbs @@ -1,7 +1,7 @@ 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": {{{inputs}}}, + "inputs": {{{inputs}}}, }) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/basicFile.hbs b/packages/inference/src/snippets/templates/python/requests/basicFile.hbs index 456b237b60..543fde00e7 100644 --- a/packages/inference/src/snippets/templates/python/requests/basicFile.hbs +++ b/packages/inference/src/snippets/templates/python/requests/basicFile.hbs @@ -1,7 +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() + 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.hbs b/packages/inference/src/snippets/templates/python/requests/documentQuestionAnswering.hbs index 9bc82c6640..6d4ed09dfb 100644 --- a/packages/inference/src/snippets/templates/python/requests/documentQuestionAnswering.hbs +++ b/packages/inference/src/snippets/templates/python/requests/documentQuestionAnswering.hbs @@ -1,9 +1,9 @@ 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": {{{inputs.asStr}}}, diff --git a/packages/inference/src/snippets/templates/python/requests/imageToImage.hbs b/packages/inference/src/snippets/templates/python/requests/imageToImage.hbs index 84a49d2fad..9a2ea381a3 100644 --- a/packages/inference/src/snippets/templates/python/requests/imageToImage.hbs +++ b/packages/inference/src/snippets/templates/python/requests/imageToImage.hbs @@ -1,13 +1,13 @@ 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": "{{{inputs.image}}}", - "parameters": {"prompt": "{{{inputs.prompt}}}"}, + "inputs": "{{{inputs.image}}}", + "parameters": {"prompt": "{{{inputs.prompt}}}"}, }) # You can access the image with PIL.Image for example diff --git a/packages/inference/src/snippets/templates/python/requests/tabular.hbs b/packages/inference/src/snippets/templates/python/requests/tabular.hbs index 9656877996..95579d7fa9 100644 --- a/packages/inference/src/snippets/templates/python/requests/tabular.hbs +++ b/packages/inference/src/snippets/templates/python/requests/tabular.hbs @@ -1,9 +1,9 @@ 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 response = query({ - "inputs": { - "data": {{{inputs}}} - }, + "inputs": { + "data": {{{inputs}}} + }, }) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/textToAudio.hbs b/packages/inference/src/snippets/templates/python/requests/textToAudio.hbs index 9503fd3630..6ff67261bf 100644 --- a/packages/inference/src/snippets/templates/python/requests/textToAudio.hbs +++ b/packages/inference/src/snippets/templates/python/requests/textToAudio.hbs @@ -1,21 +1,21 @@ {{#if (equals model.library_name "transformers")}} 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 audio_bytes = query({ - "inputs": {{{inputs}}}, + "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() - + response = requests.post(API_URL, headers=headers, json=payload) + return response.json() + audio, sampling_rate = query({ - "inputs": {{{inputs}}}, + "inputs": {{{inputs}}}, }) # You can access the audio with IPython.display for example from IPython.display import Audio diff --git a/packages/inference/src/snippets/templates/python/requests/textToImage.hbs b/packages/inference/src/snippets/templates/python/requests/textToImage.hbs index 9d54f0cd77..a4b75b584f 100644 --- a/packages/inference/src/snippets/templates/python/requests/textToImage.hbs +++ b/packages/inference/src/snippets/templates/python/requests/textToImage.hbs @@ -1,10 +1,10 @@ {{#if (equals provider "hf-inference")}} 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": {{{inputs}}}, + "inputs": {{{inputs}}}, }) # You can access the image with PIL.Image for example 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/1.requests.hf-inference.py b/packages/tasks-gen/snippets-fixtures/basic-snippet--token-classification/1.requests.hf-inference.py index 7ee7c608e7..52a5ca4217 100644 --- 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 @@ -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": "My name is Sarah Jessica Parker but you can call me Jessica", + "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/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/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 index cdf1ec4db8..e2714639b4 100644 --- a/packages/tasks-gen/snippets-fixtures/tabular/0.requests.hf-inference.py +++ b/packages/tasks-gen/snippets-fixtures/tabular/0.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 response = query({ - "inputs": { - "data": '{"Height":[11.52,12.48],"Length1":[23.2,24.0],"Length2":[25.4,26.3],"Species": ["Bream","Bream"]}' - }, + "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/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 index 40add8d37e..ad492aee35 100644 --- 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 @@ -4,11 +4,11 @@ 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() + audio, sampling_rate = query({ - "inputs": "liquid drum and bass, atmospheric synths, airy sounds", + "inputs": "liquid drum and bass, atmospheric synths, airy sounds", }) # You can access the audio with IPython.display for example from IPython.display import Audio 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 From 86e787afd272f3a4a0c8e56a224cfe36a8b6490d Mon Sep 17 00:00:00 2001 From: Wauplin Date: Fri, 7 Mar 2025 10:27:08 +0100 Subject: [PATCH 20/26] switch from handlebars to jinja --- packages/inference/package.json | 3 +- packages/inference/pnpm-lock.yaml | 48 ++----------------- packages/inference/src/snippets/python.ts | 10 ++-- .../python/fal_client/textToImage.hbs | 11 ----- .../python/fal_client/textToImage.jinja | 11 +++++ .../automaticSpeechRecognition.hbs | 1 - .../automaticSpeechRecognition.jinja | 1 + .../python/huggingface_hub/basic.hbs | 6 --- .../python/huggingface_hub/basic.jinja | 6 +++ .../python/huggingface_hub/conversational.hbs | 9 ---- .../huggingface_hub/conversational.jinja | 9 ++++ .../huggingface_hub/conversationalStream.hbs | 11 ----- .../conversationalStream.jinja | 11 +++++ .../documentQuestionAnswering.hbs | 5 -- .../documentQuestionAnswering.jinja | 5 ++ .../python/huggingface_hub/imageToImage.hbs | 6 --- .../python/huggingface_hub/imageToImage.jinja | 6 +++ ...Client.hbs => importInferenceClient.jinja} | 4 +- .../{textToImage.hbs => textToImage.jinja} | 6 +-- .../python/huggingface_hub/textToVideo.hbs | 4 -- .../python/huggingface_hub/textToVideo.jinja | 4 ++ .../python/openai/conversational.hbs | 16 ------- .../python/openai/conversational.jinja | 16 +++++++ .../python/openai/conversationalStream.hbs | 18 ------- .../python/openai/conversationalStream.jinja | 18 +++++++ ...e.hbs => automaticSpeechRecognition.jinja} | 2 +- .../requests/{basic.hbs => basic.jinja} | 4 +- ...cSpeechRecognition.hbs => basicFile.jinja} | 2 +- ...ng.hbs => documentQuestionAnswering.jinja} | 4 +- .../{imageToImage.hbs => imageToImage.jinja} | 6 +-- .../python/requests/importRequests.hbs | 7 --- .../python/requests/importRequests.jinja | 7 +++ .../requests/{tabular.hbs => tabular.jinja} | 4 +- .../{textToAudio.hbs => textToAudio.jinja} | 10 ++-- .../{textToImage.hbs => textToImage.jinja} | 6 +-- ...ation.hbs => zeroShotClassification.jinja} | 4 +- ....hbs => zeroShotImageClassification.jinja} | 4 +- .../0.huggingface_hub.hf-inference.py | 4 +- .../0.huggingface_hub.hf-inference.py | 8 ++-- .../0.huggingface_hub.together.py | 8 ++-- .../0.huggingface_hub.fireworks-ai.py | 8 ++-- .../0.huggingface_hub.hf-inference.py | 8 ++-- .../0.huggingface_hub.hf-inference.py | 4 +- .../0.huggingface_hub.hf-inference.py | 4 +- .../text-to-image/0.huggingface_hub.fal-ai.py | 4 +- .../0.huggingface_hub.hf-inference.py | 4 +- .../text-to-image/1.fal_client.fal-ai.py | 8 ++-- .../text-to-video/0.huggingface_hub.fal-ai.py | 4 +- .../0.huggingface_hub.replicate.py | 4 +- 49 files changed, 163 insertions(+), 210 deletions(-) delete mode 100644 packages/inference/src/snippets/templates/python/fal_client/textToImage.hbs create mode 100644 packages/inference/src/snippets/templates/python/fal_client/textToImage.jinja delete mode 100644 packages/inference/src/snippets/templates/python/huggingface_hub/automaticSpeechRecognition.hbs create mode 100644 packages/inference/src/snippets/templates/python/huggingface_hub/automaticSpeechRecognition.jinja delete mode 100644 packages/inference/src/snippets/templates/python/huggingface_hub/basic.hbs create mode 100644 packages/inference/src/snippets/templates/python/huggingface_hub/basic.jinja delete mode 100644 packages/inference/src/snippets/templates/python/huggingface_hub/conversational.hbs create mode 100644 packages/inference/src/snippets/templates/python/huggingface_hub/conversational.jinja delete mode 100644 packages/inference/src/snippets/templates/python/huggingface_hub/conversationalStream.hbs create mode 100644 packages/inference/src/snippets/templates/python/huggingface_hub/conversationalStream.jinja delete mode 100644 packages/inference/src/snippets/templates/python/huggingface_hub/documentQuestionAnswering.hbs create mode 100644 packages/inference/src/snippets/templates/python/huggingface_hub/documentQuestionAnswering.jinja delete mode 100644 packages/inference/src/snippets/templates/python/huggingface_hub/imageToImage.hbs create mode 100644 packages/inference/src/snippets/templates/python/huggingface_hub/imageToImage.jinja rename packages/inference/src/snippets/templates/python/huggingface_hub/{importInferenceClient.hbs => importInferenceClient.jinja} (52%) rename packages/inference/src/snippets/templates/python/huggingface_hub/{textToImage.hbs => textToImage.jinja} (55%) delete mode 100644 packages/inference/src/snippets/templates/python/huggingface_hub/textToVideo.hbs create mode 100644 packages/inference/src/snippets/templates/python/huggingface_hub/textToVideo.jinja delete mode 100644 packages/inference/src/snippets/templates/python/openai/conversational.hbs create mode 100644 packages/inference/src/snippets/templates/python/openai/conversational.jinja delete mode 100644 packages/inference/src/snippets/templates/python/openai/conversationalStream.hbs create mode 100644 packages/inference/src/snippets/templates/python/openai/conversationalStream.jinja rename packages/inference/src/snippets/templates/python/requests/{basicFile.hbs => automaticSpeechRecognition.jinja} (85%) rename packages/inference/src/snippets/templates/python/requests/{basic.hbs => basic.jinja} (81%) rename packages/inference/src/snippets/templates/python/requests/{automaticSpeechRecognition.hbs => basicFile.jinja} (85%) rename packages/inference/src/snippets/templates/python/requests/{documentQuestionAnswering.hbs => documentQuestionAnswering.jinja} (87%) rename packages/inference/src/snippets/templates/python/requests/{imageToImage.hbs => imageToImage.jinja} (73%) delete mode 100644 packages/inference/src/snippets/templates/python/requests/importRequests.hbs create mode 100644 packages/inference/src/snippets/templates/python/requests/importRequests.jinja rename packages/inference/src/snippets/templates/python/requests/{tabular.hbs => tabular.jinja} (83%) rename packages/inference/src/snippets/templates/python/requests/{textToAudio.hbs => textToAudio.jinja} (81%) rename packages/inference/src/snippets/templates/python/requests/{textToImage.hbs => textToImage.jinja} (78%) rename packages/inference/src/snippets/templates/python/requests/{zeroShotClassification.hbs => zeroShotClassification.jinja} (86%) rename packages/inference/src/snippets/templates/python/requests/{zeroShotImageClassification.hbs => zeroShotImageClassification.jinja} (91%) diff --git a/packages/inference/package.json b/packages/inference/package.json index 8f0a869cf1..473e545395 100644 --- a/packages/inference/package.json +++ b/packages/inference/package.json @@ -53,10 +53,9 @@ }, "dependencies": { "@huggingface/tasks": "workspace:^", - "handlebars": "^4.7.8" + "@huggingface/jinja": "workspace:^" }, "devDependencies": { - "@types/handlebars": "^4.1.0", "@types/node": "18.13.0" }, "resolutions": {} diff --git a/packages/inference/pnpm-lock.yaml b/packages/inference/pnpm-lock.yaml index 954979285d..082c8ed752 100644 --- a/packages/inference/pnpm-lock.yaml +++ b/packages/inference/pnpm-lock.yaml @@ -5,62 +5,20 @@ settings: excludeLinksFromLockfile: false dependencies: + '@huggingface/jinja': + specifier: workspace:^ + version: link:../jinja '@huggingface/tasks': specifier: workspace:^ version: link:../tasks - handlebars: - specifier: ^4.7.8 - version: 4.7.8 devDependencies: - '@types/handlebars': - specifier: ^4.1.0 - version: 4.1.0 '@types/node': specifier: 18.13.0 version: 18.13.0 packages: - /@types/handlebars@4.1.0: - resolution: {integrity: sha512-gq9YweFKNNB1uFK71eRqsd4niVkXrxHugqWFQkeLRJvGjnxsLr16bYtcsG4tOFwmYi0Bax+wCkbf1reUfdl4kA==} - deprecated: This is a stub types definition. handlebars provides its own type definitions, so you do not need this installed. - dependencies: - handlebars: 4.7.8 - dev: true - /@types/node@18.13.0: resolution: {integrity: sha512-gC3TazRzGoOnoKAhUx+Q0t8S9Tzs74z7m0ipwGpSqQrleP14hKxP4/JUeEQcD3W1/aIpnWl8pHowI7WokuZpXg==} dev: true - - /handlebars@4.7.8: - resolution: {integrity: sha512-vafaFqs8MZkRrSX7sFVUdo3ap/eNiLnb4IakshzvP56X5Nr1iGKAIqdX6tMlm6HcNRIkr6AxO5jFEoJzzpT8aQ==} - engines: {node: '>=0.4.7'} - hasBin: true - dependencies: - minimist: 1.2.8 - neo-async: 2.6.2 - source-map: 0.6.1 - wordwrap: 1.0.0 - optionalDependencies: - uglify-js: 3.19.3 - - /minimist@1.2.8: - resolution: {integrity: sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==} - - /neo-async@2.6.2: - resolution: {integrity: sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==} - - /source-map@0.6.1: - resolution: {integrity: sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==} - engines: {node: '>=0.10.0'} - - /uglify-js@3.19.3: - resolution: {integrity: sha512-v3Xu+yuwBXisp6QYTcH4UbH+xYJXqnq2m/LtQVWKWzYc1iehYnLixoQDN9FH6/j9/oybfd6W9Ghwkl8+UMKTKQ==} - engines: {node: '>=0.8.0'} - hasBin: true - requiresBuild: true - optional: true - - /wordwrap@1.0.0: - resolution: {integrity: sha512-gvVzJFlPycKc5dZN4yPkP8w7Dc37BtP1yczEneOb4uq34pXZcvrtRTmWV8W+Ume+XCxKgbjM+nevkyFPMybd4Q==} diff --git a/packages/inference/src/snippets/python.ts b/packages/inference/src/snippets/python.ts index a7f14033b1..2db55df8d0 100644 --- a/packages/inference/src/snippets/python.ts +++ b/packages/inference/src/snippets/python.ts @@ -9,8 +9,8 @@ import { stringifyMessages, } from "@huggingface/tasks"; import type { InferenceProvider } from "../types"; +import { Template } from "@huggingface/jinja"; import fs from "fs"; -import Handlebars from "handlebars"; import path from "path"; import { existsSync as pathExists } from "node:fs"; @@ -28,10 +28,6 @@ interface TemplateParams { importBase64?: boolean; // specific to snippetImportRequests } -Handlebars.registerHelper("equals", function (value1, value2) { - return value1 === value2; -}); - // Helpers to find + load templates const rootDirFinder = (): string => { @@ -49,12 +45,12 @@ const rootDirFinder = (): string => { }; const templatePath = (tool: string, templateName: string): string => - path.join(rootDirFinder(), "src", "snippets", "templates", "python", tool, `${templateName}.hbs`); + 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 Handlebars.compile(template); + return (data: TemplateParams) => new Template(template).render({ ...data }); }; const snippetImportInferenceClient = loadTemplate("huggingface_hub", "importInferenceClient"); diff --git a/packages/inference/src/snippets/templates/python/fal_client/textToImage.hbs b/packages/inference/src/snippets/templates/python/fal_client/textToImage.hbs deleted file mode 100644 index 137d2d54d1..0000000000 --- a/packages/inference/src/snippets/templates/python/fal_client/textToImage.hbs +++ /dev/null @@ -1,11 +0,0 @@ -{{#if (equals provider "fal-ai")}} -import fal_client - -result = fal_client.subscribe( - "{{{providerModelId}}}", - arguments={ - "prompt": {{{inputs}}}, - }, -) -print(result) -{{/if}} \ No newline at end of file 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.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/automaticSpeechRecognition.hbs deleted file mode 100644 index 1bc5cae1d0..0000000000 --- a/packages/inference/src/snippets/templates/python/huggingface_hub/automaticSpeechRecognition.hbs +++ /dev/null @@ -1 +0,0 @@ -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/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.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/basic.hbs deleted file mode 100644 index 4c84144e62..0000000000 --- a/packages/inference/src/snippets/templates/python/huggingface_hub/basic.hbs +++ /dev/null @@ -1,6 +0,0 @@ -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/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.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/conversational.hbs deleted file mode 100644 index aa69fda701..0000000000 --- a/packages/inference/src/snippets/templates/python/huggingface_hub/conversational.hbs +++ /dev/null @@ -1,9 +0,0 @@ -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/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.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/conversationalStream.hbs deleted file mode 100644 index b9f4b48ca9..0000000000 --- a/packages/inference/src/snippets/templates/python/huggingface_hub/conversationalStream.hbs +++ /dev/null @@ -1,11 +0,0 @@ -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/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.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/documentQuestionAnswering.hbs deleted file mode 100644 index ed5bae39be..0000000000 --- a/packages/inference/src/snippets/templates/python/huggingface_hub/documentQuestionAnswering.hbs +++ /dev/null @@ -1,5 +0,0 @@ -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/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.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/imageToImage.hbs deleted file mode 100644 index 1ca023b89f..0000000000 --- a/packages/inference/src/snippets/templates/python/huggingface_hub/imageToImage.hbs +++ /dev/null @@ -1,6 +0,0 @@ -# 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/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.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/importInferenceClient.jinja similarity index 52% rename from packages/inference/src/snippets/templates/python/huggingface_hub/importInferenceClient.hbs rename to packages/inference/src/snippets/templates/python/huggingface_hub/importInferenceClient.jinja index a5308a08c6..9a6514f267 100644 --- a/packages/inference/src/snippets/templates/python/huggingface_hub/importInferenceClient.hbs +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/importInferenceClient.jinja @@ -1,6 +1,6 @@ from huggingface_hub import InferenceClient client = InferenceClient( - provider="{{{provider}}}", - api_key="{{{accessToken}}}", + provider="{{ provider }}", + api_key="{{ accessToken }}", ) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/huggingface_hub/textToImage.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/textToImage.jinja similarity index 55% rename from packages/inference/src/snippets/templates/python/huggingface_hub/textToImage.hbs rename to packages/inference/src/snippets/templates/python/huggingface_hub/textToImage.jinja index eed729a3a1..5379d68b11 100644 --- a/packages/inference/src/snippets/templates/python/huggingface_hub/textToImage.hbs +++ b/packages/inference/src/snippets/templates/python/huggingface_hub/textToImage.jinja @@ -1,5 +1,5 @@ # output is a PIL.Image object image = client.text_to_image( - {{{inputs}}}, - model="{{{model.id}}}", -) \ No newline at end of file + {{ inputs }}, + model="{{ model.id }}", +) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/huggingface_hub/textToVideo.hbs b/packages/inference/src/snippets/templates/python/huggingface_hub/textToVideo.hbs deleted file mode 100644 index dc78306b07..0000000000 --- a/packages/inference/src/snippets/templates/python/huggingface_hub/textToVideo.hbs +++ /dev/null @@ -1,4 +0,0 @@ -video = client.text_to_video( - {{{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.hbs b/packages/inference/src/snippets/templates/python/openai/conversational.hbs deleted file mode 100644 index d2a0d70e40..0000000000 --- a/packages/inference/src/snippets/templates/python/openai/conversational.hbs +++ /dev/null @@ -1,16 +0,0 @@ -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/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.hbs b/packages/inference/src/snippets/templates/python/openai/conversationalStream.hbs deleted file mode 100644 index d6a4eb1072..0000000000 --- a/packages/inference/src/snippets/templates/python/openai/conversationalStream.hbs +++ /dev/null @@ -1,18 +0,0 @@ -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="") 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/basicFile.hbs b/packages/inference/src/snippets/templates/python/requests/automaticSpeechRecognition.jinja similarity index 85% rename from packages/inference/src/snippets/templates/python/requests/basicFile.hbs rename to packages/inference/src/snippets/templates/python/requests/automaticSpeechRecognition.jinja index 543fde00e7..e73c2ce79a 100644 --- a/packages/inference/src/snippets/templates/python/requests/basicFile.hbs +++ b/packages/inference/src/snippets/templates/python/requests/automaticSpeechRecognition.jinja @@ -4,4 +4,4 @@ def query(filename): response = requests.post(API_URL, headers=headers, data=data) return response.json() -output = query({{{inputs}}}) \ No newline at end of file +output = query({{ inputs }}) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/basic.hbs b/packages/inference/src/snippets/templates/python/requests/basic.jinja similarity index 81% rename from packages/inference/src/snippets/templates/python/requests/basic.hbs rename to packages/inference/src/snippets/templates/python/requests/basic.jinja index d46487b3c6..489218820d 100644 --- a/packages/inference/src/snippets/templates/python/requests/basic.hbs +++ b/packages/inference/src/snippets/templates/python/requests/basic.jinja @@ -3,5 +3,5 @@ def query(payload): return response.json() output = query({ - "inputs": {{{inputs}}}, -}) \ No newline at end of file + "inputs": {{ inputs }}, +}) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/automaticSpeechRecognition.hbs b/packages/inference/src/snippets/templates/python/requests/basicFile.jinja similarity index 85% rename from packages/inference/src/snippets/templates/python/requests/automaticSpeechRecognition.hbs rename to packages/inference/src/snippets/templates/python/requests/basicFile.jinja index 543fde00e7..e73c2ce79a 100644 --- a/packages/inference/src/snippets/templates/python/requests/automaticSpeechRecognition.hbs +++ b/packages/inference/src/snippets/templates/python/requests/basicFile.jinja @@ -4,4 +4,4 @@ def query(filename): response = requests.post(API_URL, headers=headers, data=data) return response.json() -output = query({{{inputs}}}) \ No newline at end of file +output = query({{ inputs }}) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/documentQuestionAnswering.hbs b/packages/inference/src/snippets/templates/python/requests/documentQuestionAnswering.jinja similarity index 87% rename from packages/inference/src/snippets/templates/python/requests/documentQuestionAnswering.hbs rename to packages/inference/src/snippets/templates/python/requests/documentQuestionAnswering.jinja index 6d4ed09dfb..76072952f5 100644 --- a/packages/inference/src/snippets/templates/python/requests/documentQuestionAnswering.hbs +++ b/packages/inference/src/snippets/templates/python/requests/documentQuestionAnswering.jinja @@ -6,5 +6,5 @@ def query(payload): return response.json() output = query({ - "inputs": {{{inputs.asStr}}}, -}) \ No newline at end of file + "inputs": {{ inputs.asStr }}, +}) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/imageToImage.hbs b/packages/inference/src/snippets/templates/python/requests/imageToImage.jinja similarity index 73% rename from packages/inference/src/snippets/templates/python/requests/imageToImage.hbs rename to packages/inference/src/snippets/templates/python/requests/imageToImage.jinja index 9a2ea381a3..4c05cc307a 100644 --- a/packages/inference/src/snippets/templates/python/requests/imageToImage.hbs +++ b/packages/inference/src/snippets/templates/python/requests/imageToImage.jinja @@ -6,11 +6,11 @@ def query(payload): return response.content image_bytes = query({ - "inputs": "{{{inputs.image}}}", - "parameters": {"prompt": "{{{inputs.prompt}}}"}, + "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 +image = Image.open(io.BytesIO(image_bytes)) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/importRequests.hbs b/packages/inference/src/snippets/templates/python/requests/importRequests.hbs deleted file mode 100644 index aa8bb8c63f..0000000000 --- a/packages/inference/src/snippets/templates/python/requests/importRequests.hbs +++ /dev/null @@ -1,7 +0,0 @@ -{{#if importBase64}} -import base64 -{{/if}} -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/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.hbs b/packages/inference/src/snippets/templates/python/requests/tabular.jinja similarity index 83% rename from packages/inference/src/snippets/templates/python/requests/tabular.hbs rename to packages/inference/src/snippets/templates/python/requests/tabular.jinja index 95579d7fa9..76914d4c5e 100644 --- a/packages/inference/src/snippets/templates/python/requests/tabular.hbs +++ b/packages/inference/src/snippets/templates/python/requests/tabular.jinja @@ -4,6 +4,6 @@ def query(payload): response = query({ "inputs": { - "data": {{{inputs}}} + "data": {{ inputs }} }, -}) \ No newline at end of file +}) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/textToAudio.hbs b/packages/inference/src/snippets/templates/python/requests/textToAudio.jinja similarity index 81% rename from packages/inference/src/snippets/templates/python/requests/textToAudio.hbs rename to packages/inference/src/snippets/templates/python/requests/textToAudio.jinja index 6ff67261bf..ecb2cf23ad 100644 --- a/packages/inference/src/snippets/templates/python/requests/textToAudio.hbs +++ b/packages/inference/src/snippets/templates/python/requests/textToAudio.jinja @@ -1,23 +1,23 @@ -{{#if (equals model.library_name "transformers")}} +{% 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}}}, + "inputs": {{ inputs }}, }) # You can access the audio with IPython.display for example from IPython.display import Audio Audio(audio_bytes) -{{else}} +{% else %} def query(payload): response = requests.post(API_URL, headers=headers, json=payload) return response.json() audio, sampling_rate = query({ - "inputs": {{{inputs}}}, + "inputs": {{ inputs }}, }) # You can access the audio with IPython.display for example from IPython.display import Audio Audio(audio, rate=sampling_rate) -{{/if}} \ No newline at end of file +{% endif %} \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/textToImage.hbs b/packages/inference/src/snippets/templates/python/requests/textToImage.jinja similarity index 78% rename from packages/inference/src/snippets/templates/python/requests/textToImage.hbs rename to packages/inference/src/snippets/templates/python/requests/textToImage.jinja index a4b75b584f..1c30ac461c 100644 --- a/packages/inference/src/snippets/templates/python/requests/textToImage.hbs +++ b/packages/inference/src/snippets/templates/python/requests/textToImage.jinja @@ -1,14 +1,14 @@ -{{#if (equals provider "hf-inference")}} +{% if provider == "hf-inference" %} def query(payload): response = requests.post(API_URL, headers=headers, json=payload) return response.content image_bytes = query({ - "inputs": {{{inputs}}}, + "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)) -{{/if}} \ No newline at end of file +{% endif %} \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/zeroShotClassification.hbs b/packages/inference/src/snippets/templates/python/requests/zeroShotClassification.jinja similarity index 86% rename from packages/inference/src/snippets/templates/python/requests/zeroShotClassification.hbs rename to packages/inference/src/snippets/templates/python/requests/zeroShotClassification.jinja index b55285c980..664ec00b5f 100644 --- a/packages/inference/src/snippets/templates/python/requests/zeroShotClassification.hbs +++ b/packages/inference/src/snippets/templates/python/requests/zeroShotClassification.jinja @@ -3,6 +3,6 @@ def query(payload): return response.json() output = query({ - "inputs": {{{inputs}}}, + "inputs": {{ inputs }}, "parameters": {"candidate_labels": ["refund", "legal", "faq"]}, -}) \ No newline at end of file +}) \ No newline at end of file diff --git a/packages/inference/src/snippets/templates/python/requests/zeroShotImageClassification.hbs b/packages/inference/src/snippets/templates/python/requests/zeroShotImageClassification.jinja similarity index 91% rename from packages/inference/src/snippets/templates/python/requests/zeroShotImageClassification.hbs rename to packages/inference/src/snippets/templates/python/requests/zeroShotImageClassification.jinja index 34aee45b8c..769aac400e 100644 --- a/packages/inference/src/snippets/templates/python/requests/zeroShotImageClassification.hbs +++ b/packages/inference/src/snippets/templates/python/requests/zeroShotImageClassification.jinja @@ -9,6 +9,6 @@ def query(data): return response.json() output = query({ - "image_path": {{{inputs}}}, + "image_path": {{ inputs }}, "parameters": {"candidate_labels": ["cat", "dog", "llama"]}, -}) \ No newline at end of file +}) \ 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 index 852ab032de..60eb648eba 100644 --- 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 @@ -6,8 +6,8 @@ ) 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", + 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/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 941601839e..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 317eef428c..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-vlm-stream/0.huggingface_hub.fireworks-ai.py b/packages/tasks-gen/snippets-fixtures/conversational-vlm-stream/0.huggingface_hub.fireworks-ai.py index 6cdec6118d..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 d5e2b64dae..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/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 6f46b93654..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 @@ -7,6 +7,6 @@ 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/text-classification/0.huggingface_hub.hf-inference.py b/packages/tasks-gen/snippets-fixtures/text-classification/0.huggingface_hub.hf-inference.py index 10c9d7a359..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) \ 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 index 177c07bb8f..461dbb2271 100644 --- 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 @@ -1,9 +1,9 @@ import fal_client result = fal_client.subscribe( - "", - arguments={ - "prompt": "Astronaut riding a horse", - }, + "", + arguments={ + "prompt": "Astronaut riding a horse", + }, ) print(result) \ No newline at end of file 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 From 30bbfad8787dd6f60c37123589c5ae8b39cb2a53 Mon Sep 17 00:00:00 2001 From: Lucain Date: Fri, 7 Mar 2025 11:46:29 +0100 Subject: [PATCH 21/26] Update .vscode/settings.json Co-authored-by: Simon Brandeis <33657802+SBrandeis@users.noreply.github.com> --- .vscode/settings.json | 3 --- 1 file changed, 3 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index a9c6ca92b4..e5c0fb1c32 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -18,7 +18,4 @@ "**/dist": true }, "typescript.tsdk": "node_modules/typescript/lib", - "[handlebars]": { - "editor.defaultFormatter": "mfeckies.handlebars-formatter" - } } From a280d60f855508cebfb0b43cdfe80e7972777146 Mon Sep 17 00:00:00 2001 From: Wauplin Date: Fri, 7 Mar 2025 13:04:27 +0100 Subject: [PATCH 22/26] not private --- packages/inference/src/lib/makeRequestOptions.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/inference/src/lib/makeRequestOptions.ts b/packages/inference/src/lib/makeRequestOptions.ts index 6d0394481d..061fce5637 100644 --- a/packages/inference/src/lib/makeRequestOptions.ts +++ b/packages/inference/src/lib/makeRequestOptions.ts @@ -57,14 +57,14 @@ export async function makeRequestOptions( task?: InferenceTask; chatCompletion?: boolean; /* Used internally to generate inference snippets (in which case model mapping is done separately) */ - __skipModelIdResolution?: boolean; + 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, __skipModelIdResolution } = options ?? {}; + const { includeCredentials, task, chatCompletion, signal, skipModelIdResolution } = options ?? {}; if (endpointUrl && provider !== "hf-inference") { throw new Error(`Cannot use endpointUrl with a third-party provider.`); @@ -83,7 +83,7 @@ export async function makeRequestOptions( } // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const hfModel = maybeModel ?? (await loadDefaultModel(task!)); - const model = __skipModelIdResolution + const model = skipModelIdResolution ? hfModel : providerConfig.clientSideRoutingOnly ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion From d2579ab8d41e7bb25309bfa8f5dbc044c3e16a34 Mon Sep 17 00:00:00 2001 From: Lucain Date: Tue, 11 Mar 2025 11:27:35 +0100 Subject: [PATCH 23/26] Do not export ./snippets module in browser mode (#1259) :warning: PR opened on top of https://github.com/huggingface/huggingface.js/pull/1255 Related to this thread https://github.com/huggingface/huggingface.js/pull/1255#discussion_r1985008531. @coyotte508 @SBrandeis @julien-c WDYT? :see_no_evil: for the record, I'm looking for a solution where: 1. we keep this public/open-source 2. we keep the structure of jinja templates 3. ideally no new package in hf.js 4. ideally no new tooling (e.g. to translate jinja into TS code) ~Solution here is simply to gracefully raise an error if environment not supported.~ **EDIT:** based on https://github.com/huggingface/huggingface.js/pull/1259#issuecomment-2706636392, goal is to not package the `./snippets` module in browser mode. --------- Co-authored-by: coyotte508 --- packages/inference/package.json | 13 ++++++++++--- packages/inference/src/index.ts | 2 +- packages/inference/tsup.config.ts | 23 +++++++++++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 packages/inference/tsup.config.ts diff --git a/packages/inference/package.json b/packages/inference/package.json index ee6fa68381..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": { 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/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]; From aeecd11867572f786bc2849d4c9eb0b135f0bf44 Mon Sep 17 00:00:00 2001 From: Wauplin Date: Tue, 11 Mar 2025 12:07:56 +0100 Subject: [PATCH 24/26] filter jinja in ci test --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f9111a4258..6a155ad8ea 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 From f6d81d6b33b18f89b81ffda040e55d60b4f6bc43 Mon Sep 17 00:00:00 2001 From: Wauplin Date: Tue, 11 Mar 2025 12:21:22 +0100 Subject: [PATCH 25/26] handle both ESM and CJS ? --- packages/inference/src/snippets/python.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/inference/src/snippets/python.ts b/packages/inference/src/snippets/python.ts index 2db55df8d0..f685f8b9ae 100644 --- a/packages/inference/src/snippets/python.ts +++ b/packages/inference/src/snippets/python.ts @@ -31,7 +31,10 @@ interface TemplateParams { // Helpers to find + load templates const rootDirFinder = (): string => { - let currentPath = path.normalize(import.meta.url).replace("file:", ""); + 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"))) { From e4bacdd6a3b68d70ffe10581ea245013653aa0de Mon Sep 17 00:00:00 2001 From: Wauplin Date: Tue, 11 Mar 2025 12:31:28 +0100 Subject: [PATCH 26/26] add --allow-read flag for deno import --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6a155ad8ea..fe07e409fc 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 }}