Coder Social home page Coder Social logo

stability-ai / stability-sdk Goto Github PK

View Code? Open in Web Editor NEW
2.4K 63.0 335.0 113.91 MB

SDK for interacting with stability.ai APIs (e.g. stable diffusion inference)

Home Page: https://platform.stability.ai/

License: MIT License

Python 0.21% Jupyter Notebook 99.79%
stable-diffusion ai-art generative-art latent-diffusion multimodal

stability-sdk's Introduction

stability-sdk

Open In Colab

Client implementations that interact with the Stability API.

Getting an API key

Follow the instructions on Platform to obtain an API key.

PyPI Package Installation

Install the PyPI package via:

pip install stability-sdk

Python Client

client.py is both a command line client and an API class that wraps the gRPC based API. To try the client:

  • Use Python venv: python3 -m venv pyenv
  • Set up in venv dependencies: pyenv/bin/pip3 install -e .
  • pyenv/bin/activate to use the venv.
  • Set the STABILITY_HOST environment variable. This is by default set to the production endpoint grpc.stability.ai:443.
  • Set the STABILITY_KEY environment variable.

Then to invoke:

python3 -m stability_sdk generate -W 1024 -H 1024 "A stunning house."

It will generate and put PNGs in your current directory.

To upscale: python3 -m stability_sdk upscale -i "/path/to/image.png"

Animation UI

Install with pip install stability-sdk[anim_ui]

Then run with python3 -m stability_sdk animate --gui

SDK Usage

Be sure to check out Platform for comprehensive documentation on how to interact with our API.

Command line usage

usage: python -m stability_sdk generate [-h] [--height HEIGHT] [--width WIDTH] 
                [--start_schedule START_SCHEDULE] [--end_schedule END_SCHEDULE] 
                [--cfg_scale CFG_SCALE] [--sampler SAMPLER] [--steps STEPS] 
                [--style_preset STYLE_PRESET] [--seed SEED] [--prefix PREFIX] [--engine ENGINE]
                [--num_samples NUM_SAMPLES] [--artifact_types ARTIFACT_TYPES]
                [--no-store] [--show] [--init_image INIT_IMAGE] [--mask_image MASK_IMAGE]
                [prompt ...]

positional arguments:
  prompt

options:
  -h, --help            show this help message and exit
  --height HEIGHT, -H HEIGHT
                        [1024] height of image
  --width WIDTH, -W WIDTH
                        [1024] width of image
  --start_schedule START_SCHEDULE
                        [0.5] start schedule for init image (must be greater than 0; 1 is full strength
                        text prompt, no trace of image)
  --end_schedule END_SCHEDULE
                        [0.01] end schedule for init image
  --cfg_scale CFG_SCALE, -C CFG_SCALE
                        [7.0] CFG scale factor
  --sampler SAMPLER, -A SAMPLER
                        [auto-select] (ddim, plms, k_euler, k_euler_ancestral, k_heun, k_dpm_2,
                        k_dpm_2_ancestral, k_lms, k_dpmpp_2m, k_dpmpp_2s_ancestral)
  --steps STEPS, -s STEPS
                        [auto] number of steps
  --style_preset STYLE_PRESET
                        [none] (3d-model, analog-film, anime, cinematic, comic-book, digital-art, enhance, 
                        fantasy-art, isometric, line-art, low-poly, modeling-compound, neon-punk, origami, 
                        photographic, pixel-art, tile-texture)
  --seed SEED, -S SEED  random seed to use
  --prefix PREFIX, -p PREFIX
                        output prefixes for artifacts
  --artifact_types ARTIFACT_TYPES, -t ARTIFACT_TYPES
                        filter artifacts by type (ARTIFACT_IMAGE, ARTIFACT_TEXT, ARTIFACT_CLASSIFICATIONS, etc)
  --no-store            do not write out artifacts
  --num_samples NUM_SAMPLES, -n NUM_SAMPLES
                        number of samples to generate
  --show                open artifacts using PIL
  --engine ENGINE, -e ENGINE
                        engine to use for inference
  --init_image INIT_IMAGE, -i INIT_IMAGE
                        Init image
  --mask_image MASK_IMAGE, -m MASK_IMAGE
                        Mask image

For upscale:

usage: client.py upscale
       [-h]
       --init_image INIT_IMAGE
       [--height HEIGHT] [--width WIDTH] [--prefix PREFIX] [--artifact_types ARTIFACT_TYPES]
       [--no-store] [--show] [--engine ENGINE]

positional arguments:
  prompt (ignored in esrgan engines)

