Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Inference doc] Next gen inference snippets #1643

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 1 addition & 61 deletions docs/api-inference/tasks/audio-classification.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,73 +29,13 @@ For more details about the `audio-classification` task, check out its [dedicated

### Recommended models

- [speechbrain/google_speech_command_xvector](https://huggingface.co/speechbrain/google_speech_command_xvector): An easy-to-use model for command recognition.
- [ehcalabres/wav2vec2-lg-xlsr-en-speech-emotion-recognition](https://huggingface.co/ehcalabres/wav2vec2-lg-xlsr-en-speech-emotion-recognition): An emotion recognition model.
- [facebook/mms-lid-126](https://huggingface.co/facebook/mms-lid-126): A language identification model.

Explore all available models and find the one that suits you best [here](https://huggingface.co/models?inference=warm&pipeline_tag=audio-classification&sort=trending).

### Using the API


<inferencesnippet>

<curl>
```bash
curl https://router.huggingface.co/hf-inference/models/speechbrain/google_speech_command_xvector \
-X POST \
--data-binary '@sample1.flac' \
-H 'Authorization: Bearer hf_***'
```
</curl>

<python>
```py
import requests

API_URL = "https://router.huggingface.co/hf-inference/v1"
headers = {"Authorization": "Bearer hf_***"}

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")
```

To use the Python client, see `huggingface_hub`'s [package reference](https://huggingface.co/docs/huggingface_hub/package_reference/inference_client#huggingface_hub.InferenceClient.audio_classification).
</python>

<js>
```js
async function query(filename) {
const data = fs.readFileSync(filename);
const response = await fetch(
"https://router.huggingface.co/hf-inference/models/speechbrain/google_speech_command_xvector",
{
headers: {
Authorization: "Bearer hf_***",
"Content-Type": "application/json",
},
method: "POST",
body: data,
}
);
const result = await response.json();
return result;
}

query("sample1.flac").then((response) => {
console.log(JSON.stringify(response));
});
```

To use the JavaScript client, see `huggingface.js`'s [package reference](https://huggingface.co/docs/huggingface.js/inference/classes/HfInference#audioclassification).
</js>

</inferencesnippet>
No snippet available for this task.



Expand Down
189 changes: 156 additions & 33 deletions docs/api-inference/tasks/automatic-speech-recognition.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ For more details about the `automatic-speech-recognition` task, check out its [d

### Recommended models

- [openai/whisper-large-v3](https://huggingface.co/openai/whisper-large-v3): A powerful ASR model by OpenAI.
- [facebook/seamless-m4t-v2-large](https://huggingface.co/facebook/seamless-m4t-v2-large): An end-to-end model that performs ASR and Speech Translation by MetaAI.

Explore all available models and find the one that suits you best [here](https://huggingface.co/models?inference=warm&pipeline_tag=automatic-speech-recognition&sort=trending).

Expand All @@ -39,79 +37,204 @@ Explore all available models and find the one that suits you best [here](https:/

<inferencesnippet>

<curl>
```bash
curl https://router.huggingface.co/hf-inference/models/openai/whisper-large-v3 \
-X POST \
--data-binary '@sample1.flac' \
-H 'Authorization: Bearer hf_***'

<snippet provider="fal-ai" language="python" client="huggingface_hub">

```python
from huggingface_hub import InferenceClient

client = InferenceClient(
provider="fal-ai",
api_key="hf_***",
)

output = client.automatic_speech_recognition("sample1.flac", model="openai/whisper-large-v3")
```
</curl>

<python>
```py
</snippet>

To use the Python `InferenceClient`, see the [package reference](https://huggingface.co/docs/huggingface_hub/package_reference/inference_client#huggingface_hub.InferenceClient.).

<snippet provider="fal-ai" language="python" client="requests">

```python
import requests

API_URL = "https://router.huggingface.co/hf-inference/v1"
API_URL = "https://router.huggingface.co/fal-ai/fal-ai/whisper"
headers = {"Authorization": "Bearer hf_***"}

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={"Content-Type": "audio/flac", **headers}, data=data)
return response.json()

output = query("sample1.flac")
```

To use the Python client, see `huggingface_hub`'s [package reference](https://huggingface.co/docs/huggingface_hub/package_reference/inference_client#huggingface_hub.InferenceClient.automatic_speech_recognition).
</python>
</snippet>


<snippet provider="fal-ai" language="js" client="fetch">

<js>
Using `huggingface.js`:
```js
import { HfInference } from "@huggingface/inference";
async function query(data) {
const response = await fetch(
"https://router.huggingface.co/fal-ai/fal-ai/whisper",
{
headers: {
Authorization: "Bearer hf_***",
"Content-Type": "audio/flac"
},
method: "POST",
body: JSON.stringify(data),
}
);
const result = await response.json();
return result;
}

const client = new HfInference("hf_***");
query({ inputs: "sample1.flac" }).then((response) => {
console.log(JSON.stringify(response));
});
```

</snippet>


<snippet provider="fal-ai" language="js" client="huggingface.js">

```js
import { InferenceClient } from "@huggingface/inference";

const client = new InferenceClient("hf_***");

const data = fs.readFileSync("sample1.flac");

const output = await client.automaticSpeechRecognition({
data,
model: "openai/whisper-large-v3",
provider: "hf-inference",
provider: "fal-ai",
});

console.log(output);
```

</snippet>

To use the JavaScript `InferenceClient`, see `huggingface.js`'s [package reference](https://huggingface.co/docs/huggingface.js/inference/classes/InferenceClient#).

<snippet provider="fal-ai" language="sh" client="curl">

```sh
curl https://router.huggingface.co/fal-ai/fal-ai/whisper \
-X POST \
-H 'Authorization: Bearer hf_***' \
-H 'Content-Type: audio/flac' \
--data-binary @"sample1.flac"
```

Using `fetch`:
</snippet>


<snippet provider="hf-inference" language="python" client="huggingface_hub">

```python
from huggingface_hub import InferenceClient

client = InferenceClient(
provider="hf-inference",
api_key="hf_***",
)

output = client.automatic_speech_recognition("sample1.flac", model="openai/whisper-large-v3-turbo")
```

</snippet>

To use the Python `InferenceClient`, see the [package reference](https://huggingface.co/docs/huggingface_hub/package_reference/inference_client#huggingface_hub.InferenceClient.).

<snippet provider="hf-inference" language="python" client="requests">

```python
import requests

API_URL = "https://router.huggingface.co/hf-inference/models/openai/whisper-large-v3-turbo"
headers = {"Authorization": "Bearer hf_***"}

def query(filename):
with open(filename, "rb") as f:
data = f.read()
response = requests.post(API_URL, headers={"Content-Type": "audio/flac", **headers}, data=data)
return response.json()

output = query("sample1.flac")
```

</snippet>


<snippet provider="hf-inference" language="js" client="fetch">

```js
async function query(filename) {
const data = fs.readFileSync(filename);
async function query(data) {
const response = await fetch(
"https://router.huggingface.co/hf-inference/models/openai/whisper-large-v3",
"https://router.huggingface.co/hf-inference/models/openai/whisper-large-v3-turbo",
{
headers: {
Authorization: "Bearer hf_***",
"Content-Type": "application/json",
"Content-Type": "audio/flac"
},
method: "POST",
body: data,
body: JSON.stringify(data),
}
);
const result = await response.json();
return result;
}

query("sample1.flac").then((response) => {
console.log(JSON.stringify(response));
query({ inputs: "sample1.flac" }).then((response) => {
console.log(JSON.stringify(response));
});
```

</snippet>


<snippet provider="hf-inference" language="js" client="huggingface.js">

```js
import { InferenceClient } from "@huggingface/inference";

const client = new InferenceClient("hf_***");

const data = fs.readFileSync("sample1.flac");

const output = await client.automaticSpeechRecognition({
data,
model: "openai/whisper-large-v3-turbo",
provider: "hf-inference",
});

console.log(output);
```

To use the JavaScript client, see `huggingface.js`'s [package reference](https://huggingface.co/docs/huggingface.js/inference/classes/HfInference#automaticspeechrecognition).
</js>
</snippet>

To use the JavaScript `InferenceClient`, see `huggingface.js`'s [package reference](https://huggingface.co/docs/huggingface.js/inference/classes/InferenceClient#).

<snippet provider="hf-inference" language="sh" client="curl">

```sh
curl https://router.huggingface.co/hf-inference/models/openai/whisper-large-v3-turbo \
-X POST \
-H 'Authorization: Bearer hf_***' \
-H 'Content-Type: audio/flac' \
--data-binary @"sample1.flac"
```

</snippet>


</inferencesnippet>

Expand Down
Loading