Zum Inhalt springen

Referenz f├╝r hub_sdk/hub_client.py

Hinweis

Diese Datei ist verf├╝gbar unter https://github.com/ultralytics/hub-sdk/blob/main/hub_sdk/hub_client .py. Wenn du ein Problem entdeckst, hilf bitte, es zu beheben, indem du einen Pull Request ­čŤá´ŞĆ einreichst. Vielen Dank ­čÖĆ!



hub_sdk.hub_client.HUBClient

Basen: Auth

Eine Client-Klasse f├╝r die Interaktion mit einem HUB-Service, die die Authentifizierungsfunktionen erweitert.

Attribute:

Name Typ Beschreibung
authenticated bool

Zeigt an, ob der Client authentifiziert ist.

api_key str

Der API-Schl├╝ssel f├╝r die Authentifizierung.

id_token str

Das Identit├Ąts-Token f├╝r die Authentifizierung.

Quellcode in hub_sdk/hub_client.py
class HUBClient(Auth):
    """
    A client class for interacting with a HUB service, extending authentication capabilities.

    Attributes:
        authenticated (bool): Indicates whether the client is authenticated.
        api_key (str): The API key for authentication.
        id_token (str): The identity token for authentication.
    """

    def __init__(self, credentials: Optional[Dict] = None):
        """
        Initializes the HUBClient instance.

        Args:
            credentials (dict, optional): A dictionary containing authentication credentials.
                            If None, the client will attempt to retrieve the API key
                            from the environment variable "HUB_API_KEY".
        """
        super().__init__()
        self.authenticated = False
        if not credentials:
            self.api_key = os.environ.get("HUB_API_KEY")  # Safely retrieve the API key from an environment variable.
            credentials = {"api_key": self.api_key}

        self.login(**credentials)

    def login(self, api_key=None, id_token=None, email=None, password=None):
        """
        Logs in the client using provided authentication credentials.

        Args:
            api_key (str, optional): The API key for authentication.
            id_token (str, optional): The identity token for authentication.
            email (str, optional): User's email.
            password (str, optional): User's password.
        """
        self.api_key = api_key
        self.id_token = id_token
        if (
            (self.api_key or self.id_token)
            and self.authenticate()
            or not self.api_key
            and not self.id_token
            and email
            and password
            and self.authorize(email, password)
        ):
            self.authenticated = True

    @require_authentication
    def model(self, model_id: Optional[str] = None) -> Models:
        """
        Returns an instance of the Models class for interacting with models.

        Args:
            model_id (str, optional): The identifier of the model. If provided,
                returns an instance associated with the specified model_id.

        Returns:
            (Models): An instance of the Models class.
        """
        return Models(model_id, self.get_auth_header())

    @require_authentication
    def dataset(self, dataset_id: str = None) -> Datasets:
        """
        Returns an instance of the Datasets class for interacting with datasets.

        Args:
            dataset_id (str, optional): The identifier of the dataset. If provided,
                returns an instance associated with the specified dataset_id.

        Returns:
            (Datasets): An instance of the Datasets class.
        """
        return Datasets(dataset_id, self.get_auth_header())

    @require_authentication
    def team(self, arg):
        """Returns an instance of the Teams class for interacting with teams."""
        raise Exception("Coming Soon")

    @require_authentication
    def project(self, project_id: Optional[str] = None) -> Projects:
        """
        Returns an instance of the Projects class for interacting with Projects.

        Args:
            project_id (str, optional): The identifier of the project. If provided,
                returns an instance associated with the specified project_id.

        Returns:
            (Projects): An instance of the Projects class.
        """
        return Projects(project_id, self.get_auth_header())

    @require_authentication
    def user(self, user_id: Optional[str] = None) -> Users:
        """
        Returns an instance of the Users class for interacting with Projects.

        Args:
            user_id (str, optional): The identifier of the user. If provided,
                returns an instance associated with the specified user_id.

        Returns:
            (Users): An instance of the Projects class.
        """
        return Users(user_id, self.get_auth_header())

    @require_authentication
    def model_list(self, page_size: Optional[int] = 10, public: Optional[bool] = None) -> ModelList:
        """
        Returns a ModelList instance for interacting with a list of models.

        Args:
            page_size (int, optional): The number of models per page.
            public (bool, optional): Pass true to retrieve list of Public models list.

        Returns:
            (ModelList): An instance of the ModelList class.
        """
        return ModelList(page_size, public, self.get_auth_header())

    @require_authentication
    def project_list(self, page_size: Optional[int] = 10, public: Optional[bool] = None) -> ProjectList:
        """
        Returns a ProjectList instance for interacting with a list of projects.

        Args:
            page_size (int, optional): The number of projects per page.
            public (bool, optional): Pass true to retrieve list of Public models list.

        Returns:
            (ProjectList): An instance of the ProjectList class.
        """
        return ProjectList(page_size, public, self.get_auth_header())

    @require_authentication
    def dataset_list(self, page_size: Optional[int] = 10, public: Optional[bool] = None) -> DatasetList:
        """
        Returns a DatasetList instance for interacting with a list of datasets.

        Args:
            page_size (int, optional): The number of datasets per page.
            public (bool, optional): Pass true to retrieve list of Public dataset list.

        Returns:
            (DatasetList): An instance of the DatasetList class.
        """
        return DatasetList(page_size, public, self.get_auth_header())

    @require_authentication
    def team_list(self, page_size=None, public=None):
        """Fetches a list of team members with optional pagination."""
        raise Exception("Coming Soon")

