Created
February 19, 2024 11:06
-
-
Save mllopart/b4fb204fbbeb50f3162803edaf7270ca to your computer and use it in GitHub Desktop.
Stability AI - reimagine your home's potential
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import requests | |
import io | |
import random | |
import time | |
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" | |
# 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"] = "sk-XYZ" | |
# 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 | |
) | |
# make sure the out directory exists | |
if not os.path.exists("./out"): | |
os.makedirs("./out") | |
# URL of the image you want to download | |
image_url = "https://c.pxhere.com/photos/bd/fb/bed_cot_old_historically_vintage_kl_den_burg-1175321.jpg!d" | |
# Download the image | |
response = requests.get(image_url) | |
if response.status_code == 200: | |
# If download is successful, use the image content directly in the files parameter | |
image = Image.open(io.BytesIO(response.content)) | |
# random seed value | |
seed_value = int(time.time()) | |
seed = random.seed(seed_value) | |
answers2 = stability_api.generate( | |
prompt="""Transform the interior design of the image provided to embody a rustic aesthetic. | |
Focus on incorporating elements such as natural wood textures, exposed stone, warm earthy | |
tones, and vintage furnishings to create a cozy, inviting space. Emphasize simplicity | |
and the beauty of the outdoors with handcrafted objects and textiles that suggest a | |
connection to nature. The goal is to create a serene, homey atmosphere that blends | |
traditional charm with the comfort of rural living --ar 3:2.""", | |
init_image=image, # 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=seed, # 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) | |
) | |
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( | |
f"./out/img2img_{str(artifact.seed)}.jpg" | |
) # Save our generated image with its seed number as the filename and the img2img suffix so that we know this is our transformed image. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment