Skip to content

Commit 86bff1c

Browse files
committed
stamp: clippy & format
1 parent 4fb7f28 commit 86bff1c

File tree

12 files changed

+484
-456
lines changed

12 files changed

+484
-456
lines changed

examples/ort-raw-session/index.ts

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -71,43 +71,49 @@ Deno.serve(async (_req: Request) => {
7171

7272
const { RawTensor, RawSession } = Supabase.ai;
7373

74-
const session = await RawSession.fromHuggingFace('kallebysantos/vehicle-emission', {
74+
const session = await RawSession.fromHuggingFace(
75+
"kallebysantos/vehicle-emission",
76+
{
7577
path: {
76-
modelFile: 'model.onnx',
78+
modelFile: "model.onnx",
7779
},
78-
});
80+
},
81+
);
7982

8083
Deno.serve(async (_req: Request) => {
81-
// sample data could be a JSON request
82-
const carsBatchInput = [{
83-
'Model_Year': 2021,
84-
'Engine_Size': 2.9,
85-
'Cylinders': 6,
86-
'Fuel_Consumption_in_City': 13.9,
87-
'Fuel_Consumption_in_City_Hwy': 10.3,
88-
'Fuel_Consumption_comb': 12.3,
89-
'Smog_Level': 3,
90-
}, {
91-
'Model_Year': 2023,
92-
'Engine_Size': 2.4,
93-
'Cylinders': 4,
94-
'Fuel_Consumption_in_City': 9.9,
95-
'Fuel_Consumption_in_City_Hwy': 7.0,
96-
'Fuel_Consumption_comb': 8.6,
97-
'Smog_Level': 3,
98-
}];
99-
100-
// Parsing objects to tensor input
101-
const inputTensors: Record<string, Supabase.ai.RawTensor<'float32'>> = {};
102-
session.inputs.forEach((inputKey) => {
103-
const values = carsBatchInput.map((item) => item[inputKey]);
104-
105-
inputTensors[inputKey] = new RawTensor('float32', values, [values.length, 1]);
106-
});
107-
108-
const { emissions } = await session.run(inputTensors);
109-
console.log(emissions);
110-
// [ 289.01, 199.53]
111-
112-
return Response.json({ result: emissions });
84+
// sample data could be a JSON request
85+
const carsBatchInput = [{
86+
"Model_Year": 2021,
87+
"Engine_Size": 2.9,
88+
"Cylinders": 6,
89+
"Fuel_Consumption_in_City": 13.9,
90+
"Fuel_Consumption_in_City_Hwy": 10.3,
91+
"Fuel_Consumption_comb": 12.3,
92+
"Smog_Level": 3,
93+
}, {
94+
"Model_Year": 2023,
95+
"Engine_Size": 2.4,
96+
"Cylinders": 4,
97+
"Fuel_Consumption_in_City": 9.9,
98+
"Fuel_Consumption_in_City_Hwy": 7.0,
99+
"Fuel_Consumption_comb": 8.6,
100+
"Smog_Level": 3,
101+
}];
102+
103+
// Parsing objects to tensor input
104+
const inputTensors: Record<string, Supabase.ai.RawTensor<"float32">> = {};
105+
session.inputs.forEach((inputKey) => {
106+
const values = carsBatchInput.map((item) => item[inputKey]);
107+
108+
inputTensors[inputKey] = new RawTensor("float32", values, [
109+
values.length,
110+
1,
111+
]);
112+
});
113+
114+
const { emissions } = await session.run(inputTensors);
115+
console.log(emissions);
116+
// [ 289.01, 199.53]
117+
118+
return Response.json({ result: emissions });
113119
});

examples/text-to-audio/index.ts

Lines changed: 74 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { PreTrainedTokenizer } from 'https://cdn.jsdelivr.net/npm/@huggingface/[email protected]';
1+
import { PreTrainedTokenizer } from "https://cdn.jsdelivr.net/npm/@huggingface/[email protected]";
22

33
// import 'phonemize' code from Kokoro.js repo
4-
import { phonemize } from './phonemizer.js';
4+
import { phonemize } from "./phonemizer.js";
55

66
const { RawTensor, RawSession } = Supabase.ai;
77

@@ -15,95 +15,97 @@ const SAMPLE_RATE = 24000; // 24 kHz
1515
> The size of s and c is 256 × 1
1616
*/
1717
const STYLE_DIM = 256;
18-
const MODEL_ID = 'onnx-community/Kokoro-82M-ONNX';
18+
const MODEL_ID = "onnx-community/Kokoro-82M-ONNX";
1919

2020
// https://huggingface.co/onnx-community/Kokoro-82M-ONNX#samples
2121
const ALLOWED_VOICES = [
22-
'af_bella',
23-
'af_nicole',
24-
'af_sarah',
25-
'af_sky',
26-
'am_adam',
27-
'am_michael',
28-
'bf_emma',
29-
'bf_isabella',
30-
'bm_george',
31-
'bm_lewis',
22+
"af_bella",
23+
"af_nicole",
24+
"af_sarah",
25+
"af_sky",
26+
"am_adam",
27+
"am_michael",
28+
"bf_emma",
29+
"bf_isabella",
30+
"bm_george",
31+
"bm_lewis",
3232
];
3333

3434
const session = await RawSession.fromHuggingFace(MODEL_ID);
3535

3636
Deno.serve(async (req) => {
37-
const params = new URL(req.url).searchParams;
38-
const text = params.get('text') ?? 'Hello from Supabase!';
39-
const voice = params.get('voice') ?? 'af_bella';
40-
41-
if (!ALLOWED_VOICES.includes(voice)) {
42-
return Response.json({
43-
error: `invalid voice '${voice}'`,
44-
must_be_one_of: ALLOWED_VOICES,
45-
}, { status: 400 });
46-
}
47-
48-
const tokenizer = await loadTokenizer();
49-
const language = voice.at(0); // 'a'merican | 'b'ritish
50-
const phonemes = await phonemize(text, language);
51-
const { input_ids } = tokenizer(phonemes, {
52-
truncation: true,
53-
});
54-
55-
// Select voice style based on number of input tokens
56-
const num_tokens = Math.max(
57-
input_ids.dims.at(-1) - 2, // Without padding;
58-
0,
59-
);
60-
61-
const voiceStyle = await loadVoiceStyle(voice, num_tokens);
62-
63-
const { waveform } = await session.run({
64-
input_ids,
65-
style: voiceStyle,
66-
speed: new Tensor('float32', [1], [1]),
67-
});
68-
69-
// Do `wave` encoding from rust backend
70-
const audio = await waveform.tryEncodeAudio(SAMPLE_RATE);
71-
72-
return new Response(audio, {
73-
headers: {
74-
'Content-Type': 'audio/wav',
75-
},
76-
});
37+
const params = new URL(req.url).searchParams;
38+
const text = params.get("text") ?? "Hello from Supabase!";
39+
const voice = params.get("voice") ?? "af_bella";
40+
41+
if (!ALLOWED_VOICES.includes(voice)) {
42+
return Response.json({
43+
error: `invalid voice '${voice}'`,
44+
must_be_one_of: ALLOWED_VOICES,
45+
}, { status: 400 });
46+
}
47+
48+
const tokenizer = await loadTokenizer();
49+
const language = voice.at(0); // 'a'merican | 'b'ritish
50+
const phonemes = await phonemize(text, language);
51+
const { input_ids } = tokenizer(phonemes, {
52+
truncation: true,
53+
});
54+
55+
// Select voice style based on number of input tokens
56+
const num_tokens = Math.max(
57+
input_ids.dims.at(-1) - 2, // Without padding;
58+
0,
59+
);
60+
61+
const voiceStyle = await loadVoiceStyle(voice, num_tokens);
62+
63+
const { waveform } = await session.run({
64+
input_ids,
65+
style: voiceStyle,
66+
speed: new Tensor("float32", [1], [1]),
67+
});
68+
69+
// Do `wave` encoding from rust backend
70+
const audio = await waveform.tryEncodeAudio(SAMPLE_RATE);
71+
72+
return new Response(audio, {
73+
headers: {
74+
"Content-Type": "audio/wav",
75+
},
76+
});
7777
});
7878

7979
async function loadVoiceStyle(voice: string, num_tokens: number) {
80-
const voice_url =
81-
`https://huggingface.co/onnx-community/Kokoro-82M-ONNX/resolve/main/voices/${voice}.bin?download=true`;
80+
const voice_url =
81+
`https://huggingface.co/onnx-community/Kokoro-82M-ONNX/resolve/main/voices/${voice}.bin?download=true`;
8282

83-
console.log('loading voice:', voice_url);
83+
console.log("loading voice:", voice_url);
8484

85-
const voiceBuffer = await fetch(voice_url).then(async (res) => await res.arrayBuffer());
85+
const voiceBuffer = await fetch(voice_url).then(async (res) =>
86+
await res.arrayBuffer()
87+
);
8688

87-
const offset = num_tokens * STYLE_DIM;
88-
const voiceData = new Float32Array(voiceBuffer).slice(
89-
offset,
90-
offset + STYLE_DIM,
91-
);
89+
const offset = num_tokens * STYLE_DIM;
90+
const voiceData = new Float32Array(voiceBuffer).slice(
91+
offset,
92+
offset + STYLE_DIM,
93+
);
9294

93-
return new Tensor('float32', voiceData, [1, STYLE_DIM]);
95+
return new Tensor("float32", voiceData, [1, STYLE_DIM]);
9496
}
9597

9698
async function loadTokenizer() {
97-
// BUG: invalid 'h' not JSON. That's why we need to manually fetch the assets
98-
// const tokenizer = await AutoTokenizer.from_pretrained(MODEL_ID);
99+
// BUG: invalid 'h' not JSON. That's why we need to manually fetch the assets
100+
// const tokenizer = await AutoTokenizer.from_pretrained(MODEL_ID);
99101

100-
const tokenizerData = await fetch(
101-
'https://huggingface.co/onnx-community/Kokoro-82M-ONNX/resolve/main/tokenizer.json?download=true',
102-
).then(async (res) => await res.json());
102+
const tokenizerData = await fetch(
103+
"https://huggingface.co/onnx-community/Kokoro-82M-ONNX/resolve/main/tokenizer.json?download=true",
104+
).then(async (res) => await res.json());
103105

104-
const tokenizerConfig = await fetch(
105-
'https://huggingface.co/onnx-community/Kokoro-82M-ONNX/resolve/main/tokenizer_config.json?download=true',
106-
).then(async (res) => await res.json());
106+
const tokenizerConfig = await fetch(
107+
"https://huggingface.co/onnx-community/Kokoro-82M-ONNX/resolve/main/tokenizer_config.json?download=true",
108+
).then(async (res) => await res.json());
107109

108-
return new PreTrainedTokenizer(tokenizerData, tokenizerConfig);
110+
return new PreTrainedTokenizer(tokenizerData, tokenizerConfig);
109111
}

0 commit comments

Comments
 (0)