from io import BytesIO import torch from diffusers import DiffusionPipeline from fastapi import FastAPI from fastapi.responses import StreamingResponse # load both base & refiner base = DiffusionPipeline.from_pretrained( "stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True ) # base.to("cuda") base.enable_model_cpu_offload() base.enable_attention_slicing() refiner = DiffusionPipeline.from_pretrained( "stabilityai/stable-diffusion-xl-refiner-1.0", text_encoder_2=base.text_encoder_2, vae=base.vae, torch_dtype=torch.float16, use_safetensors=True, variant="fp16", ) # refiner.to("cuda") refiner.enable_model_cpu_offload() refiner.enable_attention_slicing() # Create a new FastAPI app instance app = FastAPI() # Define a function to handle the GET request at `/generate` # The generate() function is defined as a FastAPI route that takes a # string parameter called text. The function generates text based on the # input using the pipeline() object, and returns a JSON response # containing the generated text under the key "output" @app.get("/generate") def generate(text: str): """ generate image """ # Define how many steps and what % of steps to be run on each experts (80/20) here n_steps = 40 high_noise_frac = 0.8 negative = "drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly. bad anatomy, disfigured, poorly drawn face, mutation, mutated, extra limb, ugly, disgusting, poorly drawn hands, missing limb, floating limbs, disconnected limbs, malformed hands, blurry, mutated hands and fingers, watermark, watermarked, oversaturated, censored, distorted hands, amputation, missing hands, obese, doubled face, double hands, two women, anime style, cartoon, toon." prompt = "Designs should play with different textures and layering but stick to a monochrome palette. Think leather jackets over mesh tops, or satin draped over matte cotton. in a studio. zoomed-in. single model." # run both experts image = base( prompt=prompt, negative_prompt=negative, num_inference_steps=n_steps, denoising_end=high_noise_frac, output_type="latent", ).images final_image = refiner( prompt=prompt, negative_prompt=negative, num_inference_steps=n_steps, denoising_start=high_noise_frac, image=image, ).images[0] return StreamingResponse(BytesIO(final_image), media_type="image/png") # Return the generated text in a JSON response # return {"output": output[0]["generated_text"]}