devepone

Untitled

Dec 1st, 2022
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. def get_response_image(image):
  2. pil_img = image # reads the PIL image
  3. byte_arr = io.BytesIO()
  4. pil_img.save(byte_arr, format='PNG') # convert the PIL image to byte array
  5. encoded_img = encodebytes(byte_arr.getvalue()).decode('ascii') # encode as base64
  6. return encoded_img
  7.  
  8. @app.get("/plot-picture")
  9. def plot_picture(description: str):
  10. model_id = "stabilityai/stable-diffusion-2"
  11. scheduler = EulerDiscreteScheduler.from_pretrained(model_id, subfolder="scheduler")
  12. pipe = StableDiffusionPipeline.from_pretrained(model_id, scheduler=scheduler, revision="fp16", torch_dtype=torch.float16)
  13. pipe = pipe.to("cuda")
  14. prompt = description
  15. images = pipe(prompt, height=512, width=512).images[0]
  16. encoded_img = get_response_image(images)
  17. response = { 'Status' : 'Success', 'ImageBytes': encoded_img }
  18. return response
Add Comment
Please, Sign In to add comment