forked from SesameAILabs/csm
-
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·1205 lines (1008 loc) · 46.2 KB
/
main.py
File metadata and controls
executable file
·1205 lines (1008 loc) · 46.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asyncio
import os
os.environ["OMP_NUM_THREADS"] = "1"
os.environ["MKL_NUM_THREADS"] = "1"
os.environ["CUDA_LAUNCH_BLOCKING"] = "1"
os.environ["PYTORCH_DISABLE_CUDA_GRAPHS"] = "1"
import platform
import sqlite3
import time
import threading
import json
import queue
from fastapi.websockets import WebSocketState
import torch
import torchaudio
import sounddevice as sd
import numpy as np
import whisper
from fastapi import FastAPI, WebSocket, WebSocketDisconnect, Request
from fastapi.staticfiles import StaticFiles
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.templating import Jinja2Templates
from sqlalchemy import create_engine, Column, Integer, String, Text
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from typing import Optional
from generator import Segment, load_csm_1b_local
from llm_interface import LLMInterface
from rag_system import RAGSystem
from vad import AudioStreamProcessor
from pydantic import BaseModel
import logging
from config import ConfigManager
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
import re
speaking_start_time = 0.0 # set every time the AI begins a new turn
MIN_BARGE_LATENCY = 0.9
speaker_counters = {
0: 0, # AI
1: 0 # User
}
current_generation_id = 1
pending_user_inputs = []
user_input_lock = threading.Lock()
audio_fade_duration = 0.3 # seconds for fade-out
last_interrupt_time = 0
interrupt_cooldown = 6.0 # seconds between allowed interrupts
audio_chunk_buffer = [] # Buffer to store the most recent audio chunks for fade-out
# Setup logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
model_thread = None
model_queue = queue.Queue()
model_result_queue = queue.Queue()
model_thread_running = threading.Event()
llm_lock = threading.Lock()
audio_gen_lock = threading.Lock()
# Database
Base = declarative_base()
engine = create_engine("sqlite:///companion.db")
SessionLocal = sessionmaker(bind=engine)
class Conversation(Base):
__tablename__ = "conversations"
id = Column(Integer, primary_key=True, index=True)
session_id = Column(String, index=True)
timestamp = Column(String)
user_message = Column(Text)
ai_message = Column(Text)
audio_path = Column(String)
Base.metadata.create_all(bind=engine)
# Pydantic config schema
class CompanionConfig(BaseModel):
system_prompt: str
reference_audio_path: str
reference_text: str
reference_audio_path2: Optional[str] = None # optional field
reference_text2: Optional[str] = None # optional field
reference_audio_path3: Optional[str] = None # optional field
reference_text3: Optional[str] = None # optional field
model_path: str
llm_path: str
max_tokens: int = 8192
voice_speaker_id: int = 0
vad_enabled: bool = True
vad_threshold: float = 0.5
embedding_model: str = "all-MiniLM-L6-v2"
# Global state
conversation_history = []
config = None
audio_queue = queue.Queue()
is_speaking = False
interrupt_flag = threading.Event()
generator = None
llm = None
rag = None
vad_processor = None
reference_segments = []
active_connections = []
message_queue = asyncio.Queue()
# Async event loop
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
# FastAPI
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
config_manager = ConfigManager()
model_id = "openai/whisper-large-v3-turbo"
# Whisper
whisper_model = AutoModelForSpeechSeq2Seq.from_pretrained(
model_id, torch_dtype=torch.float16, low_cpu_mem_usage=True, use_safetensors=True
)
whisper_model.to("cuda")
processor = AutoProcessor.from_pretrained(model_id)
whisper_pipe = pipeline(
"automatic-speech-recognition",
model=whisper_model,
tokenizer=processor.tokenizer,
feature_extractor=processor.feature_extractor,
torch_dtype=torch.float16,
device='cuda',
)
# Background queue
async def process_message_queue():
while True:
message = await message_queue.get()
for client in active_connections[:]:
try:
if client.client_state == WebSocketState.CONNECTED:
await client.send_json(message)
except Exception as e:
logger.error(f"Error in message queue for client: {e}")
if client in active_connections:
active_connections.remove(client)
message_queue.task_done()
def load_reference_segments(config_data: CompanionConfig):
"""Load multiple reference clips for voice‑cloning."""
global reference_segments
reference_segments = []
# Load primary reference (required)
if os.path.isfile(config_data.reference_audio_path):
logger.info(f"Loading primary reference audio: {config_data.reference_audio_path}")
wav, sr = torchaudio.load(config_data.reference_audio_path)
wav = torchaudio.functional.resample(wav.squeeze(0),
orig_freq=sr,
new_freq=24_000)
reference_segments.append(Segment(text=config_data.reference_text,
speaker=config_data.voice_speaker_id,
audio=wav))
else:
logger.warning(f"Primary reference audio '{config_data.reference_audio_path}' not found.")
# Load second reference (optional)
if config_data.reference_audio_path2 and os.path.isfile(config_data.reference_audio_path2):
logger.info(f"Loading second reference audio: {config_data.reference_audio_path2}")
wav, sr = torchaudio.load(config_data.reference_audio_path2)
wav = torchaudio.functional.resample(wav.squeeze(0),
orig_freq=sr,
new_freq=24_000)
reference_segments.append(Segment(text=config_data.reference_text2,
speaker=config_data.voice_speaker_id,
audio=wav))
# Load third reference (optional)
if config_data.reference_audio_path3 and os.path.isfile(config_data.reference_audio_path3):
logger.info(f"Loading third reference audio: {config_data.reference_audio_path3}")
wav, sr = torchaudio.load(config_data.reference_audio_path3)
wav = torchaudio.functional.resample(wav.squeeze(0),
orig_freq=sr,
new_freq=24_000)
reference_segments.append(Segment(text=config_data.reference_text3,
speaker=config_data.voice_speaker_id,
audio=wav))
logger.info(f"Loaded {len(reference_segments)} reference audio segments.")
def transcribe_audio(audio_data, sample_rate):
global whisper_model
audio_np = np.array(audio_data).astype(np.float32)
if sample_rate != 16000:
try:
audio_tensor = torch.tensor(audio_np).unsqueeze(0)
audio_tensor = torchaudio.functional.resample(audio_tensor, orig_freq=sample_rate, new_freq=16000)
audio_np = audio_tensor.squeeze(0).numpy()
except: pass
try:
with torch.jit.optimized_execution(False):
result = whisper_pipe(audio_np, generate_kwargs={"language": "english"})
return result["text"]
except:
return "[Transcription error]"
def initialize_models(config_data: CompanionConfig):
global generator, llm, rag, vad_processor, config
config = config_data
logger.info("Loading LLM …")
llm = LLMInterface(config_data.llm_path,
config_data.max_tokens)
logger.info("Loading RAG …")
rag = RAGSystem("companion.db",
model_name=config_data.embedding_model)
vad_model, vad_utils = torch.hub.load('snakers4/silero-vad',
model='silero_vad',
force_reload=False)
vad_processor = AudioStreamProcessor(
model=vad_model,
utils=vad_utils,
sample_rate=16_000,
vad_threshold=config_data.vad_threshold,
callbacks={"on_speech_start": on_speech_start,
"on_speech_end": on_speech_end},
)
load_reference_segments(config_data)
start_model_thread()
logger.info("Compiling / warming‑up voice model …")
t0 = time.time()
# send a dummy request; max 0.5 s of audio, result discarded
model_queue.put((
"warm‑up.", # text
config_data.voice_speaker_id, # speaker
[], # no context
500, # max_ms
0.7, # temperature
40, # top‑k
))
# block until worker signals EOS (None marker)
while True:
r = model_result_queue.get()
if r is None:
break
logger.info(f"Voice model ready in {time.time() - t0:.1f}s")
def on_speech_start():
asyncio.run_coroutine_threadsafe(
message_queue.put(
{
"type": "vad_status",
"status": "speech_started",
"should_interrupt": False, # always False – UI never barges-in here
}
),
loop,
)
def on_speech_end(audio_data, sample_rate):
try:
logger.info("Transcription starting")
user_text = transcribe_audio(audio_data, sample_rate)
logger.info(f"Transcription completed: '{user_text}'")
session_id = "default"
speaker_id = 1
index = speaker_counters[speaker_id]
user_audio_path = f"audio/user/{session_id}_user_{index}.wav"
os.makedirs(os.path.dirname(user_audio_path), exist_ok=True)
audio_tensor = torch.tensor(audio_data).unsqueeze(0)
save_audio_and_trim(user_audio_path, session_id, speaker_id, audio_tensor.squeeze(0), sample_rate)
add_segment(user_text, speaker_id, audio_tensor.squeeze(0))
logger.info(f"User audio saved and segment appended: {user_audio_path}")
speaker_counters[speaker_id] += 1
# Send transcription to clients
asyncio.run_coroutine_threadsafe(
message_queue.put({"type": "transcription", "text": user_text}),
loop
)
threading.Thread(target=lambda: process_user_input(user_text, session_id), daemon=True).start()
except Exception as e:
logger.error(f"VAD callback failed: {e}")
def process_pending_inputs():
"""Process only the latest user input after an interruption"""
global pending_user_inputs, is_speaking, interrupt_flag
time.sleep(0.2)
is_speaking = False
interrupt_flag.clear()
with user_input_lock:
if not pending_user_inputs:
logger.info("No pending user inputs to process")
return
# Only take the most recent input and ignore others
latest_input = pending_user_inputs[-1]
logger.info(f"Processing only latest input: '{latest_input[0]}'")
# Clear all pending inputs
pending_user_inputs = []
# Process only the latest input
user_text, session_id = latest_input
process_user_input(user_text, session_id)
def process_user_input(user_text, session_id="default"):
global config, is_speaking, pending_user_inputs, interrupt_flag
# Skip empty messages
if not user_text or user_text.strip() == "":
logger.warning("Empty user input received, ignoring")
return
interrupt_flag.clear()
is_speaking = False
# Check if we're currently supposed to be speaking
if is_speaking:
logger.info(f"AI is currently speaking, adding input to pending queue: '{user_text}'")
with user_input_lock:
# Only keep the most recent input, replacing any existing ones
pending_user_inputs = [(user_text, session_id)]
logger.info(f"Added user input as the only pending input: '{user_text}'")
# Request interruption if not already interrupted
if not interrupt_flag.is_set():
logger.info("Automatically interrupting current speech for new input")
interrupt_flag.set()
# Notify clients of interruption
asyncio.run_coroutine_threadsafe(
message_queue.put({"type": "audio_status", "status": "interrupted"}),
loop
)
# Allow a short delay before processing the new input
time.sleep(0.3)
# Process the pending input after interruption
process_pending_inputs()
return
interrupt_flag.clear()
# Normal processing continues...
logger.info(f"Processing user input: '{user_text}'")
context = "\n".join([f"User: {msg['user']}\nAI: {msg['ai']}" for msg in conversation_history[-5:]])
rag_context = rag.query(user_text)
system_prompt = config.system_prompt
if rag_context:
system_prompt += f"\n\nRelevant context:\n{rag_context}"
# Notify clients that we're thinking
asyncio.run_coroutine_threadsafe(
message_queue.put({"type": "status", "message": "Thinking..."}),
loop
)
try:
with llm_lock:
ai_response = llm.generate_response(system_prompt, user_text, context)
timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
conversation_history.append({
"timestamp": timestamp,
"user": user_text,
"ai": ai_response
})
try:
db = SessionLocal()
conv = Conversation(
session_id=session_id,
timestamp=timestamp,
user_message=user_text,
ai_message=ai_response,
audio_path=""
)
db.add(conv)
db.commit()
index = speaker_counters[0]
output_file = f"audio/ai/{session_id}_response_{index}.wav"
speaker_counters[0] += 1
conv.audio_path = output_file
db.commit()
db.close()
except Exception as e:
logger.error(f"Database error: {e}")
threading.Thread(target=lambda: rag.add_conversation(user_text, ai_response), daemon=True).start()
asyncio.run_coroutine_threadsafe(
message_queue.put({"type": "audio_status", "status": "preparing"}),
loop
)
# Small delay to ensure client is ready
time.sleep(0.2)
# Send the response to clients
asyncio.run_coroutine_threadsafe(
message_queue.put({"type": "response", "text": ai_response}),
loop
)
time.sleep(0.5)
if is_speaking:
logger.warning("Still speaking when trying to start new audio - forcing interrupt")
interrupt_flag.set()
is_speaking = False
time.sleep(0.5) # Give time for cleanup
interrupt_flag.clear() # Make absolutely sure
is_speaking = False # Reset for audio thread to take over
# Start audio generation in a new thread
threading.Thread(target=audio_generation_thread, args=(ai_response, output_file), daemon=True).start()
except Exception as e:
logger.error(f"Error generating response: {e}")
asyncio.run_coroutine_threadsafe(
message_queue.put({"type": "error", "message": "Failed to generate response"}),
loop
)
def model_worker(cfg: CompanionConfig):
global generator, model_thread_running
logger.info("Model worker thread started")
if generator is None:
torch._inductor.config.triton.cudagraphs = False # Disable cudagraphs
torch._inductor.config.fx_graph_cache = False # Disable graph caching
logger.info("Loading voice model inside worker thread …")
generator = load_csm_1b_local(cfg.model_path, "cuda")
logger.info("Voice model ready (compiled with cudagraphs)")
while model_thread_running.is_set():
try:
request = model_queue.get(timeout=0.1)
if request is None:
break
text, speaker_id, context, max_ms, temperature, topk = request
for chunk in generator.generate_stream(
text=text,
speaker=speaker_id,
context=context,
max_audio_length_ms=max_ms,
temperature=temperature,
topk=topk):
model_result_queue.put(chunk)
if not model_thread_running.is_set():
break
model_result_queue.put(None) # EOS marker
except queue.Empty:
continue
except Exception as e:
import traceback
logger.error(f"Error in model worker: {e}\n{traceback.format_exc()}")
model_result_queue.put(Exception(f"Generation error: {e}"))
logger.info("Model worker thread exiting")
def start_model_thread():
global model_thread, model_thread_running
if model_thread is not None and model_thread.is_alive():
return
model_thread_running.set()
model_thread = threading.Thread(target=model_worker,
args=(config,),
daemon=True,
name="model_worker")
model_thread.start()
logger.info("Started dedicated model worker thread")
async def run_audio_generation(text, output_file):
"""Async wrapper for audio generation that runs in the event loop thread"""
audio_generation_thread(text, output_file)
def send_to_all_clients(message: dict):
"""Send a message to all connected WebSocket clients"""
for client in active_connections[:]:
try:
if client.client_state == WebSocketState.CONNECTED:
asyncio.run_coroutine_threadsafe(client.send_json(message), loop)
logger.info(f"Sent message to client: {message}")
else:
logger.warning("Detected non-connected client; removing from active_connections")
active_connections.remove(client)
except Exception as e:
logger.error(f"Error sending message to client: {e}")
if client in active_connections:
active_connections.remove(client)
saved_audio_paths = {
"default": {
0: [], # AI
1: [] # User
}
}
MAX_AUDIO_FILES = 8
def save_audio_and_trim(path, session_id, speaker_id, tensor, sample_rate):
"""
Save audio file and trim old audio files for both AI and user to maintain storage limits.
Args:
path: Path to save the audio file
session_id: Conversation session ID
speaker_id: 0 for AI, 1 for user
tensor: Audio tensor to save
sample_rate: Audio sample rate
"""
torchaudio.save(path, tensor.unsqueeze(0), sample_rate)
saved_audio_paths.setdefault(session_id, {}).setdefault(speaker_id, []).append(path)
paths = saved_audio_paths[session_id][speaker_id]
while len(paths) > MAX_AUDIO_FILES:
old_path = paths.pop(0)
if os.path.exists(old_path):
os.remove(old_path)
logger.info(f"Removed old audio file: {old_path}")
other_speaker_id = 1 if speaker_id == 0 else 0
if other_speaker_id in saved_audio_paths[session_id]:
other_paths = saved_audio_paths[session_id][other_speaker_id]
while len(other_paths) > MAX_AUDIO_FILES:
old_path = other_paths.pop(0)
if os.path.exists(old_path):
os.remove(old_path)
logger.info(f"Removed old audio file from other speaker: {old_path}")
MAX_SEGMENTS = 8
def add_segment(text, speaker_id, audio_tensor):
"""
Add a new segment and ensure the total context stays within token limits.
This version correctly separates protected and dynamic segments, performs trimming
on the dynamic list, and rebuilds the global context list at the end.
Args:
text: Text content of the segment
speaker_id: ID of the speaker (0 for AI, 1 for user)
audio_tensor: Audio data as a tensor
"""
global reference_segments, generator, config
# Determine the number of protected, initial reference segments based on what was actually loaded.
num_protected_segments = 0
if config.reference_audio_path and os.path.exists(config.reference_audio_path):
num_protected_segments += 1
if config.reference_audio_path2 and os.path.exists(config.reference_audio_path2):
num_protected_segments += 1
if config.reference_audio_path3 and os.path.exists(config.reference_audio_path3):
num_protected_segments += 1
# Separate protected from dynamic segments from the current global state
protected_segments = reference_segments[:num_protected_segments]
dynamic_segments = reference_segments[num_protected_segments:]
# Add the new segment to the dynamic list
new_segment = Segment(text=text, speaker=speaker_id, audio=audio_tensor)
dynamic_segments.append(new_segment)
# First, trim by MAX_SEGMENTS count. The oldest dynamic segments are removed.
max_dynamic_allowed = MAX_SEGMENTS - len(protected_segments)
if len(dynamic_segments) > max_dynamic_allowed:
# Keep only the most recent dynamic segments
dynamic_segments = dynamic_segments[-max_dynamic_allowed:]
# Then, check and trim by token count if necessary.
# This loop will trim the oldest dynamic segments until the token count is acceptable.
if hasattr(generator, '_text_tokenizer'):
while dynamic_segments:
# Tentatively combine for token calculation
temp_full_list = protected_segments + dynamic_segments
total_tokens = 0
# Calculate total tokens for the current combination
for segment in temp_full_list:
tokens = generator._text_tokenizer.encode(f"[{segment.speaker}]{segment.text}")
total_tokens += len(tokens)
if segment.audio is not None:
# Approximate frame count to token conversion
audio_frames = segment.audio.size(0) // 6094
total_tokens += audio_frames
# If we are within limits, the trimming is done.
if total_tokens <= 4096:
break
# Otherwise, remove the oldest dynamic segment and re-check in the next loop iteration.
dynamic_segments.pop(0)
else:
# Fallback if tokenizer is not available
logger.warning("Unable to access tokenizer - falling back to word-based estimation for context trimming")
def estimate_tokens(segment):
words = segment.text.split()
punctuation = sum(1 for char in segment.text if char in ".,!?;:\"'()[]{}")
text_tokens = len(words) + punctuation
audio_tokens = 0
if segment.audio is not None:
audio_frames = segment.audio.size(0) // 6094
audio_tokens = audio_frames
return text_tokens + audio_tokens
while dynamic_segments:
total_estimated_tokens = sum(estimate_tokens(s) for s in protected_segments) + \
sum(estimate_tokens(s) for s in dynamic_segments)
if total_estimated_tokens <= 2048:
break
dynamic_segments.pop(0)
# Finally, overwrite the global variable with the new, correctly-trimmed list.
# This is the single source of truth for the update.
reference_segments = protected_segments + dynamic_segments
# Log the final state for debugging
logger.info(f"Context updated. Segments: {len(reference_segments)} total " +
f"({len(protected_segments)} protected, {len(dynamic_segments)} dynamic).")
def preprocess_text_for_tts(text):
"""
Removes all punctuation except periods, commas, exclamation points, and question marks
from the input text to create cleaner speech output while preserving intonation.
Args:
text (str): Input text with potential punctuation
Returns:
str: Cleaned text with only allowed punctuation
"""
# Define a regex pattern that matches all punctuation except periods, commas, exclamation points, and question marks
# This includes: ; : " ' ~ @ # $ % ^ & * ( ) _ - + = [ ] { } \ | / < >
pattern = r'[^\w\s.,!?\']'
# Replace matched punctuation with empty string
cleaned_text = re.sub(pattern, '', text)
# normalize multiple spaces to single space
cleaned_text = re.sub(r'\s+', ' ', cleaned_text)
# ensure there's a space after punctuation for better speech pacing
cleaned_text = re.sub(r'([.,!?])(\S)', r'\1 \2', cleaned_text)
return cleaned_text.strip()
def audio_generation_thread(text, output_file):
global is_speaking, interrupt_flag, audio_queue, model_thread_running, current_generation_id, speaking_start_time
current_generation_id += 1
this_id = current_generation_id
interrupt_flag.clear()
# Log the start of generation
logger.info(f"Starting audio generation for ID: {this_id}")
# Try to acquire the lock, but don't block if it's busy
if not audio_gen_lock.acquire(blocking=False):
logger.warning(f"Audio generation {this_id} - lock acquisition failed, another generation is in progress")
asyncio.run_coroutine_threadsafe(
message_queue.put({
"type": "error",
"message": "Audio generation busy, skipping synthesis",
"gen_id": this_id
}),
loop
)
return
try:
# Start the model thread if it's not already running
start_model_thread()
interrupt_flag.clear()
is_speaking = True
speaking_start_time = time.time()
# Create output directory
os.makedirs(os.path.dirname(output_file), exist_ok=True)
all_audio_chunks = []
# Prepare text
text_lower = text.lower()
text_lower = preprocess_text_for_tts(text_lower)
asyncio.run_coroutine_threadsafe(
message_queue.put({
"type": "audio_status",
"status": "preparing_generation",
"gen_id": this_id
}),
loop
)
# Give client a moment to process
time.sleep(0.2)
logger.info(f"Sending generating status with ID {this_id}")
asyncio.run_coroutine_threadsafe(
message_queue.put({
"type": "audio_status",
"status": "generating",
"gen_id": this_id # Include generation ID
}),
loop
)
# Small delay to ensure client gets the signal
time.sleep(0.2)
# Estimate audio length
words = text.split()
avg_wpm = 100
words_per_second = avg_wpm / 60
estimated_seconds = len(words) / words_per_second
max_audio_length_ms = int(estimated_seconds * 1000)
# Send request to model thread
logger.info(f"Audio generation {this_id} - sending request to model thread")
model_queue.put((
text_lower,
config.voice_speaker_id,
reference_segments,
max_audio_length_ms,
0.8, # temperature
50 # topk
))
# Start timing
generation_start = time.time()
chunk_counter = 0
# Process results as they come
while True:
try:
# Check for interruption FIRST before getting more results
if interrupt_flag.is_set():
logger.info(f"Audio generation {this_id} - interrupt detected, stopping")
# Signal model thread to exit and restart
model_thread_running.clear()
time.sleep(0.1)
model_thread_running.set()
start_model_thread()
# Clear any remaining items in the result queue
while not model_result_queue.empty():
try:
model_result_queue.get_nowait()
except queue.Empty:
pass
# Break out of the processing loop
break
# Get result with timeout to allow checking interrupt
result = model_result_queue.get(timeout=0.1)
# Check for end of generation or error
if result is None:
logger.info(f"Audio generation {this_id} - complete")
break
if isinstance(result, Exception):
logger.error(f"Audio generation {this_id} - error: {result}")
raise result
# Track timing for first chunk
if chunk_counter == 0:
first_chunk_time = time.time() - generation_start
logger.info(f"Audio generation {this_id} - first chunk latency: {first_chunk_time*1000:.1f}ms")
chunk_counter += 1
# One more interrupt check before processing chunk
if interrupt_flag.is_set():
logger.info(f"Audio generation {this_id} - interrupt flag set during chunk processing")
break
# Process this audio chunk
audio_chunk = result
all_audio_chunks.append(audio_chunk)
# Convert to numpy and send to audio queue
chunk_array = audio_chunk.cpu().numpy().astype(np.float32)
audio_queue.put(chunk_array)
if chunk_counter == 1:
logger.info(f"Sending first audio chunk with ID {this_id}")
# Notify client we're sending the first chunk
asyncio.run_coroutine_threadsafe(
message_queue.put({
"type": "audio_status",
"status": "first_chunk",
"gen_id": this_id
}),
loop
)
# Small delay
time.sleep(0.1)
# Send chunk with generation ID
asyncio.run_coroutine_threadsafe(
message_queue.put({
"type": "audio_chunk",
"audio": chunk_array.tolist(),
"sample_rate": generator.sample_rate,
"gen_id": this_id,
"chunk_num": chunk_counter # Include chunk number
}),
loop
)
except queue.Empty:
# No results yet, keep checking
continue
except Exception as e:
logger.error(f"Audio generation {this_id} - error processing result: {e}")
break
# Save complete audio if available
if all_audio_chunks and not interrupt_flag.is_set():
try:
complete_audio = torch.cat(all_audio_chunks)
save_audio_and_trim(output_file, "default", config.voice_speaker_id, complete_audio, generator.sample_rate)
add_segment(text.lower(), config.voice_speaker_id, complete_audio)
# Log statistics
total_time = time.time() - generation_start
total_audio_seconds = complete_audio.size(0) / generator.sample_rate
rtf = total_time / total_audio_seconds
logger.info(f"Audio generation {this_id} - completed in {total_time:.2f}s, RTF: {rtf:.2f}x")
except Exception as e:
logger.error(f"Audio generation {this_id} - error saving complete audio: {e}")
except Exception as e:
import traceback
logger.error(f"Audio generation {this_id} - unexpected error: {e}\n{traceback.format_exc()}")
finally:
is_speaking = False
# Signal end of audio
audio_queue.put(None)
try:
logger.info(f"Audio generation {this_id} - sending completion status")
asyncio.run_coroutine_threadsafe(
message_queue.put({
"type": "audio_status",
"status": "complete",
"gen_id": this_id
}),
loop
)
except Exception as e:
logger.error(f"Audio generation {this_id} - failed to send completion status: {e}")
# Process any pending inputs
with user_input_lock:
if pending_user_inputs:
# Process pending inputs
logger.info(f"Audio generation {this_id} - processing pending inputs")
process_pending_inputs()
# Release the lock
logger.info(f"Audio generation {this_id} - releasing lock")
audio_gen_lock.release()
def handle_interrupt(websocket):
global is_speaking, last_interrupt_time, interrupt_flag, model_thread_running, speaking_start_time
# Log the current state
logger.info(f"Interrupt requested. Current state: is_speaking={is_speaking}")
current_time = time.time()
time_since_speech_start = current_time - speaking_start_time if speaking_start_time > 0 else 999
time_since_last_interrupt = current_time - last_interrupt_time
# Only apply cooldown for established speech, not for new speech
if time_since_last_interrupt < interrupt_cooldown and time_since_speech_start > 3.0:
logger.info(f"Ignoring interrupt: too soon after previous interrupt ({time_since_last_interrupt:.1f}s < {interrupt_cooldown}s)")
# Let the client know we're not interrupting
asyncio.run_coroutine_threadsafe(
websocket.send_json({
"type": "audio_status",
"status": "interrupt_acknowledged",
"success": False,
"reason": "cooldown"
}),
loop
)
return False
# Update the last interrupt time
last_interrupt_time = current_time
# We should interrupt if we're speaking OR if model generation is in progress
if is_speaking or not model_result_queue.empty():
logger.info("Interruption processing: we are speaking or generating")
interrupt_flag.set()
# Notify clients
asyncio.run_coroutine_threadsafe(
message_queue.put({"type": "audio_status", "status": "interrupted"}),
loop
)
asyncio.run_coroutine_threadsafe(
websocket.send_json({
"type": "audio_status",
"status": "interrupt_acknowledged"
}),
loop
)
# Clear the audio queue to stop additional audio from being processed
try:
# Drain the existing queue
while not audio_queue.empty():
try:
audio_queue.get_nowait()
except queue.Empty:
break
# Add end signal
audio_queue.put(None)
logger.info("Audio queue cleared")
except Exception as e:
logger.error(f"Error clearing audio queue: {e}")
# Reset VAD to prepare for new input
if vad_processor:
try:
vad_processor.reset()
logger.info("VAD processor reset")
except Exception as e:
logger.error(f"Error resetting VAD: {e}")
# Stop current model worker if needed
if model_thread and model_thread.is_alive():
try:
# Clear the thread running flag to stop generation
model_thread_running.clear()
# Wait a brief moment for thread to notice and exit
time.sleep(0.1)
# Now restart the thread state flag
model_thread_running.set()
# And restart the thread
start_model_thread()
logger.info("Model thread restarted")
except Exception as e:
logger.error(f"Error restarting model thread: {e}")
return True
logger.info("No active speech to interrupt")
return False
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
global is_speaking, audio_queue
await websocket.accept()
active_connections.append(websocket)
saved = config_manager.load_config()
if saved:
await websocket.send_json({"type": "saved_config", "config": saved})
try:
while True:
data = await websocket.receive_json()
if data["type"] == "config":
# Config handling
try:
config_data = data["config"]