Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import numpy as np
|
|
| 5 |
import gradio as gr
|
| 6 |
import soundfile as sf
|
| 7 |
from fastapi import FastAPI, Request, HTTPException
|
|
|
|
| 8 |
from tts_arabic import tts as arabic_tts
|
| 9 |
|
| 10 |
# --------------------------
|
|
@@ -152,8 +153,8 @@ app = FastAPI()
|
|
| 152 |
@app.post("/tts")
|
| 153 |
async def tts_api(request: Request):
|
| 154 |
"""
|
| 155 |
-
Endpoint JSON صريح:
|
| 156 |
POST /tts
|
|
|
|
| 157 |
Headers:
|
| 158 |
- x-api-key: ...
|
| 159 |
- Content-Type: application/json
|
|
@@ -171,11 +172,7 @@ async def tts_api(request: Request):
|
|
| 171 |
}
|
| 172 |
|
| 173 |
Response:
|
| 174 |
-
|
| 175 |
-
"wav_base64": "...",
|
| 176 |
-
"sample_rate": 22050,
|
| 177 |
-
"status": "..."
|
| 178 |
-
}
|
| 179 |
"""
|
| 180 |
key = request.headers.get("x-api-key")
|
| 181 |
if key != API_KEY:
|
|
@@ -200,18 +197,18 @@ async def tts_api(request: Request):
|
|
| 200 |
|
| 201 |
sr, data = audio
|
| 202 |
|
| 203 |
-
#
|
| 204 |
buffer = BytesIO()
|
| 205 |
sf.write(buffer, data, sr, format="WAV")
|
| 206 |
-
|
| 207 |
-
wav_base64 = base64.b64encode(wav_bytes).decode("utf-8")
|
| 208 |
|
| 209 |
-
|
| 210 |
-
"
|
| 211 |
-
"sample_rate": sr,
|
| 212 |
-
"status": status,
|
| 213 |
}
|
| 214 |
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
# نركّب Gradio على الجذر "/"
|
| 217 |
app = gr.mount_gradio_app(app, demo, path="/")
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
import soundfile as sf
|
| 7 |
from fastapi import FastAPI, Request, HTTPException
|
| 8 |
+
from fastapi.responses import StreamingResponse
|
| 9 |
from tts_arabic import tts as arabic_tts
|
| 10 |
|
| 11 |
# --------------------------
|
|
|
|
| 153 |
@app.post("/tts")
|
| 154 |
async def tts_api(request: Request):
|
| 155 |
"""
|
|
|
|
| 156 |
POST /tts
|
| 157 |
+
|
| 158 |
Headers:
|
| 159 |
- x-api-key: ...
|
| 160 |
- Content-Type: application/json
|
|
|
|
| 172 |
}
|
| 173 |
|
| 174 |
Response:
|
| 175 |
+
- Binary audio/wav (StreamingResponse)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
"""
|
| 177 |
key = request.headers.get("x-api-key")
|
| 178 |
if key != API_KEY:
|
|
|
|
| 197 |
|
| 198 |
sr, data = audio
|
| 199 |
|
| 200 |
+
# ----- تحويل الـ numpy إلى ملف WAV في الذاكرة -----
|
| 201 |
buffer = BytesIO()
|
| 202 |
sf.write(buffer, data, sr, format="WAV")
|
| 203 |
+
buffer.seek(0)
|
|
|
|
| 204 |
|
| 205 |
+
headers = {
|
| 206 |
+
"Content-Disposition": 'attachment; filename="tts.wav"'
|
|
|
|
|
|
|
| 207 |
}
|
| 208 |
|
| 209 |
+
# StreamingResponse يرجّع ملف صوتي حقيقي
|
| 210 |
+
return StreamingResponse(buffer, media_type="audio/wav", headers=headers)
|
| 211 |
+
|
| 212 |
|
| 213 |
# نركّب Gradio على الجذر "/"
|
| 214 |
app = gr.mount_gradio_app(app, demo, path="/")
|