sergeipetrov commited on
Commit
a45bb1c
1 Parent(s): dd9df9a

Create handler.py

Browse files
Files changed (1) hide show
  1. handler.py +34 -0
handler.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from parler_tts import ParlerTTSForConditionalGeneration
3
+ from transformers import AutoTokenizer, set_seed
4
+ import soundfile as sf
5
+
6
+
7
+ class EndpointHandler:
8
+ def __init__(self, path=""):
9
+ self.device = "cuda:0" if torch.cuda.is_available() else "cpu"
10
+
11
+ self.model = ParlerTTSForConditionalGeneration.from_pretrained("parler-tts/parler-tts-mini-expresso").to(self.device)
12
+ self.tokenizer = AutoTokenizer.from_pretrained("parler-tts/parler-tts-mini-expresso")
13
+
14
+ def __call__(self, data: Any):
15
+ inputs = data["inputs"]
16
+ prompt = inputs["prompt"]
17
+ description = inputs["description"]
18
+
19
+ input_ids = self.tokenizer(description, return_tensors="pt").input_ids.to(self.device)
20
+ prompt_input_ids = self.tokenizer(prompt, return_tensors="pt").input_ids.to(self.device)
21
+
22
+ set_seed(42)
23
+ try:
24
+ generation = self.model.generate(input_ids=input_ids, prompt_input_ids=prompt_input_ids)
25
+ audio_arr = generation.cpu().numpy().squeeze()
26
+ return audio_arr
27
+
28
+ except Exception as e:
29
+ logger.error(str(e))
30
+ del inputs
31
+ gc.collect()
32
+ torch.cuda.empty_cache()
33
+
34
+ return {"error": str(e)}