options:
  -h, --help            show this help message and exit
  --init_image INIT_IMAGE, -i INIT_IMAGE
                        Init image
  --height HEIGHT, -H HEIGHT
                        height of upscaled image in pixels
  --width WIDTH, -W WIDTH
                        width of upscaled image in pixels
  --steps STEPS, -s STEPS
                        [auto] number of steps (ignored in esrgan engines)
  --seed SEED, -S SEED  random seed to use (ignored in esrgan engines)
  --cfg_scale CFG_SCALE, -C CFG_SCALE
                        [7.0] CFG scale factor (ignored in esrgan engines)
  --prefix PREFIX, -p PREFIX
                        output prefixes for artifacts
  --artifact_types ARTIFACT_TYPES, -t ARTIFACT_TYPES
                        filter artifacts by type (ARTIFACT_IMAGE, ARTIFACT_TEXT, ARTIFACT_CLASSIFICATIONS, etc)
  --no-store            do not write out artifacts
  --show                open artifacts using PIL
  --engine ENGINE, -e ENGINE
                        engine to use for upscale
  

Connecting to the API using languages other than Python

If a language you would like to connect to the API with is not currently documented on Platform you can use the following protobuf definition to compile stubs for your language:

Community-contributed clients

Stability API TOS

Usage of the Stability API falls under the STABILITY AI API Terms of Service.

stability-sdk's People

Contributors

chigozienri avatar christophervalore avatar dmarx avatar enzymezoo-code avatar frankwinfaber avatar github-actions[bot] avatar jacklangerman avatar jamalex avatar johnsabath avatar langner avatar palp avatar pharmapsychotic avatar satsumas avatar sayanarijit avatar wbrown avatar zippy731 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

stability-sdk's Issues

get account balance info from token

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?

JS SDK?

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.

error: Must provide image parameters

Started getting this error after this commit was merged.

(.venv) ➜  stability-sdk git:(main) ✗ python3 -m stability_sdk.client -W 512 -H 512 "A stunning house."
2022-09-20 12:38:58,908 INFO client.py(100811) - Opening channel to grpc.stability.ai:443
2022-09-20 12:38:58,911 INFO client.py(100811) - Channel opened to grpc.stability.ai:443
2022-09-20 12:38:58,911 INFO client.py(100811) - Sending request.
Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/home/sayanarijit/Documents/GitHub/stability-sdk/src/stability_sdk/client.py", line 469, in <module>
    for artifact in artifacts:
  File "/home/sayanarijit/Documents/GitHub/stability-sdk/src/stability_sdk/client.py", line 99, in process_artifacts_from_answers
    for resp in answers:
  File "/home/sayanarijit/Documents/GitHub/stability-sdk/src/stability_sdk/client.py", line 308, in generate
    for answer in self.stub.Generate(rq, **self.grpc_args):
  File "/home/sayanarijit/Documents/GitHub/stability-sdk/.venv/lib/python3.10/site-packages/grpc/_channel.py", line 426, in __next__
    return self._next()
  File "/home/sayanarijit/Documents/GitHub/stability-sdk/.venv/lib/python3.10/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:---------- {grpc_message:"Must provide image parameters.", grpc_status:3, created_time:"2022-09-20T12:38:59.84627592+05:30"}"
>

Found the cause to be this change: 10c6865#diff-63cf1f93510f4408aad0aea8b6b26f505c4edfcc8735c273f17966cb7a8380dbL170-L174

`protobuf` version range too tight on `ec2` instance

Error message:

ERROR: Could not find a version that satisfies the requirement protobuf<4,>=3.20.2 (from stability-sdk) (from versions: 2.0.0b0, 2.0.3, 2.3.0, 2.4.1, 2.5.0, 2.6.0, 2.6.1, 3.0.0a2, 3.0.0a3, 3.0.0b1, 3.0.0b1.post1, 3.0.0b1.post2, 3.0.0b2, 3.0.0b2.post1, 3.0.0b2.post2, 3.0.0b3, 3.0.0b4, 3.0.0, 3.1.0, 3.1.0.post1, 3.2.0rc1, 3.2.0rc1.post1, 3.2.0rc2, 3.2.0, 3.3.0, 3.4.0, 3.5.0.post1, 3.5.1, 3.5.2, 3.5.2.post1, 3.6.0, 3.6.1, 3.7.0rc2, 3.7.0rc3, 3.7.0, 3.7.1, 3.8.0rc1, 3.8.0, 3.9.0rc1, 3.9.0, 3.9.1, 3.9.2, 3.10.0rc1, 3.10.0, 3.11.0rc1, 3.11.0rc2, 3.11.0, 3.11.1, 3.11.2, 3.11.3, 3.12.0rc1, 3.12.0rc2, 3.12.0, 3.12.1, 3.12.2, 3.12.4, 3.13.0rc3, 3.13.0, 3.14.0rc1, 3.14.0rc2, 3.14.0rc3, 3.14.0, 3.15.0rc1, 3.15.0rc2, 3.15.0, 3.15.1, 3.15.2, 3.15.3, 3.15.4, 3.15.5, 3.15.6, 3.15.7, 3.15.8, 3.16.0rc1, 3.16.0rc2, 3.16.0, 3.17.0rc1, 3.17.0rc2, 3.17.0, 3.17.1, 3.17.2, 3.17.3, 3.18.0rc1, 3.18.0rc2, 3.18.0, 3.18.1, 3.18.3, 3.19.0rc1, 3.19.0rc2, 3.19.0, 3.19.1, 3.19.2, 3.19.3, 3.19.4, 3.19.5, 3.19.6, 4.0.0rc1, 4.0.0rc2, 4.21.0rc1, 4.21.0rc2, 4.21.0)
ERROR: No matching distribution found for protobuf<4,>=3.20.2

