sachin commited on
Commit
1f029a6
·
1 Parent(s): 0a3bb45

update-audio fi;e

Browse files
Files changed (1) hide show
  1. src/server/main.py +36 -14
src/server/main.py CHANGED
@@ -155,12 +155,16 @@ async def health_check():
155
  async def home():
156
  return RedirectResponse(url="/docs")
157
 
 
 
 
 
158
  @app.post("/v1/audio/speech",
159
  summary="Generate Speech from Text",
160
- description="Convert text to speech using an external TTS service.",
161
  tags=["Audio"],
162
  responses={
163
- 200: {"description": "Audio stream", "content": {"audio/mp3": {"example": "Binary audio data"}}},
164
  400: {"description": "Invalid or empty input"},
165
  502: {"description": "External TTS service unavailable"},
166
  504: {"description": "TTS service timeout"}
@@ -187,21 +191,39 @@ async def generate_audio(
187
  try:
188
  response = await tts_service.generate_speech(payload)
189
  response.raise_for_status()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
190
  except requests.HTTPError as e:
191
  logger.error(f"External TTS request failed: {str(e)}")
192
  raise HTTPException(status_code=502, detail=f"External TTS service error: {str(e)}")
193
-
194
- headers = {
195
- "Content-Disposition": "inline; filename=\"speech.mp3\"",
196
- "Cache-Control": "no-cache",
197
- "Content-Type": "audio/mp3"
198
- }
199
-
200
- return StreamingResponse(
201
- response.iter_content(chunk_size=8192),
202
- media_type="audio/mp3",
203
- headers=headers
204
- )
205
 
206
  @app.post("/v1/chat",
207
  response_model=ChatResponse,
 
155
  async def home():
156
  return RedirectResponse(url="/docs")
157
 
158
+ from fastapi.responses import FileResponse
159
+ import tempfile
160
+ import os
161
+
162
  @app.post("/v1/audio/speech",
163
  summary="Generate Speech from Text",
164
+ description="Convert text to speech using an external TTS service and return as a downloadable audio file.",
165
  tags=["Audio"],
166
  responses={
167
+ 200: {"description": "Audio file", "content": {"audio/mp3": {"example": "Binary audio data"}}},
168
  400: {"description": "Invalid or empty input"},
169
  502: {"description": "External TTS service unavailable"},
170
  504: {"description": "TTS service timeout"}
 
191
  try:
192
  response = await tts_service.generate_speech(payload)
193
  response.raise_for_status()
194
+
195
+ # Create a temporary file to store the audio
196
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_file:
197
+ for chunk in response.iter_content(chunk_size=8192):
198
+ if chunk:
199
+ temp_file.write(chunk)
200
+ temp_file_path = temp_file.name
201
+
202
+ # Prepare headers for the response
203
+ headers = {
204
+ "Content-Disposition": "attachment; filename=\"speech.mp3\"",
205
+ "Cache-Control": "no-cache",
206
+ }
207
+
208
+ # Return the file as a FileResponse
209
+ return FileResponse(
210
+ path=temp_file_path,
211
+ filename="speech.mp3",
212
+ media_type="audio/mp3",
213
+ headers=headers
214
+ )
215
+
216
  except requests.HTTPError as e:
217
  logger.error(f"External TTS request failed: {str(e)}")
218
  raise HTTPException(status_code=502, detail=f"External TTS service error: {str(e)}")
219
+ finally:
220
+ # Clean up the temporary file if it exists
221
+ if 'temp_file_path' in locals() and os.path.exists(temp_file_path):
222
+ try:
223
+ os.unlink(temp_file_path)
224
+ except Exception as e:
225
+ logger.error(f"Failed to delete temporary file: {str(e)}")
226
+
 
 
 
 
227
 
228
  @app.post("/v1/chat",
229
  response_model=ChatResponse,