Skip to content

Commit d539697

Browse files
committed
more advanced
1 parent 98500dc commit d539697

23 files changed

+1442
-1194
lines changed

docs/api-inference/tasks/audio-classification.md

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,60 +40,79 @@ Explore all available models and find the one that suits you best [here](https:/
4040

4141
<inferencesnippet>
4242

43-
<curl>
44-
```bash
45-
curl https://router.huggingface.co/hf-inference/models/speechbrain/google_speech_command_xvector \
46-
-X POST \
47-
--data-binary '@sample1.flac' \
48-
-H 'Authorization: Bearer hf_***'
43+
<snippet provider="hf-inference" language="python" client="huggingface_hub">
44+
45+
```python
46+
from huggingface_hub import InferenceClient
47+
48+
client = InferenceClient(
49+
provider="hf-inference",
50+
api_key="hf_***",
51+
)
52+
53+
output = client.audio_classification("sample1.flac", model="speechbrain/google_speech_command_xvector")
4954
```
50-
</curl>
5155

52-
<python>
53-
```py
56+
</snippet>
57+
58+
To use the Python `InferenceClient`, see the [package reference](https://huggingface.co/docs/huggingface_hub/package_reference/inference_client#huggingface_hub.InferenceClient.).
59+
<snippet provider="hf-inference" language="python" client="requests">
60+
61+
```python
5462
import requests
5563

56-
API_URL = "https://router.huggingface.co/hf-inference/v1"
64+
API_URL = "https://router.huggingface.co/hf-inference/models/speechbrain/google_speech_command_xvector"
5765
headers = {"Authorization": "Bearer hf_***"}
5866

5967
def query(filename):
60-
with open(filename, "rb") as f:
61-
data = f.read()
62-
response = requests.post(API_URL, headers=headers, data=data)
63-
return response.json()
68+
with open(filename, "rb") as f:
69+
data = f.read()
70+
response = requests.post(API_URL, headers={"Content-Type": "audio/flac", **headers}, data=data)
71+
return response.json()
6472

6573
output = query("sample1.flac")
6674
```
6775

68-
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).
69-
</python>
76+
</snippet>
7077

71-
<js>
78+
<snippet provider="hf-inference" language="js" client="fetch">
79+
7280
```js
73-
async function query(filename) {
74-
const data = fs.readFileSync(filename);
81+
async function query(data) {
7582
const response = await fetch(
7683
"https://router.huggingface.co/hf-inference/models/speechbrain/google_speech_command_xvector",
7784
{
7885
headers: {
7986
Authorization: "Bearer hf_***",
80-
"Content-Type": "application/json",
87+
"Content-Type": "audio/flac"
8188
},
8289
method: "POST",
83-
body: data,
90+
body: JSON.stringify(data),
8491
}
8592
);
8693
const result = await response.json();
8794
return result;
8895
}
8996

90-
query("sample1.flac").then((response) => {
91-
console.log(JSON.stringify(response));
97+
query({ inputs: "sample1.flac" }).then((response) => {
98+
console.log(JSON.stringify(response));
9299
});
93100
```
94101

95-
To use the JavaScript client, see `huggingface.js`'s [package reference](https://huggingface.co/docs/huggingface.js/inference/classes/HfInference#audioclassification).
96-
</js>
102+
</snippet>
103+
104+
<snippet provider="hf-inference" language="sh" client="curl">
105+
106+
```sh
107+
curl https://router.huggingface.co/hf-inference/models/speechbrain/google_speech_command_xvector \
108+
-X POST \
109+
-H 'Authorization: Bearer hf_***' \
110+
-H 'Content-Type: audio/flac' \
111+
--data-binary @"sample1.flac"
112+
```
113+
114+
</snippet>
115+
97116

98117
</inferencesnippet>
99118

docs/api-inference/tasks/automatic-speech-recognition.md

Lines changed: 65 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -39,79 +39,100 @@ Explore all available models and find the one that suits you best [here](https:/
3939

4040
<inferencesnippet>
4141

42-
<curl>
43-
```bash
44-
curl https://router.huggingface.co/hf-inference/models/openai/whisper-large-v3 \
45-
-X POST \
46-
--data-binary '@sample1.flac' \
47-
-H 'Authorization: Bearer hf_***'
42+
<snippet provider="hf-inference" language="python" client="huggingface_hub">
43+
44+
```python
45+
from huggingface_hub import InferenceClient
46+
47+
client = InferenceClient(
48+
provider="hf-inference",
49+
api_key="hf_***",
50+
)
51+
52+
output = client.automatic_speech_recognition("sample1.flac", model="openai/whisper-large-v3")
4853
```
49-
</curl>
5054

51-
<python>
52-
```py
55+
</snippet>
56+
57+
To use the Python `InferenceClient`, see the [package reference](https://huggingface.co/docs/huggingface_hub/package_reference/inference_client#huggingface_hub.InferenceClient.).
58+
<snippet provider="hf-inference" language="python" client="requests">
59+
60+
```python
5361
import requests
5462

55-
API_URL = "https://router.huggingface.co/hf-inference/v1"
63+
API_URL = "https://router.huggingface.co/hf-inference/models/openai/whisper-large-v3"
5664
headers = {"Authorization": "Bearer hf_***"}
5765

5866
def query(filename):
59-
with open(filename, "rb") as f:
60-
data = f.read()
61-
response = requests.post(API_URL, headers=headers, data=data)
62-
return response.json()
67+
with open(filename, "rb") as f:
68+
data = f.read()
69+
response = requests.post(API_URL, headers={"Content-Type": "audio/flac", **headers}, data=data)
70+
return response.json()
6371

6472
output = query("sample1.flac")
6573
```
6674

67-
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).
68-
</python>
69-
70-
<js>
71-
Using `huggingface.js`:
72-
```js
73-
import { HfInference } from "@huggingface/inference";
74-
75-
const client = new HfInference("hf_***");
76-
77-
const data = fs.readFileSync("sample1.flac");
78-
79-
const output = await client.automaticSpeechRecognition({
80-
data,
81-
model: "openai/whisper-large-v3",
82-
provider: "hf-inference",
83-
});
75+
</snippet>
8476

85-
console.log(output);
86-
87-
```
88-
89-
Using `fetch`:
77+
<snippet provider="hf-inference" language="js" client="fetch">
78+
9079
```js
91-
async function query(filename) {
92-
const data = fs.readFileSync(filename);
80+
async function query(data) {
9381
const response = await fetch(
9482
"https://router.huggingface.co/hf-inference/models/openai/whisper-large-v3",
9583
{
9684
headers: {
9785
Authorization: "Bearer hf_***",
98-
"Content-Type": "application/json",
86+
"Content-Type": "audio/flac"
9987
},
10088
method: "POST",
101-
body: data,
89+
body: JSON.stringify(data),
10290
}
10391
);
10492
const result = await response.json();
10593
return result;
10694
}
10795

108-
query("sample1.flac").then((response) => {
109-
console.log(JSON.stringify(response));
96+
query({ inputs: "sample1.flac" }).then((response) => {
97+
console.log(JSON.stringify(response));
11098
});
11199
```
112100

113-
To use the JavaScript client, see `huggingface.js`'s [package reference](https://huggingface.co/docs/huggingface.js/inference/classes/HfInference#automaticspeechrecognition).
114-
</js>
101+
</snippet>
102+
103+
<snippet provider="hf-inference" language="js" client="huggingface.js">
104+
105+
```js
106+
import { InferenceClient } from "@huggingface/inference";
107+
108+
const client = new InferenceClient("hf_***");
109+
110+
const data = fs.readFileSync("sample1.flac");
111+
112+
const output = await client.automaticSpeechRecognition({
113+
data,
114+
model: "openai/whisper-large-v3",
115+
provider: "hf-inference",
116+
});
117+
118+
console.log(output);
119+
```
120+
121+
</snippet>
122+
123+
To use the JavaScript `InferenceClient`, see `huggingface.js`'s [package reference](https://huggingface.co/docs/huggingface.js/inference/classes/InferenceClient#).
124+
<snippet provider="hf-inference" language="sh" client="curl">
125+
126+
```sh
127+
curl https://router.huggingface.co/hf-inference/models/openai/whisper-large-v3 \
128+
-X POST \
129+
-H 'Authorization: Bearer hf_***' \
130+
-H 'Content-Type: audio/flac' \
131+
--data-binary @"sample1.flac"
132+
```
133+
134+
</snippet>
135+
115136

116137
</inferencesnippet>
117138

0 commit comments

Comments
 (0)