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.2.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.2-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: astrux-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 2abc5a02095e930d94850610cd22362428d4d64846b6f02c223521251bee58bb
MD5 6633874afa7acda7dc43ad34e0914fa7
BLAKE2b-256 eb7e9297dc510a631898289139bfa8255860e33831f6f51d5659cf73262a2e2c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: astrux-0.1.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 feec8c4b93be2383d16fe7465466924606793feb78f2d14bc4351ceb83e9780b
MD5 20317a522ec30ed7d7d5ab0d9e39c117
BLAKE2b-256 32ad465e7d20a121c537402e72bd512526381c049553186ac00cdb440be1c078

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