Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,10 +3,12 @@ from fastapi.responses import StreamingResponse
|
|
| 3 |
from piper import PiperVoice
|
| 4 |
import io
|
| 5 |
import os
|
|
|
|
| 6 |
import wave
|
| 7 |
from pydantic import BaseModel
|
|
|
|
| 8 |
|
| 9 |
-
app = FastAPI()
|
| 10 |
|
| 11 |
# Path where models will be stored in the container
|
| 12 |
MODEL_DIR = "./models"
|
|
@@ -40,6 +42,13 @@ VOICE_MAP = {
|
|
| 40 |
"cy": {"gendered": False, "default": "cy_GB-gwryw_gogleddol-medium"}
|
| 41 |
}
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
# Cache for loaded models to avoid re-loading from disk every request
|
| 44 |
loaded_voices = {}
|
| 45 |
|
|
@@ -58,9 +67,10 @@ def get_voice(model_name: str):
|
|
| 58 |
class TTSRequest(BaseModel):
|
| 59 |
text: str
|
| 60 |
language: str
|
| 61 |
-
gender:
|
|
|
|
| 62 |
|
| 63 |
-
@app.post("/tts")
|
| 64 |
async def tts_post(request: TTSRequest):
|
| 65 |
try:
|
| 66 |
lang_code = request.language.lower()
|
|
@@ -98,7 +108,57 @@ async def tts_post(request: TTSRequest):
|
|
| 98 |
except Exception as e:
|
| 99 |
print(f"Error during TTS: {e}")
|
| 100 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
@app.get("/health")
|
| 103 |
def home():
|
| 104 |
return {"status": "Piper TTS is running"}
|
|
@@ -114,5 +174,5 @@ def home():
|
|
| 114 |
return {
|
| 115 |
"message": "Piper TTS API is running",
|
| 116 |
"models_in_folder": files,
|
| 117 |
-
"supported_languages": [v for v in list(VOICE_MAP.keys())]
|
| 118 |
}
|
|
|
|
| 3 |
from piper import PiperVoice
|
| 4 |
import io
|
| 5 |
import os
|
| 6 |
+
import httpx
|
| 7 |
import wave
|
| 8 |
from pydantic import BaseModel
|
| 9 |
+
from typing import Optional, Literal
|
| 10 |
|
| 11 |
+
app = FastAPI("TTS App for my projects")
|
| 12 |
|
| 13 |
# Path where models will be stored in the container
|
| 14 |
MODEL_DIR = "./models"
|
|
|
|
| 42 |
"cy": {"gendered": False, "default": "cy_GB-gwryw_gogleddol-medium"}
|
| 43 |
}
|
| 44 |
|
| 45 |
+
IRISH_MAP = {
|
| 46 |
+
"Donegal": {"gendered":True, "male": "Dónall", "female":"Áine"},
|
| 47 |
+
"Kerry": {"gendered":True, "male": "Colm", "female":"Neasa"},
|
| 48 |
+
"Ring": {"gendered":False,"default":"Fianait"},
|
| 49 |
+
"Connemara": {"gendered":False,"default":"Sibéal"}
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
# Cache for loaded models to avoid re-loading from disk every request
|
| 53 |
loaded_voices = {}
|
| 54 |
|
|
|
|
| 67 |
class TTSRequest(BaseModel):
|
| 68 |
text: str
|
| 69 |
language: str
|
| 70 |
+
gender: Literal["male","female"] = "male"
|
| 71 |
+
dialect: Optional[Literal["Kerry", "Donegal", "Ring", "Connemara"]] = None
|
| 72 |
|
| 73 |
+
@app.post("/tts/piper")
|
| 74 |
async def tts_post(request: TTSRequest):
|
| 75 |
try:
|
| 76 |
lang_code = request.language.lower()
|
|
|
|
| 108 |
except Exception as e:
|
| 109 |
print(f"Error during TTS: {e}")
|
| 110 |
raise HTTPException(status_code=500, detail=str(e))
|
| 111 |
+
|
| 112 |
+
ABAIR_URL = "https://www.abair.ie/api2/synthesize"
|
| 113 |
+
|
| 114 |
+
@app.post("/tts/irish")
|
| 115 |
+
async def get_irish_tts(
|
| 116 |
+
request: TTSRequest
|
| 117 |
+
):
|
| 118 |
+
"""
|
| 119 |
+
Fetches Irish speech from ABAIR and returns an audio/wav stream.
|
| 120 |
+
"""
|
| 121 |
+
if request.dialect:
|
| 122 |
+
dialect=request.dialect
|
| 123 |
+
else:
|
| 124 |
+
dialect="Donegal"
|
| 125 |
+
|
| 126 |
+
if not IRISH_MAP.get(dialect, {}):
|
| 127 |
+
voice = "Áine"
|
| 128 |
+
dialect="Donegal"
|
| 129 |
+
elif IRISH_MAP[dialect]["gendered"]:
|
| 130 |
+
voice = IRISH_MAP[dialect][request.gender]
|
| 131 |
+
else:
|
| 132 |
+
voice =IRISH_MAP[dialect]["default"]
|
| 133 |
|
| 134 |
+
# Parameters for the ABAIR API
|
| 135 |
+
dialect_map = {
|
| 136 |
+
"Donegal": "ga_UL",
|
| 137 |
+
"Kerry": "ga_MU",
|
| 138 |
+
"Connemara": "ga_CO",
|
| 139 |
+
"Ring": "ga_MU"
|
| 140 |
+
}
|
| 141 |
+
params = {
|
| 142 |
+
"text": request.text,
|
| 143 |
+
"voice": voice,
|
| 144 |
+
"dialect": dialect_map.get(dialect, "ga_UL"),
|
| 145 |
+
"format": "wav",
|
| 146 |
+
"speed": 1.0
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
async with httpx.AsyncClient() as client:
|
| 150 |
+
try:
|
| 151 |
+
response = await client.get(ABAIR_URL, params=params, timeout=10.0)
|
| 152 |
+
|
| 153 |
+
if response.status_code != 200:
|
| 154 |
+
raise HTTPException(status_code=502, detail="ABAIR service error")
|
| 155 |
+
|
| 156 |
+
# Return the audio binary directly to the browser/client
|
| 157 |
+
return Response(content=response.content, media_type="audio/wav")
|
| 158 |
+
|
| 159 |
+
except httpx.RequestError as exc:
|
| 160 |
+
raise HTTPException(status_code=503, detail=f"Could not connect to ABAIR: {exc}")
|
| 161 |
+
|
| 162 |
@app.get("/health")
|
| 163 |
def home():
|
| 164 |
return {"status": "Piper TTS is running"}
|
|
|
|
| 174 |
return {
|
| 175 |
"message": "Piper TTS API is running",
|
| 176 |
"models_in_folder": files,
|
| 177 |
+
"supported_languages": [v for v in list(VOICE_MAP.keys())].append("ga")
|
| 178 |
}
|