Skip to content

Commit 2c073ab

Browse files
committed
1. fixed memory leak issues in multi-session scenarios.
1 parent aadbddc commit 2c073ab

File tree

2 files changed

+56
-39
lines changed

2 files changed

+56
-39
lines changed

ailice/modules/ASpeech.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ def strip(txt: str) -> str:
1414
translation_table = str.maketrans("", "", string.whitespace)
1515
return txt.translate(translation_table)
1616

17+
t2s = None
18+
s2t = None
19+
1720
class ASpeech():
1821
def __init__(self):
1922
self.textQue = queue.Queue(maxsize=100)
2023
self.audioQue = queue.Queue(maxsize=100)
21-
self.t2s = None
22-
self.s2t = None
2324

2425
self.inputDone = True
2526
self.lock = threading.Lock()
@@ -39,30 +40,39 @@ def ModuleInfo(self):
3940
"SWITCH-TONE": {"func": "SwitchTone", "prompt": "Switch the TTS system to a new tone.", "type": "primary"}}}
4041

4142
def PrepareModel(self):
42-
if None in [self.t2s, self.s2t]:
43-
self.t2s = T2S_ChatTTS()
44-
self.s2t = S2T_WhisperLarge()
43+
global s2t, t2s
44+
45+
if None in [t2s, s2t]:
46+
t2s = T2S_ChatTTS()
47+
s2t = S2T_WhisperLarge()
4548
return
4649

4750
def SetDevices(self, deviceMap: dict[str,str]):
51+
global s2t, t2s
52+
4853
if "stt" in deviceMap:
49-
self.s2t.To(deviceMap['stt'])
54+
s2t.To(deviceMap['stt'])
5055
elif "tts" in deviceMap:
51-
self.t2s.To(deviceMap['tts'])
56+
t2s.To(deviceMap['tts'])
5257
return
5358

5459
def Speech2Text(self, wav: np.ndarray, sr: int) -> str:
55-
return self.s2t.recognize(audio_data_to_numpy((wav, sr)))
60+
global s2t
61+
return s2t.recognize(audio_data_to_numpy((wav, sr)))
5662

5763
def Text2Speech(self, txt: str) -> tuple[np.ndarray, int]:
64+
global t2s
65+
5866
if (None == txt) or ("" == strip(txt)):
5967
return (np.zeros(1), 24000)
60-
return self.t2s(txt)
68+
return t2s(txt)
6169

6270
def GetAudio(self) -> str:
71+
global s2t
72+
6373
self.inputDone = True
6474
with self.lock:
65-
ret = self.s2t()
75+
ret = s2t()
6676
return ret
6777

6878
def Speak(self, txt: str):
@@ -74,15 +84,17 @@ def Speak(self, txt: str):
7484
return
7585

7686
def SwitchTone(self) -> str:
77-
return self.t2s.SwitchTone()
87+
global t2s
88+
return t2s.SwitchTone()
7889

7990
def ProcessText(self):
91+
global t2s
8092
while True:
8193
#The inter-thread synchronization issue here is more complex than it appears.
8294
self.noTextLeft = (self.inputDone and self.textQue.empty())
8395
text = self.textQue.get()
8496
try:
85-
self.audioQue.put(self.t2s(text))
97+
self.audioQue.put(t2s(text))
8698
except Exception as e:
8799
print('EXCEPTION in ProcessText(). continue. e: ',str(e))
88100
continue

ailice/modules/AStorageVecDB.py

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
MODEL = 'nomic-ai/nomic-embed-text-v1-GGUF'
2323
FILE_NAME = 'nomic-embed-text-v1.Q8_0.gguf'
2424

25+
model = None
26+
modelLock = Lock()
27+
2528
class AStorageVecDB():
2629
def __init__(self):
27-
self.model = None
28-
self.modelLock = Lock()
2930
self.data = {"model": MODEL, "file": FILE_NAME, "collections": {}}
3031
self.dir = None
3132
self.buffers = {}
@@ -38,8 +39,9 @@ def ModuleInfo(self):
3839
return {"NAME": "storage", "ACTIONS": {}}
3940

4041
def CalcEmbeddings(self, txts: list[str]):
41-
with self.modelLock:
42-
return np.array(self.model.embed(txts))
42+
global model, modelLock
43+
with modelLock:
44+
return np.array(model.embed(txts))
4345

4446
def Hippocampus(self):
4547
while True:
@@ -73,30 +75,33 @@ def Load(self, dir):
7375
return
7476

7577
def PrepareModel(self) -> str:
78+
global model, modelLock
79+
7680
ggufFile = hf_hub_download(repo_id=self.data['model'],filename=self.data['file'])
77-
if self.model and ggufFile == self.model.model_path:
78-
return f"Embedding model {self.model} has already been loaded."
79-
80-
if "llama_cpp" == INFERENCE_ENGINE:
81-
self.model = Llama(
82-
model_path=ggufFile,
83-
embedding=True,
84-
n_gpu_layers=-1, # Uncomment to use GPU acceleration
85-
# seed=1337, # Uncomment to set a specific seed
86-
# n_ctx=2048, # Uncomment to increase the context window
87-
)
88-
return "Embedding model has been loaded."
89-
elif "gpt4all" == INFERENCE_ENGINE:
90-
gpus = []
91-
try:
92-
gpus = GPT4All.list_gpus()
93-
device = gpus[0] if len(gpus) > 0 else "cpu"
94-
except Exception as e:
95-
device = "cpu"
96-
self.model = Embed4All(ggufFile, device = device)
97-
return f"GPUs found on this device: {gpus}. Embedding model has been loaded on {device}."
98-
else:
99-
return "No inference engine was found. Please use one of the following commands to install: `pip install gpt4all` or `ailice_turbo`."
81+
with modelLock:
82+
if model and ggufFile == model.model_path:
83+
return f"Embedding model {self.data['model']} has already been loaded."
84+
85+
if "llama_cpp" == INFERENCE_ENGINE:
86+
model = Llama(
87+
model_path=ggufFile,
88+
embedding=True,
89+
n_gpu_layers=-1, # Uncomment to use GPU acceleration
90+
# seed=1337, # Uncomment to set a specific seed
91+
# n_ctx=2048, # Uncomment to increase the context window
92+
)
93+
return "Embedding model has been loaded."
94+
elif "gpt4all" == INFERENCE_ENGINE:
95+
gpus = []
96+
try:
97+
gpus = GPT4All.list_gpus()
98+
device = gpus[0] if len(gpus) > 0 else "cpu"
99+
except Exception as e:
100+
device = "cpu"
101+
model = Embed4All(ggufFile, device = device)
102+
return f"GPUs found on this device: {gpus}. Embedding model has been loaded on {device}."
103+
else:
104+
return "No inference engine was found. Please use one of the following commands to install: `pip install gpt4all` or `ailice_turbo`."
100105

101106
def Open(self, directory: str) -> str:
102107
try:

0 commit comments

Comments
 (0)