To reproduce, cd to the repository on an ec2 instance and run,

pip3 install -e .

proposed solution: drop ,<4.

StatusCode.INVALID_ARGUMENT----- details = "Must provide image parameters"

I have not touched my application's code but recently within the past hours it has stopped working. I get this error message when I send in request using the python modules
<_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.21.212:443 {created_time:"2022-10-13T23:50:58.054306143+00:00", grpc_status:3, grpc_message:"Must provide image parameters."}"
>

This error shows up regardless of whether or not I provide an init_image to the client object.

Error - Output name too long

Hi guys,

On windows system which doesn't support long path, an error is raised:
with open(out_p, "wb") as f: OSError: [Errno 22] Invalid argument: 'X-a43072aa-5c60-4d71-a91d-e89bd0d549e1:0:0-0774785a-830f-4f79-b4a3-e84054e8e194-0.png'\r\n"

The filename is defined here,
artifact_p = f"{prefix}-{resp.request_id}-{resp.answer_id}-{idx}"
where ids are uuid (36 chars).

In addition, for multiple requests, there is no way to map request results with initial prompts.

It would be shorter and more useful to use a format like
f"{prefix}-{request_idx}-{response_idx}",
where request_idx = prompt position:

python3 client.py -W 512 -H 512 -n 2 "A stunning house." "A creepy house."
-> generation-0-0.png, fisrt stunning house
-> generation-0-1.png, second stunning  house
-> generation-1-0.png, fisrt creepy house
-> generation-1-1.png, second creepy house

sometimes execution takes a lot of time to respond and causes failure

Hi,
Good day. Thank you for this fantastic tool.
we're using your package https://github.com/Stability-AI/stability-sdk (stability-sdk version: 0.2.4) in our Django project on the ubuntu virtual machine using the apache2 webserver but sometimes when we try to generate the image then it takes much time to generate and also sometimes execution takes a lot of time that causes the internal server error and our server not responding for the next 10 minutes
can you explain why it's happened and how to avoid it?

stability_api = client.StabilityInference(key=settings.AI_IMAGE_API_KEY, verbose=True,)
answers = stability_api.generate(prompt=title, seed=34632, steps=25, samples=2,)
 for resp in answers:
       for artifact in resp.artifacts:
             if artifact.finish_reason == generation.FILTER:
                     return Response({"message":"Please modify the prompt and try again"},status=status.HTTP_400_BAD_REQUEST)
              if artifact.type == generation.ARTIFACT_IMAGE:
                      im = Image.open(BytesIO(artifact.binary))

Safety filters getting activated whenever init_image parameter is used

Whenever I add the init_image parameter to the client.StabilityInference.generate method, the finish reason: FILTER gets activated.

This is how I'm reading in the image and passing it into

# Read base images
base = Image.open("base_images/cat.jpg")
base = base.resize((512, 512))

answers = stability_api.generate(
    prompt="A cat with a crown",
    init_image=base
    steps=50
    cfg_scale=15,
    start_schedule=0.01,
)

No matter what image/prompt I use, this safety filter always gets activated. I've tried different image formats too. I also get the NSFW filter when using images from SD.

I can't replicate the content filter warnings when running the same prompt/params in DreamStudio

js/ts sdk is outdated

just found out that we don't have ScheduleParameters used for init image on the js/ts SDK, thus we can't use correctly init image with the JS/TS image

"14 UNAVAILABLE: failed to connect to all addresses" When Using BloomRPC

I'm trying to connect to the gRPC server using BloomRPC.

I imported generation.proto, set the url to "grpc.stability.ai:443", and enabled TLS by selecting "Server Certificate"

In the Metadata, I added "Authorization : Bearer [my api key]".

BloomRPC automatically generates default values for the request, the only one I changed was setting engine_id to "stable-diffusion-v1-5"

The response I get is:
{ "error": "14 UNAVAILABLE: failed to connect to all addresses" }

stability-bloom

I'm new to gRPC. Any ideas would be much appreciated!

UNKNOWN:Illegal header value

