Skip to main content

modelly_client: Use a Modelly app as an API -- in 3 lines of Python

This directory contains the source code for modelly_client, a lightweight Python library that makes it very easy to use any Modelly app as an API.

As an example, consider this Hugging Face Space that transcribes audio files that are recorded from the microphone.

Using the modelly_client library, we can easily use the Modelly as an API to transcribe audio files programmatically.

Here's the entire code to do it:

from modelly_client import Client

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

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

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

Installation

If you already have a recent version of modelly, then the modelly_client is included as a dependency.

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

$ pip install modelly_client

Basic Usage

Connecting to a Space or a Modelly app

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

Connecting to a Space

from modelly_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

from modelly_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 modelly_client includes a class method: Client.duplicate() to make this process simple:

from modelly_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 Modelly 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 Modelly app that is running on a share URL:

from modelly_client import Client

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

Inspecting the API endpoints

Once you have connected to a Modelly 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 Modelly 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:

from modelly_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:

from modelly_client import Client

client = Client("modelly/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.

from modelly_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 Modelly Python Client, check out our dedicated Guide on the Python client, available here: https://www.modelly.khulnasoft.com/guides/getting-started-with-the-python-client

Metadata

Release files for modelly-client 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for modelly-client 1.0.0
File Size Uploaded Provenance
modelly_client-1.0.0.tar.gz 303.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for modelly-client 1.0.0
File Interpreter ABI Platform
modelly_client-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 611.0 kB

Release files / modelly_client-1.0.0.tar.gz

Download URL modelly_client-1.0.0.tar.gz
Size 303.8 kB
Tags Source
SHA-256 checksum
How to use checksums
fe51b24ae5f0c99f0d5cb19ceb21c1e88cc72b0fccca5455b91af668f951cace
BLAKE2b-256 checksum
How to use checksums
4c83973c9da067b27e559edc78124afca01bb7f270a40a1e924d58e468eab42f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.1

Release files / modelly_client-1.0.0-py3-none-any.whl

Download URL modelly_client-1.0.0-py3-none-any.whl
Size 307.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
447fed454c6a7241dd0f01a688b5fe94e782a18d2a488ff9aaff53613b7890f5
BLAKE2b-256 checksum
How to use checksums
50674b0735782c79ff9cfd808d64faf2bd27b16e5b16e40d4213125b619072e0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.1

Security for modelly-client 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page