Fix handler
Browse files- handler.py +18 -11
handler.py
CHANGED
|
@@ -3,19 +3,26 @@ import torch
|
|
| 3 |
import numpy as np
|
| 4 |
import scipy
|
| 5 |
import io
|
|
|
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
def
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
byte_io.seek(0)
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import scipy
|
| 5 |
import io
|
| 6 |
+
import base64
|
| 7 |
|
| 8 |
+
class EndpointHandler:
|
| 9 |
+
def __init__(self, path=""):
|
| 10 |
+
preload_models()
|
| 11 |
|
| 12 |
+
def __call__(self, data):
|
| 13 |
+
text = data.get("inputs", "")
|
| 14 |
+
if not text.strip():
|
| 15 |
+
return {"error": "No input text provided."}
|
| 16 |
|
| 17 |
+
audio_array = generate_audio(text, history_prompt="v2/ru_speaker_0")
|
| 18 |
|
| 19 |
+
byte_io = io.BytesIO()
|
| 20 |
+
scipy.io.wavfile.write(byte_io, rate=24000, data=audio_array)
|
| 21 |
+
byte_io.seek(0)
|
|
|
|
| 22 |
|
| 23 |
+
audio_base64 = base64.b64encode(byte_io.read()).decode("utf-8")
|
| 24 |
+
|
| 25 |
+
return {
|
| 26 |
+
"audio_base64": audio_base64,
|
| 27 |
+
"content_type": "audio/wav"
|
| 28 |
+
}
|