`E0913 23:00:44.799000000 8924 src/core/lib/security/credentials/plugin/plugin_credentials.cc:86] validate_metadata_from_plugin: UNKNOWN:Illegal header value {raw_bytes:"42 65 61 72 65 72 20 16 'Bearer .'\x00", offset:7, created_time:"2022-09-13T20:00:44.7994232+00:00"}
E0913 23:00:44.804000000 8924 src/core/lib/security/credentials/plugin/plugin_credentials.cc:87] Plugin added invalid metadata value.
Traceback (most recent call last):
File "C:\Users\onyx\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\onyx\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in run_code
exec(code, run_globals)
File "c:\Users\onyx.vscode\extensions\ms-python.python-2022.14.0\pythonFiles\lib\python\debugpy_main
.py", line 39, in
cli.main()
File "c:\Users\onyx.vscode\extensions\ms-python.python-2022.14.0\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 430, in main
run()
File "c:\Users\onyx.vscode\extensions\ms-python.python-2022.14.0\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 284, in run_file
runpy.run_path(target, run_name="main")
File "c:\Users\onyx.vscode\extensions\ms-python.python-2022.14.0\pythonFiles\lib\python\debugpy_vendored\pydevd_pydevd_bundle\pydevd_runpy.py", line 321, in run_path
return _run_module_code(code, init_globals, run_name,
File "c:\Users\onyx.vscode\extensions\ms-python.python-2022.14.0\pythonFiles\lib\python\debugpy_vendored\pydevd_pydevd_bundle\pydevd_runpy.py", line 135, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "c:\Users\onyx.vscode\extensions\ms-python.python-2022.14.0\pythonFiles\lib\python\debugpy_vendored\pydevd_pydevd_bundle\pydevd_runpy.py", line 124, in _run_code
exec(code, run_globals)
File "c:\Users\onyx\a\ali\runScript.py", line 35, in
for resp in answers:
File "c:\Users\onyx\a\ali\lib\site-packages\stability_sdk\client.py", line 300, in generate
for answer in self.stub.Generate(rq, **self.grpc_args):
File "c:\Users\onyx\a\ali\lib\site-packages\grpc_channel.py", line 426, in next
return self._next()
File "c:\Users\onyx\a\ali\lib\site-packages\grpc_channel.py", line 826, in _next
raise self
grpc._channel._MultiThreadedRendezvous: <_MultiThreadedRendezvous of RPC that terminated with:
status = StatusCode.UNAVAILABLE
details = "Illegal metadata"
debug_error_string = "UNKNOWN:Error received from peer grpc.stability.ai:443 {grpc_message:"Illegal metadata", grpc_status:14, created_time:"2022-09-13T20:00:44.8064471+00:00"}"

`

I receive this error ? how to fix ?

Having trouble installing and running SDK on server

Hi guys,

I have installed the SDK with pip install stability-sdk which was done succesfully:

Running the command again seems to show that everything is ok:

pip install stability-sdk
Requirement already satisfied: stability-sdk in /usr/local/lib/python2.7/dist-packages
Requirement already satisfied: python-magic in /usr/local/lib/python2.7/dist-packages (from stability-sdk)
Requirement already satisfied: grpcio-tools in /usr/local/lib/python2.7/dist-packages (from stability-sdk)
Requirement already satisfied: python-dotenv in /usr/local/lib/python2.7/dist-packages (from stability-sdk)
Requirement already satisfied: Pillow in /usr/local/lib/python2.7/dist-packages (from stability-sdk)
Requirement already satisfied: grpcio in /usr/local/lib/python2.7/dist-packages (from stability-sdk)
Requirement already satisfied: setuptools in /usr/lib/python2.7/dist-packages (from grpcio-tools->stability-sdk)
Requirement already satisfied: protobuf<4.0dev,>=3.5.0.post1 in /usr/local/lib/python2.7/dist-packages (from grpcio-tools->stability-sdk)
Requirement already satisfied: typing; python_version < "3.5" in /usr/local/lib/python2.7/dist-packages (from python-dotenv->stability-sdk)
Requirement already satisfied: enum34>=1.0.4; python_version < "3.4" in /usr/lib/python2.7/dist-packages (from grpcio->stability-sdk)
Requirement already satisfied: six>=1.5.2 in /usr/lib/python2.7/dist-packages (from grpcio->stability-sdk)
Requirement already satisfied: futures>=2.2.0; python_version < "3.2" in /usr/local/lib/python2.7/dist-packages (from grpcio->stability-sdk)

I then created a file called execute.py and copied the code from the google collab script and tried executing it with python execute.py

At first it threw an error:

python execute.py
Enter your API Key
Traceback (most recent call last):
  File "execute.py", line 12, in <module>
    from IPython.display import display
ImportError: No module named IPython.display

I then installed ipython with pip install ipython

Now when i execute python execute.py

