Commit
·
cf7e3c5
1
Parent(s):
afcd079
Update handler
Browse files- handler.py +19 -18
handler.py
CHANGED
|
@@ -4,20 +4,23 @@ from typing import Dict, List, Any
|
|
| 4 |
# import torch
|
| 5 |
from datetime import datetime
|
| 6 |
|
|
|
|
| 7 |
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
import requests
|
| 11 |
-
from PIL import Image
|
| 12 |
-
from transformers import Blip2Processor, Blip2ForConditionalGeneration
|
| 13 |
-
|
| 14 |
|
| 15 |
class EndpointHandler():
|
| 16 |
|
| 17 |
def __init__(self, path=""):
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 23 |
# self.model.eval()
|
|
@@ -64,18 +67,16 @@ class EndpointHandler():
|
|
| 64 |
|
| 65 |
|
| 66 |
# pip install accelerate
|
| 67 |
-
|
| 68 |
-
img_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
|
| 69 |
|
|
|
|
|
|
|
| 70 |
now = datetime.now()
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
out = self.model.generate(**inputs)
|
| 78 |
-
output_text = self.processor.decode(out[0], skip_special_tokens=True)
|
| 79 |
|
| 80 |
current = datetime.now()
|
| 81 |
|
|
@@ -100,4 +101,4 @@ class EndpointHandler():
|
|
| 100 |
# new_tokens = output_ids[0, len(input_ids[0]) :]
|
| 101 |
# output_text = self.tokenizer.decode(new_tokens, skip_special_tokens=True)
|
| 102 |
|
| 103 |
-
return [{"
|
|
|
|
| 4 |
# import torch
|
| 5 |
from datetime import datetime
|
| 6 |
|
| 7 |
+
import torch
|
| 8 |
|
| 9 |
+
# torch.backends.cuda.matmul.allow_tf32 = True
|
| 10 |
|
| 11 |
+
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
class EndpointHandler():
|
| 14 |
|
| 15 |
def __init__(self, path=""):
|
| 16 |
+
|
| 17 |
+
# Use the DPMSolverMultistepScheduler (DPM-Solver++) scheduler here instead
|
| 18 |
+
self.pipe = StableDiffusionPipeline.from_pretrained(path, torch_dtype=torch.float16)
|
| 19 |
+
self.pipe.scheduler = DPMSolverMultistepScheduler.from_config(self.pipe.scheduler.config)
|
| 20 |
+
self.pipe = self.pipe.to("cuda")
|
| 21 |
+
|
| 22 |
+
# self.pipe.enable_attention_slicing()
|
| 23 |
+
self.pipe.enable_xformers_memory_efficient_attention()
|
| 24 |
|
| 25 |
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 26 |
# self.model.eval()
|
|
|
|
| 67 |
|
| 68 |
|
| 69 |
# pip install accelerate
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
batch_size = data.pop("batch_size",data)
|
| 72 |
+
|
| 73 |
now = datetime.now()
|
| 74 |
|
| 75 |
+
with torch.inference_mode():
|
| 76 |
+
prompt = "a photo of an astronaut riding a horse on mars"
|
| 77 |
+
image = pipe([prompt]*batch_size, num_inference_steps=20)
|
| 78 |
+
|
| 79 |
+
# image.save("astronaut_rides_horse.png")
|
|
|
|
|
|
|
| 80 |
|
| 81 |
current = datetime.now()
|
| 82 |
|
|
|
|
| 101 |
# new_tokens = output_ids[0, len(input_ids[0]) :]
|
| 102 |
# output_text = self.tokenizer.decode(new_tokens, skip_special_tokens=True)
|
| 103 |
|
| 104 |
+
return [{"batch_size":batch_size, "time_elapsed": str(current-now)}]
|