Skip to content

Commit

Permalink
Merge branch 'master' into add-metrics/wer
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Cochrane committed Nov 9, 2023
2 parents e81cb15 + 5cf9f07 commit fafec79
Show file tree
Hide file tree
Showing 16 changed files with 184 additions and 99 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.12.0] - 2023-11-03

### Added

- Add support for batch auto chapters

## [1.11.1] - 2023-10-19

### Added
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,12 @@ A complete list of commands and flags can be found in the SDK docs at https://sp
speechmatics batch transcribe --detect-topics --output-format json-v2 example_audio.wav
```
- Submit a job with auto chapters
```bash
speechmatics batch transcribe --detect-chapters --output-format json-v2 example_audio.wav
```
### Custom Transcription Config File
- Instead of passing all the transcription options via the command line you can also pass a transcription config file.
The config file is a JSON file that contains the transcription options.
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.11.1
1.12.0
66 changes: 43 additions & 23 deletions docs/_modules/speechmatics/batch_client.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ <h1>Source code for speechmatics.batch_client</h1><div class="highlight"><pre>

<span class="kn">import</span> <span class="nn">httpx</span>
<span class="kn">from</span> <span class="nn">polling2</span> <span class="kn">import</span> <span class="n">poll</span>
<span class="kn">from</span> <span class="nn">tenacity</span> <span class="kn">import</span> <span class="n">retry</span><span class="p">,</span> <span class="n">retry_if_exception_type</span><span class="p">,</span> <span class="n">stop_after_attempt</span>

<span class="kn">from</span> <span class="nn">speechmatics.exceptions</span> <span class="kn">import</span> <span class="n">JobNotFoundException</span><span class="p">,</span> <span class="n">TranscriptionError</span>
<span class="kn">from</span> <span class="nn">speechmatics.helpers</span> <span class="kn">import</span> <span class="n">get_version</span>
Expand Down Expand Up @@ -199,11 +200,19 @@ <h1>Source code for speechmatics.batch_client</h1><div class="highlight"><pre>

<span class="sd"> :raises httpx.HTTPError: When a request fails, raises an HTTPError</span>
<span class="sd"> &quot;&quot;&quot;</span>

<span class="c1"># pylint: disable=no-member</span>
<span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">api_client</span><span class="o">.</span><span class="n">stream</span><span class="p">(</span><span class="n">method</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="k">as</span> <span class="n">response</span><span class="p">:</span>
<span class="n">response</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="n">response</span><span class="o">.</span><span class="n">raise_for_status</span><span class="p">()</span>
<span class="k">return</span> <span class="n">response</span></div>
<span class="nd">@retry</span><span class="p">(</span>
<span class="n">stop</span><span class="o">=</span><span class="n">stop_after_attempt</span><span class="p">(</span><span class="mi">2</span><span class="p">),</span>
<span class="n">retry</span><span class="o">=</span><span class="n">retry_if_exception_type</span><span class="p">(</span><span class="n">httpx</span><span class="o">.</span><span class="n">RemoteProtocolError</span><span class="p">),</span>
<span class="p">)</span>
<span class="k">def</span> <span class="nf">send</span><span class="p">():</span>
<span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">api_client</span><span class="o">.</span><span class="n">stream</span><span class="p">(</span><span class="n">method</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="k">as</span> <span class="n">response</span><span class="p">:</span>
<span class="n">response</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="n">response</span><span class="o">.</span><span class="n">raise_for_status</span><span class="p">()</span>
<span class="k">return</span> <span class="n">response</span>

<span class="k">return</span> <span class="n">send</span><span class="p">()</span></div>

<div class="viewcode-block" id="BatchClient.list_jobs"><a class="viewcode-back" href="../../batch_client.html#speechmatics.batch_client.BatchClient.list_jobs">[docs]</a> <span class="k">def</span> <span class="nf">list_jobs</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">Any</span><span class="p">]]:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
Expand Down Expand Up @@ -256,28 +265,39 @@ <h1>Source code for speechmatics.batch_client</h1><div class="highlight"><pre>
<span class="p">)</span>

<span class="c1"># If audio=None, fetch_data must be specified</span>
<span class="k">if</span> <span class="n">audio</span> <span class="ow">and</span> <span class="s2">&quot;fetch_data&quot;</span> <span class="ow">in</span> <span class="n">config_dict</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Only one of audio or fetch_data can be set at a time&quot;</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">audio</span> <span class="ow">and</span> <span class="s2">&quot;fetch_data&quot;</span> <span class="ow">in</span> <span class="n">config_dict</span><span class="p">:</span>
<span class="n">audio_data</span> <span class="o">=</span> <span class="kc">None</span>
<span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">audio</span><span class="p">,</span> <span class="p">(</span><span class="nb">str</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">PathLike</span><span class="p">)):</span>
<span class="k">with</span> <span class="n">Path</span><span class="p">(</span><span class="n">audio</span><span class="p">)</span><span class="o">.</span><span class="n">expanduser</span><span class="p">()</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">&quot;rb&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">file</span><span class="p">:</span>
<span class="n">audio_data</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">file</span><span class="o">.</span><span class="n">name</span><span class="p">),</span> <span class="n">file</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">audio</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)</span> <span class="ow">and</span> <span class="s2">&quot;fetch_data&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">config_dict</span><span class="p">:</span>
<span class="n">audio_data</span> <span class="o">=</span> <span class="n">audio</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Audio must be a filepath or a tuple of (filename, bytes)&quot;</span><span class="p">)</span>
<span class="n">file_object</span> <span class="o">=</span> <span class="kc">None</span>
<span class="k">try</span><span class="p">:</span>
<span class="k">if</span> <span class="n">audio</span> <span class="ow">and</span> <span class="s2">&quot;fetch_data&quot;</span> <span class="ow">in</span> <span class="n">config_dict</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Only one of audio or fetch_data can be set at a time&quot;</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">audio</span> <span class="ow">and</span> <span class="s2">&quot;fetch_data&quot;</span> <span class="ow">in</span> <span class="n">config_dict</span><span class="p">:</span>
<span class="n">audio_data</span> <span class="o">=</span> <span class="kc">None</span>
<span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">audio</span><span class="p">,</span> <span class="p">(</span><span class="nb">str</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">PathLike</span><span class="p">)):</span>
<span class="c1"># httpx performance is better when using a file-like object</span>
<span class="c1"># compared to passing the file contents as bytes.</span>
<span class="n">file_object</span> <span class="o">=</span> <span class="n">Path</span><span class="p">(</span><span class="n">audio</span><span class="p">)</span><span class="o">.</span><span class="n">expanduser</span><span class="p">()</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">&quot;rb&quot;</span><span class="p">)</span>
<span class="n">audio_data</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">file_object</span><span class="o">.</span><span class="n">name</span><span class="p">),</span> <span class="n">file_object</span>
<span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">audio</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)</span> <span class="ow">and</span> <span class="s2">&quot;fetch_data&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">config_dict</span><span class="p">:</span>
<span class="n">audio_data</span> <span class="o">=</span> <span class="n">audio</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
<span class="s2">&quot;Audio must be a filepath or a tuple of (filename, bytes)&quot;</span>
<span class="p">)</span>

<span class="c1"># httpx seems to expect an un-nested json, throws a type error otherwise.</span>
<span class="n">config_data</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;config&quot;</span><span class="p">:</span> <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">config_dict</span><span class="p">,</span> <span class="n">ensure_ascii</span><span class="o">=</span><span class="kc">False</span><span class="p">)}</span>
<span class="c1"># httpx seems to expect an un-nested json, throws a type error otherwise.</span>
<span class="n">config_data</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;config&quot;</span><span class="p">:</span> <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">config_dict</span><span class="p">,</span> <span class="n">ensure_ascii</span><span class="o">=</span><span class="kc">False</span><span class="p">)}</span>

<span class="k">if</span> <span class="n">audio_data</span><span class="p">:</span>
<span class="n">audio_file</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;data_file&quot;</span><span class="p">:</span> <span class="n">audio_data</span><span class="p">}</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">audio_file</span> <span class="o">=</span> <span class="n">_ForceMultipartDict</span><span class="p">()</span>
<span class="k">if</span> <span class="n">audio_data</span><span class="p">:</span>
<span class="n">audio_file</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;data_file&quot;</span><span class="p">:</span> <span class="n">audio_data</span><span class="p">}</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">audio_file</span> <span class="o">=</span> <span class="n">_ForceMultipartDict</span><span class="p">()</span>

<span class="n">response</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">send_request</span><span class="p">(</span><span class="s2">&quot;POST&quot;</span><span class="p">,</span> <span class="s2">&quot;jobs&quot;</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">config_data</span><span class="p">,</span> <span class="n">files</span><span class="o">=</span><span class="n">audio_file</span><span class="p">)</span>
<span class="k">return</span> <span class="n">response</span><span class="o">.</span><span class="n">json</span><span class="p">()[</span><span class="s2">&quot;id&quot;</span><span class="p">]</span></div>
<span class="n">response</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">send_request</span><span class="p">(</span>
<span class="s2">&quot;POST&quot;</span><span class="p">,</span> <span class="s2">&quot;jobs&quot;</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">config_data</span><span class="p">,</span> <span class="n">files</span><span class="o">=</span><span class="n">audio_file</span>
<span class="p">)</span>
<span class="k">return</span> <span class="n">response</span><span class="o">.</span><span class="n">json</span><span class="p">()[</span><span class="s2">&quot;id&quot;</span><span class="p">]</span>
<span class="k">finally</span><span class="p">:</span>
<span class="k">if</span> <span class="n">file_object</span><span class="p">:</span>
<span class="n">file_object</span><span class="o">.</span><span class="n">close</span><span class="p">()</span></div>

<span class="k">def</span> <span class="nf">submit_jobs</span><span class="p">(</span>
<span class="bp">self</span><span class="p">,</span>
Expand Down
Loading

0 comments on commit fafec79

Please sign in to comment.