Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remaining Credits API #84

Merged
merged 4 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion tabpfn_client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# Copyright (c) Prior Labs GmbH 2025.
# Licensed under the Apache License, Version 2.0

from tabpfn_client.config import init, reset, get_access_token, set_access_token
from tabpfn_client.config import (
init,
reset,
get_access_token,
set_access_token,
get_api_usage,
)
from tabpfn_client.estimator import TabPFNClassifier, TabPFNRegressor
from tabpfn_client.service_wrapper import UserDataClient

Expand All @@ -13,4 +19,5 @@
"UserDataClient",
"get_access_token",
"set_access_token",
"get_api_usage",
]
16 changes: 16 additions & 0 deletions tabpfn_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,3 +855,19 @@ def try_browser_login(cls) -> tuple[bool, str]:
return True, token

return False, "Browser login failed or was cancelled"

@classmethod
def get_api_usage(cls, access_token: str):
"""
Retrieve current API usage data of the user from the server.
Returns summary: str
"""
response = cls.httpx_client.post(
cls.server_endpoints.get_api_usage.path,
headers={"Authorization": f"Bearer {access_token}"},
)
cls._validate_response(response, "get_api_usage")

response = response.json()

return f"Currently, you have used {response['current_usage']} of the allowed limit of {'Unlimited' if int(response['usage_limit']) == -1 else response['usage_limit']} credits. The limit will reset at {response['reset_time']}."
5 changes: 5 additions & 0 deletions tabpfn_client/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,8 @@ def get_access_token() -> str:
def set_access_token(access_token: str):
UserAuthenticationClient.set_token(access_token)
Config.is_initialized = True


def get_api_usage() -> dict:
access_token = get_access_token()
return ServiceClient.get_api_usage(access_token)
5 changes: 5 additions & 0 deletions tabpfn_client/server_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,8 @@ endpoints:
methods: [ "DELETE" ]
description: "Delete user account, alongside all associated data"

get_api_usage:
path: "/get_api_usage/"
methods: [ "POST" ]
description: "Get prediction hits data for a given user"