Update handler.py
Browse files- handler.py +6 -4
handler.py
CHANGED
@@ -3,10 +3,10 @@ from transformers import AutoProcessor, MusicgenForConditionalGeneration
|
|
3 |
import torch
|
4 |
|
5 |
class EndpointHandler:
|
6 |
-
def __init__(self, path="
|
7 |
# load model and processor from path
|
8 |
self.processor = AutoProcessor.from_pretrained(path)
|
9 |
-
self.model = MusicgenForConditionalGeneration.from_pretrained(path).to("cuda")
|
10 |
|
11 |
def __call__(self, data: Dict[str, Any]) -> Dict[str, str]:
|
12 |
"""
|
@@ -26,9 +26,11 @@ class EndpointHandler:
|
|
26 |
|
27 |
# pass inputs with all kwargs in data
|
28 |
if parameters is not None:
|
29 |
-
|
|
|
30 |
else:
|
31 |
-
|
|
|
32 |
|
33 |
# postprocess the prediction
|
34 |
prediction = outputs[0].cpu().numpy()
|
|
|
3 |
import torch
|
4 |
|
5 |
class EndpointHandler:
|
6 |
+
def __init__(self, path=""):
|
7 |
# load model and processor from path
|
8 |
self.processor = AutoProcessor.from_pretrained(path)
|
9 |
+
self.model = MusicgenForConditionalGeneration.from_pretrained(path, torch_dtype=torch.float16).to("cuda")
|
10 |
|
11 |
def __call__(self, data: Dict[str, Any]) -> Dict[str, str]:
|
12 |
"""
|
|
|
26 |
|
27 |
# pass inputs with all kwargs in data
|
28 |
if parameters is not None:
|
29 |
+
with torch.autocast("cuda"):
|
30 |
+
outputs = self.model.generate(**inputs, **parameters)
|
31 |
else:
|
32 |
+
with torch.autocast("cuda"):
|
33 |
+
outputs = self.model.generate(**inputs,)
|
34 |
|
35 |
# postprocess the prediction
|
36 |
prediction = outputs[0].cpu().numpy()
|