1import{a as Z,f as aa}from"./CUKjtEtE.js";import"./BiGR57hC.js";import{s as n,f as na,c as s,n as a,r as e}from"./CK77Fcuk.js";import{C as t}from"./DBQHUn2Y.js";var sa=aa(`<h1>Introduction</h1> <h2>The lightweight Gradio client libraries make it easy to use any Gradio app as an API. We currently support both a Python client library as well as a JavaScript client library.</h2> <h2>The Python client library is gradio_client. Itâs included in the latest versions of the gradio package, but for a more lightweight experience, you can install it using pip without having to install gradio:</h2> <div class="codeblock"><!><pre class="language-bash"><code class="language-bash">pip <span class="token function">install</span> gradio_client</code></pre></div> <h1>Getting Started with the Gradio Python client</h1> <h2>The Gradio Python client makes it very easy to use any Gradio app as an API. As an example, consider this <a href="https://huggingface.co/spaces/abidlabs/whisper" rel="nofollow">Hugging Face Space that transcribes audio files</a> that are recorded from the microphone.</h2> <p><img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/gradio-guides/whisper-screenshot.jpg"/></p> <h2>Using the <code>gradio_client</code> library, we can easily use the Gradio as an API to transcribe audio files programmatically.</h2> <h2>Hereâs the entire code to do it:</h2> <div class="codeblock"><!><pre class="language-python"><code class="language-python"><span class="token keyword">from</span> gradio_client <span class="token keyword">import</span> Client<span class="token punctuation">,</span> <span class="token builtin">file</span> 2 3client <span class="token operator">=</span> Client<span class="token punctuation">(</span><span class="token string">"abidlabs/whisper"</span><span class="token punctuation">)</span> 4 5client<span class="token punctuation">.</span>predict<span class="token punctuation">(</span> 6 audio<span class="token operator">=</span><span class="token builtin">file</span><span class="token punctuation">(</span><span class="token string">"audio_sample.wav"</span><span class="token punctuation">)</span> 7<span class="token punctuation">)</span> 8 9<span class="token operator">>></span> <span class="token string">"This is a test of the whisper speech recognition model."</span></code></pre></div> <p>The Gradio client works with any hosted Gradio app! Although the Client is mostly used with apps hosted on <a href="https://hf.space" rel="nofollow">Hugging Face Spaces</a>, your app can be hosted anywhere, such as your own server.</p> <p><strong>Prerequisites</strong>: To use the Gradio client, you do <em>not</em> need to know the <code>gradio</code> library in great detail. However, it is helpful to have general familiarity with Gradioâs concepts of input and output components.</p> <h3 class="group header-tag" id="installation">Installation<a href="#installation" class="invisible group-hover-visible"><img src="https://raw.githubusercontent.com/gradio-app/gradio/main/js/_website/src/lib/assets/img/anchor.svg" class="anchor-img-small"/></a></h3> <p>If you already have a recent version of <code>gradio</code>, then the <code>gradio_client</code> is included as a dependency. But note that this documentation reflects the latest version of the <code>gradio_client</code>, so upgrade if youâre not sure!</p> <p>The lightweight <code>gradio_client</code> package can be installed from pip (or pip3) and is tested to work with <strong>Python versions 3.9 or higher</strong>:</p> <div class="codeblock"><!><pre class="language-bash"><code class="language-bash">$ pip <span class="token function">install</span> <span class="token parameter variable">--upgrade</span> gradio_client</code></pre></div> <h3 class="group header-tag" id="connecting-to-a-gradio-app-on-hugging-face-spaces">Connecting to a Gradio App on Hugging Face Spaces<a href="#connecting-to-a-gradio-app-on-hugging-face-spaces" class="invisible group-hover-visible"><img src="https://raw.githubusercontent.com/gradio-app/gradio/main/js/_website/src/lib/assets/img/anchor.svg" class="anchor-img-small"/></a></h3> <p>Start by connecting instantiating a <code>Client</code> object and connecting it to a Gradio app that is running on Hugging Face Spaces.</p> <div class="codeblock"><!><pre class="language-python"><code class="language-python"><span class="token keyword">from</span> gradio_client <span class="token keyword">
9import</span> Client 10 11client <span class="token operator">=</span> Client<span class="token punctuation">(</span><span class="token string">"abidlabs/en2fr"</span><span class="token punctuation">)</span> <span class="token comment"># a Space that translates from English to French</span></code></pre></div> <p>You can also connect to private Spaces by passing in your HF token with the <code>hf_token</code> parameter. You can get your HF token here: <a href="https://huggingface.co/settings/tokens" rel="nofollow">https://huggingface.co/settings/tokens</a></p> <div class="codeblock"><!><pre class="language-python"><code class="language-python"><span class="token keyword">from</span> gradio_client <span class="token keyword">import</span> Client 12 13client <span class="token operator">=</span> Client<span class="token punctuation">(</span><span class="token string">"abidlabs/my-private-space"</span><span class="token punctuation">,</span> hf_token<span class="token operator">=</span><span class="token string">"..."</span><span class="token punctuation">)</span></code></pre></div> <h3 class="group header-tag" id="duplicating-a-space-for-private-use">Duplicating a Space for private use<a href="#duplicating-a-space-for-private-use" class="invisible group-hover-visible"><img src="https://raw.githubusercontent.com/gradio-app/gradio/main/js/_website/src/lib/assets/img/anchor.svg" class="anchor-img-small"/></a></h3> <p>While you can use any public Space as an API, you may get rate limited by Hugging Face if you make too many requests. For unlimited usage of a Space, simply duplicate the Space to create a private Space, 14and then use it to make as many requests as youâd like!</p> <p>The <code>gradio_client</code> includes a class method: <code>Client.duplicate()</code> to make this process simple (youâll need to pass in your <a href="https://huggingface.co/settings/tokens" rel="nofollow">Hugging Face token</a> or be logged in using the Hugging Face CLI):</p> <div class="codeblock"><!><pre class="language-python"><code class="language-python"><span class="token keyword">import</span> os 15<span class="token keyword">from</span> gradio_client <span class="token keyword">import</span> Client<span class="token punctuation">,</span> <span class="token builtin">file</span> 16 17HF_TOKEN <span class="token operator">=</span> os<span class="token punctuation">.</span>environ<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">"HF_TOKEN"</span><span class="token punctuation">)</span> 18 19client <span class="token operator">=</span> Client<span class="token punctuation">.</span>duplicate<span class="token punctuation">(</span><span class="token string">"abidlabs/whisper"</span><span class="token punctuation">,</span> hf_token<span class="token operator">=</span>HF_TOKEN<span class="token punctuation">)</span> 20client<span class="token punctuation">.</span>predict<span class="token punctuation">(</span><span class="token builtin">file</span><span class="token punctuation">(</span><span class="token string">"audio_sample.wav"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> 21 22<span class="token operator">>></span> <span class="token string">"This is a test of the whisper speech recognition model."</span></code></pre></div> <p>If you have previously duplicated a Space, re-running <code>duplicate()</code> will <em>not</em> create a new Space. Instead, the Client will attach to the previously-created Space. So it is safe to re-run the <code>Client.duplicate()</code> method multiple times.</p> <p><strong>Note:</strong> if the original Space uses GPUs, your private Space will as well, and your Hugging Face account will get billed based on the price of the GPU. To minimize charges, your Space will automatically go to sleep after 1 hour of inactivity. You can also set the hardware using the <code>hardware</code> parameter of <code>duplicate()</code>.</p> <h3 class="group header-tag" id="connecting-a-general-gradio-app">Connecting a general Gradio app<a href="#connecting-a-general-gradio-app" class="invisible group-hover-visible"><img src="https://raw.githubusercontent.com/gradio-app/gradio/main/js/_website/src/lib/assets/img/anchor.svg" class="anchor-img-small"/></a></h3> <p>If your app is running somewhere else, just provide the full URL instead, including the âhttp://â or âhttps://â. Hereâs an example of making predictions to a Gradio app that is running on a share URL:</p> <div class="codeblock"><!><pre class="language-python"><code class="language-python"><span class="token keyword">from</span> gradio_client <span class="token keyword">
22import</span> Client 23 24client <span class="token operator">=</span> Client<span class="token punctuation">(</span><span class="token string">"https://bec81a83-5b5c-471e.gradio.live"</span><span class="token punctuation">)</span></code></pre></div> <h3 class="group header-tag" id="inspecting-the-api-endpoints">Inspecting the API endpoints<a href="#inspecting-the-api-endpoints" class="invisible group-hover-visible"><img src="https://raw.githubusercontent.com/gradio-app/gradio/main/js/_website/src/lib/assets/img/anchor.svg" class="anchor-img-small"/></a></h3> <p>Once you have connected to a Gradio app, you can view the APIs that are available to you by calling the <code>Client.view_api()</code> method. For the Whisper Space, we see the following:</p> <div class="codeblock"><!><pre class="language-bash"><code class="language-bash">Client.predict<span class="token punctuation">(</span><span class="token punctuation">)</span> Usage Info 25--------------------------- 26Named API endpoints: <span class="token number">1</span> 27 28 - predict<span class="token punctuation">(</span>audio, <span class="token assign-left variable">api_name</span><span class="token operator">=</span><span class="token string">"/predict"</span><span class="token punctuation">)</span> -<span class="token operator">></span> output 29 Parameters: 30 - <span class="token punctuation">[</span>Audio<span class="token punctuation">]</span> audio: filepath <span class="token punctuation">(</span>required<span class="token punctuation">)</span> 31 Returns: 32 - <span class="token punctuation">[</span>Textbox<span class="token punctuation">]</span> output: str </code></pre></div> <p>We see that we have 1 API endpoint in this space, and shows us how to use the API endpoint to make a prediction: we should call the <code>.predict()</code> method (which we will explore below), providing a parameter <code>input_audio</code> of type <code>str</code>, which is a <code>filepath or URL</code>.</p> <p>We should also provide the <code>api_name='/predict'</code> argument to the <code>predict()</code> method. Although this isnât necessary if a Gradio app has only 1 named endpoint, it does allow us to call different endpoints in a single app if they are available.</p> <h3 class="group header-tag" id="the-view-api-page">The âView APIâ Page<a href="#the-view-api-page" class="invisible group-hover-visible"><img src="https://raw.githubusercontent.com/gradio-app/gradio/main/js/_website/src/lib/assets/img/anchor.svg" class="anchor-img-small"/></a></h3> <p>As an alternative to running the <code>.view_api()</code> method, you can click on the âUse via APIâ link in the footer of the Gradio app, which shows us the same information, along with example usage.</p> <p><img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/gradio-guides/view-api.png"/></p> <p>The View API page also includes an âAPI Recorderâ that lets you interact with the Gradio UI normally and converts your interactions into the corresponding code to run with the Python Client.</p> <h3 class="group header-tag" id="making-a-prediction">Making a prediction<a href="#making-a-prediction" class="invisible group-hover-visible"><img src="https://raw.githubusercontent.com/gradio-app/gradio/main/js/_website/src/lib/assets/img/anchor.svg" class="anchor-img-small"/></a></h3> <p>The simplest way to make a prediction is simply to call the <code>.predict()</code> function with the appropriate arguments:</p> <div class="codeblock"><!><pre class="language-python"><code class="language-python"><span class="token keyword">from</span> gradio_client <span class="token keyword">import</span> Client 33 34client <span class="token operator">=</span> Client<span class="token punctuation">(</span><span class="token string">"abidlabs/en2fr"</span><span class="token punctuation">,</span> api_name<span class="token operator">=</span><span class="token string">'/predict'</span><span class="token punctuation">)</span> 35client<span class="token punctuation">.</span>predict<span class="token punctuation">(</span><span class="token string">"Hello"</span><span class="token punctuation">)</span> 36 37<span class="token operator">>></span> Bonjour</code></pre></div> <p>If there are multiple parameters, then you should pass them as separate arguments to <code>.predict()</code>, like this:</p> <div class="codeblock"><!><pre class="language-python"><code class="language-python"><span class="token keyword">from</span> gradio_client <span class="token keyword">
37import</span> Client 38 39client <span class="token operator">=</span> Client<span class="token punctuation">(</span><span class="token string">"gradio/calculator"</span><span class="token punctuation">)</span> 40client<span class="token punctuation">.</span>predict<span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token string">"add"</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span> 41 42<span class="token operator">>></span> <span class="token number">9.0</span></code></pre></div> <p>It is recommended to provide key-word arguments instead of positional arguments:</p> <div class="codeblock"><!><pre class="language-python"><code class="language-python"><span class="token keyword">from</span> gradio_client <span class="token keyword">import</span> Client 43 44client <span class="token operator">=</span> Client<span class="token punctuation">(</span><span class="token string">"gradio/calculator"</span><span class="token punctuation">)</span> 45client<span class="token punctuation">.</span>predict<span class="token punctuation">(</span>num1<span class="token operator">=</span><span class="token number">4</span><span class="token punctuation">,</span> operation<span class="token operator">=</span><span class="token string">"add"</span><span class="token punctuation">,</span> num2<span class="token operator">=</span><span class="token number">5</span><span class="token punctuation">)</span> 46 47<span class="token operator">>></span> <span class="token number">9.0</span></code></pre></div> <p>This allows you to take advantage of default arguments. For example, this Space includes the default value for the Slider component so you do not need to provide it when accessing it with the client.</p> <div class="codeblock"><!><pre class="language-python"><code class="language-python"><span class="token keyword">from</span> gradio_client <span class="token keyword">import</span> Client 48 49client <span class="token operator">=</span> Client<span class="token punctuation">(</span><span class="token string">"abidlabs/image_generator"</span><span class="token punctuation">)</span> 50client<span class="token punctuation">.</span>predict<span class="token punctuation">(</span>text<span class="token operator">=</span><span class="token string">"an astronaut riding a camel"</span><span class="token punctuation">)</span></code></pre></div> <p>The default value is the initial value of the corresponding Gradio component. If the component does not have an initial value, but if the corresponding argument in the predict function has a default value of <code>None</code>, then that parameter is also optional in the client. Of course, if youâd like to override it, you can include it as well:</p> <div class="codeblock"><!><pre class="language-python"><code class="language-python"><span class="token keyword">from</span> gradio_client <span class="token keyword">import</span> Client 51 52client <span class="token operator">=</span> Client<span class="token punctuation">(</span><span class="token string">"abidlabs/image_generator"</span><span class="token punctuation">)</span> 53client<span class="token punctuation">.</span>predict<span class="token punctuation">(</span>text<span class="token operator">=</span><span class="token string">"an astronaut riding a camel"</span><span class="token punctuation">,</span> steps<span class="token operator">=</span><span class="token number">25</span><span class="token punctuation">)</span></code></pre></div> <p>For providing files or URLs as inputs, you should pass in the filepath or URL to the file enclosed within <code>gradio_client.file()</code>. This takes care of uploading the file to the Gradio server and ensures that the file is preprocessed correctly:</p> <div class="codeblock"><!><pre class="language-python"><code class="language-python"><span class="token keyword">from</span> gradio_client <span class="token keyword">import</span> Client<span class="token punctuation">,</span> <span class="token builtin">file</span> 54 55client <span class="token operator">=</span> Client<span class="token punctuation">(</span><span class="token string">"abidlabs/whisper"</span><span class="token punctuation">)</span> 56client<span class="token punctuation">.</span>predict<span class="token punctuation">(</span> 57 audio<span class="token operator">=</span><span class="token builtin">file</span><span class="token punctuation">(</span><span class="token string">"https://audio-samples.github.io/samples/mp3/blizzard_unconditional/sample-0.mp3"</span><span class="token punctuation">)</span> 58<span class="token punctuation">)</span> 59 60<span class="token operator">>></span> <span class="token string">"My thought I have nobody by a beauty and will as you poured. Mr. Rochester is serve in that so don't find simpus, and devoted abode, to at might in a râ"</span></code></pre></div> <h3 class="group header-tag" id="running-jobs-asynchronously">Running jobs asynchronously<a href="#running-jobs-asynchronously" class="invisible group-hover-visible"><img src="https://raw.githubusercontent.com/gradio-app/gradio/main/js/_website/src/lib/assets/img/anchor.svg" class="anchor-img-small"/></a></h3> <p>Oe should note that <code>.predict()</code> is a <em>blocking</em> operation as it waits for the operation to complete before returning the prediction.</p> <p>In many cases, you may be better off letting the job run in the background until you need the results of the prediction. You can do this by creating a <code>Job</code> instance using the <code>.submit()</code> method, and then later calling <code>.result()</code> on the job to get the result. For example:</p> <div class="codeblock"><!><pre class="language-python"><code class="language-python"><span class="token keyword">from</span> gradio_client <span class="token keyword">
60import</span> Client 61 62client <span class="token operator">=</span> Client<span class="token punctuation">(</span>space<span class="token operator">=</span><span class="token string">"abidlabs/en2fr"</span><span class="token punctuation">)</span> 63job <span class="token operator">=</span> client<span class="token punctuation">.</span>submit<span class="token punctuation">(</span><span class="token string">"Hello"</span><span class="token punctuation">,</span> api_name<span class="token operator">=</span><span class="token string">"/predict"</span><span class="token punctuation">)</span> <span class="token comment"># This is not blocking</span> 64 65<span class="token comment"># Do something else</span> 66 67job<span class="token punctuation">.</span>result<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment"># This is blocking</span> 68 69<span class="token operator">>></span> Bonjour</code></pre></div> <h3 class="group header-tag" id="adding-callbacks">Adding callbacks<a href="#adding-callbacks" class="invisible group-hover-visible"><img src="https://raw.githubusercontent.com/gradio-app/gradio/main/js/_website/src/lib/assets/img/anchor.svg" class="anchor-img-small"/></a></h3> <p>Alternatively, one can add one or more callbacks to perform actions after the job has completed running, like this:</p> <div class="codeblock"><!><pre class="language-python"><code class="language-python"><span class="token keyword">from</span> gradio_client <span class="token keyword">import</span> Client 70 71<span class="token keyword">def</span> <span class="token function">print_result</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">:</span> 72 <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"The translated result is: {x}"</span><span class="token punctuation">)</span> 73 74client <span class="token operator">=</span> Client<span class="token punctuation">(</span>space<span class="token operator">=</span><span class="token string">"abidlabs/en2fr"</span><span class="token punctuation">)</span> 75 76job <span class="token operator">=</span> client<span class="token punctuation">.</span>submit<span class="token punctuation">(</span><span class="token string">"Hello"</span><span class="token punctuation">,</span> api_name<span class="token operator">=</span><span class="token string">"/predict"</span><span class="token punctuation">,</span> result_callbacks<span class="token operator">=</span><span class="token punctuation">[</span>print_result<span class="token punctuation">]</span><span class="token punctuation">)</span> 77 78<span class="token comment"># Do something else</span> 79 80<span class="token operator">>></span> The translated result <span class="token keyword">is</span><span class="token punctuation">:</span> Bonjour 81</code></pre></div> <h3 class="group header-tag" id="status">Status<a href="#status" class="invisible group-hover-visible"><img src="https://raw.githubusercontent.com/gradio-app/gradio/main/js/_website/src/lib/assets/img/anchor.svg" class="anchor-img-small"/></a></h3> <p>The <code>Job</code> object also allows you to get the status of the running job by calling the <code>.status()</code> method. This returns a <code>StatusUpdate</code> object with the following attributes: <code>code</code> (the status code, one of a set of defined strings representing the status. See the <code>utils.Status</code> class), <code>rank</code> (the current position of this job in the queue), <code>queue_size</code> (the total queue size), <code>eta</code> (estimated time this job will complete), <code>success</code> (a boolean representing whether the job completed successfully), and <code>time</code> (the time that the status was generated).</p> <div class="codeblock"><!><pre class="language-py"><code class="language-py"><span class="token keyword">from</span> gradio_client <span class="token keyword">import</span> Client 82 83client <span class="token operator">=</span> Client<span class="token punctuation">(</span>src<span class="token operator">=</span><span class="token string">"gradio/calculator"</span><span class="token punctuation">)</span> 84job <span class="token operator">=</span> client<span class="token punctuation">.</span>submit<span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token string">"add"</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> api_name<span class="token operator">=</span><span class="token string">"/predict"</span><span class="token punctuation">)</span> 85job<span class="token punctuation">.</span>status<span class="token punctuation">(</span><span class="token punctuation">)</span> 86 87<span class="token operator">>></span> <span class="token operator"><</span>Status<span class="token punctuation">.</span>STARTING<span class="token punctuation">:</span> <span class="token string">
87'STARTING'</span><span class="token operator">></span></code></pre></div> <p><em>Note</em>: The <code>Job</code> class also has a <code>.done()</code> instance method which returns a boolean indicating whether the job has completed.</p> <h3 class="group header-tag" id="cancelling-jobs">Cancelling Jobs<a href="#cancelling-jobs" class="invisible group-hover-visible"><img src="https://raw.githubusercontent.com/gradio-app/gradio/main/js/_website/src/lib/assets/img/anchor.svg" class="anchor-img-small"/></a></h3> <p>The <code>Job</code> class also has a <code>.cancel()</code> instance method that cancels jobs that have been queued but not started. For example, if you run:</p> <div class="codeblock"><!><pre class="language-py"><code class="language-py">client <span class="token operator">=</span> Client<span class="token punctuation">(</span><span class="token string">"abidlabs/whisper"</span><span class="token punctuation">)</span> 88job1 <span class="token operator">=</span> client<span class="token punctuation">.</span>submit<span class="token punctuation">(</span><span class="token builtin">file</span><span class="token punctuation">(</span><span class="token string">"audio_sample1.wav"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> 89job2 <span class="token operator">=</span> client<span class="token punctuation">.</span>submit<span class="token punctuation">(</span><span class="token builtin">file</span><span class="token punctuation">(</span><span class="token string">"audio_sample2.wav"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> 90job1<span class="token punctuation">.</span>cancel<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment"># will return False, assuming the job has started</span> 91job2<span class="token punctuation">.</span>cancel<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment"># will return True, indicating that the job has been canceled</span></code></pre></div> <p>If the first job has started processing, then it will not be canceled. If the second job 92has not yet started, it will be successfully canceled and removed from the queue.</p> <h3 class="group header-tag" id="generator-endpoints">Generator Endpoints<a href="#generator-endpoints" class="invisible group-hover-visible"><img src="https://raw.githubusercontent.com/gradio-app/gradio/main/js/_website/src/lib/assets/img/anchor.svg" class="anchor-img-small"/></a></h3> <p>Some Gradio API endpoints do not return a single value, rather they return a series of values. You can get the series of values that have been returned at any time from such a generator endpoint by running <code>job.outputs()</code>:</p> <div class="codeblock"><!><pre class="language-py"><code class="language-py"><span class="token keyword">from</span> gradio_client <span class="token keyword">import</span> Client 93 94client <span class="token operator">=</span> Client<span class="token punctuation">(</span>src<span class="token operator">=</span><span class="token string">"gradio/count_generator"</span><span class="token punctuation">)</span> 95job <span class="token operator">=</span> client<span class="token punctuation">.</span>submit<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> api_name<span class="token operator">=</span><span class="token string">"/count"</span><span class="token punctuation">)</span> 96<span class="token keyword">while</span> <span class="token keyword">not</span> job<span class="token punctuation">.</span>done<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> 97 time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">0.1</span><span class="token punctuation">)</span> 98job<span class="token punctuation">.</span>outputs<span class="token punctuation">(</span><span class="token punctuation">)</span> 99 100<span class="token operator">>></span> <span class="token punctuation">[</span><span class="token string">'0'</span><span class="token punctuation">,</span> <span class="token string">'1'</span><span class="token punctuation">,</span> <span class="token string">'2'</span><span class="token punctuation">]</span></code></pre></div> <p>Note that running <code>job.result()</code> on a generator endpoint only gives you the <em>first</em> value returned by the endpoint.</p> <p>The <code>Job</code> object is also iterable, which means you can use it to display the results of a generator function as they are returned from the endpoint. Hereâs the equivalent example using the <code>Job</code> as a generator:</p> <div class="codeblock"><!><pre class="language-py"><code class="language-py"><span class="token keyword">from</span> gradio_client <span class="token keyword">
100import</span> Client 101 102client <span class="token operator">=</span> Client<span class="token punctuation">(</span>src<span class="token operator">=</span><span class="token string">"gradio/count_generator"</span><span class="token punctuation">)</span> 103job <span class="token operator">=</span> client<span class="token punctuation">.</span>submit<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> api_name<span class="token operator">=</span><span class="token string">"/count"</span><span class="token punctuation">)</span> 104 105<span class="token keyword">for</span> o <span class="token keyword">in</span> job<span class="token punctuation">:</span> 106 <span class="token keyword">print</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span> 107 108<span class="token operator">>></span> <span class="token number">0</span> 109<span class="token operator">>></span> <span class="token number">1</span> 110<span class="token operator">>></span> <span class="token number">2</span></code></pre></div> <p>You can also cancel jobs that that have iterative outputs, in which case the job will finish as soon as the current iteration finishes running.</p> <div class="codeblock"><!><pre class="language-py"><code class="language-py"><span class="token keyword">from</span> gradio_client <span class="token keyword">import</span> Client 111<span class="token keyword">import</span> time 112 113client <span class="token operator">=</span> Client<span class="token punctuation">(</span><span class="token string">"abidlabs/test-yield"</span><span class="token punctuation">)</span> 114job <span class="token operator">=</span> client<span class="token punctuation">.</span>submit<span class="token punctuation">(</span><span class="token string">"abcdef"</span><span class="token punctuation">)</span> 115time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span> 116job<span class="token punctuation">.</span>cancel<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment"># job cancels after 2 iterations</span></code></pre></div> <h3 class="group header-tag" id="demos-with-session-state">Demos with Session State<a href="#demos-with-session-state" class="invisible group-hover-visible"><img src="https://raw.githubusercontent.com/gradio-app/gradio/main/js/_website/src/lib/assets/img/anchor.svg" class="anchor-img-small"/></a></h3> <p>Gradio demos can include <a href="https://www.gradio.app/guides/state-in-blocks" rel="nofollow">session state</a>, which provides a way for demos to persist information from user interactions within a page session.</p> <p>For example, consider the following demo, which maintains a list of words that a user has submitted in a <code>gr.State</code> component. When a user submits a new word, it is added to the state, and the number of previous occurrences of that word is displayed:</p> <div class="codeblock"><!><pre class="language-python"><code class="language-python"><span class="token keyword">import</span> gradio <span class="token keyword">as</span> gr 117 118<span class="token keyword">def</span> <span class="token function">count</span><span class="token punctuation">(</span>word<span class="token punctuation">,</span> list_of_words<span class="token punctuation">)</span><span class="token punctuation">:</span> 119 <span class="token keyword">return</span> list_of_words<span class="token punctuation">.</span>count<span class="token punctuation">(</span>word<span class="token punctuation">)</span><span class="token punctuation">,</span> list_of_words <span class="token operator">+</span> <span class="token punctuation">[</span>word<span class="token punctuation">]</span> 120 121<span class="token keyword">with</span> gr<span class="token punctuation">.</span>Blocks<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">as</span> demo<span class="token punctuation">:</span> 122 words <span class="token operator">=</span> gr<span class="token punctuation">.</span>State<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> 123 textbox <span class="token operator">=</span> gr<span class="token punctuation">.</span>Textbox<span class="token punctuation">(</span><span class="token punctuation">)</span> 124 number <span class="token operator">=</span> gr<span class="token punctuation">.</span>Number<span class="token punctuation">(</span><span class="token punctuation">)</span> 125 textbox<span class="token punctuation">.</span>submit<span class="token punctuation">(</span>count<span class="token punctuation">,</span>
125 inputs<span class="token operator">=</span><span class="token punctuation">[</span>textbox<span class="token punctuation">,</span> words<span class="token punctuation">]</span><span class="token punctuation">,</span> outputs<span class="token operator">=</span><span class="token punctuation">[</span>number<span class="token punctuation">,</span> words<span class="token punctuation">]</span><span class="token punctuation">)</span> 126 127demo<span class="token punctuation">.</span>launch<span class="token punctuation">(</span><span class="token punctuation">)</span></code></pre></div> <p>If you were to connect this this Gradio app using the Python Client, you would notice that the API information only shows a single input and output:</p> <div class="codeblock"><!><pre class="language-csv"><code class="language-csv">Client.predict() Usage Info 128--------------------------- 129Named API endpoints: 1 130 131 - predict(word, api_name="/count") -> value_31 132 Parameters: 133 - [Textbox] word: str (required) 134 Returns: 135 - [Number] value_31: float </code></pre></div> <p>That is because the Python client handles state automatically for you â as you make a series of requests, the returned state from one request is stored internally and automatically supplied for the subsequent request. If youâd like to reset the state, you can do that by calling <code>Client.reset_session()</code>.</p>`,1);function ia(P){var x=sa(),o=n(na(x),6),A=s(o);t(A,{content:"pip install gradio_client"}),a(),e(o);var p=n(o,12),G=s(p);t(G,{content:`from gradio_client import Client, file 136 137client = Client("abidlabs/whisper") 138 139client.predict( 140 audio=file("audio_sample.wav") 141) 142 143>> "This is a test of the whisper speech recognition model."`}),a(),e(p);var i=n(p,12),F=s(i);t(F,{content:"$ pip install --upgrade gradio_client"}),a(),e(i);var c=n(i,6),H=s(c);t(H,{content:`from gradio_client import Client 144 145client = Client("abidlabs/en2fr") # a Space that translates from English to French`}),a(),e(c);var l=n(c,4),N=s(l);t(N,{content:`from gradio_client import Client 146 147client = Client("abidlabs/my-private-space", hf_token="...")`}),a(),e(l);var r=n(l,8),q=s(r);t(q,{content:`import os 148from gradio_client import Client, file 149 150HF_TOKEN = os.environ.get("HF_TOKEN") 151 152client = Client.duplicate("abidlabs/whisper", hf_token=HF_TOKEN) 153client.predict(file("audio_sample.wav")) 154 155>> "This is a test of the whisper speech recognition model."`}),a(),e(r);var u=n(r,10),R=s(u);t(R,{content:`from gradio_client import Client 156 157client = Client("https://bec81a83-5b5c-471e.gradio.live")`}),a(),e(u);var d=n(u,6),U=s(d);t(U,{content:`Client.predict() Usage Info 158--------------------------- 159Named API endpoints: 1 160 161 - predict(audio, api_name="/predict") -> output 162 Parameters: 163 - [Audio] audio: filepath (required) 164 Returns: 165 - [Textbox] output: str `}),a(),e(d);var g=n(d,18),B=s(g);t(B,{content:`from gradio_client import Client 166 167client = Client("abidlabs/en2fr", api_name='/predict') 168client.predict("Hello") 169 170>> Bonjour`}),a(),e(g);var h=n(g,4),E=s(h);t(E,{content:`from gradio_client import Client 171 172client = Client("gradio/calculator") 173client.predict(4, "add", 5) 174 175>> 9.0`}),a(),e(h);var k=n(h,4),O=s(k);t(O,{content:`from gradio_client import Client 176 177client = Client("gradio/calculator") 178client.predict(num1=4, operation="add", num2=5) 179 180>> 9.0`}),a(),e(k);var m=n(k,4),J=s(m);t(J,{content:`from gradio_client import Client 181 182client = Client("abidlabs/image_generator") 183client.predict(text="an astronaut riding a camel")`}),a(),e(m);var b=n(m,4),z=s(b);t(z,{content:`from gradio_client import Client 184 185client = Client("abidlabs/image_generator") 186client.predict(text="an astronaut riding a camel", steps=25)`}),a(),e(b);var v=n(b,4),D=s(v);t(D,{content:`from gradio_client import Client, file 187 188client = Client("abidlabs/whisper") 189client.predict( 190 audio=file("https://audio-samples.github.io/samples/mp3/blizzard_unconditional/sample-0.mp3") 191) 192 193>> "My thought I have nobody by a beauty and will as you poured. Mr. Rochester is serve in that so don't find simpus, and devoted abode, to at might in a râ"`}),a(),e(v);var f=n(v,8),K=s(f);t(K,{content:`from gradio_client import Client 194 195client = Client(space="abidlabs/en2fr") 196job = client.submit("Hello", api_name="/predict") # This is not blocking 197 198# Do something else 199 200job.result() # This is blocking 201 202>> Bonjour`}),a(),e(f);var w=n(f,6),L=s(w);t(L,{content:`from gradio_client import Client 203 204def print_result(x): 205 print("The translated result is: {x}") 206 207client = Client(space="abidlabs/en2fr") 208 209job = client.submit("Hello", api_name="/predict", result_callbacks=[print_result]) 210 211# Do something else 212 213>> The translated result is: Bonjour 214`}),a(),e(w);var y=n(w,6),W=s(y);t(W,{content:`from gradio_client import Client 215 216client = Client(src="gradio/calculator") 217job = client.submit(5, "add", 4, api_name="/predict") 218job.status() 219 220>> <Status.STARTING: 'STARTING'>`}),a(),e(y);var _=n(y,8),Y=s(_);t(Y,{content:`client = Client("abidlabs/whisper") 221job1 = client.submit(file("audio_sample1.wav")) 222job2 = client.submit(file("audio_sample2.wav")) 223job1.cancel() # will return False, assuming the job has started 224job2.cancel() # will return True, indicating that the job has been canceled`}),a(),e(_);var C=n(_,8),M=s(C);t(M,{content:`from gradio_client import Client 225 226client = Client(src="gradio/count_generator") 227job = client.submit(3, api_name="/count") 228while not job.done(): 229 time.sleep(0.1) 230job.outputs() 231 232>> ['0', '1', '2']`}),a(),e(C);var j=n(C,6),V=s(j);t(V,{content:`from gradio_client import Client 233 234client = Client(src="gradio/count_generator") 235job = client.submit(3, api_name="/count") 236 237for o in job: 238 print(o) 239 240>> 0 241>> 1 242>> 2`}),a(),e(j);var T=n(j,4),$=s(T);t($,{content:`from gradio_client import Client 243import time 244 245client = Client("abidlabs/test-yield") 246job = client.submit("abcdef") 247time.sleep(3) 248job.cancel() # job cancels after 2 iterations`}),a(),e(T);var I=n(T,8),Q=s(I);t(Q,{content:`import gradio as gr 249 250def count(word, list_of_words): 251 return list_of_words.count(word), list_of_words + [word] 252 253with gr.Blocks() as demo: 254 words = gr.State([]) 255 textbox = gr.Textbox() 256 number = gr.Number() 257 textbox.submit(count, inputs=[textbox, words], outputs=[number, words]) 258 259demo.launch()`}),a(),e(I);var S=n(I,4),X=s(S);t(X,{content:`Client.predict() Usage Info 260--------------------------- 261Named API endpoints: 1 262 263 - predict(word, api_name="/count") -> value_31 264 Parameters: 265 - [Textbox] word: str (required) 266 Returns: 267 - [Number] value_31: float `}),a(),e(S),a(2),Z(P,x)}export{ia as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.