Enter your API Key
Traceback (most recent call last):
  File "execute.py", line 14, in <module>
    from stability_sdk import client
  File "/usr/local/lib/python2.7/dist-packages/stability_sdk/client.py", line 30
    algorithms: Dict[str, int] = {
              ^
SyntaxError: invalid syntax

I would rather use the SDK directly from a command line, but your instructions on github are very vague, you say to install the package with PIP then you run commands on a file called client.py with PIP3 but there is no client.py, only if you pull the repo and go to src....its very confusing.

Can you guys please help?

Thank you,

Alex

Using API via Python xmlrpclib

The client application I try to use the API with is Python 2 based, so I can't use the SDK.

I try to open a channel via xmlrpclib.ServerProxy('https://grpc.stability.ai:443'). When I try to call a method, I get an error 404. Any hints how to solve this?

Is RPC the only way to use the API?

Thanks!

Protofile not up to date, other issues

Hello,

you said that people should using generation.proto but this does not work to be honest. It is not based on your current API.
So many very important parameters are missing here:

  • scheduleParameters (used in Python client)
  • mask artifact type

Also in PromptParameters there is a "weight" float which nobody know how to use it. It does not make sense.
There is also no masking example and juest with wild guesses there will garbage results.
Using "improbable-eng/grpc-web" in your example JS is also a not so good idea because it just does not work in every browser environment (official grpc-web has no issues). Your example Colab does not use current API as well.

I am on a tight deadline and so your client API is useless for me at the moment. Can you provide a contact person in your company who can help the find the correct parameters - please contact me on Twitter: https://twitter.com/NicolayMausz
Thank you very much!

Questions about the parameters and their relation to options available on the dreamstudio website

Good day. Thank you for this fantastic tool. I am trying to adapt the sdk to my javascript based project but I have some doubts about how to replicate certain functionalities available in dreamstudio web related to img2img.

In dreamstudio when we add an image we can define the strength parameter; however in the sdk I don't see that parameter. I understand that it is related to start and end schedule. But I would like to know the exact relation. For example, if the strength on the web is 20%, what values would have the start and end schedule?

Additionally in the sdk there is the parameter init_image and mask_image. I don't understand what is the functionality of the mask_image and if I should pass it the same value as init_image or they are different values. What functionality of dreamstudio uses the mask_image?

Thank you very much for any help you can give me.

Error generating image on Windows and Linux

I'm just testing out the SDK and I'm getting a different error on Windows and Linux.

I got OSError: [Errno 22] Invalid argument on Windows so I figured it must be Windows directories, so I installed Linuxmint and gave it a shot only to get this error TypeError: __call__() got an unexpected keyword argument 'wait_for_ready'

Windows

PS C:\Users\User\Desktop\Stability\stability-sdk-main\src\stability_sdk> python client.py -W 512 -H 512 "A stunning house."
2022-08-26 15:50:59,819 INFO client.py(7812) - Opening channel to grpc.stability.ai:443
2022-08-26 15:50:59,823 INFO client.py(7812) - Channel opened to grpc.stability.ai:443
2022-08-26 15:50:59,824 INFO client.py(7812) - Sending request.
2022-08-26 15:51:05,177 INFO client.py(7812) - Got keepalive 8e0e8b37-ab2f-48b7-bbd3-7cf8ee7cb3ef in 5.35s
2022-08-26 15:51:05,877 INFO client.py(7812) - Got d5b78459-bbc5-4560-a467-61eb47bbd73d with ['ARTIFACT_IMAGE', 'ARTIFACT_CLASSIFICATIONS'] in 0.70s
Traceback (most recent call last):
  File "client.py", line 374, in <module>
    for artifact in artifacts:
  File "client.py", line 92, in process_artifacts_from_answers
    with open(out_p, "wb") as f:
OSError: [Errno 22] Invalid argument: 'generation-ee77115a-ce17-48de-ac67-9ace16ba0d0f:0:0-d5b78459-bbc5-4560-a467-61eb47bbd73d-0.png'

Linuxmint

~/Desktop/stability-sdk-main/src/stability_sdk$ python3 client.py -W 512 -H 512 "A stunning house."
2022-08-26 15:58:11,459 INFO client.py(1827) - Opening channel to grpc.stability.ai:443
2022-08-26 15:58:11,461 INFO client.py(1827) - Channel opened to grpc.stability.ai:443
2022-08-26 15:58:11,461 INFO client.py(1827) - Sending request.
Traceback (most recent call last):
  File "client.py", line 374, in <module>
    for artifact in artifacts:
  File "client.py", line 75, in process_artifacts_from_answers
    for resp in answers:
  File "client.py", line 246, in generate
    for answer in self.stub.Generate(rq, **self.grpc_args):
TypeError: __call__() got an unexpected keyword argument 'wait_for_ready'

wrapping client.generate in an ASGI server

I'm using FastAPI + uvicorn with the following dummy endpoint:

@dummy_endpoint_wrapper
async def generate_images():
    # some code omitted
    stability_api = client.StabilityInference(
        key=Config.STABILITY_KEY, 
        verbose=True,
    )
    body = {
        "prompt": "dog on a beach",
        "height": 512,
        "width": 512,
        "cfg_scale": 7.5,
        "sampler": 6,
        "seed": 0,
        "steps": 10,
        "samples": 1,
    }
    stability_stub = stability_api.generate(**body)
    for resp in stability_stub:
        image = None
        print(resp)
        print(resp.artifacts)
        for artifact in resp.artifacts:
            if artifact.finish_reason == generation.FILTER:
                raise ValueError("NSFW triggered")
            if artifact.type == generation.ARTIFACT_IMAGE:
                image = artifact.binary
        if image is None:
            raise ValueError("No image found in response artifacts of stability request")


    # raises the image = None value error

The artifacts are also empty.

Update:
Ok so with the help from Freon on the discord servers, it actually seems to be something because of async handling rather than cors.

Ok so it seemed I just needed to consume the generator.

    images = []
    for resp in stability_stub:
        for artifact in resp.artifacts:
            if artifact.finish_reason == generation.FILTER:
                raise ValueError("NSFW triggered")
            if artifact.type == generation.ARTIFACT_IMAGE:
                images.append(artifact.binary)

then use images

Same generation.proto error as before

It might be that I am doing something wrong but I am getting the same error as 3 days ago:

TypeError: Couldn't build proto file into descriptor pool! Invalid proto descriptor for file "generation.proto": generation.proto: A file with this name is already in the pool.
I was really hoping to implement img to img to my application I am developing. Is it that the api interfaces hadn't yet been incorporated to sdk? I just assumed it was since the sdk was updated yesterday.

Thank you in advance for your help.

Images generated are extremely poor

I'm playing with all parameters, including engine, steps, cfg... also trying with defaults:

python3 -m stability_sdk.client -W 512 -H 512 "A stunning house"

Results:
image

Other examples from my app:
image

init-img support missing from python client

There doesn't seem to be a key for passing in an init image in the stability_api.generate function. I believe it's supported when calling client.py from the command line so would expect it here, too.

Am I missing something obvious?

The API for Image Generation has huge discrepancies between beta.dreamstudio.ai, init_image is discarded

Original Image:
test1

Web UI Settings (https://beta.dreamstudio.ai/dream):
wdith/height: 512x512
cfg_scale: 7
steps: 30
sampler: k_lms
model: Stable Difussion 1.5
Seed: 2306870202
prompt: lunar landscape

web ui output:
dreambeta_variant

API call settings:
python stability-sdk==0.2.3

	img = Image.open(path)
	answers = stability_api.generate(
	    prompt="lunar landscape",
	    init_image=img,
	    mask_image=img,
	    width=512,
	    height=512,
	    seed=2306870202,
	    # safety=False,
	    steps=30, # defaults to 50 if not specified
	    cfg_scale=7.0,
	)

API Call Output:
api_image

API Call Output with mask_image=None
api_image_no_mask

For some reason, all API calls discard the init_image, and just use the mask_image parameter

I can't seem to make the API calls generate images based on the init_image parameter, there is a big discrepancy between the WebUI and the Library API.

Blurred results

I've noticed that sometimes, some returned samples are completely blurred. This happens in DreamStudio and with client.py

See an example generated with -C 12 --show -n 4 -s 10 'lovecraft holding a book, 16 bit color, game art, pixelated'
generation-7b937860-572a-401d-9d87-3f413b693184:0:1-9f7f5c75-f2ce-4828-81c3-4f8beee5d246-6

pip freeze
grpcio==1.47.0
grpcio-tools==1.47.0
Pillow==9.2.0
protobuf==3.20.1
python-dotenv==0.20.0
python-magic==0.4.27
six==1.16.0
stability-sdk==0.1.1

Is this an expected behavior?

gRPC multithreaded rendezvous error

[/usr/local/lib/python3.7/dist-packages/vktrs/api.py](https://localhost:8080/#) in get_image_for_prompt(prompt, max_retries, **kargs)
     23             response = process_response(answers)
     24 
---> 25             for img in response:
     26 
     27                 yield img

[/usr/local/lib/python3.7/dist-packages/vktrs/api.py](https://localhost:8080/#) in process_response(answers)
     37 
     38     # iterating over the generator produces the api response
---> 39     for resp in answers:
     40 
     41         for artifact in resp.artifacts:

[/usr/local/lib/python3.7/dist-packages/stability_sdk/client.py](https://localhost:8080/#) in generate(self, prompt, init_image, mask_image, height, width, start_schedule, end_schedule, cfg_scale, sampler, steps, seed, samples, safety, classifiers)
    298 
    299         start = time.time()
--> 300         for answer in self.stub.Generate(rq, **self.grpc_args):
    301             duration = time.time() - start
    302             if self.verbose:

[/usr/local/lib/python3.7/dist-packages/grpc/_channel.py](https://localhost:8080/#) in __next__(self)
    424 
    425     def __next__(self):
--> 426         return self._next()
    427 
    428     def _next(self):

[/usr/local/lib/python3.7/dist-packages/grpc/_channel.py](https://localhost:8080/#) in _next(self)
    824                     raise StopIteration()
    825                 elif self._state.code is not None:
--> 826                     raise self
    827 
    828 

_MultiThreadedRendezvous: <_MultiThreadedRendezvous of RPC that terminated with:
	status = StatusCode.INTERNAL
	details = "Received http2 header with status: 400"
	debug_error_string = "UNKNOWN:Error received from peer ipv4:104.18.21.212:443 {grpc_message:"Received http2 header with status: 400", grpc_status:13, created_time:"2022-09-27T18:00:39.653236233+00:00"}"
>

There is a issue with python dotenv restricting Azure Function usage with this SDK

Stability SDK is relying on python-dotenv package which is having or causing issue with its implementation as mentioned in the dotenv github issues here.

It would be helpful to overcome and fix this for the SDK.

Main issue:

Result: Failure Exception: ModuleNotFoundError: No module named 'main' Stack: File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 448, in _handle__invocation_request call_result = await self._loop.run_in_executor( File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 691, in _run_sync_func return ExtensionManager.get_sync_invocation_wrapper(context, File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/extension.py", line 215, in _raw_invocation_wrapper result = function(**args) File "/home/site/wwwroot/HttpTrigger_jetson_01_test/init.py", line 43, in main container_client = adls.get_container_client(place_name) File "/home/site/wwwroot/adls.py", line 14, in get_container_client dotenv.load_dotenv() File "/home/site/wwwroot/.python_packages/lib/site-packages/dotenv/main.py", line 322, in load_dotenv dotenv_path = find_dotenv() File "/home/site/wwwroot/.python_packages/lib/site-packages/dotenv/main.py", line 275, in find_dotenv if usecwd or _is_interactive() or getattr(sys, 'frozen', False): File "/home/site/wwwroot/.python_packages/lib/site-packages/dotenv/main.py", line 272, in _is_interactive main = import('main', None, None, fromlist=['file'])

the latest version code doesn’t work on my mac

After I pull the latest code (from main branch), I execute the following command, then i got this error

  • generation.proto: A file with this name is already in the pool.

image

Then I tried the following commands to solve the problem

  • pip uninstall protobuf
  • pip install --no-binary protobuf protobuf

after, run 'python3 client.py -W 512 -H 512 "superman"', then i got this error

  • Traceback (most recent call last):
    File "client.py", line 26, in
    import stability_sdk.interfaces.gooseai.generation.generation_pb2_grpc as generation_grpc
    File "/root/miniconda3/lib/python3.8/site-packages/stability_sdk/interfaces/gooseai/generation/generation_pb2_grpc.py", line 5, in
    import generation_pb2 as generation__pb2
    File "/root/code/stability-sdk/src/stability_sdk/interfaces/gooseai/generation/generation_pb2.py", line 75, in
    _SCHEDULEPARAMETERS = DESCRIPTOR.message_types_by_name['ScheduleParameters']
    KeyError: 'ScheduleParameters'
    image

What is the cause of the problem? How to fix it?

README.md has not updated the description of init_img, when will the description of the latest version be updated?

Looking forward to your reply. thanks

Not getting PNG output from command line client, but long-name empty files instead

The client.py isnt outputting PNGs in the directory like the instructions say. Note, i'm running on an Anaconda env.

image

I run the above, and it outputs the below

image

super long file name, not long enough to include the .json bit at the end, looks like its an empty file, generally not sure what to do with this, the instructions are unclear. Please advise.

Example in notebook throws INVALID_ARGUMENT error

Hello,

The code snippet that I was using was working up to 2 weeks ago that I checked and it is almost identical with the example notebook.

import os
import io
import warnings

from PIL import Image
from stability_sdk import client
import stability_sdk.interfaces.gooseai.generation.generation_pb2 as generation
from dotenv import load_dotenv

load_dotenv()


def main():
    stability_api = client.StabilityInference(
        key=os.environ["STABILITY_KEY"],
        verbose=True,
    )
    answers = stability_api.generate(
        prompt="houston, we are a 'go' for launch!",
        seed=34567,  # if provided, specifying a random seed makes results deterministic
        steps=30,  # defaults to 50 if not specified
    )
    for resp in answers:
        for artifact in resp.artifacts:
            if artifact.finish_reason == generation.FILTER:
                return warnings.warn(
                    "Your request activated the API's safety filters and \
              could not be processed."
                    "Please modify the prompt and try again."
                )
            if artifact.type == generation.ARTIFACT_IMAGE:
                image_name = "rocket"
                path = os.environ("IMAGE_DIR") + image_name + ".png"
                img = Image.open(io.BytesIO(artifact.binary))
                img.save(path)
                print("done")


if __name__ == "__main__":
    main()

I believed that the only parameter that you need to input is the prompt. It throws the error:

Traceback (most recent call last):
File "/Users/blackbak/Github/Suede/Exploratory/stabilityapi.py", line 42, in
main()
File "/Users/blackbak/Github/Suede/Exploratory/stabilityapi.py", line 23, in main
for resp in answers:
File "/opt/anaconda3/envs/suede/lib/python3.9/site-packages/stability_sdk/client.py", line 300, in generate
for answer in self.stub.Generate(rq, **self.grpc_args):
File "/opt/anaconda3/envs/suede/lib/python3.9/site-packages/grpc/_channel.py", line 426, in next
return self._next()
File "/opt/anaconda3/envs/suede/lib/python3.9/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.21.212:443 {created_time:"2022-10-21T13:21:01.529499-04:00", grpc_status:3, grpc_message:"Must provide image parameters."}"

img2img?

Hi there, confused about this, does the stability ai API have params that can be set for img2img generation (provide image + prompt as input)?

If not what would be the best option to setup an API for that, this: https://github.com/hlky/stable-diffusion ?

Expected size 24 but got size 23 for tensor number 1 in the list.

Hello I am using the stability-sdk installed from pip: stability-sdk==0.2.3,

I am trying to follow the jupyter notebook examples and generate images based on an init_image and I encounter the following exception:

  File "/home/marius/.local/lib/python3.10/site-packages/stability_sdk/client.py", line 300, in generate
    for answer in self.stub.Generate(rq, **self.grpc_args):
  File "/home/marius/.local/lib/python3.10/site-packages/grpc/_channel.py", line 426, in __next__
    return self._next()
  File "/home/marius/.local/lib/python3.10/site-packages/grpc/_channel.py", line 826, in _next
    raise self
grpc._channel._MultiThreadedRendezvous: <_MultiThreadedRendezvous of RPC that terminated with:
	status = StatusCode.UNKNOWN
	details = "Exception iterating responses: Sizes of tensors must match except in dimension 1. Expected size 24 but got size 23 for tensor number 1 in the list."
	debug_error_string = "UNKNOWN:Error received from peer ipv4:104.18.20.212:443 {created_time:"2022-09-27T08:20:28.458839484+02:00", grpc_status:2, grpc_message:"Exception iterating responses: Sizes of tensors must match except in dimension 1. Expected size 24 but got size 23 for tensor number 1 in the list."}"

The Image I use is a jpg frame from a mp4 video.

The code snippet I use to generate the Image is:

	img = Image.open(path)
	answers = stability_api.generate(
	    prompt=text,
	    init_image=img,
	    steps=30, # defaults to 50 if not specified
	)
	name=path.split('/')[-1]
	for resp in answers:
	    for artifact in resp.artifacts:
	        if artifact.finish_reason == generation.FILTER:
	            warnings.warn(
	                "Your request activated the API's safety filters and could not be processed."
	                "Please modify the prompt and try again.")
	        if artifact.type == generation.ARTIFACT_IMAGE:
	            img = Image.open(io.BytesIO(artifact.binary))
	            img.save(output+"/"+text+name+".jpg")

What are the constraints my images need to follow in order for this generation to succeed?

I have tried the image via the web gui of dreamstudio and it worked flawless!

client.py usage disagrees with generation.proto from latest version from api-interfaces

generation.proto from api-interfaces has ImageParameters and ClassifierParameters in a oneof (https://github.com/Stability-AI/api-interfaces/blob/main/src/proto/generation.proto#L177)

However client.py sets ClassifierParameters by default, meaning the ImageParameters get ignored (https://github.com/Stability-AI/stability-sdk/blob/main/src/stability_sdk/client.py#L237)

(Since there isn't anyone raising a bug about ImageParameters not working, does the current live server not use the latest version of api-interfaces?)

Specifying the seed value results in an error

Specifying the seed value does not work at the moment and results in an exception inside protobuf:

File "/Users/michael/Projects/genv/dreamstudio/venv/lib/python3.9/site-packages/google/protobuf/internal/containers.py", line 166, in extend
elem_seq_iter = iter(elem_seq)
TypeError: 'int' object is not iterable

As protobuf always expects an interable (there is a long standing TODO item at the crash site) this can be trivially fixed by adding an else case at https://github.com/Stability-AI/stability-sdk/blob/main/src/stability_sdk/client.py#L213 that wraps the seed value in a list.

    if not seed:
        seed = [random.randrange(0, 4294967295)]
    else:
        seed = [seed]

Because even the latest version of protobuf does not support this union handling properly, the line at https://github.com/Stability-AI/stability-sdk/blob/main/src/stability_sdk/client.py#L185 could be simplified to:

    seed: Sequence[int] = (0,),

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.