ModelScope API Python SDK
一个异步、类型安全的 Python SDK,用于调用 ModelScope 开放 API。
它提供了直观的对象化接口,覆盖了合集、创空间、魔粒、用户等核心模块,让您能以 Pythonic 的方式与 ModelScope 平台交互。
✨ 特性
- 🚀 全异步 – 基于
httpx.AsyncClient,支持高并发请求。 - 🧩 模块化设计 – 按业务领域拆分:
User、Magicube、Studio、Collection等。 - 📦 类型安全 – 使用 Pydantic 数据模型,提供 IDE 自动补全和校验。
- 🔐 自动认证 – 通过环境变量或构造参数传入 API Token,自动注入请求头。
- 🛠️ 开箱即用 – 支持
.env配置,内置随机 UA 伪装,简化开发。
📦 安装
pip install modelscope-api
或直接从源码安装:
git clone https://github.com/Jerry-Wu-GitHub/modelscope-api.git
cd modelscope-api
pip install -e .
🚀 快速开始
1. 获取 API Token
访问 ModelScope 个人设置 生成您的 MODELSCOPE_API_TOKEN。
2. 配置环境变量(推荐)
在项目根目录创建 .env 文件:
MODELSCOPE_API_TOKEN=your_token_here
也可以直接在代码中传入:
from modelscope_api import ModelScopeClient
client = ModelScopeClient(api_key="your_token_here")
3. 基础用法
import asyncio
from modelscope_api import ModelScopeClient
async def main():
async with ModelScopeClient() as client:
# 获取当前用户信息
user_info = await client.user.get_current_user_info()
print(f"当前用户: {user_info.username}")
# 查询魔粒余额
balance = await client.magicube.query_magicube_balance()
print(f"可用魔粒: {balance.available_balance}")
# 搜索创空间
studios = await client.studio.search_studio_infos(search="chatbot", owner="iic")
for studio in studios:
print(f"- {studio.display_name} ({studio.id})")
if __name__ == "__main__":
asyncio.run(main())
📖 API 概览
SDK 的所有功能通过 ModelScopeClient 聚合,您可以通过其属性访问各子客户端:
| 子客户端 | 说明 | 主要方法 |
|---|---|---|
client.user |
用户信息 | get_current_user_info() |
client.magicube |
魔粒(积分) | query_magicube_balance() |
client.studio |
创空间管理 | search_studio_infos(), create_studio(), get_studio() |
client.collection |
合集管理 | search_collection_infos(), create_collection(), get_collection() |
每个子客户端返回的数据均为 Pydantic 模型,可直接访问属性。
详细示例:操作创空间
async def studio_example(client):
# 创建创空间
studio = await client.studio.create_studio(
repo_name="my-awesome-space",
display_name="我的炫酷空间",
visibility="public",
sdk_type="gradio",
hardware="platform/2v-cpu-16g-mem"
)
print(f"创建成功: {studio.id}")
# 部署
runtime = await studio.deploy(timeout=30)
print(f"部署状态: {runtime.status}")
# 查看日志
logs = await studio.get_logs("run", page_size=50)
for line in logs.logs:
print(line)
# 添加环境变量
await studio.variables.add("MY_KEY", "my_value")
详细示例:操作合集
async def collection_example(client):
# 创建合集
collection = await client.collection.create_collection(
title="我的收藏",
visibility="private"
)
# 添加条目(模型)
failed = await collection.items.add_items([
{
"item_type": "model",
"item_object_id": "Qwen/Qwen3.8-27B",
"position": 1
}
])
if failed:
print("添加失败:", failed)
# 获取条目列表
items = await collection.items.get_items()
for item in items:
print(f"- {item.item_object_id}")
# 删除合集
await collection.delete()
🧪 运行测试
项目包含完整的测试用例,位于 tests/ 目录。运行前请确保已设置有效的 MODELSCOPE_API_TOKEN。
📄 许可证
本项目使用 MIT 许可证。
Metadata
Release files for modelscope-api 0.0.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| modelscope_api-0.0.6.tar.gz | 30.6 kB | Without provenance | Details for modelscope_api-0.0.6.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| modelscope_api-0.0.6-py3-none-any.whl | Python 3 | none | any | Details for modelscope_api-0.0.6-py3-none-any.whl |
Total release size: 72.2 kB
Release files / modelscope_api-0.0.6.tar.gz
| Download URL | modelscope_api-0.0.6.tar.gz |
|---|---|
| Size | 30.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e278bb957a8d33a19d4a0248e7e2b8fa74f3b00fd6fa32819c8176a74e19b837
|
|
BLAKE2b-256 checksum How to use checksums |
f75fe88218976b149b19b4d6932a8bc212bbe2dc0b39fbd8543e3cf21c36f94d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.7
|
Release files / modelscope_api-0.0.6-py3-none-any.whl
| Download URL | modelscope_api-0.0.6-py3-none-any.whl |
|---|---|
| Size | 41.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1f761b313a21238aa9cf51ef1b75c220abd27d7c0cdca03d5ecf7eb57ba6f32c
|
|
BLAKE2b-256 checksum How to use checksums |
bf64beecf44e73d16ca828bb582ac61998bcb27dbe9d753b54c219956c42504f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.7
|
Security for modelscope-api 0.0.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.