Download tts_engine.py from build-small-hackathon/patient-virtuel-dentiste: direct link, hf CLI and curl.
- Browser
- Download file 1.21 kB
-
https://huggingface.co/spaces/build-small-hackathon/patient-virtuel-dentiste/resolve/b05542b6cc08dc3eaa013120cc6ab932aedd31af/tts_engine.py
- Command line
-
hf download hf://spaces/build-small-hackathon/patient-virtuel-dentiste@b05542b6cc08dc3eaa013120cc6ab932aedd31af/tts_engine.py
-
curl -L -o tts_engine.py https://huggingface.co/spaces/build-small-hackathon/patient-virtuel-dentiste/resolve/b05542b6cc08dc3eaa013120cc6ab932aedd31af/tts_engine.py
1.21 kB
| import os | |
| import httpx | |
| MODAL_ENDPOINT = os.environ.get("MODAL_ENDPOINT_MISTRAL_TTS", "") | |
| MISTRAL_API_KEY = os.environ.get("MISTRAL_API_KEY", "") | |
| MODAL_AUTH_TOKEN = os.environ.get("MODAL_AUTH_TOKEN", "") | |
| VOICE = "french_female" | |
| MODEL = "mistral-tts" | |
| def synthesize(text: str) -> bytes | None: | |
| """Tier-1: call Mistral API directly from the Space (simplest, free tier).""" | |
| if not MISTRAL_API_KEY: | |
| return _fallback_modal(text) | |
| resp = httpx.post( | |
| "https://api.mistral.ai/v1/audio/speech", | |
| headers={"Authorization": f"Bearer {MISTRAL_API_KEY}"}, | |
| json={ | |
| "model": MODEL, | |
| "input": text, | |
| "voice": VOICE, | |
| "response_format": "wav", | |
| }, | |
| timeout=25, | |
| ) | |
| if resp.is_error: | |
| return _fallback_modal(text) | |
| return resp.content | |
| def _fallback_modal(text: str) -> bytes | None: | |
| """Tier-2: fall back to Modal-hosted Mistral TTS.""" | |
| if not MODAL_ENDPOINT: | |
| raise RuntimeError("No TTS endpoint available") | |
| resp = httpx.post( | |
| MODAL_ENDPOINT, | |
| json={"text": text, "token": MODAL_AUTH_TOKEN}, | |
| timeout=30, | |
| ) | |
| resp.raise_for_status() | |
| return resp.content | |