Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sets up the Python gradio client #3300

Merged
merged 119 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from 112 commits
Commits
Show all changes
119 commits
Select commit Hold shift + click to select a range
1e2712c
placeholder
abidlabs Feb 23, 2023
1e34aa7
changelog
abidlabs Feb 24, 2023
5f0daa4
added to readme
abidlabs Feb 24, 2023
3081ab2
client
abidlabs Feb 24, 2023
1b22da2
Merge branch 'main' into py-client
abidlabs Mar 1, 2023
4123328
implement futures
abidlabs Mar 1, 2023
532631b
Merge branch 'main' into py-client
abidlabs Mar 1, 2023
0e0b459
utils
abidlabs Mar 2, 2023
a410cc4
Merge branch 'main' into py-client
abidlabs Mar 2, 2023
da96e53
scripts
abidlabs Mar 2, 2023
dead22a
Merge branch 'py-client' of github.com:gradio-app/gradio into py-client
abidlabs Mar 2, 2023
a7da8e0
lint
abidlabs Mar 2, 2023
72295d7
reorg
abidlabs Mar 2, 2023
27ecb66
scripts
abidlabs Mar 2, 2023
125b4c2
serialization
abidlabs Mar 2, 2023
ac1ee64
cleanup
abidlabs Mar 2, 2023
2b1c4d6
fns
abidlabs Mar 2, 2023
9e02818
serialize
abidlabs Mar 2, 2023
de6eef1
cache
abidlabs Mar 2, 2023
6fad4e8
callbacks
abidlabs Mar 2, 2023
b7e12cc
updates
abidlabs Mar 2, 2023
e30c7b6
Merge branch 'main' into py-client
abidlabs Mar 6, 2023
dc3fad6
Merge branch 'main' into py-client
abidlabs Mar 9, 2023
ea90f25
formatting
abidlabs Mar 9, 2023
3176af7
Merge branch 'main' into py-client
abidlabs Mar 9, 2023
978c088
packaging
abidlabs Mar 9, 2023
37593ad
requirements
abidlabs Mar 9, 2023
007ec54
remove changelog
abidlabs Mar 9, 2023
ac414a6
Merge branch 'main' into py-client
abidlabs Mar 9, 2023
f3e0737
client
abidlabs Mar 9, 2023
d7d9499
access token
abidlabs Mar 10, 2023
d916615
formatting
abidlabs Mar 10, 2023
7c5cd1d
deprecate
abidlabs Mar 10, 2023
135a3fe
format backend
abidlabs Mar 10, 2023
7dc1d32
client replace
abidlabs Mar 10, 2023
79760ea
updates
abidlabs Mar 10, 2023
4901199
moving from utils
abidlabs Mar 10, 2023
cf672b8
remove code duplication
abidlabs Mar 10, 2023
862df4b
rm duplicates
abidlabs Mar 10, 2023
22cf733
simplify
abidlabs Mar 10, 2023
f3fee63
galleryserializer
abidlabs Mar 10, 2023
effdc2e
serializable
abidlabs Mar 10, 2023
5b7873b
load serializers
abidlabs Mar 10, 2023
171c3b8
fixing errors
abidlabs Mar 10, 2023
a718db2
errors
abidlabs Mar 10, 2023
7c116c3
typing
abidlabs Mar 10, 2023
250351d
tests
abidlabs Mar 10, 2023
e0655bb
Merge branch 'main' into py-client
abidlabs Mar 10, 2023
8c00ed8
changelog
abidlabs Mar 10, 2023
2bbbdd1
Merge branch 'main' into py-client
abidlabs Mar 14, 2023
70041ff
lint
abidlabs Mar 14, 2023
71f9a3e
Merge branch 'py-client' of github.com:gradio-app/gradio into py-client
abidlabs Mar 14, 2023
189cc73
Merge branch 'main' into py-client
abidlabs Mar 15, 2023
c8bba50
fix lint
abidlabs Mar 15, 2023
8f4ff85
fixing files
abidlabs Mar 15, 2023
3ab7ac8
formatting
abidlabs Mar 15, 2023
1b2a159
type
abidlabs Mar 15, 2023
ad7a048
fix type checking
abidlabs Mar 15, 2023
e438d71
changelog
abidlabs Mar 15, 2023
6e009ee
changelog
abidlabs Mar 15, 2023
363956e
merge
abidlabs Mar 15, 2023
e11132d
Update client/python/gradio_client/client.py
abidlabs Mar 15, 2023
a1f7ce9
Merge branch 'main' into py-client
abidlabs Mar 15, 2023
e612e4a
formatting, tests
abidlabs Mar 15, 2023
e92f858
Merge branch 'py-client' of github.com:gradio-app/gradio into py-client
abidlabs Mar 15, 2023
4fdb956
formatting, tests
abidlabs Mar 15, 2023
4cb3ac5
Merge branch 'main' into py-client
abidlabs Mar 15, 2023
131a648
gr.load
abidlabs Mar 15, 2023
f34349a
Merge branch 'py-client' of github.com:gradio-app/gradio into py-client
abidlabs Mar 15, 2023
2deb1c7
refactoring
abidlabs Mar 16, 2023
96153a2
refactoring'
abidlabs Mar 16, 2023
2a4100c
formatting
abidlabs Mar 16, 2023
0135d01
formatting
abidlabs Mar 16, 2023
19c213a
tests
abidlabs Mar 16, 2023
a6a601f
tests
abidlabs Mar 16, 2023
6cce06b
fix tests
abidlabs Mar 16, 2023
d6d0321
cleanup
abidlabs Mar 16, 2023
e513d22
added tests
abidlabs Mar 16, 2023
ed4d851
adding scripts
abidlabs Mar 16, 2023
a60585e
formatting
abidlabs Mar 16, 2023
17db4ec
address review comments
abidlabs Mar 16, 2023
4b7c9d4
readme
abidlabs Mar 16, 2023
ae2a740
serialize info
abidlabs Mar 16, 2023
7c27d7a
remove from changelog
abidlabs Mar 16, 2023
fd66924
version 0.0.2 released
abidlabs Mar 16, 2023
ce8d66f
lint
abidlabs Mar 16, 2023
6422f28
Merge branch 'main' into py-client
abidlabs Mar 16, 2023
24dbb31
Merge branch 'main' into py-client
abidlabs Mar 18, 2023
6dd57a9
Merge branch 'main' into py-client
abidlabs Mar 19, 2023
8754b74
client utils
abidlabs Mar 21, 2023
83fa33e
type fix
abidlabs Mar 21, 2023
6186736
Merge branch 'main' into py-client
abidlabs Mar 21, 2023
36eecd0
check
abidlabs Mar 21, 2023
5d709b3
Merge branch 'py-client' of github.com:gradio-app/gradio into py-client
abidlabs Mar 21, 2023
02d6f72
type issues
abidlabs Mar 21, 2023
78caf50
hf_token
abidlabs Mar 21, 2023
5ac8870
update hf token
abidlabs Mar 21, 2023
566ff8f
telemetry
abidlabs Mar 22, 2023
d7df04c
docs, circle dependency
abidlabs Mar 22, 2023
47aa858
hf token
abidlabs Mar 22, 2023
3c2f286
formatting
abidlabs Mar 22, 2023
8f3fcd6
updates
abidlabs Mar 22, 2023
e21158a
sort
abidlabs Mar 22, 2023
cc35b57
script
abidlabs Mar 22, 2023
76df75e
external
abidlabs Mar 22, 2023
6a87b70
docs
abidlabs Mar 22, 2023
96247fe
formatting
abidlabs Mar 22, 2023
66872db
fixes
abidlabs Mar 22, 2023
dfb468e
scripts
abidlabs Mar 22, 2023
1f018cf
requirements
abidlabs Mar 22, 2023
bce41b3
fix tests
abidlabs Mar 22, 2023
f0dc386
context
abidlabs Mar 22, 2023
0ca7848
Merge branch 'main' into py-client
abidlabs Mar 23, 2023
d85365f
changes
abidlabs Mar 23, 2023
d411500
formatting
abidlabs Mar 23, 2023
6592dde
Merge branch 'main' into py-client
abidlabs Mar 23, 2023
da7d62a
fixes
abidlabs Mar 23, 2023
71cbd73
format fix
abidlabs Mar 23, 2023
ede1f6c
Merge branch 'main' into py-client
abidlabs Mar 23, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions client/python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# `gradio_client`: Use any Gradio app as an API -- in 3 lines of Python

