stability-sdk icon indicating copy to clipboard operation
stability-sdk copied to clipboard

JS SDK?

Open altryne opened this issue 2 years ago • 22 comments

Is there a plan to provide a JS SDK, or expose the API over HTTP so we can use this API from frontend apps without backend?

This can greatly reduce time to deploy, will allow many frontend developers to built UIs and applications, will allow for "self serve" applications, that users could paste their own API keys to, chrome extensions and more.

altryne avatar Aug 23 '22 03:08 altryne

Same question here

Pau-Lozano avatar Aug 23 '22 09:08 Pau-Lozano

We've uploaded the protobuf definitions for the API here: https://github.com/Stability-AI/stability-sdk/blob/main/src/proto/generation.proto

This should enable users to build a client in any language that supports gRPC. We'll roll out code to demonstrate connecting in other languages soon. I'm hoping the protobuf spec will be enough to get the ball rolling for the time being though.

dmarx avatar Aug 25 '22 00:08 dmarx

We've uploaded the protobuf definitions for the API here: https://github.com/Stability-AI/stability-sdk/blob/main/src/proto/generation.proto

This should enable users to build a client in any language that supports gRPC. We'll roll out code to demonstrate connecting in other languages soon. I'm hoping the protobuf spec will be enough to get the ball rolling for the time being though.

Thank you! I'm currently working on a client-side Swift implementation for iOS.

Update: Working like a charm!

kaishin avatar Aug 26 '22 11:08 kaishin

I was unable to use the proto file. Hope you make a javascript sdk.

svumme avatar Aug 29 '22 02:08 svumme

@svumme I've added precompiled stubs for javascript and typescript here: https://github.com/Stability-AI/stability-sdk/tree/main/src/js

I'll close this issue once we've got demo usage documented

dmarx avatar Aug 30 '22 03:08 dmarx

I've actually created a typescript client/CLI that uses the stability-sdk typescript that you've precompiled, @dmarx. (CC @altryne @Pau-Lozano @kaishin @svumm)

https://github.com/jakiestfu/stability-ts

jakiestfu avatar Sep 01 '22 17:09 jakiestfu

In case folks are interested, I've got another example of using the gRPC service from TypeScript at https://github.com/jeffposnick/magic-realism-dream-bot/tree/main/proto

That example uses the https://www.npmjs.com/package/@grpc/grpc-js client, which I believe is the most "modern" of the various gRPC libraries for JavaScript/TypeScript.

jeffposnick avatar Sep 02 '22 14:09 jeffposnick

I've also since updated the JS SDK to use the new stability-sdk repo https://github.com/jakiestfu/stability-ts

jakiestfu avatar Sep 13 '22 19:09 jakiestfu

@jakiestfu I'm getting this error when trying to use it

Error: Must provide image parameters.

CedYF avatar Sep 15 '22 06:09 CedYF

Anyone know what's going on here?

"UNKNOWN:Error received from peer ipv4:104.18.20.212:443 {created_time:"2022-09-22T16:12:19.839528-06:00", grpc_status:3, grpc_message:"Must provide image parameters."}"

dholbrook86 avatar Sep 22 '22 22:09 dholbrook86

Anyone know what's going on here?

"UNKNOWN:Error received from peer ipv4:104.18.20.212:443 {created_time:"2022-09-22T16:12:19.839528-06:00", grpc_status:3, grpc_message:"Must provide image parameters."}"

