import gradio as gr from stability_sdk import client import stability_sdk.interfaces.gooseai.generation.generation_pb2 as generation from PIL import Image import io import os import warnings from dotenv import load_dotenv # load secrets load_dotenv() SD_KEY = os.getenv("SD_KEY") HF_KEY = os.getenv("HF_KEY") USERNAME = os.getenv("USERNAME") PASS = os.getenv("PASS") # set up dataset writer hf_writer_gen = gr.HuggingFaceDatasetSaver(HF_KEY, "helms/master-thesis-generated-images", private=True, separate_dirs=False) def infer(prompt): stability_api = client.StabilityInference( key=SD_KEY, # AaPI Key reference. verbose=True, # Print debug messages. engine="stable-diffusion-xl-1024-v1-0", # Set the engine to use for generation. # Available engines: stable-diffusion-xl-1024-v1-0 stable-diffusion-xl-1024-v0-9 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 ) answers = stability_api.generate( prompt=prompt, # seed=992446758, # 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=30, # Amount of inference steps performed on image generation. Defaults to 30. cfg_scale=7.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) ) for resp in answers: for artifact in resp.artifacts: if artifact.finish_reason == generation.FILTER: warnings.warn( "Your request activated the APIs 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)) return img with gr.Blocks(title="Master Thesis Image Generator") as demo: with gr.Row(): gr.HTML("""
Welcome to this demo app for the Master thesis of Fabian Helms, TU Dortmund.
Contact: fabian.helms@tu-dortmund.de
Generated Images