From 1d62cdf7581247507f25ace6ef58e2a6b449bc25 Mon Sep 17 00:00:00 2001 From: nsde Date: Tue, 27 Dec 2022 15:58:42 +0100 Subject: [PATCH] Improved stuff --- README.md | 2 +- requirements.txt | 4 ++-- sandbox/discord.py | 0 test.py | 19 +++++++++++++++++++ violet/lang.py | 10 ++++++---- violet/model-german.txt | 6 ++++++ violet/model.txt | 16 ++++++---------- violet/stt.py | 16 ++++++++++------ violet/tts.py | 4 +++- 9 files changed, 53 insertions(+), 24 deletions(-) create mode 100644 sandbox/discord.py create mode 100644 test.py create mode 100644 violet/model-german.txt diff --git a/README.md b/README.md index 21cfcdd..fa00089 100644 --- a/README.md +++ b/README.md @@ -40,4 +40,4 @@ sudo apt-get install python3-pyaudio ## Example tasks - What WiFi am I connected with (on Linux)? - Tell me a short story! -- Translate "Hello there"! +- Translate "Hello there" into Spanish! diff --git a/requirements.txt b/requirements.txt index f89652a..168e500 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,7 @@ gtts openai -pyaudio +# pyaudio colorama playsound -langdetect +# langdetect speechrecognition \ No newline at end of file diff --git a/sandbox/discord.py b/sandbox/discord.py new file mode 100644 index 0000000..e69de29 diff --git a/test.py b/test.py new file mode 100644 index 0000000..6977550 --- /dev/null +++ b/test.py @@ -0,0 +1,19 @@ +# TODO Test this on Windows + +import platform +import subprocess + +# Check the operating system +os_name = platform.system() + +# Set the Discord binary path based on the operating system +if os_name == 'Windows': + discord_path = 'C:\Program Files (x86)\Discord\Discord.exe' +elif os_name == 'Darwin': # macOS + discord_path = '/Applications/Discord.app/Contents/MacOS/Discord' +else: + # Assume Linux + discord_path = '/usr/bin/discord' + +# Open Discord using the default binary path +subprocess.run([discord_path]) diff --git a/violet/lang.py b/violet/lang.py index 216d9ae..3301bb8 100644 --- a/violet/lang.py +++ b/violet/lang.py @@ -1,3 +1,5 @@ +"""Language detection module. Currently not used, as langdetect is too inaccurate!""" + import langdetect LANGS = { @@ -8,10 +10,10 @@ LANGS = { } def detect(text: str): - detected = langdetect.detect(text) + # detected = langdetect.detect(text) - for lang in LANGS: - if lang == detected: - return [lang, LANGS[lang]] + # for lang in LANGS: + # if lang == detected: + # return [lang, LANGS[lang]] return 'en-US' diff --git a/violet/model-german.txt b/violet/model-german.txt new file mode 100644 index 0000000..a0373e3 --- /dev/null +++ b/violet/model-german.txt @@ -0,0 +1,6 @@ +Human: Fass mir Harry Potter in 2 Sätze zusammen. +AI: Harry Potter ist eine populäre Fantasy-Buchreihe und Filmreihe, die von J.K. Rowling geschrieben wurde. Die Geschichte dreht sich um den titelgebenden Helden Harry Potter, einen jungen Zauberer, der erfährt, dass er berühmt und berüchtigt ist und sich einer Reihe von Abenteuern stellen muss, während er versucht, den bösen Lord Voldemort zu besiegen und die Welt der Magie zu retten. +Human: Was ist 2 plus 3 mal 5 +AI: 17 +Human: Was ist die Entfernung von Erde zu Mond? +AI: Die durchschnittliche Entfernung von der Erde zum Mond beträgt etwa 384.400 Kilometer. Die Entfernung kann jedoch je nach der Position der Erde und des Mondes in ihren jeweiligen Umlaufbahnen variieren. \ No newline at end of file diff --git a/violet/model.txt b/violet/model.txt index ed886f0..4b69964 100644 --- a/violet/model.txt +++ b/violet/model.txt @@ -7,11 +7,9 @@ Human: Hello, who are you? AI: I am an AI created by OpenAI. How can I help you today? Human: What's the current time? AI: ``` -import time - -current_time = time.localtime() -current_time = time.strftime("%H:%M:%S", current_time) +import datetime +current_time = datetime.datetime.now().strftime("%I:%M %p") print(f"The current time is {current_time}.") ``` Human: Thanks. Please create a file called "todo.txt" and save "Read a book" in it." @@ -40,9 +38,7 @@ Human: What's 3 to the power of 4 plus 6 multiplied by 5? AI: ``` print(f"{3**4+6*5}") ``` -Human: Fass mir Harry Potter in 2 Sätze zusammen. -AI: Harry Potter ist eine populäre Fantasy-Buchreihe und Filmreihe, die von J.K. Rowling geschrieben wurde. Die Geschichte dreht sich um den titelgebenden Helden Harry Potter, einen jungen Zauberer, der erfährt, dass er berühmt und berüchtigt ist und sich einer Reihe von Abenteuern stellen muss, während er versucht, den bösen Lord Voldemort zu besiegen und die Welt der Magie zu retten. -Human: Was ist 2 plus 3 mal 5 -AI: 17 -Human: Was ist die Entfernung von Erde zu Mond? -AI: Die durchschnittliche Entfernung von der Erde zum Mond beträgt etwa 384.400 Kilometer. Die Entfernung kann jedoch je nach der Position der Erde und des Mondes in ihren jeweiligen Umlaufbahnen variieren. \ No newline at end of file +Human: Tell me a short story. +AI: One day, a kind man named John helped a traveler in need. In return, the traveler gave John a magical stone that brought him good luck and happiness. John used its magic to help others, and the villagers learned to be more compassionate. The end. +Human: Start Discord. +AI: \ No newline at end of file diff --git a/violet/stt.py b/violet/stt.py index bf41c95..cd9f5fd 100644 --- a/violet/stt.py +++ b/violet/stt.py @@ -1,16 +1,20 @@ -import speech_recognition as sr +import colorama +import speech_recognition -recognizer = sr.Recognizer() +recognizer = speech_recognition.Recognizer() def ask() -> str: try: - with sr.Microphone() as source2: + with speech_recognition.Microphone() as source2: recognizer.adjust_for_ambient_noise(source2, duration=1) recording = recognizer.listen(source2, timeout=5) - return recognizer.recognize_google(recording, language='en-US') # lang.detect(text)[1] + + recognized_text = recognizer.recognize_google(recording, language='en-US') # lang.detect(text)[1] + print(f'{colorama.Fore.BLUE}[STT] {recognized_text}') + return recognized_text - except sr.RequestError: + except speech_recognition.RequestError: return 'I did not understand what you said.' - except sr.UnknownValueError: + except speech_recognition.UnknownValueError: return 'There was an error processing what you said.' diff --git a/violet/tts.py b/violet/tts.py index 5b49fa3..7bc79ac 100644 --- a/violet/tts.py +++ b/violet/tts.py @@ -1,12 +1,14 @@ import os import gtts +import colorama import playsound from . import lang def say(text: str) -> None: """Says a plain text using Google TTS.""" - tts = gtts.gTTS(text=text, lang=lang.detect(text)[0]) #, lang='en' + # print(f'{colorama.Fore.YELLOW}[TTS] Detected language: {lang.detect(text)}') + tts = gtts.gTTS(text=text, lang='en') filename = 'tts.temp.mp3' tts.save(filename)