@CedYF @dholbrook86 @jakiestfu You should no longer pass ClassifierParameters in the request (for Jake's TS client, remove line 145 and 146 of index.ts)

Apologies for the breaking change. The python client should work if you update the pip package.

chigozienri avatar Sep 28 '22 12:09 chigozienri

So is it possible to connect to beta.dreamstudio.ai or any other URL from Stability AI to use the Stable Diffusion? Should I send protocol buffers as binary data over the HTTP POST request? What URL should I communicate with?

photopea avatar Oct 04 '22 13:10 photopea

Anyone know what's going on here? "UNKNOWN:Error received from peer ipv4:104.18.20.212:443 {created_time:"2022-09-22T16:12:19.839528-06:00", grpc_status:3, grpc_message:"Must provide image parameters."}"

@CedYF @dholbrook86 @jakiestfu You should no longer pass ClassifierParameters in the request (for Jake's TS client, remove line 145 and 146 of index.ts)

Apologies for the breaking change. The python client should work if you update the pip package.

but when at the linux, it still appear the same error message, even if I use the command pip install --upgrade stability-sdk, at the windows, it can work now, any other steps should I do at the linux?

TandK8600 avatar Oct 14 '22 03:10 TandK8600

@photopea

So is it possible to connect to beta.dreamstudio.ai or any other URL from Stability AI to use the Stable Diffusion? Should I send protocol buffers as binary data over the HTTP POST request? What URL should I communicate with?

You should use https://grpc.stability.ai:443 (which is the default) for the URL. There are no other production URLs at present. The API keys can be managed at https://beta.dreamstudio.ai/membership?tab=apiKeys

Currently, the best way to send the request in js/ts is to make a submodule of https://github.com/Stability-AI/api-interfaces. Then import Generation from "api-interfaces/gooseai/generation/generation_pb"; and import { grpc } from "@improbable-eng/grpc-web"; You can then construct a request const request = new Generation.Request(); and send it over grpc. We hope to provide an easier official way of doing this soon, but in the mean time you can have a look at https://github.com/vpzomtrrfrt/stability-client/blob/main/src/index.ts for reference of what needs to go into the request (or use @vpzomtrrfrt 's package https://github.com/vpzomtrrfrt/stability-client )

chigozienri avatar Oct 14 '22 09:10 chigozienri

@TandK8600

Anyone know what's going on here? "UNKNOWN:Error received from peer ipv4:104.18.20.212:443 {created_time:"2022-09-22T16:12:19.839528-06:00", grpc_status:3, grpc_message:"Must provide image parameters."}"

@CedYF @dholbrook86 @jakiestfu You should no longer pass ClassifierParameters in the request (for Jake's TS client, remove line 145 and 146 of index.ts) Apologies for the breaking change. The python client should work if you update the pip package.

but when at the linux, it still appear the same error message, even if I use the command pip install --upgrade stability-sdk, at the windows, it can work now, any other steps should I do at the linux?

On your linux box, are you using the pip package, or a cloned version of this repository? (if the command you run to invoke is python3 -m stability_sdk.client then you are probably using the pip version, if the command you run to invoke is python3 src/stability_sdk/client.py then you are using the cloned repository.

If the pip package, when you run pip show stability-sdk do you see version 0.2.5? If the cloned repo, make sure that you have the latest (git pull and git submodule update --init --recursive)

chigozienri avatar Oct 14 '22 10:10 chigozienri

@chigozienri Thanks, but I am getting "404 page not found" as a HTTP response. I tried to send empty data both through GET and POST method. Do I need to set any specific HTTP headers? See my code here: https://jsfiddle.net/qehusm9f/

Is there any clear description of the API, which does not require using any of your libraries?

photopea avatar Oct 15 '22 17:10 photopea

(I'm not affiliated with this project.)

@photopea, I think your best bet would be to read through https://grpc.io/about/, as from what I can tell, the Stable Diffusion API is exposed as a "standard" gRPC endpoint. It's not trivial to interact with an gRPC endpoint without using a helper library, as you'd need to serialize and deserialize protobuffs yourself.

https://grpc.io/docs/platforms/web/basics/ is the library that most folks use for accessing gRPC services from the browser, and it will handle both the transport and serialization/deserialization for you.

jeffposnick avatar Oct 15 '22 20:10 jeffposnick

@photopea Thanks for sending your code snippet, that makes it clearer. As @jeffposnick says, you can't simply send an HTTP GET or POST request, it needs to be a gRPC call. We do not have a REST API available at present (there is a small discussion on this here) - nor, unfortunately, do we have a description for using the API other than to look at the protos, or to emulate how they are used in the python client in this repo/ other people's implementations such as https://github.com/vpzomtrrfrt/stability-client

chigozienri avatar Oct 16 '22 07:10 chigozienri

I am familiar with protocol buffers and I am planning to communicate through them. But I am curious, why your server does not return any error message, but returns "404" instead.

If I use the same code as in https://jsfiddle.net/qehusm9f/, but send a valid protocol buffer data, will it start to work?

photopea avatar Oct 17 '22 18:10 photopea

Hi, with regards to this issue ""UNKNOWN:Error received from peer ipv4:104.18.20.212:443 {created_time:"2022-09-22T16:12:19.839528-06:00", grpc_status:3, grpc_message:"Must provide image parameters."}"" The error still persist on linux despite updating/upgrading to the newest version 0.2.6 (I,m working with AWS Lambda Python Version 3.7). Does anyone have any idea how to recitfy this ?

CommanderCookyee avatar Oct 19 '22 10:10 CommanderCookyee

Same issue on Google Cloud Functions: line 48, in stabilityai for resp in answers: File "/layers/google.python.pip/pip/lib/python3.7/site-packages/stability_sdk/client.py", line 300, in generate for answer in self.stub.Generate(rq, **self.grpc_args): File "/layers/google.python.pip/pip/lib/python3.7/site-packages/grpc/_channel.py", line 426, in __next__ return self._next() File "/layers/google.python.pip/pip/lib/python3.7/site-packages/grpc/_channel.py", line 826, in _next raise self grpc._channel._MultiThreadedRendezvous: <_MultiThreadedRendezvous of RPC that terminated with: status = StatusCode.INVALID_ARGUMENT details = "Must provide image parameters." debug_error_string = "UNKNOWN:Error received from peer ipv4:104.18.20.212:443 {created_time:"2022-10-19T15:46:11.888314867+00:00", grpc_status:3, grpc_message:"Must provide image parameters."}"

mohsendelsouz avatar Oct 19 '22 16:10 mohsendelsouz

@mohsendelsouz Hi, it seems that this issue was solved at #55 and subsequently merged with #64, where all we needed to do was remove the classifiers. Credit to @sayanarijit.

CommanderCookyee avatar Oct 21 '22 14:10 CommanderCookyee

Could you make a simple HTTP API, which does not require implementing Protocol Buffers, or using huge JS libraries?

Look at the API of DALL-E: https://beta.openai.com/docs/api-reference/images/create

photopea avatar Nov 06 '22 16:11 photopea

I would definitely echo @photopea's comment. A REST API would open up a lot of possibilities.

To give you some additional tangibles, here are other environments/projects where it's very difficult to implement gRPC:

  • Krita plugins (@imperator-maximus created his own python grpc implementation, because Krita doesn't allow 3rd party libs)
  • Blender add-ons (I jumped through a lot of hoops to create my own wrapper API for Stability SDK, because Blender doesn't have reliable/easy support for outside libs)
  • Any website, like others have discussed above

PS. I LOVE Stable Diffusion, and just want more developers to be able to integrate it!

benrugg avatar Nov 14 '22 18:11 benrugg

We've uploaded the protobuf definitions for the API here: https://github.com/Stability-AI/stability-sdk/blob/main/src/proto/generation.proto This should enable users to build a client in any language that supports gRPC. We'll roll out code to demonstrate connecting in other languages soon. I'm hoping the protobuf spec will be enough to get the ball rolling for the time being though.

Thank you! I'm currently working on a client-side Swift implementation for iOS.

Update: Working like a charm!

May I ask how you achieved it? I want to inherit it into Macapp, which is also developed in Swift.

kakamove6 avatar Nov 15 '22 07:11 kakamove6

I would definitely echo @photopea's comment. A REST API would open up a lot of possibilities.

To give you some additional tangibles, here are other environments/projects where it's very difficult to implement gRPC:

  • Krita plugins (@imperator-maximus created his own python grpc implementation, because Krita doesn't allow 3rd party libs)
  • Blender add-ons (I jumped through a lot of hoops to create my own wrapper API for Stability SDK, because Blender doesn't have reliable/easy support for outside libs)
  • Any website, like others have discussed above

PS. I LOVE Stable Diffusion, and just want more developers to be able to integrate it!

see my detailed comment on this: https://github.com/Stability-AI/stability-sdk/discussions/51#discussioncomment-4171959 In short: grpc was a nice idea but it failed. so better use REST only in future.

imperator-maximus avatar Nov 23 '22 14:11 imperator-maximus

@all see this comment: https://github.com/Stability-AI/stability-sdk/discussions/51#discussioncomment-4218518

It sounds like JS will be much more easily supported soon!

benrugg avatar Nov 23 '22 17:11 benrugg

@svumme I've added precompiled stubs for javascript and typescript here: https://github.com/Stability-AI/stability-sdk/tree/main/src/js

I'll close this issue once we've got demo usage documented

broken link

wishcraft0 avatar Nov 27 '22 23:11 wishcraft0

REST API developer preview was just released! https://twitter.com/jcsabath/status/1597629332184145920

benrugg avatar Nov 29 '22 17:11 benrugg

Could you support CORS in your REST API? One website can not load a single byte of data (HTTP request) from another domain, if you dont allow it explicitly in CORS headers.

photopea avatar Dec 02 '22 16:12 photopea