diff --git a/requirements.txt b/requirements.txt index b08395f..43ea7b1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,4 +8,6 @@ torch==2.5.1 ollama==0.4.7 scipy==1.15.1 kokoro==0.7.12 -flask==3.1.0 \ No newline at end of file +flask==3.1.0 +soundfile==0.13.1 +termcolor \ No newline at end of file diff --git a/sources/llm_provider.py b/sources/llm_provider.py index d9321bd..c5c2f86 100644 --- a/sources/llm_provider.py +++ b/sources/llm_provider.py @@ -11,7 +11,7 @@ class Provider: def __init__(self, provider_name, model, server_address = "127.0.0.1:5000"): self.provider_name = provider_name.lower() self.model = model - self.server = self.check_address_format(server_address) + self.server = self.check_address_format(server_address) self.available_providers = { "ollama": self.ollama_fn, "server": self.server_fn, @@ -25,7 +25,7 @@ class Provider: def check_address_format(self, address): """ - Validate if the address is valid IP. + Validate if the address is valid IP. """ try: ip, port = address.rsplit(":", 1) @@ -35,7 +35,7 @@ class Provider: except ValueError as e: raise Exception(f"Invalid address format: {e}") return address - + def respond(self, history, verbose = True): """ Use the choosen provider to generate text. @@ -61,7 +61,7 @@ class Provider: except Exception as e: print(f"An error occurred: {e}") return False - + def server_fn(self, history, verbose = True): """ Use a remote server wit LLM to generate text. @@ -99,12 +99,12 @@ class Provider: thought += chunk['message']['content'] except ollama.ResponseError as e: if e.status_code == 404: - ollama.pull(self._model) + ollama.pull(self.model) if "refused" in str(e): raise Exception("Ollama connection failed. is the server running ?") raise e return thought - + def test_fn(self, history, verbose = True): """ Test function to generate text. @@ -121,4 +121,4 @@ class Provider: echo "Hello world from bash" ``` """ - return thought \ No newline at end of file + return thought diff --git a/sources/text_to_speech.py b/sources/text_to_speech.py index dfb52fe..1335186 100644 --- a/sources/text_to_speech.py +++ b/sources/text_to_speech.py @@ -3,11 +3,13 @@ from IPython.display import display, Audio import soundfile as sf import subprocess import re +import platform + class Speech(): def __init__(self, language = "english") -> None: self.lang_map = { - "english": 'a', # πŸ‡ΊπŸ‡Έ 'a' => American English + "english": 'a', # πŸ‡ΊπŸ‡Έ 'a' => American English "chinese": 'z', # πŸ‡―πŸ‡΅ 'j' => Japanese: pip install misaki[ja] "japanese": 'j' # # πŸ‡¨πŸ‡³ 'z' => Mandarin Chinese: pip install misaki[zh] } @@ -20,11 +22,16 @@ class Speech(): speed=1, split_pattern=r'\n+' ) for i, (gs, ps, audio) in enumerate(generator): - audio_file = f'sample.wav' + audio_file = 'sample.wav' + print(audio_file) display(Audio(data=audio, rate=24000, autoplay=i==0)) sf.write(audio_file, audio, 24000) # save each audio file - subprocess.call(["afplay", audio_file]) - + if platform.system().lower() != "windows": + subprocess.call(["afplay", audio_file]) + else: + import winsound + winsound.PlaySound(audio_file, winsound.SND_FILENAME) + def clean_sentence(self, sentence): sentence = re.sub(r'`.*?`', '', sentence) sentence = re.sub(r'[^a-zA-Z0-9.,!? ]+', '', sentence) diff --git a/sources/utility.py b/sources/utility.py index 5e8204c..fb3140c 100644 --- a/sources/utility.py +++ b/sources/utility.py @@ -1,20 +1,37 @@ from colorama import Fore +from termcolor import colored +import platform + def pretty_print(text, color = "info"): """ print text with color """ - color_map = { - "success": Fore.GREEN, - "failure": Fore.RED, - "status": Fore.LIGHTGREEN_EX, - "code": Fore.LIGHTBLUE_EX, - "warning": Fore.YELLOW, - "output": Fore.LIGHTCYAN_EX, - } - if color not in color_map: - print(text) - pretty_print("Invalid color in pretty_print", "warning") - return - print(color_map[color], text, Fore.RESET) \ No newline at end of file + if platform.system().lower() != "windows": + color_map = { + "success": Fore.GREEN, + "failure": Fore.RED, + "status": Fore.LIGHTGREEN_EX, + "code": Fore.LIGHTBLUE_EX, + "warning": Fore.YELLOW, + "output": Fore.LIGHTCYAN_EX, + } + if color not in color_map: + print(text) + pretty_print("Invalid color in pretty_print", "warning") + return + print(color_map[color], text, Fore.RESET) + else: + color_map = { + "success": "green", + "failure": "red", + "status": "light_green", + "code": "light_blue", + "warning": "yello", + "output": "cyan", + "default": "black" + } + if color not in color_map: + color = "default" + print(colored(text, color_map[color]))