freealise commited on
Commit
ba90d1c
·
verified ·
1 Parent(s): 3f5a6b6

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +37 -8
index.js CHANGED
@@ -1,6 +1,6 @@
1
- class MyClassificationPipeline {
2
- static task = 'text-classification';
3
- static model = 'Xenova/distilbert-base-uncased-finetuned-sst-2-english';
4
  static instance = null;
5
 
6
  static async getInstance(progress_callback = null) {
@@ -17,22 +17,51 @@ class MyClassificationPipeline {
17
  return this.instance;
18
  }
19
  }
20
- //MyClassificationPipeline.getInstance();
21
 
22
 
23
  const http = require('http');
24
  const url = require('url');
 
25
 
26
  http.createServer(async (req, res) => {
27
  res.writeHead(200, {'Content-Type': 'text/html'});
28
  var u = url.parse(req.url, true);
29
 
30
  if (u.query.q) {
31
- const classifier = await MyClassificationPipeline.getInstance();
32
- const response = await classifier(u.query.q);
33
- res.end(JSON.stringify(response));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  } else {
35
- res.end("Empty query string");
36
  }
37
 
38
  }).listen(8080);
 
1
+ class MyPipeline {
2
+ static task = 'automatic-speech-recognition';
3
+ static model = 'Xenova/whisper-tiny.en';
4
  static instance = null;
5
 
6
  static async getInstance(progress_callback = null) {
 
17
  return this.instance;
18
  }
19
  }
20
+ //MyPipeline.getInstance();
21
 
22
 
23
  const http = require('http');
24
  const url = require('url');
25
+ const wavefile = require('wavefile');
26
 
27
  http.createServer(async (req, res) => {
28
  res.writeHead(200, {'Content-Type': 'text/html'});
29
  var u = url.parse(req.url, true);
30
 
31
  if (u.query.q) {
32
+ const transcriber = await MyPipeline.getInstance();
33
+
34
+ //let url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav';
35
+ let buffer = Buffer.from(await fetch(u.query.q).then(x => x.arrayBuffer()))
36
+
37
+ // Read .wav file and convert it to required format
38
+ let wav = new wavefile.WaveFile(buffer);
39
+ wav.toBitDepth('32f'); // Pipeline expects input as a Float32Array
40
+ wav.toSampleRate(16000); // Whisper expects audio with a sampling rate of 16000
41
+ let audioData = wav.getSamples();
42
+ if (Array.isArray(audioData)) {
43
+ if (audioData.length > 1) {
44
+ const SCALING_FACTOR = Math.sqrt(2);
45
+
46
+ // Merge channels (into first channel to save memory)
47
+ for (let i = 0; i < audioData[0].length; ++i) {
48
+ audioData[0][i] = SCALING_FACTOR * (audioData[0][i] + audioData[1][i]) / 2;
49
+ }
50
+ }
51
+
52
+ // Select first channel
53
+ audioData = audioData[0];
54
+ }
55
+
56
+ // Run model
57
+ let start = performance.now();
58
+ let output = await transcriber(audioData);
59
+ let end = performance.now();
60
+
61
+ res.write(`Execution duration: ${(end - start) / 1000} seconds <br/>`);
62
+ res.end(JSON.stringify(output));
63
  } else {
64
+ res.end("Empty query");
65
  }
66
 
67
  }).listen(8080);