File size: 1,590 Bytes
cc6274a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""ModelSource ABC — HF and ModelScope implement this."""

from __future__ import annotations

from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Any


@dataclass(frozen=True)
class SiblingFile:
    """One file in the model repo. `size` is bytes, or None if unknown."""

    filename: str
    size: int | None


@dataclass(frozen=True)
class ModelArtifact:
    """The raw material a ModelSource returns.

    We do NOT interpret anything here — interpretation lives in `architecture/`
    and `weight_analyzer/`. This is the thin "fetch" layer.
    """

    source: str  # "huggingface" | "modelscope"
    model_id: str
    commit_sha: str | None  # HF provides this; used as cache key component
    config: dict[str, Any]  # parsed config.json
    siblings: tuple[SiblingFile, ...]  # all files in the repo


class ModelNotFoundError(Exception):
    """Model id does not exist on this source."""


class AuthRequiredError(Exception):
    """Model is gated / private — user must set a token."""


class SourceUnavailableError(Exception):
    """Network error, timeout, rate limit, etc."""


class ModelSource(ABC):
    """Abstract interface for HF / ModelScope / future sources."""

    name: str  # subclasses override

    @abstractmethod
    def fetch(self, model_id: str) -> ModelArtifact:
        """Fetch config.json + siblings for the given model.

        Raises:
            ModelNotFoundError: 404.
            AuthRequiredError: 401/403 (gated/private).
            SourceUnavailableError: 429, 5xx, timeout, network down.
        """