__init__(credentials=None)

Initialisiert die HUBClient-Instanz.

Parameter:

Name Typ Beschreibung Standard
credentials dict

Ein W├Ârterbuch mit Authentifizierungsdaten. Falls keine, versucht der Client, den API-Schl├╝ssel aus der Umgebungsvariablen "HUB_API_KEY" abzurufen.

None
Quellcode in hub_sdk/hub_client.py
def __init__(self, credentials: Optional[Dict] = None):
    """
    Initializes the HUBClient instance.

    Args:
        credentials (dict, optional): A dictionary containing authentication credentials.
                        If None, the client will attempt to retrieve the API key
                        from the environment variable "HUB_API_KEY".
    """
    super().__init__()
    self.authenticated = False
    if not credentials:
        self.api_key = os.environ.get("HUB_API_KEY")  # Safely retrieve the API key from an environment variable.
        credentials = {"api_key": self.api_key}

    self.login(**credentials)

dataset(dataset_id=None)

Liefert eine Instanz der Klasse Datasets f├╝r die Interaktion mit Datasets.

Parameter:

Name Typ Beschreibung Standard
dataset_id str

Der Bezeichner des Datensatzes. Falls angegeben, eine Instanz zur├╝ck, die mit der angegebenen dataset_id verkn├╝pft ist.

None

Retouren:

Typ Beschreibung
Datasets

Eine Instanz der Klasse Datasets.

Quellcode in hub_sdk/hub_client.py
@require_authentication
def dataset(self, dataset_id: str = None) -> Datasets:
    """
    Returns an instance of the Datasets class for interacting with datasets.

    Args:
        dataset_id (str, optional): The identifier of the dataset. If provided,
            returns an instance associated with the specified dataset_id.

    Returns:
        (Datasets): An instance of the Datasets class.
    """
    return Datasets(dataset_id, self.get_auth_header())

dataset_list(page_size=10, public=None)

Gibt eine DatasetList-Instanz f├╝r die Interaktion mit einer Liste von Datens├Ątzen zur├╝ck.

Parameter:

Name Typ Beschreibung Standard
page_size int

Die Anzahl der Datens├Ątze pro Seite.

10
public bool

Gib true ein, um die Liste der ├Âffentlichen Datens├Ątze abzurufen.

