Skip to content

Commit

Permalink
fix config none
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron_ng committed Nov 3, 2023
1 parent 72e6100 commit 985e113
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 97 deletions.
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
1 change: 1 addition & 0 deletions docs/_static/pygments.css
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ span.linenos.special { color: #000000; background-color: #ffffc0; padding-left:
.highlight .cs { color: #8f5902; font-style: italic } /* Comment.Special */
.highlight .gd { color: #a40000 } /* Generic.Deleted */
.highlight .ge { color: #000000; font-style: italic } /* Generic.Emph */
.highlight .ges { color: #000000 } /* Generic.EmphStrong */
.highlight .gr { color: #ef2929 } /* Generic.Error */
.highlight .gh { color: #000080; font-weight: bold } /* Generic.Heading */
.highlight .gi { color: #00A000 } /* Generic.Inserted */
Expand Down
Loading

0 comments on commit 985e113

Please sign in to comment.