Jofthomas HF staff commited on
Commit
f670ab7
1 Parent(s): fb67188

Update TextGen/router.py

Browse files
Files changed (1) hide show
  1. TextGen/router.py +20 -6
TextGen/router.py CHANGED
@@ -1,6 +1,9 @@
1
  import os
2
  import time
3
  from io import BytesIO
 
 
 
4
  from langchain_core.pydantic_v1 import BaseModel, Field
5
  from fastapi import FastAPI, HTTPException, Query, Request
6
  from fastapi.responses import StreamingResponse,Response
@@ -285,6 +288,7 @@ async def generate_wav(message: VoiceMessage):
285
  # yield chunk
286
 
287
  # return StreamingResponse(audio_stream(), media_type="audio/mpeg")
 
288
  @app.get("/generate_voice_coqui", response_class=StreamingResponse)
289
  @app.post("/generate_voice_coqui", response_class=StreamingResponse)
290
  def generate_voice_coqui(message: VoiceMessage = None):
@@ -306,13 +310,23 @@ def generate_voice_coqui(message: VoiceMessage = None):
306
  no_lang_auto_detect=False,
307
  agree=True,
308
  )
309
- # Generate the audio stream from ElevenLabs
310
  for chunk in result:
311
- print("received : ",chunk)
312
- # Assuming chunk is a tuple, where the second element is the audio data array
313
- yield chunk[1].tobytes()
314
-
315
- return StreamingResponse(audio_stream())
 
 
 
 
 
 
 
 
 
 
316
  @app.post("/generate_song")
317
  @app.get("/generate_song")
318
  async def generate_song(request:SongRequest,httprequest: Request):
 
1
  import os
2
  import time
3
  from io import BytesIO
4
+ import io
5
+ import wave
6
+ import numpy as np
7
  from langchain_core.pydantic_v1 import BaseModel, Field
8
  from fastapi import FastAPI, HTTPException, Query, Request
9
  from fastapi.responses import StreamingResponse,Response
 
288
  # yield chunk
289
 
290
  # return StreamingResponse(audio_stream(), media_type="audio/mpeg")
291
+
292
  @app.get("/generate_voice_coqui", response_class=StreamingResponse)
293
  @app.post("/generate_voice_coqui", response_class=StreamingResponse)
294
  def generate_voice_coqui(message: VoiceMessage = None):
 
310
  no_lang_auto_detect=False,
311
  agree=True,
312
  )
313
+
314
  for chunk in result:
315
+ print("received : ", chunk)
316
+ sample_rate, audio_data = chunk # Assuming chunk[0] is sample rate, chunk[1] is audio NumPy array
317
+
318
+ # Convert NumPy array to bytes in WAV format
319
+ with io.BytesIO() as wav_buffer:
320
+ with wave.open(wav_buffer, 'wb') as wav_file:
321
+ wav_file.setnchannels(1) # Mono channel
322
+ wav_file.setsampwidth(2) # Sample width in bytes (2 bytes for int16)
323
+ wav_file.setframerate(sample_rate)
324
+ wav_file.writeframes(audio_data.astype(np.int16).tobytes())
325
+
326
+ wav_buffer.seek(0)
327
+ yield wav_buffer.read()
328
+
329
+ return StreamingResponse(audio_stream(), media_type="audio/wav")
330
  @app.post("/generate_song")
331
  @app.get("/generate_song")
332
  async def generate_song(request:SongRequest,httprequest: Request):