None

Retouren:

Typ Beschreibung
DatasetList

Eine Instanz der DatasetList-Klasse.

Quellcode in hub_sdk/hub_client.py
@require_authentication
def dataset_list(self, page_size: Optional[int] = 10, public: Optional[bool] = None) -> DatasetList:
    """
    Returns a DatasetList instance for interacting with a list of datasets.

    Args:
        page_size (int, optional): The number of datasets per page.
        public (bool, optional): Pass true to retrieve list of Public dataset list.

    Returns:
        (DatasetList): An instance of the DatasetList class.
    """
    return DatasetList(page_size, public, self.get_auth_header())

login(api_key=None, id_token=None, email=None, password=None)

Meldet den Client mit den angegebenen Authentifizierungsdaten an.

Parameter:

Name Typ Beschreibung Standard
api_key str

Der API-Schl├╝ssel f├╝r die Authentifizierung.

None
id_token str

Das Identit├Ąts-Token f├╝r die Authentifizierung.

None
email str

E-Mail des Nutzers.

None
password str

Das Passwort des Benutzers.

None
Quellcode in hub_sdk/hub_client.py
def login(self, api_key=None, id_token=None, email=None, password=None):
    """
    Logs in the client using provided authentication credentials.

    Args:
        api_key (str, optional): The API key for authentication.
        id_token (str, optional): The identity token for authentication.
        email (str, optional): User's email.
        password (str, optional): User's password.
    """
    self.api_key = api_key
    self.id_token = id_token
    if (
        (self.api_key or self.id_token)
        and self.authenticate()
        or not self.api_key
        and not self.id_token
        and email
        and password
        and self.authorize(email, password)
    ):
        self.authenticated = True

model(model_id=None)

Gibt eine Instanz der Models-Klasse zur├╝ck, um mit den Modellen zu interagieren.

Parameter:

Name Typ Beschreibung Standard
model_id str

Der Bezeichner des Modells. Falls angegeben, eine Instanz zur├╝ck, die mit der angegebenen model_id verkn├╝pft ist.

None

Retouren:

Typ Beschreibung
Models

Eine Instanz der Klasse Models.

Quellcode in hub_sdk/hub_client.py
@require_authentication
def model(self, model_id: Optional[str] = None) -> Models:
    """
    Returns an instance of the Models class for interacting with models.

    Args:
        model_id (str, optional): The identifier of the model. If provided,
            returns an instance associated with the specified model_id.

    Returns:
        (Models): An instance of the Models class.
    """
    return Models(model_id, self.get_auth_header())

model_list(page_size=10, public=None)

Gibt eine ModelList-Instanz f├╝r die Interaktion mit einer Liste von Modellen zur├╝ck.

Parameter:

Name Typ Beschreibung Standard
page_size int

Die Anzahl der Modelle pro Seite.

10
public bool

Gib true ein, um die Liste der ├Âffentlichen Modelle abzurufen.

None

Retouren:

Typ Beschreibung
ModelList

Eine Instanz der ModelList-Klasse.

Quellcode in hub_sdk/hub_client.py
@require_authentication
def model_list(self, page_size: Optional[int] = 10, public: Optional[bool] = None) -> ModelList:
    """
    Returns a ModelList instance for interacting with a list of models.

    Args:
        page_size (int, optional): The number of models per page.
        public (bool, optional): Pass true to retrieve list of Public models list.

    Returns:
        (ModelList): An instance of the ModelList class.
    """
    return ModelList(page_size, public, self.get_auth_header())

project(project_id=None)

Gibt eine Instanz der Klasse Projects zur├╝ck, um mit Projects zu interagieren.

Parameter:

Name Typ Beschreibung Standard
project_id str

Die Kennung des Projekts. Falls angegeben, eine Instanz zur├╝ck, die mit der angegebenen project_id verkn├╝pft ist.

None

Retouren:

Typ Beschreibung
Projects

