Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_response_image(image):
- pil_img = image # reads the PIL image
- byte_arr = io.BytesIO()
- pil_img.save(byte_arr, format='PNG') # convert the PIL image to byte array
- encoded_img = encodebytes(byte_arr.getvalue()).decode('ascii') # encode as base64
- return encoded_img
- @app.get("/plot-picture")
- def plot_picture(description: str):
- model_id = "stabilityai/stable-diffusion-2"
- scheduler = EulerDiscreteScheduler.from_pretrained(model_id, subfolder="scheduler")
- pipe = StableDiffusionPipeline.from_pretrained(model_id, scheduler=scheduler, revision="fp16", torch_dtype=torch.float16)
- pipe = pipe.to("cuda")
- prompt = description
- images = pipe(prompt, height=512, width=512).images[0]
- encoded_img = get_response_image(images)
- response = { 'Status' : 'Success', 'ImageBytes': encoded_img }
- return response
Add Comment
Please, Sign In to add comment