PyPI - Version

Introduction

The client library for Python offers an easy-to-use interface to interact with Sunra’s services.

Installation

To integrate the client into your project, install it using pip:
pip install sunra-client

Features

Calling Endpoints

Sunra manages endpoint requests through a queue system, ensuring reliability and scalability. Use the subscribe method to submit a request and await the result. Example:
import sunra_client

result = sunra_client.subscribe(
    "black-forest-labs/flux-kontext-pro/text-to-image",
    arguments={
    	"prompt": 'A Studio Ghibli-inspired seaside town with colorful houses, laundry flapping, and cats sleeping on windowsills.',
    	"width": 1024,
    	"height": 768,
    	"output_format": 'jpeg'
    },
    with_logs=True,
    on_enqueue=print,
    on_queue_update=print,
)
print(result)

Queue Management

Manage your requests with these methods:
Submitting a Request
Submit a request and retrieve the request_id for later use. Example:
import sunra_client

handler = sunra_client.submit(
    "black-forest-labs/flux-kontext-pro/text-to-image",
    arguments={
      "prompt": 'A Studio Ghibli-inspired seaside town with colorful houses, laundry flapping, and cats sleeping on windowsills.',
      "width": 1024,
      "height": 768,
      "output_format": 'jpeg'
    },
    webhook_url="https://optional.webhook.url/for/results",
)
request_id = handler.request_id
Checking Request Status
Retrieve the status of a request. Example:
import sunra_client
status = sunra_client.status(request_id, with_logs=True)
Retrieving Request Results
Fetch the result of a completed request. Example:
import sunra_client
result = sunra_client.result(request_id)

File Uploads

Upload files to obtain URLs for use in asynchronous requests. This is essential for models that process files, such as image-to-video or speech-to-text converters.
Maximum file size: 100MB
Uploading a Local File
You can easily upload a file from your local filesystem. This is useful for scripts or applications that process local data.
import sunra_client

# Initialize the synchronous client
client = sunra_client.SyncClient()

try:
  # Upload a file from a given path
  file_url = client.upload_file(path="path/to/your/image.png")
  print(f"File uploaded successfully: {file_url}")
  # This URL can now be used with a model endpoint
except FileNotFoundError:
  print("Error: The file was not found at the specified path.")
except Exception as e:
  print(f"An error occurred: {e}")
Uploading In-Memory Data
You can also upload data that is held in memory, such as the content of an image created with PIL (Pillow) or a file received in a web request.
import sunra_client
from PIL import Image
import io

# Initialize the synchronous client
client = sunra_client.SyncClient()

# Create an image in memory, for example with Pillow
image = Image.new("RGB", (600, 400), color = 'red')
byte_arr = io.BytesIO()
image.save(byte_arr, format='PNG')
image_bytes = byte_arr.getvalue()

# Upload the in-memory image data using the `upload` method
image_url = client.upload(
    data=image_bytes,
    content_type="image/png"
)
print(f"Image uploaded successfully: {image_url}")

Support

Join our community for help or discussions: We’re here to assist you!