Update app.py
Browse files
app.py
CHANGED
|
@@ -35,8 +35,8 @@ RAM = memory_limit_gb() or "?"
|
|
| 35 |
print(f"[resources] Effective cores: {CORES} | RAM limit: {RAM} GB")
|
| 36 |
|
| 37 |
# ===== 2. FASTAPI SERVER & CORS SETUP =====
|
| 38 |
-
|
| 39 |
-
|
| 40 |
CORSMiddleware,
|
| 41 |
allow_origins=["*"],
|
| 42 |
allow_credentials=True,
|
|
@@ -73,15 +73,8 @@ def format_pitch(pitch_input):
|
|
| 73 |
# ===== 4. EXTERNAL STREAMING ENDPOINTS =====
|
| 74 |
|
| 75 |
# --- Direct Audio Stream (GET & POST) ---
|
| 76 |
-
@
|
| 77 |
async def tts_stream(request: Request):
|
| 78 |
-
"""
|
| 79 |
-
Accepts:
|
| 80 |
-
- text (string, required)
|
| 81 |
-
- voice (string, default: en-US-AriaNeural)
|
| 82 |
-
- speed / rate (float or string, e.g. 1.2 or '+20%')
|
| 83 |
-
- pitch (string or int, e.g. '+5Hz' or '+10%')
|
| 84 |
-
"""
|
| 85 |
if request.method == "POST":
|
| 86 |
try:
|
| 87 |
data = await request.json()
|
|
@@ -115,7 +108,7 @@ async def tts_stream(request: Request):
|
|
| 115 |
)
|
| 116 |
|
| 117 |
# --- OpenAI Audio Speech Compatible Endpoint ---
|
| 118 |
-
@
|
| 119 |
async def openai_speech(request: Request):
|
| 120 |
try:
|
| 121 |
data = await request.json()
|
|
@@ -136,7 +129,7 @@ async def openai_speech(request: Request):
|
|
| 136 |
return StreamingResponse(generate_audio(), media_type="audio/mpeg")
|
| 137 |
|
| 138 |
# --- Get All Available Voices ---
|
| 139 |
-
@
|
| 140 |
async def list_voices():
|
| 141 |
voices = await edge_tts.list_voices()
|
| 142 |
return JSONResponse(voices)
|
|
@@ -190,10 +183,11 @@ with gr.Blocks(title="High-Speed Edge-TTS API") as demo:
|
|
| 190 |
|
| 191 |
btn.click(fn=gradio_tts, inputs=[text_input, voice_dropdown, speed_slider, pitch_input], outputs=audio_output)
|
| 192 |
|
| 193 |
-
# ===== 6. MOUNT GRADIO
|
| 194 |
-
app = gr.mount_gradio_app(
|
| 195 |
|
| 196 |
if __name__ == "__main__":
|
| 197 |
import uvicorn
|
|
|
|
| 198 |
port = int(os.environ.get("PORT", 7860))
|
| 199 |
-
uvicorn.run(app, host="0.0.0.0", port=port)
|
|
|
|
| 35 |
print(f"[resources] Effective cores: {CORES} | RAM limit: {RAM} GB")
|
| 36 |
|
| 37 |
# ===== 2. FASTAPI SERVER & CORS SETUP =====
|
| 38 |
+
fastapi_app = FastAPI(title="Edge-TTS-Server")
|
| 39 |
+
fastapi_app.add_middleware(
|
| 40 |
CORSMiddleware,
|
| 41 |
allow_origins=["*"],
|
| 42 |
allow_credentials=True,
|
|
|
|
| 73 |
# ===== 4. EXTERNAL STREAMING ENDPOINTS =====
|
| 74 |
|
| 75 |
# --- Direct Audio Stream (GET & POST) ---
|
| 76 |
+
@fastapi_app.api_route("/tts", methods=["GET", "POST"])
|
| 77 |
async def tts_stream(request: Request):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
if request.method == "POST":
|
| 79 |
try:
|
| 80 |
data = await request.json()
|
|
|
|
| 108 |
)
|
| 109 |
|
| 110 |
# --- OpenAI Audio Speech Compatible Endpoint ---
|
| 111 |
+
@fastapi_app.api_route("/v1/audio/speech", methods=["POST"])
|
| 112 |
async def openai_speech(request: Request):
|
| 113 |
try:
|
| 114 |
data = await request.json()
|
|
|
|
| 129 |
return StreamingResponse(generate_audio(), media_type="audio/mpeg")
|
| 130 |
|
| 131 |
# --- Get All Available Voices ---
|
| 132 |
+
@fastapi_app.get("/tts/voices")
|
| 133 |
async def list_voices():
|
| 134 |
voices = await edge_tts.list_voices()
|
| 135 |
return JSONResponse(voices)
|
|
|
|
| 183 |
|
| 184 |
btn.click(fn=gradio_tts, inputs=[text_input, voice_dropdown, speed_slider, pitch_input], outputs=audio_output)
|
| 185 |
|
| 186 |
+
# ===== 6. MOUNT GRADIO ON FASTAPI & EXPORT =====
|
| 187 |
+
app = gr.mount_gradio_app(fastapi_app, demo, path="/")
|
| 188 |
|
| 189 |
if __name__ == "__main__":
|
| 190 |
import uvicorn
|
| 191 |
+
# Only run uvicorn locally when app.py is run directly
|
| 192 |
port = int(os.environ.get("PORT", 7860))
|
| 193 |
+
uvicorn.run("app:app", host="0.0.0.0", port=port, reload=False)
|