Pushkar02-n commited on
Commit
bcc2f7a
·
verified ·
1 Parent(s): 75d6bee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -3
app.py CHANGED
@@ -7,6 +7,7 @@ from fastapi.security import HTTPBasic, HTTPBasicCredentials
7
  from fastapi.responses import FileResponse
8
  import nemo.collections.asr as nemo_asr
9
  from piper.voice import PiperVoice
 
10
 
11
  app = FastAPI(title="ASR & TTS API")
12
  security = HTTPBasic()
@@ -78,8 +79,11 @@ async def transcribe(file: UploadFile = File(...), _: str = Depends(verify_crede
78
  os.remove(audio_path)
79
  return {"text": transcription}
80
 
 
 
 
81
  @app.post("/tts")
82
- async def synthesize(text: str, _: str = Depends(verify_credentials)):
83
  if not tts_voice:
84
  raise HTTPException(status_code=503, detail="TTS model not loaded")
85
 
@@ -93,8 +97,14 @@ async def synthesize(text: str, _: str = Depends(verify_credentials)):
93
  wav_file.setnchannels(1)
94
  wav_file.setsampwidth(2)
95
  wav_file.setframerate(tts_voice.config.sample_rate)
96
- tts_voice.synthesize(text, wav_file)
 
97
 
98
  await loop.run_in_executor(None, generate_audio)
99
 
100
- return FileResponse(output_path, media_type="audio/wav")
 
 
 
 
 
 
7
  from fastapi.responses import FileResponse
8
  import nemo.collections.asr as nemo_asr
9
  from piper.voice import PiperVoice
10
+ from pydantic import BaseModel
11
 
12
  app = FastAPI(title="ASR & TTS API")
13
  security = HTTPBasic()
 
79
  os.remove(audio_path)
80
  return {"text": transcription}
81
 
82
+ class TTSRequest(BaseModel):
83
+ text: str
84
+
85
  @app.post("/tts")
86
+ async def synthesize(req: TTSRequest, _: str = Depends(verify_credentials)):
87
  if not tts_voice:
88
  raise HTTPException(status_code=503, detail="TTS model not loaded")
89
 
 
97
  wav_file.setnchannels(1)
98
  wav_file.setsampwidth(2)
99
  wav_file.setframerate(tts_voice.config.sample_rate)
100
+ # Make sure to call req.text here!
101
+ tts_voice.synthesize(req.text, wav_file)
102
 
103
  await loop.run_in_executor(None, generate_audio)
104
 
105
+ return FileResponse(
106
+ output_path,
107
+ media_type="audio/wav",
108
+ headers={"Content-Disposition": "attachment; filename=tts_output.wav"}
109
+ )
110
+