### Install Stability SDK Source: https://platform.stability.ai/docs/features/text-to-image?tab=typescript Install the Stability SDK package using pip. This is the first step to using the SDK. ```bash pip install stability-sdk ``` -------------------------------- ### Image-to-Image Transformation Setup Source: https://platform.stability.ai/docs/features/image-to-image?tab=typescript Prepare to modify an existing image using a new prompt. This involves setting up an initial image based on a previous generation or a provided source. ```python ``` -------------------------------- ### Setup Environment Variables and API Key Source: https://platform.stability.ai/docs/features/text-to-image?tab=typescript Import necessary dependencies and configure environment variables for the Stability AI API, including the host URL and API key. Ensure your API key is kept secure. ```python 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 os.environ['STABILITY_HOST'] = 'grpc.stability.ai:443' os.environ['STABILITY_KEY'] = 'key-goes-here' ``` -------------------------------- ### Multi-prompt text-to-image parameters Source: https://platform.stability.ai/docs/features/multi-prompting Configure parameters for multi-prompt text-to-image generation, allowing for weighted prompts and negative prompting to guide the image generation process. ```python # Note: With multi-prompting, we can provide each prompt a specific weight. Negative weights are used to prompt the model to avoid certain concepts. ``` -------------------------------- ### Import Dependencies and Set Up Environment Source: https://platform.stability.ai/docs/features/inpainting?tab=typescript Import necessary libraries and configure environment variables for API key and host. Ensure your API key is kept secure. ```python 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 torchvision.transforms import GaussianBlur # Our host url should not be prepended with "https" nor should it have a trailing slash. os.environ['STABILITY_HOST'] = 'grpc.stability.ai:443' # Sign up for an account at the following link to get an API Key. # https://platform.stability.ai/ # Click on the following link once you have created an account to be taken to your API Key. # https://platform.stability.ai/account/keys # Paste your API Key below. os.environ['STABILITY_KEY'] = 'apikeyhere' ``` -------------------------------- ### Set Up Environment Variables and API Key Source: https://platform.stability.ai/docs/features/image-to-image?tab=typescript Import dependencies, configure API host, and set your Stability AI API key using environment variables. Ensure your API key is kept secure. ```python import io import os import warnings from PIL import Image from stability_sdk import client import stability_sdk.interfaces.gooseai.generation.generation_pb2 as generation # Our Host URL should not be prepended with "https" nor should it have a trailing slash. os.environ['STABILITY_HOST'] = 'grpc.stability.ai:443' # Sign up for an account at the following link to get an API Key. # https://platform.stability.ai/ # Click on the following link once you have created an account to be taken to your API Key. # https://platform.stability.ai/account/keys # Paste your API Key below. os.environ['STABILITY_KEY'] = 'key-goes-here' ``` -------------------------------- ### Generate Image with Parameters Source: https://platform.stability.ai/docs/features/text-to-image?tab=typescript Set up initial generation parameters for text-to-image generation, including prompt, seed, steps, CFG scale, dimensions, and sampler. The code also includes logic to save the generated image and warn if safety filters are triggered. ```python # Set up our initial generation parameters. answers = stability_api.generate( prompt="expansive landscape rolling greens with gargantuan yggdrasil, intricate world-spanning roots towering under a blue alien sky, masterful, ghibli", seed=4253978046, # If a seed is provided, the resulting generated image will be deterministic. # What this means is that as long as all generation parameters remain the same, you can always recall the same image simply by generating it again. # Note: This isn't quite the case for Clip Guided generations, which we'll tackle in a future example notebook. steps=50, # Amount of inference steps performed on image generation. Defaults to 30. cfg_scale=8.0, # Influences how strongly your generation is guided to match your prompt. # Setting this value higher increases the strength in which it tries to match your prompt. # Defaults to 7.0 if not specified. width=1024, # Generation width, defaults to 512 if not included. height=1024, # Generation height, defaults to 512 if not included. samples=1, # Number of images to generate, defaults to 1 if not included. sampler=generation.SAMPLER_K_DPMPP_2M # Choose which sampler we want to denoise our generation with. # Defaults to k_dpmpp_2m if not specified. Clip Guidance only supports ancestral samplers. # (Available Samplers: ddim, plms, k_euler, k_euler_ancestral, k_heun, k_dpm_2, k_dpm_2_ancestral, k_dpmpp_2s_ancestral, k_lms, k_dpmpp_2m, k_dpmpp_sde) ) # Set up our warning to print to the console if the adult content classifier is tripped. # If adult content classifier is not tripped, save generated images. 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(str(artifact.seed)+ ".png") # Save our generated images with their seed number as the filename. ``` -------------------------------- ### Set Initial Generation Parameters Source: https://platform.stability.ai/docs/features/image-to-image?tab=typescript Configure parameters for image generation, including prompt, initial image, seed, and generation settings. Ensure all necessary imports are present before execution. ```python answers2 = stability_api.generate( prompt="crayon drawing of rocket ship launching from forest", init_image=img, # Assign our previously generated img as our Initial Image for transformation. start_schedule=0.6, # Set the strength of our prompt in relation to our initial image. seed=123463446, # If attempting to transform an image that was previously generated with our API, # initial images benefit from having their own distinct seed rather than using the seed of the original image generation. steps=50, # Amount of inference steps performed on image generation. Defaults to 30. cfg_scale=8.0, # Influences how strongly your generation is guided to match your prompt. # Setting this value higher increases the strength in which it tries to match your prompt. # Defaults to 7.0 if not specified. width=1024, # Generation width, defaults to 512 if not included. height=1024, # Generation height, defaults to 512 if not included. sampler=generation.SAMPLER_K_DPMPP_2M # Choose which sampler we want to denoise our generation with. # Defaults to k_dpmpp_2m if not specified. Clip Guidance only supports ancestral samplers. # (Available Samplers: ddim, plms, k_euler, k_euler_ancestral, k_heun, k_dpm_2, k_dpm_2_ancestral, k_dpmpp_2s_ancestral, k_lms, k_dpmpp_2m, k_dpmpp_sde) ) ``` -------------------------------- ### Basic text-to-image generation parameters Source: https://platform.stability.ai/docs/features/multi-prompting Configure parameters for text-to-image generation, including prompt, seed, steps, CFG scale, dimensions, and sampler. The safety filter status is checked, and images are saved with their seed as the filename. ```python answers = stability_api.generate( prompt="beautiful night sky above japanese town, anime style", seed=1229080980, # If a seed is provided, the resulting generated image will be deterministic. # What this means is that as long as all generation parameters remain the same, you can always recall the same image simply by generating it again. # Note: This isn't quite the case for CLIP Guided generations, which we tackle in the CLIP Guidance documentation. steps=50, # Amount of inference steps performed on image generation. Defaults to 30. cfg_scale=8.0, # Influences how strongly your generation is guided to match your prompt. # Setting this value higher increases the strength in which it tries to match your prompt. # Defaults to 7.0 if not specified. width=1024, # Generation width, if not included defaults to 512 or 1024 depending on the engine. height=1024, # Generation height, if not included defaults to 512 or 1024 depending on the engine. samples=1, # Number of images to generate, defaults to 1 if not included. sampler=generation.SAMPLER_K_DPMPP_2M # Choose which sampler we want to denoise our generation with. # Defaults to k_dpmpp_2m if not specified. Clip Guidance only supports ancestral samplers. # (Available Samplers: ddim, plms, k_euler, k_euler_ancestral, k_heun, k_dpm_2, k_dpm_2_ancestral, k_dpmpp_2s_ancestral, k_lms, k_dpmpp_2m, k_dpmpp_sde) ) # Set up our warning to print to the console if the adult content classifier is tripped. # If adult content classifier is not tripped, save generated images. 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(str(artifact.seed)+ ".png") # Save our generated images with their seed number as the filename. ``` -------------------------------- ### Generate Image with Inpainting Source: https://platform.stability.ai/docs/features/inpainting?tab=typescript Generate a new image using a prompt, an initial image, and a mask. Parameters like steps, cfg_scale, and sampler can be adjusted to control the generation process. ```python answers = stability_api.generate( prompt="crayon drawing of rocket ship launching from forest", init_image=img, mask_image=mask, start_schedule=1, seed=44332211, # If attempting to transform an image that was previously generated with our API, # initial images benefit from having their own distinct seed rather than using the seed of the original image generation. steps=50, # Amount of inference steps performed on image generation. Defaults to 30. cfg_scale=8.0, # Influences how strongly your generation is guided to match your prompt. # Setting this value higher increases the strength in which it tries to match your prompt. # Defaults to 7.0 if not specified. width=1024, # Generation width, if not included defaults to 512 or 1024 depending on the engine. height=1024, # Generation height, if not included defaults to 512 or 1024 depending on the engine. sampler=generation.SAMPLER_K_DPMPP_2M # Choose which sampler we want to denoise our generation with. # Defaults to k_lms if not specified. Clip Guidance only supports ancestral samplers. # (Available Samplers: ddim, plms, k_euler, k_euler_ancestral, k_heun, k_dpm_2, k_dpm_2_ancestral, k_dpmpp_2s_ancestral, k_lms, k_dpmpp_2m, k_dpmpp_sde) ) # Set up our warning to print to the console if the adult content classifier is tripped. # If adult content classifier is not tripped, save generated image. 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: global img2 img2 = Image.open(io.BytesIO(artifact.binary)) img2.save(str(artifact.seed)+ ".png") # Save our completed image with its seed number as the filename. ``` -------------------------------- ### gRPC API Parameters Source: https://platform.stability.ai/docs/features/api-parameters This section outlines the parameters for the gRPC API. The 'prompt' is the only required parameter. Other parameters allow for customization of image generation. ```APIDOC ## gRPC API Parameters When calling the gRPC API, `prompt` is the only required variable. Provided alone, this call will generate an image according to our default generation settings. The gRPC response will contain a `finish_reason` specifying the outcome of your request in addition to the delivered asset. If the `finish_reason` is `filter`, this means our safety filter has been activated and the resulting image will be blurred. This is by design. These calls can be assigned a variety of parameters, some of which have an effect on pricing, as detailed below. ### Prompt Accepts singular as well as multiple prompts with weights. See the Multi-prompting documentation for more information. Parameter| Default| Affects Pricing? ---|---|--- prompt| none| No ### Height Measured in pixels. Pixel limit is `1048576`, so technically any dimension is allowable within that amount. Parameter| Default| Typical| Allowable| Affects Pricing? ---|---|---|---|--- height| 512| 512| See Below| Yes ### Width Measured in pixels. Pixel limit is `1048576`, so technically any dimension is allowable within that amount. Parameter| Default| Typical| Allowable| Affects Pricing? ---|---|---|---|--- width| 512| 512| See Below| Yes ### About Dimensions A minimum of `262k` pixels and a maximum of `1.04m` pixels are recommended when generating images with `512px` models, and a minimum of `589k` pixels and a maximum of `1.04m` pixels for `768px` models. The true pixel limit is `1048576`. To avoid the dreaded `6N IndexError` it is advised to use `64px` increments when choosing an aspect ratio. Popular ratio combinations for `512px` models include `1536 x 512` and `1536 x 384`, while `1536 x 640` and `1024 x 576` are recommended for `768px` models. For `512px` models, the minimum useful sizes are `192-256` in one dimension. For `768px` models the minimum useful size is `384` in one dimension. Generating images under the recommended dimensions may result in undesirable artifacts. ### Steps Affects the number of diffusion steps performed on the requested generation. Parameter| Default| Typical| Allowable| Affects Pricing? ---|---|---|---|--- steps| 30| 30-50| 10-150| Yes ### Samples Number of images to generate. Allows for batch image generations. Parameter| Default| Typical| Allowable| Affects Pricing? ---|---|---|---|--- num_samples| 1| 1-9| 1-10| Yes ### CFG Scale Dictates how closely the engine attempts to match a generation to the provided prompt. v2-x models respond well to lower CFG (IE: 4-8), where as v1-x models respond well to a higher range (IE: 7-14) and SDXL models respond well to a wider range (IE: 4-12). Parameter| Default| Typical| Allowable| Affects Pricing? ---|---|---|---|--- cfg_scale| 7.0| 4-14| 1-35| No ### Engine Engine (model) to use. Available engines: **_Note: The models below have special pricing considerations. For additional information please check out thePricing page._** * (`SDXL`) stable-diffusion-xl-1024-v0-9 * (`SDXL`) stable-diffusion-xl-1024-v1-0 * esrgan-v1-x2plus **_Note: Engines in the list below are now deprecated and can no longer be accessed. Please use one of the models listed above._** * stable-diffusion-v1 * stable-diffusion-v1-5 * stable-diffusion-512-v2-0 * stable-diffusion-768-v2-0 * stable-diffusion-512-v2-1 * stable-diffusion-768-v2-1 * stable-diffusion-xl-beta-v2-2-2 * stable-inpainting-v1-0 * stable-inpainting-512-v2-0 * stable-diffusion-x4-latent-upscaler Parameter| Default| Typical| Allowable| Affects Pricing? ---|---|---|---|--- engine| stable-diffusion-xl-1024-v0-9| stable-diffusion-xl-1024-v0-9| See Above| See Above ### Image Upscaler Dictates the parameters sent to our image upscaling engine. Check out the examples on our Image Upscaling documentation to learn how to execute an image upscaling call via our gRPC API. Parameter| Default| Typical| Allowable| Affects Pricing? ---|---|---|---|--- stability_api.upscale| init_image=| init_image=| init_image=, width= **or** height= (but not both)| Yes ### Sampler Sampling engine to use. If no sampler is declared, an appropriate default sampler for the declared inference engine will be applied automatically. Available samplers: * ddim * plms * k_euler * k_euler_ancestral * k_heun * k_dpm_2 * k_dpm_2_ancestral * k_dpmpp_2s_ancestral * k_dpmpp_2m * k_dpmpp_sde Parameter| Default| Typical| Allowable| Affects Pricing? ---|---|---|---|--- sampler| k_dpmpp_2m| k_dpmpp_2m| Check Comments| No ``` -------------------------------- ### Generate Images with Weighted Prompts Source: https://platform.stability.ai/docs/features/multi-prompting Use this snippet to generate images with specific prompts and control their influence using weights. Prompts exceeding 77 tokens will be truncated. Default prompt weight is 1 if not specified. Recommended prompt weight range is [-2, 2]. ```python answers = stability_api.generate( prompt= [generation.Prompt(text="beautiful night sky above japanese town, anime style",parameters=generation.PromptParameters(weight=1)), generation.Prompt(text="clouds",parameters=generation.PromptParameters(weight=-1))], # In the example above we are negative prompting clouds out of the initial concept. # When determining prompt weights, the total possible range is [-10, 10] but we recommend staying within the range of [-2, 2]. seed=1229080980, # If a seed is provided, the resulting generated image will be deterministic. # What this means is that as long as all generation parameters remain the same, you can always recall the same image simply by generating it again. # Note: This is only true for non-CLIP Guided generations. steps=50, # Amount of inference steps performed on image generation. Defaults to 30. cfg_scale=8.0, # Influences how strongly your generation is guided to match your prompt. # Setting this value higher increases the strength in which it tries to match your prompt. # Defaults to 7.0 if not specified. width=1024, # Generation width, if not included defaults to 512 or 1024 depending on the engine. height=1024, # Generation height, if not included defaults to 512 or 1024 depending on the engine. samples=1, # Number of images to generate, defaults to 1 if not included. sampler=generation.SAMPLER_K_DPMPP_2M # Choose which sampler we want to denoise our generation with. # Defaults to k_dpmpp_2m if not specified. Clip Guidance only supports ancestral samplers. # (Available Samplers: ddim, plms, k_euler, k_euler_ancestral, k_heun, k_dpm_2, k_dpm_2_ancestral, k_dpmpp_2s_ancestral, k_lms, k_dpmpp_2m, k_dpmpp_sde) ) ``` -------------------------------- ### Establish API Connection Source: https://platform.stability.ai/docs/features/text-to-image?tab=typescript Establish a connection to the Stability AI API using the configured API key and specifying the desired generation engine. The 'verbose' option can be enabled for debugging. ```python stability_api = client.StabilityInference( key=os.environ['STABILITY_KEY'], # API Key reference. verbose=True, # Print debug messages. engine="stable-diffusion-xl-1024-v1-0", # Set the engine to use for generation. # Check out the following link for a list of available engines: https://platform.stability.ai/docs/features/api-parameters#engine ) ``` -------------------------------- ### Establish API Connection Source: https://platform.stability.ai/docs/features/inpainting?tab=typescript Connect to the Stability AI API using your API key and specifying the desired engine. The engine determines the model used for generation. ```python # Set up our connection to the API. stability_api = client.StabilityInference( key=os.environ['STABILITY_KEY'], # API Key reference. verbose=True, # Print debug messages. engine="stable-diffusion-xl-1024-v1-0", # Set the engine to use for generation. # Check out the following link for a list of available engines: https://platform.stability.ai/docs/features/api-parameters#engine ) ``` -------------------------------- ### Load Image and Mask Source: https://platform.stability.ai/docs/features/inpainting?tab=typescript Load the image to be modified and a mask image to define areas for inpainting. Feathering the mask can improve results. ```python img = Image.open('/content/image.png') mask_i = Image.open('/content/mask.png') # Feathering the edges of our mask generally helps provide a better result. Alternately, you can feather the mask in a suite like Photoshop or GIMP. blur = GaussianBlur(11,20) mask = blur(mask_i) ``` -------------------------------- ### Process Generation Response and Save Image Source: https://platform.stability.ai/docs/features/image-to-image?tab=typescript Iterate through the generation response to check for safety filters and save the generated image. The image is saved with its seed as the filename and an '.png' extension. ```python for resp in answers2: 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: global img2 img2 = Image.open(io.BytesIO(artifact.binary)) img2.save(str(artifact.seed)+ "-img2img.png") # Save our generated image with its seed number as the filename and the img2img suffix so that we know this is our transformed image. ``` -------------------------------- ### Handle Safety Filters and Save Images Source: https://platform.stability.ai/docs/features/multi-prompting This code iterates through generated answers, checks for safety filter activations, and saves generated images using their seed number as the filename. It requires the 'warnings' and 'PIL' libraries. ```python import warnings from PIL import Image import io # Set up our warning to print to the console if the adult content classifier is tripped. # If adult content classifier is not tripped, save generated images. 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(str(artifact.seed)+ ".png") # Save our generated images with their seed number as the filename. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.