Spaces:
Sleeping
Sleeping
Create tts.py
Browse files- services/tts.py +30 -0
services/tts.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, uuid, base64, requests
|
| 2 |
+
from functools import lru_cache
|
| 3 |
+
|
| 4 |
+
TTS_API = os.getenv(
|
| 5 |
+
"TTS_API_URL",
|
| 6 |
+
"https://rahul7star-Chatterbox-Multilingual-TTS-API.hf.space/tts"
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
SESSION = requests.Session()
|
| 10 |
+
|
| 11 |
+
@lru_cache(maxsize=128)
|
| 12 |
+
def synthesize(text: str, lang: str) -> str:
|
| 13 |
+
payload = {
|
| 14 |
+
"text": text,
|
| 15 |
+
"language_id": lang,
|
| 16 |
+
"mode": "Speak 🗣️"
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
r = SESSION.post(TTS_API, json=payload, timeout=None)
|
| 20 |
+
r.raise_for_status()
|
| 21 |
+
|
| 22 |
+
path = f"/tmp/tts_{uuid.uuid4().hex}.wav"
|
| 23 |
+
|
| 24 |
+
if r.headers.get("content-type", "").startswith("audio"):
|
| 25 |
+
open(path, "wb").write(r.content)
|
| 26 |
+
return path
|
| 27 |
+
|
| 28 |
+
audio = r.json().get("audio") or r.json().get("audio_base64")
|
| 29 |
+
open(path, "wb").write(base64.b64decode(audio))
|
| 30 |
+
return path
|