Eine Instanz der Klasse Projects.

Quellcode in hub_sdk/hub_client.py
@require_authentication
def project(self, project_id: Optional[str] = None) -> Projects:
    """
    Returns an instance of the Projects class for interacting with Projects.

    Args:
        project_id (str, optional): The identifier of the project. If provided,
            returns an instance associated with the specified project_id.

    Returns:
        (Projects): An instance of the Projects class.
    """
    return Projects(project_id, self.get_auth_header())

project_list(page_size=10, public=None)

Gibt eine ProjectList-Instanz f├╝r die Interaktion mit einer Liste von Projekten zur├╝ck.

Parameter:

Name Typ Beschreibung Standard
page_size int

Die Anzahl der Projekte pro Seite.

10
public bool

Gib true ein, um die Liste der ├Âffentlichen Modelle abzurufen.

None

Retouren:

Typ Beschreibung
ProjectList

Eine Instanz der ProjectList-Klasse.

Quellcode in hub_sdk/hub_client.py
@require_authentication
def project_list(self, page_size: Optional[int] = 10, public: Optional[bool] = None) -> ProjectList:
    """
    Returns a ProjectList instance for interacting with a list of projects.

    Args:
        page_size (int, optional): The number of projects per page.
        public (bool, optional): Pass true to retrieve list of Public models list.

    Returns:
        (ProjectList): An instance of the ProjectList class.
    """
    return ProjectList(page_size, public, self.get_auth_header())

team(arg)

Gibt eine Instanz der Klasse Teams f├╝r die Interaktion mit Teams zur├╝ck.

Quellcode in hub_sdk/hub_client.py
@require_authentication
def team(self, arg):
    """Returns an instance of the Teams class for interacting with teams."""
    raise Exception("Coming Soon")

team_list(page_size=None, public=None)

Holt eine Liste von Teammitgliedern mit optionaler Seitennummerierung.

Quellcode in hub_sdk/hub_client.py
@require_authentication
def team_list(self, page_size=None, public=None):
    """Fetches a list of team members with optional pagination."""
    raise Exception("Coming Soon")

user(user_id=None)

Gibt eine Instanz der Klasse Users f├╝r die Interaktion mit Projects zur├╝ck.

Parameter:

Name Typ Beschreibung Standard
user_id str

Die Kennung des Nutzers. Falls angegeben, eine Instanz zur├╝ck, die mit der angegebenen user_id verkn├╝pft ist.

None

Retouren:

Typ Beschreibung
Users

Eine Instanz der Klasse Projects.

Quellcode in hub_sdk/hub_client.py
@require_authentication
def user(self, user_id: Optional[str] = None) -> Users:
    """
    Returns an instance of the Users class for interacting with Projects.

    Args:
        user_id (str, optional): The identifier of the user. If provided,
            returns an instance associated with the specified user_id.

    Returns:
        (Users): An instance of the Projects class.
    """
    return Users(user_id, self.get_auth_header())



hub_sdk.hub_client.require_authentication(func)

Eine Dekorfunktion, die sicherstellt, dass die umschlossene Methode nur ausgef├╝hrt werden kann, wenn der Client authentifiziert ist.

Parameter:

Name Typ Beschreibung Standard
func callable

Die Methode, die gewickelt werden soll.

erforderlich

Retouren:

Typ Beschreibung
callable

Die Wrapped-Methode.

Quellcode in hub_sdk/hub_client.py
def require_authentication(func) -> callable:
    """
    A decorator function to ensure that the wrapped method can only be executed if the client is authenticated.

    Args:
        func (callable): The method to be wrapped.

    Returns:
        (callable): The wrapped method.
    """

    def wrapper(self, *args, **kwargs):
        """Decorator to ensure a method is called only if the user is authenticated."""
        if not self.authenticated and not kwargs.get("public"):
            raise PermissionError("Access Denied: Authentication required.")
        return func(self, *args, **kwargs)

    return wrapper