MCAI SDK to develop Python worker
Project description
Python SDK for Media Cloud AI workers
Based on mcai_worker_sdk, this SDK uses the PyO3 crate to export a compiled module compatible with CPython ABI.
Build
Before using the Python module you should build it as a CPython library. This will require a virtualenv (where the module will be installed) and maturin to compile the module.
virtualenv venv # Create your environment
source venv/bin/activate # Launch it
You can then either build the module in development mode (this will build and install the module in your virtualenv):
maturin develop --features extension-module # Build and install the module
Or build the wheel file and install it manually via pip
:
maturin build --features extension-module # Build the wheel file to install the module
pip install path/to/generated/wheel/file
You will now be able to import the module in your Python's scripts by doing:
import mcai_worker_sdk as mcai
Check out maturin's docs for more information on building the module!
Supported version
We intempt to support as many distribution and architecture as we can, however if pip
doesn't find any compatible version for your installation it will download the source and try to compile them directly.
This operation supposes that you have at least Rust 1.62.
We currently support the following version of Python implementations:
- CPython 3.7
- CPython 3.8
- CPython 3.9
- CPython 3.10
- CPython 3.11
- Pypy 3.8
- Pypy 3.9
And the following core architectures:
- x86_64
Test
To run tests you must have json-strong-typing
installed:
pip install json-strong-typing
Then launch tests basically:
cargo test
cargo test --features media
Usage
1. Create your pyproject.toml file
To implement a worker, a pyproject.toml
file must be created with metadata describing the worker.
It must at least contain both project
and build-system
sections.
Example: (minimal configuration)
[project]
name = "my_python_worker"
version = "1.2.3"
description = "My Python worker"
license = { text = "MIT" }
[build-system]
requires = []
2) Install the Python SDK
Two versions of the SDK are currently available: mcai_worker_sdk
and mcai_worker_sdk_media
. This is due to Maturin not supporting Python's extra yet...
You can install the module via pip
:
pip install mcai_worker_sdk # Or mcai_worker_sdk_media if you're developping a media worker...
3) Develop your worker following the guidelines
You can now write the code of your worker. The SDK tries to provide a straightforward structure for your code described above.
For further details, please check out the provided examples: worker.py and media_worker.py.
Worker parameters
Your worker will surely need parameters to handle the jobs you want him to process. These parameters must be described through a Python class inheriting from WorkerParameters
. Each parameter type must be explicitly set.
Example:
import typing
import mcai_worker_sdk as mcai
class MyWorkerParameters(mcai.WorkerParameters):
a_parameter: int
another_parameter: typing.Optional[str] = None
Worker
The Python worker itself must be defined as a Python class inheriting from mcai.Worker
and implementing some methods:
setup(self)
:- Optional worker setup function. May be used to load models, do some checks...
process(self, handle_callback, parameters, job_id) -> dict
withparameters
instance of the worker parameter class:- Execute the worker process and return the job result.
If the media
feature is enabled, the following methods are required:
init_process(self, stream_handler, format_context, parameters) -> list
withparameters
instance of the worker parameter class:- Initialize the media worker process and return a list of
GenericStreamDescriptor
s
- Initialize the media worker process and return a list of
process_frames(self, job_id, stream_index, frames) -> dict
:- Process some input audio/video frames and return the job result.
process_ebu_ttml_live(self, job_id, stream_index, ttml_contents) -> dict
:- Process some input EBU TTML frames and return the job result.
ending_process(self)
:- Optional worker ending process method. May be used to clear some objects...
NB: the process(self, handle_callback, parameters, job_id) -> dict
function is not called when the media
feature is
enabled.
Running examples
Build the Python module
In your virtual environment:
maturin develop
Simple worker
RUST_LOG=debug \
SOURCE_ORDERS="examples/message.json" \
PYTHON_WORKER_FILENAME="worker.py" \
SOURCE_PATH="README.md" \
DESTINATION_PATH="README.md.out" \
python worker.py
Media worker
First set the media filename:
export SOURCE_PATH="/folder/filename.ext"
Then run the SDK with these parameters:
RUST_LOG=debug \
SOURCE_ORDERS="examples/message.json" \
PYTHON_WORKER_FILENAME="media_worker.py" \
DESTINATION_PATH="results.json" \
cargo run --features media
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for mcai_worker_sdk_media-2.0.0rc4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 56a8bcdfbd12567048ef73ceb888023994fe5d707bbcf32da550b3b5938b1f57 |
|
MD5 | 3823dcf36589d84a6911dc21c6fdbf2e |
|
BLAKE2b-256 | ce52097a6921b4d93cb41045284c1129fb4c554e442f773dd1065aa0294dee84 |
Hashes for mcai_worker_sdk_media-2.0.0rc4-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e0994d26e7ecbde0a68fc787091bb0683f14526bc9252fa36ef4c59416c9c745 |
|
MD5 | d035dcacec10afc4e8ede61a61576868 |
|
BLAKE2b-256 | 451397fdd4a01ca814f6d1a1783b1d2c7d5815f180ea4d27e40001f154cfbf93 |
Hashes for mcai_worker_sdk_media-2.0.0rc4-pp38-pypy38_pp73-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d9dbff7d6628b13703b0643eed17ec5f0877ed32d259ee81f85b4bd8d9115a54 |
|
MD5 | 05b172642d34412459b08532abcb1820 |
|
BLAKE2b-256 | a9612cd4743740f388dcbfc1fac43d0c9e8534854574e5a407aa62ad8c2c80d1 |
Hashes for mcai_worker_sdk_media-2.0.0rc4-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8a6234c422844cfee5ea084a3706f6c8b0ed294887f067dfb98ea0ce053b2433 |
|
MD5 | d831fdce61cc96339cbfa4a46bed52a2 |
|
BLAKE2b-256 | 231ef56d3e333325f6f37c636513aedb8c85a29d1879223663358b131709b668 |
Hashes for mcai_worker_sdk_media-2.0.0rc4-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5245f15941464bba037c4b08bf32de1af66b352aa469e6fab4a3b8d28415e46f |
|
MD5 | 2f95d045787ff64906192481f730735a |
|
BLAKE2b-256 | 10a278f9ed00c3d80ed77939267551c5c7428208ae58306566f02eadfac6ca11 |
Hashes for mcai_worker_sdk_media-2.0.0rc4-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f7e5ead670800b54d6a03af3add45534316f7c1ab49fb1eafe97114fdd2dcaad |
|
MD5 | 0ef52c224844e91e9f78b51fe2446173 |
|
BLAKE2b-256 | ebf7f83564102e069a6e7f61c7e21c0044df6e3a3ff2c526ba09e76a93bdbdc5 |
Hashes for mcai_worker_sdk_media-2.0.0rc4-cp38-cp38-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aa690012c4c34b5b843bb3093673789ab49b7f89aa8ef77b316d224af8661b4f |
|
MD5 | ac63de34db1ed25fb0cba4aa69b49f41 |
|
BLAKE2b-256 | 861e5d9d6030f6b90b2c3ac5e1403ff646cd7730dd1ea0d9610f40285adb0894 |