Metadata-Version: 2.4
Name: aigear
Version: 0.0.1
Summary: Machine learning microservices based on gRPC
Author: Retail AI Groups Inc.
License: MIT License
        
        Copyright (c) 2024 RetailAI Inc.
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/retail-ai-inc/gear
Project-URL: Repository, https://github.com/retail-ai-inc/aigear.git
Project-URL: Issues, https://github.com/retail-ai-inc/aigear/issues
Keywords: MLOps,AI,ML,Model Serving,Model Deployment,Model Training
Classifier: Intended Audience :: Science/Research
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: Unix
Classifier: Operating System :: MacOS
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: aigear[common]
Requires-Dist: aigear[docker]
Requires-Dist: aigear[msgrpc]
Requires-Dist: aigear[gcp]
Provides-Extra: common
Requires-Dist: tabulate>=0.9; extra == "common"
Requires-Dist: cloudpickle>=2.0.0; extra == "common"
Requires-Dist: pydantic>=2.11.9; extra == "common"
Requires-Dist: datamodel_code_generator>=0.34.0; extra == "common"
Provides-Extra: docker
Requires-Dist: docker>=6.13; extra == "docker"
Provides-Extra: msgrpc
Requires-Dist: grpcio>=1.54.2; extra == "msgrpc"
Requires-Dist: protobuf>=4.23.3; extra == "msgrpc"
Requires-Dist: grpcio-health-checking>=1.56.0; extra == "msgrpc"
Requires-Dist: sentry-sdk>=1.29.2; extra == "msgrpc"
Provides-Extra: gcp
Requires-Dist: google-cloud-logging>=3.0.0; extra == "gcp"
Requires-Dist: google-cloud-secret-manager>=2.24.0; extra == "gcp"
Requires-Dist: google-cloud-storage>=3.2.0; extra == "gcp"
Requires-Dist: google-cloud-compute>=1.38.0; extra == "gcp"
Dynamic: license-file

# Aigear
Aigear is an MLOps work orchestration framework that is serverless and aims to help you build pipelines more quickly.
It will not change the programming style of Python and provides an optimized out-of-the-box toolbox.

Aigear believes that everything is a task, and it allows you to make any extensions according to the task.
Within Aigear, it performs topology analysis on tasks to achieve optimal parallel execution.

You need to note that aigear only executes functions created by yourself in parallel.

## Getting started

Aigear requires Python 3.8 or later. To install the latest or upgrade to the latest version of Aigear, run the following command:

```bash
pip install -U aigear
```

With just two decorators(`@task` and `@workflow`), you can create a pipeline.
Please refer to `/aigear/example/pipeline`.

Here is an example of iris classification:
```python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from aigear.pipeline import workflow, task
import pickle
import json
import os


@task
def load_data():
    iris = load_iris()
    return iris


@task
def split_dataset(iris):
    X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=42)
    return X_train, X_test, y_train, y_test


@task
def fit_model(X_train, y_train):
    clf = LogisticRegression(random_state=0, max_iter=1000, solver="lbfgs")
    model = clf.fit(X_train, y_train)
    return model


@task
def evaluate(clf, X_test, y_test):
    y_pred = clf.predict(X_test)
    accuracy = accuracy_score(y_test, y_pred)
    return y_pred, accuracy


@task
def get_env_variables():
    with open("env.json", "r") as f:
        env = json.load(f)
    model_path = env.get("grpc", {}).get("servers", {}).get("demo", {}).get("modelPath")
    return model_path


@task
def save_model(model, model_path):
    with open(model_path, "wb") as md:
        pickle.dump(model, md)


@workflow
def my_pipeline():
    iris = load_data()
    X_train, X_test, y_train, y_test = split_dataset(iris)
    model = fit_model(X_train, y_train)
    y_pred, accuracy = evaluate(model, X_test, y_test)
    print("accuracy：", accuracy)

    model_path = get_env_variables()
    save_model(model, model_path)


if __name__ == '__main__':
    # # Run the pipeline directly
    # my_pipeline()
    
    # Run pipeline in parallel
    my_pipeline.run_in_executor()
```

Similarly, the task can also be executed in two ways.
```python
if __name__ == "__main__":
    # # Run the pipeline directly
    # load_data()
    
    # Run pipeline in parallel
    load_data.run_in_executor()
```

If you want to deploy a pipeline or create a model service in local docker, it's quite simple.

```python
if __name__ == '__main__':
    current_directory = os.getcwd()
    volumes = {
        current_directory: {'bind': "/pipeline", 'mode': 'rw'}
    }
    hostname = "demo-host"
    ports = {'50051/tcp': 50051}
    service_dir = "demo"

    my_pipeline.deploy(
        volumes=volumes,
        skip_build_image=False
    ).to_service(
        hostname=hostname,
        ports=ports,
        volumes=volumes,
        tag=service_dir,
    )
```


