Skip to main content

Astrux Python SDK

Project description

Astrux Python SDK

PyPI version Python versions License: MIT

The official Python SDK for interacting with the Astrux API. Make machine learning predictions in just a few lines of code.

Installation

pip install astrux

Quick Start

from astrux import Astrux

# Initialize the client
client = Astrux(api_key="sk_your_api_key")

# Make a prediction
result = client.models.predict(
    model="my-model",
    input={"feature1": 10, "feature2": 20}
)

print(result["score"])  # Prediction score

Configuration

API Key

You can provide your API key in two ways:

1. As a parameter (recommended):

client = Astrux(api_key="sk_your_api_key")

2. Via environment variable:

export ASTRUX_API_KEY=sk_your_api_key
client = Astrux()  # Automatically loads from ASTRUX_API_KEY

Custom Timeout

The default timeout is 30 seconds. You can customize it:

client = Astrux(api_key="sk_your_api_key", timeout=60.0)

Usage

Simple Prediction

from astrux import Astrux

client = Astrux(api_key="sk_your_api_key")

# Regression
result = client.models.predict(
    model="house-prices",
    input={
        "sqft": 120,
        "bedrooms": 3,
        "city": "Paris"
    }
)
print(f"Estimated price: {result['score']}")

Prediction with Specific Version

# Use a specific model version
result = client.models.predict(
    model="sentiment-analysis",
    input={"text": "This product is excellent!"},
    version=2
)

Classification

For classification tasks, the response includes the predicted class and probabilities:

result = client.models.predict(
    model="image-classification",
    input={"image_url": "https://example.com/image.jpg"}
)

print(f"Class: {result['class_']}")  # Predicted class
print(f"Probabilities: {result['proba']}")  # List of probabilities

Model Metadata

The response may include metadata about the model used:

result = client.models.predict(
    model="my-model",
    input={"feature": 42}
)

print(f"Model ID: {result.get('model_id')}")
print(f"Model name: {result.get('model_name')}")
print(f"Version: {result.get('version')}")
print(f"Task type: {result.get('task_type')}")

Error Handling

The SDK provides specific exceptions for different error types:

from astrux import Astrux
from astrux._errors import (
    AuthenticationError,
    NotFoundError,
    ValidationError,
    RateLimitError,
    ServerError,
    AstruxError
)

client = Astrux(api_key="sk_your_api_key")

try:
    result = client.models.predict(
        model="my-model",
        input={"feature": "value"}
    )
except AuthenticationError as e:
    print(f"Authentication error: {e}")
except NotFoundError as e:
    print(f"Model not found: {e}")
except ValidationError as e:
    print(f"Invalid data: {e}")
    print(f"Details: {e.payload}")
except RateLimitError as e:
    print(f"Rate limit exceeded: {e}")
except ServerError as e:
    print(f"Server error: {e}")
except AstruxError as e:
    print(f"Astrux error: {e}")
    print(f"Status code: {e.status}")

Closing the Client

To release resources, close the client after use:

client = Astrux(api_key="sk_your_api_key")

try:
    result = client.models.predict(model="my-model", input={"x": 1})
    print(result)
finally:
    client.close()

Response Structure

Regression

{
    "score": 42.5,
    "model_id": "abc123",
    "model_name": "my-model",
    "version": 1,
    "task_type": "regression"
}

Classification

{
    "score": 0.95,
    "class_": "positive",  # Note: remapped from "class"
    "proba": [0.05, 0.95],
    "model_id": "def456",
    "model_name": "sentiment",
    "version": 2,
    "task_type": "classification"
}

Requirements

  • Python >= 3.8
  • httpx >= 0.27.0

Links

License

MIT - see LICENSE file for details.

Author

Thomas Bodénan - thomas.bodenan@gmail.com

Support

For questions or issues, consult the official documentation or contact Astrux support.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

astrux-0.1.0.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

astrux-0.1.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file astrux-0.1.0.tar.gz.

File metadata

  • Download URL: astrux-0.1.0.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for astrux-0.1.0.tar.gz
Algorithm Hash digest
SHA256 dc6789c28d8906062bfdc893533927f347199e6190ff7044f19a94a72af18aae
MD5 d07a5db50ce08787d28e0dfbb3842fcc
BLAKE2b-256 9af904eca670b7902b882e3ff06e37b6e3917965942770feead9be60e796995d

See more details on using hashes here.

File details

Details for the file astrux-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: astrux-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for astrux-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 760758a0f7beb0a95dce13067cea041aebc1632ef7d8a349bf2d41c85277ac83
MD5 bbebc59b112a14a2bac9a331a6d4e18c
BLAKE2b-256 1c87aeacff71cfcc7b769e214a85aac929af12e4011aa175be637981770c396f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page