Spaces:
Sleeping
Sleeping
Update api.py
Browse files
api.py
CHANGED
|
@@ -2,8 +2,9 @@ import os
|
|
| 2 |
import uuid
|
| 3 |
import unicodedata
|
| 4 |
import re
|
|
|
|
| 5 |
from fastapi import FastAPI, HTTPException, Query
|
| 6 |
-
from fastapi.responses import FileResponse
|
| 7 |
from pydantic import BaseModel
|
| 8 |
import edge_tts
|
| 9 |
|
|
@@ -39,11 +40,13 @@ class TTSRequest(BaseModel):
|
|
| 39 |
gender: str = "female"
|
| 40 |
rate: str = "+0%"
|
| 41 |
|
| 42 |
-
@app.get("/")
|
| 43 |
-
def
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
# JSON endpoint (original)
|
| 47 |
@app.post("/tts")
|
| 48 |
async def synthesize(req: TTSRequest):
|
| 49 |
text = clean_text(req.text)
|
|
@@ -67,8 +70,6 @@ async def synthesize(req: TTSRequest):
|
|
| 67 |
|
| 68 |
return FileResponse(path=output_path, media_type="audio/mpeg", filename="output.mp3", headers={"X-Voice": voice})
|
| 69 |
|
| 70 |
-
|
| 71 |
-
# Plain text endpoint (easier for Swagger)
|
| 72 |
@app.post("/tts-text")
|
| 73 |
async def synthesize_text(
|
| 74 |
text: str = Query(..., description="النص المراد تحويله"),
|
|
@@ -97,7 +98,6 @@ async def synthesize_text(
|
|
| 97 |
|
| 98 |
return FileResponse(path=output_path, media_type="audio/mpeg", filename="output.mp3", headers={"X-Voice": voice})
|
| 99 |
|
| 100 |
-
|
| 101 |
@app.get("/voices")
|
| 102 |
def get_voices():
|
| 103 |
return VOICES
|
|
|
|
| 2 |
import uuid
|
| 3 |
import unicodedata
|
| 4 |
import re
|
| 5 |
+
import pathlib
|
| 6 |
from fastapi import FastAPI, HTTPException, Query
|
| 7 |
+
from fastapi.responses import FileResponse, HTMLResponse
|
| 8 |
from pydantic import BaseModel
|
| 9 |
import edge_tts
|
| 10 |
|
|
|
|
| 40 |
gender: str = "female"
|
| 41 |
rate: str = "+0%"
|
| 42 |
|
| 43 |
+
@app.get("/", response_class=HTMLResponse)
|
| 44 |
+
def get_app():
|
| 45 |
+
html_path = pathlib.Path("index.html")
|
| 46 |
+
if html_path.exists():
|
| 47 |
+
return html_path.read_text(encoding="utf-8")
|
| 48 |
+
return "<h1>TTS API is running</h1>"
|
| 49 |
|
|
|
|
| 50 |
@app.post("/tts")
|
| 51 |
async def synthesize(req: TTSRequest):
|
| 52 |
text = clean_text(req.text)
|
|
|
|
| 70 |
|
| 71 |
return FileResponse(path=output_path, media_type="audio/mpeg", filename="output.mp3", headers={"X-Voice": voice})
|
| 72 |
|
|
|
|
|
|
|
| 73 |
@app.post("/tts-text")
|
| 74 |
async def synthesize_text(
|
| 75 |
text: str = Query(..., description="النص المراد تحويله"),
|
|
|
|
| 98 |
|
| 99 |
return FileResponse(path=output_path, media_type="audio/mpeg", filename="output.mp3", headers={"X-Voice": voice})
|
| 100 |
|
|
|
|
| 101 |
@app.get("/voices")
|
| 102 |
def get_voices():
|
| 103 |
return VOICES
|