Vaibhav Srivastav
commited on
Commit
•
5f6ee1c
1
Parent(s):
43cbd17
up
Browse files- handler.py +50 -0
handler.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, List, Any
|
2 |
+
from transformers import AutoProcessor, MusicgenForConditionalGeneration
|
3 |
+
import torch
|
4 |
+
|
5 |
+
processor = AutoProcessor.from_pretrained("facebook/musicgen-small")
|
6 |
+
model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small")
|
7 |
+
|
8 |
+
inputs = processor(
|
9 |
+
text=["80s pop track with bassy drums and synth", "90s rock song with loud guitars and heavy drums"],
|
10 |
+
padding=True,
|
11 |
+
return_tensors="pt",
|
12 |
+
)
|
13 |
+
|
14 |
+
audio_values = model.generate(**inputs, max_new_tokens=256)
|
15 |
+
|
16 |
+
class EndpointHandler:
|
17 |
+
def __init__(self, path=""):
|
18 |
+
# load model and processor from path
|
19 |
+
self.processor = AutoProcessor.from_pretrained(path)
|
20 |
+
self.model = MusicgenForConditionalGeneration.from_pretrained(path)
|
21 |
+
# self.model = AutoModelForSeq2SeqLM.from_pretrained(path, device_map="auto", load_in_8bit=True)
|
22 |
+
# self.tokenizer = AutoTokenizer.from_pretrained(path)
|
23 |
+
|
24 |
+
def __call__(self, data: Dict[str, Any]) -> Dict[str, str]:
|
25 |
+
"""
|
26 |
+
Args:
|
27 |
+
data (:dict:):
|
28 |
+
The payload with the text prompt and generation parameters.
|
29 |
+
"""
|
30 |
+
# process input
|
31 |
+
inputs = data.pop("inputs", data)
|
32 |
+
parameters = data.pop("parameters", None)
|
33 |
+
|
34 |
+
# preprocess
|
35 |
+
# input_ids = self.tokenizer(inputs, return_tensors="pt").input_ids
|
36 |
+
inputs = processor(
|
37 |
+
text=inputs,
|
38 |
+
padding=True,
|
39 |
+
return_tensors="pt",)
|
40 |
+
|
41 |
+
# pass inputs with all kwargs in data
|
42 |
+
if parameters is not None:
|
43 |
+
outputs = self.model.generate(inputs, max_new_tokens=256, **parameters)
|
44 |
+
else:
|
45 |
+
outputs = self.model.generate(inputs, max_new_tokens=256)
|
46 |
+
|
47 |
+
# postprocess the prediction
|
48 |
+
prediction = outputs[0].numpy()
|
49 |
+
|
50 |
+
return [{"generated_audio": prediction}]
|