Upload handler.py
Browse files- handler.py +40 -0
handler.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, Any
|
2 |
+
from textToStoryGeneration import *
|
3 |
+
import logging
|
4 |
+
import torch
|
5 |
+
import soundfile as sf
|
6 |
+
from transformers import AutoTokenizer, AutoModelForTextToWaveform
|
7 |
+
|
8 |
+
# Configure logging
|
9 |
+
logging.basicConfig(level=logging.DEBUG)
|
10 |
+
# Configure logging
|
11 |
+
logging.basicConfig(level=logging.ERROR)
|
12 |
+
# Configure logging
|
13 |
+
logging.basicConfig(level=logging.WARNING)
|
14 |
+
|
15 |
+
|
16 |
+
|
17 |
+
class CustomHandler:
|
18 |
+
def __init__(self):
|
19 |
+
|
20 |
+
self.tokenizer = AutoTokenizer.from_pretrained("facebook/mms-tts-eng")
|
21 |
+
self.model= AutoModelForTextToWaveform.from_pretrained("facebook/mms-tts-eng")
|
22 |
+
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
23 |
+
# Prepare the payload with input data
|
24 |
+
logging.warning(f"------input_data-- {str(data)}")
|
25 |
+
payload = str(data)
|
26 |
+
logging.warning(f"payload----{str(payload)}")
|
27 |
+
# Set headers with API token
|
28 |
+
inputs = self.tokenizer(payload, return_tensors="pt")
|
29 |
+
|
30 |
+
# Generate the waveform from the input text
|
31 |
+
with torch.no_grad():
|
32 |
+
outputs = self.model(**inputs)
|
33 |
+
|
34 |
+
# Save the audio to a file
|
35 |
+
sf.write("StoryAudio.wav", outputs["waveform"][0].numpy(), self.model.config.sampling_rate)
|
36 |
+
|
37 |
+
return 'StoryAudio.wav'
|
38 |
+
# Check if the request was successful
|
39 |
+
|
40 |
+
|