Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,22 @@
|
|
| 1 |
-
import os
|
| 2 |
import uuid
|
| 3 |
from pathlib import Path
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
from fastapi.responses import FileResponse, JSONResponse
|
| 7 |
from pydantic import BaseModel
|
| 8 |
-
import edge_tts
|
| 9 |
|
| 10 |
app = FastAPI(title="Edge TTS API")
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
TMP_DIR = Path("/tmp/edge_tts_api")
|
| 13 |
TMP_DIR.mkdir(parents=True, exist_ok=True)
|
| 14 |
|
|
@@ -21,6 +29,22 @@ class TTSRequest(BaseModel):
|
|
| 21 |
pitch: str = "+0Hz"
|
| 22 |
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
@app.get("/")
|
| 25 |
async def root():
|
| 26 |
return {
|
|
@@ -29,8 +53,8 @@ async def root():
|
|
| 29 |
"routes": {
|
| 30 |
"health": "/health",
|
| 31 |
"voices": "/voices",
|
| 32 |
-
"tts": "/tts"
|
| 33 |
-
}
|
| 34 |
}
|
| 35 |
|
| 36 |
|
|
@@ -40,15 +64,59 @@ async def health():
|
|
| 40 |
|
| 41 |
|
| 42 |
@app.get("/voices")
|
| 43 |
-
async def voices(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
try:
|
| 45 |
voices_data = await edge_tts.list_voices()
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
except Exception as e:
|
| 49 |
raise HTTPException(status_code=500, detail=f"Failed to fetch voices: {str(e)}")
|
| 50 |
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
@app.post("/tts")
|
| 53 |
async def tts(req: TTSRequest):
|
| 54 |
text = req.text.strip()
|
|
@@ -64,7 +132,7 @@ async def tts(req: TTSRequest):
|
|
| 64 |
voice=req.voice,
|
| 65 |
rate=req.rate,
|
| 66 |
volume=req.volume,
|
| 67 |
-
pitch=req.pitch
|
| 68 |
)
|
| 69 |
await communicate.save(str(output_file))
|
| 70 |
|
|
@@ -74,7 +142,7 @@ async def tts(req: TTSRequest):
|
|
| 74 |
return FileResponse(
|
| 75 |
path=str(output_file),
|
| 76 |
media_type="audio/mpeg",
|
| 77 |
-
filename="speech.mp3"
|
| 78 |
)
|
| 79 |
except Exception as e:
|
| 80 |
raise HTTPException(status_code=500, detail=f"TTS generation failed: {str(e)}")
|
|
|
|
|
|
|
| 1 |
import uuid
|
| 2 |
from pathlib import Path
|
| 3 |
|
| 4 |
+
import edge_tts
|
| 5 |
+
from fastapi import FastAPI, HTTPException, Query
|
| 6 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
from fastapi.responses import FileResponse, JSONResponse
|
| 8 |
from pydantic import BaseModel
|
|
|
|
| 9 |
|
| 10 |
app = FastAPI(title="Edge TTS API")
|
| 11 |
|
| 12 |
+
app.add_middleware(
|
| 13 |
+
CORSMiddleware,
|
| 14 |
+
allow_origins=["*"],
|
| 15 |
+
allow_credentials=False,
|
| 16 |
+
allow_methods=["*"],
|
| 17 |
+
allow_headers=["*"],
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
TMP_DIR = Path("/tmp/edge_tts_api")
|
| 21 |
TMP_DIR.mkdir(parents=True, exist_ok=True)
|
| 22 |
|
|
|
|
| 29 |
pitch: str = "+0Hz"
|
| 30 |
|
| 31 |
|
| 32 |
+
def simplify_voice(v: dict) -> dict:
|
| 33 |
+
short_name = v.get("ShortName", "")
|
| 34 |
+
locale = v.get("Locale", "")
|
| 35 |
+
return {
|
| 36 |
+
"Name": v.get("Name", ""),
|
| 37 |
+
"ShortName": short_name,
|
| 38 |
+
"FriendlyName": v.get("FriendlyName", ""),
|
| 39 |
+
"Locale": locale,
|
| 40 |
+
"Gender": v.get("Gender", ""),
|
| 41 |
+
"SuggestedCodec": v.get("SuggestedCodec", ""),
|
| 42 |
+
"Status": v.get("Status", ""),
|
| 43 |
+
"IsMultilingual": "Multilingual" in short_name,
|
| 44 |
+
"IsBulgarian": locale == "bg-BG",
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
|
| 48 |
@app.get("/")
|
| 49 |
async def root():
|
| 50 |
return {
|
|
|
|
| 53 |
"routes": {
|
| 54 |
"health": "/health",
|
| 55 |
"voices": "/voices",
|
| 56 |
+
"tts": "/tts",
|
| 57 |
+
},
|
| 58 |
}
|
| 59 |
|
| 60 |
|
|
|
|
| 64 |
|
| 65 |
|
| 66 |
@app.get("/voices")
|
| 67 |
+
async def voices(
|
| 68 |
+
locale: str | None = Query(default=None),
|
| 69 |
+
multilingual_only: bool = Query(default=False),
|
| 70 |
+
bulgarian_only: bool = Query(default=False),
|
| 71 |
+
):
|
| 72 |
try:
|
| 73 |
voices_data = await edge_tts.list_voices()
|
| 74 |
+
simplified = [simplify_voice(v) for v in voices_data]
|
| 75 |
+
simplified.sort(key=lambda v: (v["Locale"], v["ShortName"]))
|
| 76 |
+
|
| 77 |
+
if locale:
|
| 78 |
+
simplified = [v for v in simplified if v["Locale"].lower() == locale.lower()]
|
| 79 |
+
|
| 80 |
+
if multilingual_only:
|
| 81 |
+
simplified = [v for v in simplified if v["IsMultilingual"]]
|
| 82 |
+
|
| 83 |
+
if bulgarian_only:
|
| 84 |
+
simplified = [v for v in simplified if v["IsBulgarian"]]
|
| 85 |
+
|
| 86 |
+
return JSONResponse(
|
| 87 |
+
content={
|
| 88 |
+
"count": len(simplified),
|
| 89 |
+
"voices": simplified,
|
| 90 |
+
}
|
| 91 |
+
)
|
| 92 |
except Exception as e:
|
| 93 |
raise HTTPException(status_code=500, detail=f"Failed to fetch voices: {str(e)}")
|
| 94 |
|
| 95 |
|
| 96 |
+
@app.get("/voices/bulgarian")
|
| 97 |
+
async def voices_bulgarian():
|
| 98 |
+
try:
|
| 99 |
+
voices_data = await edge_tts.list_voices()
|
| 100 |
+
simplified = [simplify_voice(v) for v in voices_data]
|
| 101 |
+
simplified = [v for v in simplified if v["IsBulgarian"]]
|
| 102 |
+
simplified.sort(key=lambda v: v["ShortName"])
|
| 103 |
+
return {"count": len(simplified), "voices": simplified}
|
| 104 |
+
except Exception as e:
|
| 105 |
+
raise HTTPException(status_code=500, detail=f"Failed to fetch Bulgarian voices: {str(e)}")
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
@app.get("/voices/multilingual")
|
| 109 |
+
async def voices_multilingual():
|
| 110 |
+
try:
|
| 111 |
+
voices_data = await edge_tts.list_voices()
|
| 112 |
+
simplified = [simplify_voice(v) for v in voices_data]
|
| 113 |
+
simplified = [v for v in simplified if v["IsMultilingual"]]
|
| 114 |
+
simplified.sort(key=lambda v: (v["Locale"], v["ShortName"]))
|
| 115 |
+
return {"count": len(simplified), "voices": simplified}
|
| 116 |
+
except Exception as e:
|
| 117 |
+
raise HTTPException(status_code=500, detail=f"Failed to fetch multilingual voices: {str(e)}")
|
| 118 |
+
|
| 119 |
+
|
| 120 |
@app.post("/tts")
|
| 121 |
async def tts(req: TTSRequest):
|
| 122 |
text = req.text.strip()
|
|
|
|
| 132 |
voice=req.voice,
|
| 133 |
rate=req.rate,
|
| 134 |
volume=req.volume,
|
| 135 |
+
pitch=req.pitch,
|
| 136 |
)
|
| 137 |
await communicate.save(str(output_file))
|
| 138 |
|
|
|
|
| 142 |
return FileResponse(
|
| 143 |
path=str(output_file),
|
| 144 |
media_type="audio/mpeg",
|
| 145 |
+
filename="speech.mp3",
|
| 146 |
)
|
| 147 |
except Exception as e:
|
| 148 |
raise HTTPException(status_code=500, detail=f"TTS generation failed: {str(e)}")
|