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.1.tar.gz (4.2 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.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: astrux-0.1.1.tar.gz
  • Upload date:
  • Size: 4.2 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.1.tar.gz
Algorithm Hash digest
SHA256 73e111f85e968f0390fa37106c391ff82ac13441100ac7df2d0f9b032a02cbc7
MD5 b96e43fdb5da5eb7cfb2a09b2851467f
BLAKE2b-256 440c8022a569c3846d0df117de4da0afd7b6ba5f867430d28c25674d555ce38b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: astrux-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8960fac1b637f9f9d36c6e876baa06ef837b21012da85d60e17e1b38b3f43b74
MD5 ef27dba8ab07152459c044f1b9cf8d07
BLAKE2b-256 1deb4510cff82b7a161029accbf54abe163a9657957f8b445a02dafd6929734f

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