Stability-AI / stability-sdk

SDK for interacting with stability.ai APIs (e.g. stable diffusion inference)
https://platform.stability.ai/
MIT License
2.42k stars 336 forks source link

get account balance info from token #23

Open rifeWithKaiju opened 2 years ago

rifeWithKaiju commented 2 years ago

I'd like users to be able to put their own tokens in our software and use the balance they have at dreamstudio using a different front-end.

Is it currently possible (and if not, I'd like to request this feature), to get the balance of the account, so I can provide this information to users without them having to go to a browser and log into dreamstudio?

SiyrisSoul commented 2 years ago

This would also be useful to set up an alert system when paying for things on a large scale to ensure the app doesn't become unusable during a massive surge in traffic and so I don't have to keep checking manually.

dmarx commented 2 years ago

coming soon, but hasn't been incorporated into this package yet. If you don't mind getting under the hood the grpc stubs you'll need can be found here:

jessekoska commented 1 year ago

@dmarx I've managed to use the dashboard stub to access GetMe and I'm getting a permission error... is this something that i can get opened up? if so how?

grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with: status = StatusCode.PERMISSION_DENIED details = "You do not have permission to access this resource." debug_error_string = "UNKNOWN:Error received from peer ipv4:104.18.21.212:443 {grpc_message:"You do not have permission to access this resource.", grpc_status:7, created_time:"2022-10-06T00:52:45.118895296+00:00"}"

I'm using the same StabilityInference example in client.py to open up the channel. generation works fine with the api token i'm using.

wd = pathlib.Path(file).parent.resolve() dashPath = wd / "stability-sdk/api-interfaces/gooseai/dashboard" sys.path.append(str(dashPath))

import dashboard_pb2 as dashboard import dashboard_pb2_grpc as dashboard_grpc

then inside class StabilityInference:

def get_me(self): er = dashboard.EmptyRequest() stub = dashboard_grpc.DashboardServiceStub(self.channel) return stub.GetClientSettings(er, **self.grpc_args)

johnsabath commented 1 year ago

@jessekoska We'll need to do some back-end work to support this, as API keys have a very limited permission set at the moment. Allowing API key permissions to be customized at creation time is on our radar.

cou929 commented 1 year ago

As same to @CEOSiyris is saying, I also would like to monitor the remaining balance to use DreamStudio API in production.

But I've not figured out how to get the access token in a canonical way. Could you have some advice?

It worked, but copying the token by the dev tool doesn't seem like the right way to do it.

jessekoska commented 1 year ago

yeah i attempted this approach too (but must have not done something correct in the metadata approach cause i didn't get it to connect) but i also noticed that my account's Bearer token changed at some point so i stopped trying as i wasn't confident of how long the token would be valid or how i might refresh it

johnsabath commented 1 year ago

With a recent update, API keys now have read-only access to certain dashboard operations, such as GetMe and GetOrganization, so this is now possible.

pharmapsychotic commented 1 year ago

There's a handy function for this the animation notebook uses to fetch the user's profile picture and credit balance you could use: https://github.com/Stability-AI/stability-sdk/blob/main/src/stability_sdk/api.py#L181 It will probably shift locations as StabilityInference and api.Context merge up but that's one way to get it at the moment.