ModelScope
ModelScope is an open-source model-as-a-service (MaaS) platform from Alibaba Damo Academy, providing a wide range of AI models (vision, NLP, audio, multimodal) for easy deployment and use. It abstracts complex AI model inference into a simple API and offers functionalities for model discovery, download, and fine-tuning. The library is actively developed, with its current version at 1.35.3 and frequent releases.
Warnings
- breaking The `ms_dataset` module underwent significant refactoring to align with `datasets` library 4.x. If you're using dataset functionalities, ensure your `datasets` library is updated to version 4.0 or above, or fix potential breaking changes in your code.
- deprecated For security reasons, `delete_repo`, `delete_model`, and `delete_dataset` methods within the `HubApi` have been temporarily deprecated. Calling these methods will now issue a `DeprecationWarning`.
- gotcha Hub API authentication token priority changed in v1.34.0. The token priority is now: function parameter > instance attribute > environment variable (`MS_TOKEN`). Be explicit if your application relies on a specific token source.
- gotcha Users encountering `ModuleNotFoundError` for `packaging` or issues with `oss2` (e.g., global import conflicts) in versions prior to 1.35.1 might experience unexpected runtime errors or installation failures.
- gotcha Long-running dataset downloads using OSS (Object Storage Service) previously suffered from STS token expiration issues. This has been addressed, but older versions might still be susceptible.
Install
-
pip install modelscope -
pip install 'modelscope[cv]' # For computer vision tasks pip install 'modelscope[nlp]' # For natural language processing tasks pip install 'modelscope[audio]' # For audio tasks
Imports
- pipeline
from modelscope.pipelines import pipeline
- Tasks
from modelscope.utils.constant import Tasks
- snapshot_download
from modelscope.hub.snapshot_download import snapshot_download
- Model
from modelscope.models import Model
- MsDataset
from modelscope.msdatasets import MsDataset
Quickstart
import os
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
# Initialize an image classification pipeline
classifier = pipeline(task=Tasks.image_classification, model='damo/cv_resnest50_image-classification_damo')
# Example input image URL
image_url = 'https://modelscope.cn/api/v1/models/damo/cv_resnest50_image-classification_damo/repo/files/animal.JPEG'
# Perform inference
result = classifier(image_url)
print(f"Image classification result: {result}")
# You can also download a model first
# from modelscope.hub.snapshot_download import snapshot_download
# model_dir = snapshot_download('damo/cv_resnest50_image-classification_damo')
# local_classifier = pipeline(task=Tasks.image_classification, model=model_dir)
# print(f"Local inference result: {local_classifier(image_url)}")