Metadata-Version: 2.4
Name: mldash-sdk
Version: 0.2.2
Summary: Export trained ML models to ML-Dash
Author: Miguel Rocha
License-Expression: MIT
Project-URL: Homepage, https://github.com/mrocha/ml-dash
Requires-Python: >=3.8
Description-Content-Type: text/markdown
Requires-Dist: joblib>=1.0
Requires-Dist: requests>=2.20

# mldash

Export trained ML models from notebooks to [ML-Dash](https://github.com/mrocha/ml-dash).

## Install

```bash
pip install mldash-sdk
```

## Quick Start

```python
import mldash

# One-time login (saved to ~/.config/mldash/credentials)
mldash.login()

# See your projects
mldash.list_projects()

# Set active project for this notebook (by name or ID)
mldash.init(project="Churn Analysis")

# Train your model
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier()
model.fit(X_train, y_train)

# Export — just model + name, everything else is resolved
mldash.export(model=model, name="Churn Predictor")
```

## Authentication

### `mldash.login()` — One-time setup

Run once per machine. You'll be prompted for your ML-Dash URL and API key:

```
>>> mldash.login()
ML-Dash URL: http://localhost:8000
Get your API key from: http://localhost:8000/developers/keys
API Key: ········
Authenticated with http://localhost:8000
Credentials saved to ~/.config/mldash/credentials
```

Credentials are stored locally at `~/.config/mldash/credentials` (chmod 600) and reused automatically.

You can also pass values directly (useful for CI or scripting):
```python
mldash.login(url="http://localhost:8000", api_key="mldash_xxx")
```

### `mldash.logout()`

```python
mldash.logout()  # removes stored credentials
```

### `mldash.whoami()`

```python
mldash.whoami()
# URL:     http://localhost:8000
# API Key: mldash_abcde...
# Project: Churn Analysis (id=3)
```

### Alternative auth methods

These work without calling `login()`:

**Environment variables:**
```bash
export MLDASH_URL=http://localhost:8000
export MLDASH_API_KEY=mldash_your_key_here
```

**Google Colab Secrets:**

Add `MLDASH_URL` and `MLDASH_API_KEY` to your Colab Secrets (key icon in sidebar).

## Browsing

### `mldash.list_projects()`

```python
mldash.list_projects()
# ID     Name                           Models   Datasets
# ----------------------------------------------------
# 1      Churn Analysis                 3        2
# 2      Imported Models                1        0
```

### `mldash.list_models()`

```python
mldash.list_models()                          # all models
mldash.list_models(project="Churn Analysis")  # filter by project
mldash.list_models(project=1)                 # filter by project ID
```

## Project Association

### `mldash.init()` — Set active project

Accepts a project name (string) or project ID (int):

```python
mldash.init(project="Churn Analysis")   # by name
mldash.init(project=3)                  # by ID

# All exports now go to that project
mldash.export(model=model1, name="Logistic v1")
mldash.export(model=model2, name="XGBoost v1")
```

The project is saved to credentials, so it persists across sessions.
Override per-export with the `project=` argument:

```python
mldash.export(model=model, name="Experiment", project="Other Project")
```

If no project is set, exports go to an auto-created **Imported Models** project.

## Usage

### Export with artifacts

```python
mldash.export(
    model=model,
    name="Sales Predictor",
    scaler=scaler,              # StandardScaler, MinMaxScaler, etc.
    encoder=encoder,            # OneHotEncoder, LabelEncoder, etc.
    label_map={"0": "no", "1": "yes"},
    features={"age": "numeric", "dept": "categorical", "review": "text"},
    target={"churn": "categorical"},
)
```

### Save to local ZIP (offline)

```python
mldash.save(
    model=model,
    name="My Model",
    path="./my_model.zip",
)
# Upload the ZIP manually via the ML-Dash web UI
```

### Auto-detection

The package auto-detects:
- **task_type** — classification or regression (from sklearn mixins)
- **framework** — sklearn, xgboost, lightgbm, etc. (from module path)
- **hyperparameters** — via `model.get_params()`
- **model_class** — e.g., `RandomForestClassifier`
- **n_features** — from `model.n_features_in_`
- **class_labels** — from `model.classes_`

## Typical Notebook Workflow

```python
# Cell 1: Setup (run once)
import mldash
mldash.login()
mldash.init(project="MAS 648 Final")

# Cell 2-N: Train models...
model = RandomForestClassifier(n_estimators=100)
model.fit(X_train, y_train)

# Cell N+1: Export
mldash.export(model=model, name="RF 100 trees")
```

## Errors

```python
from mldash import MLDashValidationError, MLDashAuthError, MLDashUploadError

try:
    mldash.export(model=model, name="Test")
except MLDashValidationError as e:
    print(f"Invalid input: {e}")
except MLDashAuthError as e:
    print(f"Auth failed: {e}")
except MLDashUploadError as e:
    print(f"Upload failed: {e}")
```
