modelscope-api 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. modelscope_api/__init__.py +1 -0
  2. modelscope_api/config.py +23 -0
  3. modelscope_api/data_models/__init__.py +3 -0
  4. modelscope_api/data_models/base.py +22 -0
  5. modelscope_api/data_models/collection/__init__.py +6 -0
  6. modelscope_api/data_models/collection/collection.py +110 -0
  7. modelscope_api/data_models/collection/collection_item.py +92 -0
  8. modelscope_api/data_models/magicube/__init__.py +5 -0
  9. modelscope_api/data_models/magicube/magicube_blance.py +31 -0
  10. modelscope_api/data_models/studio/__init__.py +13 -0
  11. modelscope_api/data_models/studio/base_image.py +26 -0
  12. modelscope_api/data_models/studio/environment_variable.py +38 -0
  13. modelscope_api/data_models/studio/hardware.py +87 -0
  14. modelscope_api/data_models/studio/logs.py +54 -0
  15. modelscope_api/data_models/studio/sdk.py +41 -0
  16. modelscope_api/data_models/studio/studio.py +207 -0
  17. modelscope_api/data_models/user/__init__.py +5 -0
  18. modelscope_api/data_models/user/user.py +47 -0
  19. modelscope_api/exceptions.py +59 -0
  20. modelscope_api/models/__init__.py +9 -0
  21. modelscope_api/models/_sub_client.py +71 -0
  22. modelscope_api/models/collection/__init__.py +8 -0
  23. modelscope_api/models/collection/collection.py +122 -0
  24. modelscope_api/models/collection/collection_client.py +132 -0
  25. modelscope_api/models/collection/collection_item.py +92 -0
  26. modelscope_api/models/collection/collection_item_client.py +247 -0
  27. modelscope_api/models/magicube/__init__.py +5 -0
  28. modelscope_api/models/magicube/magicube_client.py +46 -0
  29. modelscope_api/models/modelscope_client.py +206 -0
  30. modelscope_api/models/studio/__init__.py +7 -0
  31. modelscope_api/models/studio/environment_variable_client.py +107 -0
  32. modelscope_api/models/studio/studio.py +197 -0
  33. modelscope_api/models/studio/studio_client.py +237 -0
  34. modelscope_api/models/user/__init__.py +5 -0
  35. modelscope_api/models/user/user_client.py +46 -0
  36. modelscope_api/utils/regex.py +12 -0
  37. modelscope_api/utils/typing.py +12 -0
  38. modelscope_api-0.0.1.dist-info/METADATA +20 -0
  39. modelscope_api-0.0.1.dist-info/RECORD +42 -0
  40. modelscope_api-0.0.1.dist-info/WHEEL +5 -0
  41. modelscope_api-0.0.1.dist-info/licenses/LICENSE +21 -0
  42. modelscope_api-0.0.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1 @@