This directory contains the source code for `gradio_client`, a lightweight Python library that makes it very easy to use any Gradio app as an API. This library is CURRENTLY IN ALPHA. APIS MAY CHANGE. WE DO NOT RECOMMEND USING IT EXCEPT IN EXPERIMENTAL PROJECTS.
abidlabs marked this conversation as resolved.
Show resolved Hide resolved

As an example, consider the Stable Diffusion Gradio app, which is hosted on Hugging Face Spaces, and which generates images given a text prompt. Using the `gradio_client` library, we can easily use the Gradio as an API to generates images programmatically.

Here's the entire code to do it:

```python
import gradio_client as grc

client = grc.Client(space="stability-ai/stable-diffusion")
job = client.predict("a hyperrealistic portrait of a cat wearing cyberpunk armor")
job.result()

>> URL
abidlabs marked this conversation as resolved.
Show resolved Hide resolved

```

## Installation

If you already have a recent version of `gradio`, then the `gradio_client` is included as a dependency.

Otherwise, the lightweight `gradio_client` package can be installed from pip (or pip3) and works with Python versions 3.7 or higher:

```bash
$ pip install gradio_client
```

## Usage

### Connecting to a Space or a Gradio app

Start by connecting instantiating a `Client` object and connecting it to a Gradio app
that is running on Spaces (or anywhere else)!

**Connecting to a Space**

```python
import gradio_client as grc

client = grc.Client(space="abidlabs/en2fr")
```

**Connecting a general Gradio app**

If your app is running somewhere else, provide the full URL instead to the `src` argument. Here's an example of making predictions to a Gradio app that is running on a share URL:

```python
import gradio_client as grc

client = grc.Client(src="btd372-js72hd.gradio.app")
```

### Making a prediction

The simplest way to make a prediction is simply to call the `.predict()` function with the appropriate arguments and then immediately calling `.result()`, like this:


```python
import gradio_client as grc

client = grc.Client(space="abidlabs/en2fr")

client.predict("Hello").result()

>> Bonjour
```

**Running jobs asyncronously**

Oe should note that `.result()` is a *blocking* operation as it waits for the operation to complete before returning the prediction.

In many cases, you may be better off letting the job run asynchronously and waiting to call `.result()` when you need the results of the prediction. For example:


```python
import gradio_client as grc

client = grc.Client(space="abidlabs/en2fr")

job = client.predict("Hello")

# Do something else

job.result()

>> Bonjour
```

**Adding callbacks**

Alternatively, one can add callbacks to perform actions after the job has completed running, like this:


```python
import gradio_client as grc

def print_result(x):
print(x"The translated result is: {x}")

client = grc.Client(space="abidlabs/en2fr")

job = client.predict("Hello", callbacks=[print_result])
abidlabs marked this conversation as resolved.
Show resolved Hide resolved
```
2 changes: 2 additions & 0 deletions client/python/gradio_client/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from gradio_client.client import Client
from gradio_client.utils import __version__
Loading