osanseviero
HF staff
commited on
Commit
e4f0af6
1 Parent(s): ae3be96
Files changed (4) hide show
  1. .gitattributes +1 -0
  2. char.dict +28 -0
  3. hubert_asr.ckpt +3 -0
  4. model.py +89 -0
.gitattributes CHANGED
@@ -15,3 +15,4 @@
15
  *.pt filter=lfs diff=lfs merge=lfs -text
16
  *.pth filter=lfs diff=lfs merge=lfs -text
17
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
15
  *.pt filter=lfs diff=lfs merge=lfs -text
16
  *.pth filter=lfs diff=lfs merge=lfs -text
17
  *tfevents* filter=lfs diff=lfs merge=lfs -text
18
+ *.ckpt* filter=lfs diff=lfs merge=lfs -text
char.dict ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ | 1980202
2
+ E 1091870
3
+ T 789572
4
+ A 689048
5
+ O 647720
6
+ N 591778
7
+ I 585614
8
+ H 557204
9
+ S 545238
10
+ R 499568
11
+ D 380912
12
+ L 344952
13
+ U 242014
14
+ M 217730
15
+ C 210734
16
+ W 204598
17
+ F 195086
18
+ G 174098
19
+ Y 168548
20
+ P 146722
21
+ B 129608
22
+ V 81496
23
+ K 65070
24
+ ' 19660
25
+ X 12530
26
+ J 12062
27
+ Q 8164
28
+ Z 4916
hubert_asr.ckpt ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fff3c43e5ecff336801d7c436282780e9c0ea4dfbaf3fa3df55267190d5d5fd2
3
+ size 513967537
model.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ This is just an example of what people would submit for
3
+ inference.
4
+ """
5
+
6
+ from s3prl.downstream.runner import Runner
7
+ from typing import Dict
8
+ import torch
9
+ from datasets import load_dataset
10
+
11
+
12
+ class PreTrainedModel(Runner):
13
+ def __init__(self):
14
+ """
15
+ Loads model and tokenizer from local directory
16
+ """
17
+ ckp_file = "hubert_asr.ckpt"
18
+ ckp = torch.load(ckp_file, map_location='cpu')
19
+ ckp["Args"].init_ckpt = ckp_file
20
+ ckp["Args"].mode = "inference"
21
+ ckp["Args"].device = "cpu" # Just to try in my computer
22
+ ckp["Config"]["downstream_expert"]["datarc"]["dict_path"]='char.dict'
23
+
24
+ Runner.__init__(self, ckp["Args"], ckp["Config"])
25
+
26
+ def __call__(self, inputs)-> Dict[str, str]:
27
+ """
28
+ Args:
29
+ inputs (:obj:`np.array`):
30
+ The raw waveform of audio received. By default at 16KHz.
31
+ Return:
32
+ A :obj:`dict`:. The object return should be liked {"text": "XXX"} containing
33
+ the detected text from the input audio.
34
+ """
35
+ for entry in self.all_entries:
36
+ entry.model.eval()
37
+
38
+ inputs = [torch.FloatTensor(inputs)]
39
+
40
+ with torch.no_grad():
41
+ features = self.upstream.model(inputs)
42
+ features = self.featurizer.model(inputs, features)
43
+ preds = self.downstream.model.inference(features, [])
44
+ return preds[0]
45
+
46
+
47
+ """
48
+ import subprocess
49
+ import numpy as np
50
+ # This is already done in the Inference API
51
+ def ffmpeg_read(bpayload: bytes, sampling_rate: int) -> np.array:
52
+ ar = f"{sampling_rate}"
53
+ ac = "1"
54
+ format_for_conversion = "f32le"
55
+ ffmpeg_command = [
56
+ "ffmpeg",
57
+ "-i",
58
+ "pipe:0",
59
+ "-ac",
60
+ ac,
61
+ "-ar",
62
+ ar,
63
+ "-f",
64
+ format_for_conversion,
65
+ "-hide_banner",
66
+ "-loglevel",
67
+ "quiet",
68
+ "pipe:1",
69
+ ]
70
+
71
+ ffmpeg_process = subprocess.Popen(
72
+ ffmpeg_command, stdin=subprocess.PIPE, stdout=subprocess.PIPE
73
+ )
74
+ output_stream = ffmpeg_process.communicate(bpayload)
75
+ out_bytes = output_stream[0]
76
+
77
+ audio = np.frombuffer(out_bytes, np.float32).copy()
78
+ if audio.shape[0] == 0:
79
+ raise ValueError("Malformed soundfile")
80
+ return audio
81
+
82
+
83
+ model = PreTrainedModel()
84
+ ds = load_dataset("patrickvonplaten/librispeech_asr_dummy", "clean", split="validation")
85
+ filename = ds[0]["file"]
86
+ with open(filename, "rb") as f:
87
+ data = ffmpeg_read(f.read(), 16000)
88
+ print(model(data))
89
+ """