Added Streaming Stopping

Currently this is in testing. 

curl -X POST http://localhost:7851/api/stop-generation

Would stop the current streaming generation.

This is more a v2 feature that is just getting tried out by someone else ATM.
This commit is contained in:
erew123 authored and GitHub committed 2024-04-28 20:37:41 +01:00
1 parent 1e1b7fdd8c
commit eb08d31509
1 file changed
+17 -1
+17 -1
View File
@@ -486,9 +486,12 @@ async def generate_audio(text, voice, language, temperature, repetition_penalty,
return response
async for _ in response:
pass
stop_generation = False
async def generate_audio_internal(text, voice, language, temperature, repetition_penalty, output_file, streaming):
global model
global stop_generation
if params["low_vram"] and device == "cpu":
await switch_device()
generate_start_time = time.time() # Record the start time of generating TTS
@@ -539,6 +542,11 @@ async def generate_audio_internal(text, voice, language, temperature, repetition
yield wav_buf.read()
for i, chunk in enumerate(output):
if stop_generation:
print(f"[{params['branding']}TTSGen] Stopping audio generation.")
file_chunks.clear() # Clear the file_chunks list
stop_generation = False
break
file_chunks.append(chunk)
if isinstance(chunk, list):
chunk = torch.cat(chunk, dim=0)
@@ -547,6 +555,8 @@ async def generate_audio_internal(text, voice, language, temperature, repetition
chunk = np.clip(chunk, -1, 1)
chunk = (chunk * 32767).astype(np.int16)
yield chunk.tobytes()
#if streaming_debug:
# print(f"Stream audio generation: Yielded audio chunk {i}.")
else:
# Non-streaming-specific operation
torchaudio.save(output_file, torch.tensor(output["wav"]).unsqueeze(0), 24000)
@@ -854,6 +864,12 @@ async def tts_generate_streaming(request: Request, text: str = Form(...), voice:
print(f"An error occurred: {e}")
return JSONResponse(content={"error": "An error occurred"}, status_code=500)
@app.put("/api/stop-generation")
async def stop_generation_endpoint():
global stop_generation
stop_generation = True
return {"message": "Generation stopped"}
##############################
#### Standard Generation ####
##############################