Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ from fastapi.responses import StreamingResponse
|
|
| 3 |
from piper import PiperVoice
|
| 4 |
import io
|
| 5 |
import os
|
|
|
|
| 6 |
from pydantic import BaseModel
|
| 7 |
|
| 8 |
app = FastAPI()
|
|
@@ -84,7 +85,15 @@ async def tts_post(request: TTSRequest):
|
|
| 84 |
# 3. Load and Synthesize
|
| 85 |
voice = get_voice(model_name)
|
| 86 |
wav_buffer = io.BytesIO()
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
wav_buffer.seek(0)
|
| 89 |
|
| 90 |
return Response(content=wav_buffer.getvalue(), media_type="audio/wav")
|
|
|
|
| 3 |
from piper import PiperVoice
|
| 4 |
import io
|
| 5 |
import os
|
| 6 |
+
import wave
|
| 7 |
from pydantic import BaseModel
|
| 8 |
|
| 9 |
app = FastAPI()
|
|
|
|
| 85 |
# 3. Load and Synthesize
|
| 86 |
voice = get_voice(model_name)
|
| 87 |
wav_buffer = io.BytesIO()
|
| 88 |
+
with wave.open(wav_buffer, "wb") as wav_file:
|
| 89 |
+
# Piper yields raw PCM; you must set these params manually
|
| 90 |
+
wav_file.setnchannels(1) # Mono
|
| 91 |
+
wav_file.setsampwidth(2) # 16-bit
|
| 92 |
+
wav_file.setframerate(voice.config.sample_rate)
|
| 93 |
+
|
| 94 |
+
# Synthesize returns an iterator of audio chunks
|
| 95 |
+
for audio_bytes in voice.synthesize(request.text):
|
| 96 |
+
wav_file.writeframes(audio_bytes)
|
| 97 |
wav_buffer.seek(0)
|
| 98 |
|
| 99 |
return Response(content=wav_buffer.getvalue(), media_type="audio/wav")
|