1
+ from .models import ModelScopeClient
@@ -0,0 +1,23 @@
1
+ """
2
+ 配置
3
+ """
4
+
5
+ import os
6
+ from typing import Optional
7
+
8
+ from dotenv import load_dotenv
9
+ from yarl import URL
10
+
11
+
12
+ # 加载 .env 文件
13
+ load_dotenv()
14
+
15
+ # 用于 ModelScope 用户进行身份验证的令牌
16
+ MODELSCOPE_API_TOKEN: Optional[str] = os.getenv("MODELSCOPE_API_TOKEN")
17
+
18
+ # OpenAPI 基础地址
19
+ MODELSCOPE_OPENAPI_VERSION: str = "v1"
20
+ MODELSCOPE_OPENAPI_BASE_URL: URL = URL(os.getenv(
21
+ "MODELSCOPE_OPENAPI_BASE_URL",
22
+ "https://modelscope.cn/openapi"
23
+ ))
@@ -0,0 +1,3 @@
1
+ """
2
+ 数据模型。
3
+ """
@@ -0,0 +1,22 @@
1
+ """
2
+ 数据类基类。
3
+ """
4
+
5
+ from __future__ import annotations
6
+
7
+ from typing import List
8
+
9
+ from ..utils.typing import JsonObject
10
+
11
+
12
+ class BaseDataClass:
13
+ """
14
+ 数据模型基类,提供从 JSON 构造的通用方法。
15
+ """
16
+
17
+ @classmethod
18
+ def from_json(cls, data: JsonObject | List[JsonObject]) -> BaseDataClass:
19
+ """
20
+ 从 JSON 数据对象构造。
21
+ """
22
+ return cls(**data)
@@ -0,0 +1,6 @@
1
+ """
2
+ 与合集(Collection)有关的数据模型。
3
+ """
4
+
5
+ from .collection import CollectionInfo, CollectionTheme, CollectionVisibility
6
+ from .collection_item import CollectionItemInfo, CollectionItemType, CollectionItemVisibility
@@ -0,0 +1,110 @@
1
+ """
2
+ 合集(Collection)数据模型。
3
+ """
4
+
5
+ from __future__ import annotations
6
+
7
+ from enum import StrEnum
8
+ from typing import List, Optional
9
+
10
+ from pydantic import Field
11
+ from pydantic.dataclasses import dataclass
12
+
13
+ from ...utils.regex import COLLECTION_SLUG_PATTERN
14
+ from ..base import BaseDataClass
15
+ from .collection_item import CollectionItemInfo
16
+
17
+
18
+
19
+ class CollectionVisibility(StrEnum):
20
+ """
21
+ 合集可见性。
22
+ """
23
+ PUBLIC = "public" # 公开
24
+ PRIVATE = "private" # 私有
25
+
26
+
27
+ class CollectionTheme(StrEnum):
28
+ """
29
+ 合集的主题色。
30
+ """
31
+ BLUE = "Blue"
32
+ PINK = "Pink"
33
+ PURPLE = "Purple"
34
+ CYAN = "Cyan"
35
+
36
+
37
+ @dataclass(frozen=True)
38
+ class CollectionInfo(BaseDataClass):
39
+ """
40
+ 合集(collection)信息。
41
+ """
42
+
43
+ slug: str = Field(
44
+ description=(
45
+ "Collection 唯一标识,格式 {owner}/{slug}。"
46
+ "新建为 {owner}/{title_slug};历史为 {owner}/{title_slug}-{short_id}"
47
+ ),
48
+ pattern=COLLECTION_SLUG_PATTERN.pattern
49
+ )
50
+
51
+ title: Optional[str] = Field(
52
+ description="合集标题。",
53
+ default=None
54
+ )
55
+
56
+ description: Optional[str] = Field(
57
+ description="合集简介描述(Markdown)。",
58
+ default=None
59
+ )
60
+
61
+ owner: str = Field(
62
+ description="合集的所有者(用户名或组织名)。"
63
+ )
64
+
65
+ visibility: CollectionVisibility = Field(
66
+ description="可见性:public(公开)/ private(私有)。"
67
+ )
68
+
69
+ theme: CollectionTheme = Field(
70
+ description="合集页面主题样式标识。"
71
+ )
72
+
73
+ item_count: int = Field(
74
+ description="喜欢数。",
75
+ ge=0
76
+ )
77
+
78
+ likes: int = Field(
79
+ description="合集点赞数",
80
+ ge=0
81
+ )
82
+
83
+ view_count: int = Field(
84
+ description="浏览量。",
85
+ ge=0
86
+ )
87
+
88
+ items: list[CollectionItemInfo] = Field(
89
+ description="合集内部条目列表,数组元素为合集子项对象,为空时返回空列表",
90
+ default_factory=list
91
+ )
92
+
93
+ created_at: Optional[str] = Field(
94
+ description="合集创建时间,ISO‑8601格式UTC时间字符串。",
95
+ default=None
96
+ )
97
+
98
+ updated_at: Optional[str] = Field(
99
+ description="合集最后更新时间,ISO‑8601格式UTC时间字符串。",
100
+ default=None
101
+ )
102
+
103
+ url: str = Field(
104
+ description="合集网页访问完整URL地址。"
105
+ )
106
+
107
+ install_command: List[str] = Field(
108
+ description="当 items 包含 skill 时,返回下载命令数组。",
109
+ default_factory=list
110
+ )
@@ -0,0 +1,92 @@
1
+ """
2
+ 合集(Collection)数据模型。
3
+ """
4
+
5
+ from __future__ import annotations
6
+
7
+ from enum import StrEnum
8
+ from typing import Optional
9
+
10
+ from pydantic import Field
11
+ from pydantic.dataclasses import dataclass
12
+
13
+ from ..base import BaseDataClass
14
+
15
+
16
+
17
+ class CollectionItemType(StrEnum):
18
+ """
19
+ 合集中的条目的资源类型。
20
+ """
21
+ MODEL = "model"
22
+ DATASET = "dataset"
23
+ STUDIO = "studio"
24
+ PAPER = "paper"
25
+ SKILL = "skill"
26
+ MCP = "mcp"
27
+
28
+
29
+ class CollectionItemVisibility(StrEnum):
30
+ """
31
+ 合集中的条目的可见性。
32
+ """
33
+ PUBLIC = "public" # 公开
34
+ PROTECTED = "protected" # 仅公开体验
35
+ PRIVATE = "private" # 私有
36
+
37
+
38
+ @dataclass(frozen=True)
39
+ class CollectionItemInfo(BaseDataClass):
40
+ """
41
+ 合集中的条目信息。
42
+ """
43
+ item_type: CollectionItemType = Field(
44
+ description="资源类型:model / dataset / studio / paper / skill / mcp,用于定位"
45
+ )
46
+
47
+ item_object_id: str = Field(
48
+ description="资源标识,用于定位,如 damo/nlp_bert_base 或论文 ID"
49
+ )
50
+
51
+ note: Optional[str] = Field(
52
+ description="用户对该条目的备注说明(Markdown)。",
53
+ default=None
54
+ )
55
+
56
+ position: Optional[int] = Field(
57
+ description="排序位置(从 1 开始)。",
58
+ default=None,
59
+ ge=1
60
+ )
61
+
62
+ created_at: Optional[str] = Field(
63
+ description="添加时间,ISO 8601 UTC。",
64
+ default=None
65
+ )
66
+
67
+ visibility: Optional[CollectionItemVisibility] = Field(
68
+ description="可见性:public / protected / private。",
69
+ default=None
70
+ )
71
+
72
+ gated: Optional[bool] = Field(
73
+ description="visibility 为 public 时,是否需先申请并经同意后才可访问。",
74
+ default=None
75
+ )
76
+
77
+ protected_mode: Optional[int] = Field(
78
+ description=(
79
+ "protected(仅公开体验)模式标识;"
80
+ "用于表达条目/资源的仅公开体验语义,具体取值含义以产品后端实现为准(如 0=无、2=仅公开体验)。"
81
+ ),
82
+ default=None
83
+ )
84
+
85
+ reason: Optional[str] = Field(
86
+ description=(
87
+ "失败原因错误码,如 ResourceNotFound, ItemAlreadyInCollection, "
88
+ "ItemNotInCollection, PermissionDenied。"
89
+ "仅当添加或更新条目失败时有效。"
90
+ ),
91
+ default=None
92
+ )
@@ -0,0 +1,5 @@
1
+ """
2
+ 与魔粒(Magicube)有关的数据模型。
3
+ """
4
+
5
+ from .magicube_blance import MagicubeBalanceInfo
@@ -0,0 +1,31 @@
1
+ """
2
+ 魔粒余额数据模型。
3
+ """
4
+
5
+ from pydantic import Field
6
+ from pydantic.dataclasses import dataclass
7
+
8
+ from ...utils.typing import Real
9
+ from ..base import BaseDataClass
10
+
11
+
12
+
13
+ @dataclass(frozen=True)
14
+ class MagicubeBalanceInfo(BaseDataClass):
15
+ """
16
+ 魔粒余额信息。
17
+ """
18
+ total_balance: Real = Field(
19
+ description="总额度(可用额度 + 预扣额度)",
20
+ ge=0
21
+ )
22
+
23
+ available_balance: Real = Field(
24
+ description="可用额度",
25
+ ge=0
26
+ )
27
+
28
+ frozen_amount: Real = Field(
29
+ description="预扣额度(进行中任务未返回结果时预扣减)",
30
+ ge=0
31
+ )
@@ -0,0 +1,13 @@
1
+ """
2
+ 与创空间(Studio)有关的数据模型。
3
+ """
4
+
5
+ from .base_image import BaseImageInfo
6
+ from .environment_variable import EnvironmentVariableInfo, EnvironmentVariableType
7
+ from .hardware import HardwareInfo
8
+ from .logs import LogsInfo, LogsType
9
+ from .sdk import SDKType, SDKVersionInfo
10
+ from .studio import (
11
+ StudioActiveConfig, StudioRuntimeStatus, StudioRuntimeInfo,
12
+ StudioVisibility, StudioInfo
13
+ )
@@ -0,0 +1,26 @@
1
+ """
2
+ 基础镜像数据模型。
3
+ """
4
+
5
+ from typing import Optional
6
+
7
+ from pydantic import Field
8
+ from pydantic.dataclasses import dataclass
9
+
10
+ from ..base import BaseDataClass
11
+
12
+
13
+
14
+ @dataclass(frozen=True)
15
+ class BaseImageInfo(BaseDataClass):
16
+ """
17
+ 基础镜像信息。
18
+ """
19
+ name: str = Field(
20
+ description="镜像名称,例如 ubuntu22.04-py311-torch2.9.1-modelscope1.35.0。"
21
+ )
22
+
23
+ tag: Optional[str] = Field(
24
+ description="标签,例如 latest",
25
+ default=None
26
+ )
@@ -0,0 +1,38 @@
1
+ """
2
+ 环境变量数据模型。
3
+ """
4
+
5
+ from enum import StrEnum
6
+ from typing import Optional
7
+
8
+ from pydantic import Field
9
+ from pydantic.dataclasses import dataclass
10
+
11
+ from ..base import BaseDataClass
12
+
13
+
14
+
15
+ class EnvironmentVariableType(StrEnum):
16
+ """
17
+ 环境变量类型。
18
+ """
19
+ # 明文变量
20
+ VARIABLE = "variable"
21
+
22
+ # 密文变量
23
+ SECRET = "secret"
24
+
25
+
26
+ @dataclass(frozen=True)
27
+ class EnvironmentVariableInfo(BaseDataClass):
28
+ """
29
+ 环境变量信息。
30
+ """
31
+ key: str = Field(
32
+ description="变量名称。"
33
+ )
34
+
35
+ value: Optional[str] = Field(
36
+ description="变量值。密文变量为 None。",
37
+ default=None
38
+ )
@@ -0,0 +1,87 @@
1
+ """
2
+ 硬件数据模型。
3
+ """
4
+
5
+ from typing import List, Literal, Optional
6
+
7
+ from pydantic import Field
8
+ from pydantic.dataclasses import dataclass
9
+
10
+ from ..base import BaseDataClass
11
+
12
+
13
+
14
+ @dataclass(frozen=True)
15
+ class HardwareInfo(BaseDataClass):
16
+ """
17
+ 硬件资源配置详细信息,用于描述运行环境的具体规格、库存及成本。
18
+ """
19
+ name: str = Field(
20
+ description="OpenAPI hardware 参数值。付费资源格式为 paid/<InstanceType>。"
21
+ )
22
+
23
+ resource_type: Literal["free", "paid"] = Field(
24
+ description="资源类型,free 表示平台免费资源,paid 表示使用用户自己的云账号付费部署。"
25
+ )
26
+
27
+ instance_type: Optional[str] = Field(
28
+ description="ECS 规格名,仅付费资源返回,例如 ecs.gn7i-c8g1.2xlarge。",
29
+ default=None
30
+ )
31
+
32
+ cpu: Optional[int] = Field(
33
+ description="CPU 核数,仅付费资源返回",
34
+ default=None,
35
+ ge=0
36
+ )
37
+
38
+ gpu: Optional[int] = Field(
39
+ description="GPU 数量,仅付费资源返回",
40
+ default=None,
41
+ ge=0
42
+ )
43
+
44
+ memory: Optional[int] = Field(
45
+ description="内存大小(GB),仅付费资源返回",
46
+ default=None,
47
+ ge=0
48
+ )
49
+
50
+ gpu_type: Optional[str] = Field(
51
+ description="GPU 类型,仅付费资源返回,如 NVIDIA A100、V100 等",
52
+ default=None
53
+ )
54
+
55
+ gpu_memory: Optional[int] = Field(
56
+ description="显存(GB),仅付费资源返回",
57
+ default=None,
58
+ ge=0
59
+ )
60
+
61
+ supported_sdk_types: List[str] = Field(
62
+ description="该硬件所支持的 SDK 类型列表,例如 ['gradio', 'streamlit']",
63
+ default_factory=list
64
+ )
65
+
66
+ has_stock: Optional[bool] = Field(
67
+ description="是否有库存,仅付费资源返回",
68
+ default=None
69
+ )
70
+
71
+ stock: Optional[int] = Field(
72
+ description="库存数量",
73
+ default=None,
74
+ ge=0
75
+ )
76
+
77
+ cost_after_discount: Optional[float] = Field(
78
+ description="折后价格,仅付费资源返回",
79
+ default=None,
80
+ ge=0.0
81
+ )
82
+
83
+ original_cost: Optional[float] = Field(
84
+ description="原价,仅付费资源返回",
85
+ default=None,
86
+ ge=0.0
87
+ )
@@ -0,0 +1,54 @@
1
+ """
2
+ 日志数据模型。
3
+ """
4
+
5
+ from enum import StrEnum
6
+ from typing import List
7
+
8
+ from pydantic import Field
9
+ from pydantic.dataclasses import dataclass
10
+
11
+ from ..base import BaseDataClass
12
+
13
+
14
+
15
+ class LogsType(StrEnum):
16
+ """
17
+ 日志类型。
18
+ """
19
+ BUILD = "build"
20
+ RUN = "run"
21
+
22
+
23
+ @dataclass(frozen=True)
24
+ class LogsInfo(BaseDataClass):
25
+ """
26
+ 日志信息。
27
+ """
28
+ logs: List[str] = Field(
29
+ description="日志内容,每行为一条日志。",
30
+ default_factory=list
31
+ )
32
+
33
+ page_num: int = Field(
34
+ description="页码。",
35
+ default=1,
36
+ ge=1
37
+ )
38
+
39
+ page_size: int = Field(
40
+ description="每页的日志条数。",
41
+ default=100,
42
+ ge=1,
43
+ le=500
44
+ )
45
+
46
+ total_count: int = Field(
47
+ description="日志总数。",
48
+ ge=0
49
+ )
50
+
51
+ total_page_num: int = Field(
52
+ description="页面总数。",
53
+ ge=0
54
+ )
@@ -0,0 +1,41 @@
1
+ """
2
+ SDK 版本数据模型。
3
+ """
4
+
5
+ from enum import StrEnum
6
+ from typing import Optional
7
+
8
+ from pydantic import Field
9
+ from pydantic.dataclasses import dataclass
10
+
11
+ from ..base import BaseDataClass
12
+
13
+
14
+
15
+ class SDKType(StrEnum):
16
+ """
17
+ 创空间 SDK 类型。
18
+ """
19
+ GRADIO = "gradio"
20
+ STREAMLIT = "streamlit"
21
+ DOCKER = "docker"
22
+ STATIC = "static"
23
+
24
+
25
+ @dataclass(frozen=True)
26
+ class SDKVersionInfo(BaseDataClass):
27
+ """
28
+ SDK 版本信息。
29
+ """
30
+ sdk_type: SDKType = Field(
31
+ description="SDK 类型"
32
+ )
33
+
34
+ tag: Optional[str] = Field(
35
+ description="版本标签",
36
+ default=None
37
+ )
38
+
39
+ version: str = Field(
40
+ description="SDK 版本"
41
+ )