emilstabil commited on
Commit
d1372b2
·
verified ·
1 Parent(s): de465d1

Add ONNX and Transformers Python quickstarts

Browse files
Files changed (1) hide show
  1. README.md +54 -0
README.md CHANGED
@@ -53,6 +53,60 @@ For longer transcripts, the runtime uses 128-token windows with 12 words of over
53
  and keeps every input word in order. Pass continuous text when possible so PnC can
54
  use context across speech recognition chunks.
55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  Version 2 is tuned for **continuous speech** such as dictation and meetings.
57
  The earlier version often missed sentence endings when given a long transcript;
58
  it had mainly learned from short, isolated utterances. Version 2 was trained
 
53
  and keeps every input word in order. Pass continuous text when possible so PnC can
54
  use context across speech recognition chunks.
55
 
56
+ ### Python with ONNX
57
+
58
+ Install `ekko-stt[pnc]` in your Python environment. This loads the int8 ONNX
59
+ export and applies the same windowing as the command above:
60
+
61
+ ```python
62
+ from ekko import Punctuator
63
+ from ekko.artifacts import ensure_pnc_directory
64
+
65
+ pnc = Punctuator(ensure_pnc_directory())
66
+ print(pnc("hej mit navn er emil hvordan går det i dag"))
67
+ ```
68
+
69
+ <details>
70
+ <summary>Python with Transformers (PyTorch)</summary>
71
+
72
+ Install `transformers` and `torch` to use the PyTorch weights directly. This
73
+ short-input example maps predictions from the first token of each word back to
74
+ the original words:
75
+
76
+ ```python
77
+ import torch
78
+ from transformers import AutoModelForTokenClassification, AutoTokenizer
79
+
80
+ repo = "RyeAI/ekko-pnc"
81
+ tokenizer = AutoTokenizer.from_pretrained(repo)
82
+ model = AutoModelForTokenClassification.from_pretrained(repo).eval()
83
+ words = "hej mit navn er emil hvordan går det i dag".split()
84
+ inputs = tokenizer(words, is_split_into_words=True, return_tensors="pt")
85
+
86
+ with torch.inference_mode():
87
+ predictions = model(**inputs).logits.argmax(dim=-1)[0].tolist()
88
+
89
+ output, seen, capitalize_next = [], set(), True
90
+ for word_id, prediction in zip(inputs.word_ids(), predictions):
91
+ if word_id is None or word_id in seen:
92
+ continue
93
+ seen.add(word_id)
94
+ label = model.config.id2label[prediction]
95
+ mark = label.removesuffix("|U")
96
+ word = words[word_id]
97
+ if capitalize_next or label.endswith("|U"):
98
+ word = word[:1].upper() + word[1:]
99
+ output.append(word + ("" if mark == "O" else mark))
100
+ capitalize_next = mark in {".", "?", "!"}
101
+
102
+ print(" ".join(output))
103
+ ```
104
+
105
+ </details>
106
+
107
+ For longer transcripts, use the ONNX example above; it preserves all words
108
+ across overlapping windows.
109
+
110
  Version 2 is tuned for **continuous speech** such as dictation and meetings.
111
  The earlier version often missed sentence endings when given a long transcript;
112
  it had mainly learned from short, isolated utterances. Version 2 was trained