gradio-client


Namegradio-client JSON
Version 0.16.1 PyPI version JSON
download
home_pageNone
SummaryPython library for easily interacting with trained machine learning models
upload_time2024-05-03 18:15:31
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseNone
keywords api client machine learning
VCS
bugtrack_url
requirements aiofiles altair fastapi ffmpy gradio_client httpx huggingface_hub importlib_resources Jinja2 markupsafe matplotlib numpy orjson packaging pandas pillow pydantic python-multipart pydub pyyaml semantic_version typing_extensions urllib3 uvicorn typer tomlkit ruff
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # `gradio_client`: Use a 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.

As an example, consider this [Hugging Face Space that transcribes audio files](https://huggingface.co/spaces/abidlabs/whisper) that are recorded from the microphone.

![](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/gradio-guides/whisper-screenshot.jpg)

Using the `gradio_client` library, we can easily use the Gradio as an API to transcribe audio files programmatically.

Here's the entire code to do it:

```python
from gradio_client import Client

client = Client("abidlabs/whisper")
client.predict("audio_sample.wav")

>> "This is a test of the whisper speech recognition model."
```

The Gradio client works with any Gradio Space, whether it be an image generator, a stateful chatbot, or a tax calculator.

## 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.8 or higher:

```bash
$ pip install gradio_client
```

## Basic 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
from gradio_client import Client

client = Client("abidlabs/en2fr")  # a Space that translates from English to French
```

You can also connect to private Spaces by passing in your HF token with the `hf_token` parameter. You can get your HF token here: https://huggingface.co/settings/tokens

```python
from gradio_client import Client

client = Client("abidlabs/my-private-space", hf_token="...")
```

**Duplicating a Space for private use**

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,
and then use it to make as many requests as you'd like!

The `gradio_client` includes a class method: `Client.duplicate()` to make this process simple:

```python
from gradio_client import Client

client = Client.duplicate("abidlabs/whisper")
client.predict("audio_sample.wav")

>> "This is a test of the whisper speech recognition model."
```

If you have previously duplicated a Space, re-running `duplicate()` will _not_ create a new Space. Instead, the Client will attach to the previously-created Space. So it is safe to re-run the `Client.duplicate()` method multiple times.

**Note:** 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 `hardware` parameter of `duplicate()`.

**Connecting a general Gradio app**

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:

```python
from gradio_client import Client

client = Client("https://bec81a83-5b5c-471e.gradio.live")
```

### Inspecting the API endpoints

Once you have connected to a Gradio app, you can view the APIs that are available to you by calling the `.view_api()` method. For the Whisper Space, we see the following:

```
Client.predict() Usage Info
---------------------------
Named API endpoints: 1

 - predict(input_audio, api_name="/predict") -> value_0
    Parameters:
     - [Audio] input_audio: str (filepath or URL)
    Returns:
     - [Textbox] value_0: str (value)
```

This shows us 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 `.predict()` method, providing a parameter `input_audio` of type `str`, which is a `filepath or URL`.

We should also provide the `api_name='/predict'` argument. Although this isn't necessary if a Gradio app has a single named endpoint, it does allow us to call different endpoints in a single app if they are available. If an app has unnamed API endpoints, these can also be displayed by running `.view_api(all_endpoints=True)`.

### Making a prediction

The simplest way to make a prediction is simply to call the `.predict()` function with the appropriate arguments:

```python
from gradio_client import Client

client = Client("abidlabs/en2fr")
client.predict("Hello")

>> Bonjour
```

If there are multiple parameters, then you should pass them as separate arguments to `.predict()`, like this:

```python
from gradio_client import Client

client = Client("gradio/calculator")
client.predict(4, "add", 5)

>> 9.0
```

For certain inputs, such as images, you should pass in the filepath or URL to the file. Likewise, for the corresponding output types, you will get a filepath or URL returned.

```python
from gradio_client import Client

client = Client("abidlabs/whisper")
client.predict("https://audio-samples.github.io/samples/mp3/blizzard_unconditional/sample-0.mp3")

>> "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—"
```

## Advanced Usage

For more ways to use the Gradio Python Client, check out our dedicated Guide on the Python client, available here: https://www.gradio.app/guides/getting-started-with-the-python-client

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "gradio-client",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "API, client, machine learning",
    "author": null,
    "author_email": "Abubakar Abid <gradio-team@huggingface.co>, Ali Abid <gradio-team@huggingface.co>, Ali Abdalla <gradio-team@huggingface.co>, Dawood Khan <gradio-team@huggingface.co>, Ahsen Khaliq <gradio-team@huggingface.co>, Pete Allen <gradio-team@huggingface.co>, Freddy Boulton <gradio-team@huggingface.co>",
    "download_url": "https://files.pythonhosted.org/packages/36/77/d9d014ff3aba494a9b2df27e1d90638e5d9674294e20a1896882bcb8aa12/gradio_client-0.16.1.tar.gz",
    "platform": null,
    "description": "# `gradio_client`: Use a Gradio app as an API -- in 3 lines of Python\n\nThis 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.\n\nAs an example, consider this [Hugging Face Space that transcribes audio files](https://huggingface.co/spaces/abidlabs/whisper) that are recorded from the microphone.\n\n![](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/gradio-guides/whisper-screenshot.jpg)\n\nUsing the `gradio_client` library, we can easily use the Gradio as an API to transcribe audio files programmatically.\n\nHere's the entire code to do it:\n\n```python\nfrom gradio_client import Client\n\nclient = Client(\"abidlabs/whisper\")\nclient.predict(\"audio_sample.wav\")\n\n>> \"This is a test of the whisper speech recognition model.\"\n```\n\nThe Gradio client works with any Gradio Space, whether it be an image generator, a stateful chatbot, or a tax calculator.\n\n## Installation\n\nIf you already have a recent version of `gradio`, then the `gradio_client` is included as a dependency.\n\nOtherwise, the lightweight `gradio_client` package can be installed from pip (or pip3) and works with Python versions 3.8 or higher:\n\n```bash\n$ pip install gradio_client\n```\n\n## Basic Usage\n\n### Connecting to a Space or a Gradio app\n\nStart by connecting instantiating a `Client` object and connecting it to a Gradio app that is running on Spaces (or anywhere else)!\n\n**Connecting to a Space**\n\n```python\nfrom gradio_client import Client\n\nclient = Client(\"abidlabs/en2fr\")  # a Space that translates from English to French\n```\n\nYou can also connect to private Spaces by passing in your HF token with the `hf_token` parameter. You can get your HF token here: https://huggingface.co/settings/tokens\n\n```python\nfrom gradio_client import Client\n\nclient = Client(\"abidlabs/my-private-space\", hf_token=\"...\")\n```\n\n**Duplicating a Space for private use**\n\nWhile 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,\nand then use it to make as many requests as you'd like!\n\nThe `gradio_client` includes a class method: `Client.duplicate()` to make this process simple:\n\n```python\nfrom gradio_client import Client\n\nclient = Client.duplicate(\"abidlabs/whisper\")\nclient.predict(\"audio_sample.wav\")\n\n>> \"This is a test of the whisper speech recognition model.\"\n```\n\nIf you have previously duplicated a Space, re-running `duplicate()` will _not_ create a new Space. Instead, the Client will attach to the previously-created Space. So it is safe to re-run the `Client.duplicate()` method multiple times.\n\n**Note:** 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 `hardware` parameter of `duplicate()`.\n\n**Connecting a general Gradio app**\n\nIf 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:\n\n```python\nfrom gradio_client import Client\n\nclient = Client(\"https://bec81a83-5b5c-471e.gradio.live\")\n```\n\n### Inspecting the API endpoints\n\nOnce you have connected to a Gradio app, you can view the APIs that are available to you by calling the `.view_api()` method. For the Whisper Space, we see the following:\n\n```\nClient.predict() Usage Info\n---------------------------\nNamed API endpoints: 1\n\n - predict(input_audio, api_name=\"/predict\") -> value_0\n    Parameters:\n     - [Audio] input_audio: str (filepath or URL)\n    Returns:\n     - [Textbox] value_0: str (value)\n```\n\nThis shows us 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 `.predict()` method, providing a parameter `input_audio` of type `str`, which is a `filepath or URL`.\n\nWe should also provide the `api_name='/predict'` argument. Although this isn't necessary if a Gradio app has a single named endpoint, it does allow us to call different endpoints in a single app if they are available. If an app has unnamed API endpoints, these can also be displayed by running `.view_api(all_endpoints=True)`.\n\n### Making a prediction\n\nThe simplest way to make a prediction is simply to call the `.predict()` function with the appropriate arguments:\n\n```python\nfrom gradio_client import Client\n\nclient = Client(\"abidlabs/en2fr\")\nclient.predict(\"Hello\")\n\n>> Bonjour\n```\n\nIf there are multiple parameters, then you should pass them as separate arguments to `.predict()`, like this:\n\n```python\nfrom gradio_client import Client\n\nclient = Client(\"gradio/calculator\")\nclient.predict(4, \"add\", 5)\n\n>> 9.0\n```\n\nFor certain inputs, such as images, you should pass in the filepath or URL to the file. Likewise, for the corresponding output types, you will get a filepath or URL returned.\n\n```python\nfrom gradio_client import Client\n\nclient = Client(\"abidlabs/whisper\")\nclient.predict(\"https://audio-samples.github.io/samples/mp3/blizzard_unconditional/sample-0.mp3\")\n\n>> \"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\u2014\"\n```\n\n## Advanced Usage\n\nFor more ways to use the Gradio Python Client, check out our dedicated Guide on the Python client, available here: https://www.gradio.app/guides/getting-started-with-the-python-client\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Python library for easily interacting with trained machine learning models",
    "version": "0.16.1",
    "project_urls": {
        "Homepage": "https://github.com/gradio-app/gradio"
    },
    "split_keywords": [
        "api",
        " client",
        " machine learning"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b59c4454452f9863d71ff112ab2355d8b789e38ad36aa176df96b50093ac9519",
                "md5": "e97832dbd200a67380fa2cc21fdfd3a4",
                "sha256": "9da92f892d0f9e50a0a0eae7ff65aeeab0495919295dbc079703d1a99c13c980"
            },
            "downloads": -1,
            "filename": "gradio_client-0.16.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e97832dbd200a67380fa2cc21fdfd3a4",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 314644,
            "upload_time": "2024-05-03T18:15:29",
            "upload_time_iso_8601": "2024-05-03T18:15:29.096594Z",
            "url": "https://files.pythonhosted.org/packages/b5/9c/4454452f9863d71ff112ab2355d8b789e38ad36aa176df96b50093ac9519/gradio_client-0.16.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3677d9d014ff3aba494a9b2df27e1d90638e5d9674294e20a1896882bcb8aa12",
                "md5": "6cfa49ac336c73394ee3c5df193d74ff",
                "sha256": "28f6799366ca795fbd6ea72f328ee75ea2322417cf11d6a23ce825bca8954be1"
            },
            "downloads": -1,
            "filename": "gradio_client-0.16.1.tar.gz",
            "has_sig": false,
            "md5_digest": "6cfa49ac336c73394ee3c5df193d74ff",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 312264,
            "upload_time": "2024-05-03T18:15:31",
            "upload_time_iso_8601": "2024-05-03T18:15:31.320016Z",
            "url": "https://files.pythonhosted.org/packages/36/77/d9d014ff3aba494a9b2df27e1d90638e5d9674294e20a1896882bcb8aa12/gradio_client-0.16.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-03 18:15:31",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "gradio-app",
    "github_project": "gradio",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "aiofiles",
            "specs": [
                [
                    "<",
                    "24.0"
                ],
                [
                    ">=",
                    "22.0"
                ]
            ]
        },
        {
            "name": "altair",
            "specs": [
                [
                    "<",
                    "6.0"
                ],
                [
                    ">=",
                    "4.2.0"
                ]
            ]
        },
        {
            "name": "fastapi",
            "specs": []
        },
        {
            "name": "ffmpy",
            "specs": []
        },
        {
            "name": "gradio_client",
            "specs": [
                [
                    "==",
                    "0.16.1"
                ]
            ]
        },
        {
            "name": "httpx",
            "specs": [
                [
                    ">=",
                    "0.24.1"
                ]
            ]
        },
        {
            "name": "huggingface_hub",
            "specs": [
                [
                    ">=",
                    "0.19.3"
                ]
            ]
        },
        {
            "name": "importlib_resources",
            "specs": [
                [
                    "<",
                    "7.0"
                ],
                [
                    ">=",
                    "1.3"
                ]
            ]
        },
        {
            "name": "Jinja2",
            "specs": [
                [
                    "<",
                    "4.0"
                ]
            ]
        },
        {
            "name": "markupsafe",
            "specs": [
                [
                    "~=",
                    "2.0"
                ]
            ]
        },
        {
            "name": "matplotlib",
            "specs": [
                [
                    "~=",
                    "3.0"
                ]
            ]
        },
        {
            "name": "numpy",
            "specs": [
                [
                    "~=",
                    "1.0"
                ]
            ]
        },
        {
            "name": "orjson",
            "specs": [
                [
                    "~=",
                    "3.0"
                ]
            ]
        },
        {
            "name": "packaging",
            "specs": []
        },
        {
            "name": "pandas",
            "specs": [
                [
                    "<",
                    "3.0"
                ],
                [
                    ">=",
                    "1.0"
                ]
            ]
        },
        {
            "name": "pillow",
            "specs": [
                [
                    ">=",
                    "8.0"
                ],
                [
                    "<",
                    "11.0"
                ]
            ]
        },
        {
            "name": "pydantic",
            "specs": [
                [
                    ">=",
                    "2.0"
                ]
            ]
        },
        {
            "name": "python-multipart",
            "specs": [
                [
                    ">=",
                    "0.0.9"
                ]
            ]
        },
        {
            "name": "pydub",
            "specs": []
        },
        {
            "name": "pyyaml",
            "specs": [
                [
                    ">=",
                    "5.0"
                ],
                [
                    "<",
                    "7.0"
                ]
            ]
        },
        {
            "name": "semantic_version",
            "specs": [
                [
                    "~=",
                    "2.0"
                ]
            ]
        },
        {
            "name": "typing_extensions",
            "specs": [
                [
                    "~=",
                    "4.0"
                ]
            ]
        },
        {
            "name": "urllib3",
            "specs": [
                [
                    "~=",
                    "2.0"
                ]
            ]
        },
        {
            "name": "uvicorn",
            "specs": [
                [
                    ">=",
                    "0.14.0"
                ]
            ]
        },
        {
            "name": "typer",
            "specs": [
                [
                    ">=",
                    "0.12"
                ],
                [
                    "<",
                    "1.0"
                ]
            ]
        },
        {
            "name": "tomlkit",
            "specs": [
                [
                    "==",
                    "0.12.0"
                ]
            ]
        },
        {
            "name": "ruff",
            "specs": [
                [
                    ">=",
                    "0.2.2"
                ]
            ]
        }
    ],
    "lcname": "gradio-client"
}
        
Elapsed time: 0.26144s