diff --git a/requirements.txt b/requirements.txt index 3f4e1ba..f89652a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,7 @@ +gtts openai -colorama \ No newline at end of file +pyaudio +colorama +playsound +langdetect +speechrecognition \ No newline at end of file diff --git a/violet/__init__.py b/violet/__init__.py index 2afa198..296fc62 100644 --- a/violet/__init__.py +++ b/violet/__init__.py @@ -1,4 +1,7 @@ +from .stt import * +from .tts import * from .gpt import * from .main import * +from .lang import * from .helpers import * from .sandbox import * diff --git a/violet/gpt.py b/violet/gpt.py index 413f1fb..40e7dca 100644 --- a/violet/gpt.py +++ b/violet/gpt.py @@ -9,6 +9,7 @@ openai.api_key = os.getenv('OPENAI_API_KEY') error_model = open('violet/error-model.txt').read() def generate(history: str, prompt: str) -> str: + print('Generating OpenAI completion!') completions = openai.Completion.create( model="text-davinci-003", prompt=f'{history}\nHuman: {prompt}\nAI: ', @@ -24,6 +25,7 @@ def generate(history: str, prompt: str) -> str: return message[1:] def explain_error(prompt: str) -> str: + print('Explaining error...') completions = openai.Completion.create( model="text-davinci-003", prompt=f'{error_model}\nHuman: {prompt}\nAI: ', diff --git a/violet/lang.py b/violet/lang.py new file mode 100644 index 0000000..216d9ae --- /dev/null +++ b/violet/lang.py @@ -0,0 +1,17 @@ +import langdetect + +LANGS = { + 'en': 'en-US', + 'de': 'de-DE', + 'fr': 'fr-FR', + 'es': 'es-ES' +} + +def detect(text: str): + detected = langdetect.detect(text) + + for lang in LANGS: + if lang == detected: + return [lang, LANGS[lang]] + + return 'en-US' diff --git a/violet/main.py b/violet/main.py index 0c47ecf..93af6da 100644 --- a/violet/main.py +++ b/violet/main.py @@ -15,8 +15,14 @@ def respond(prompt: str) -> str: if CODE_START in response and CODE_END in response: code = response.split(CODE_START)[1].split(CODE_END)[0] - response = sandbox.run(code) - colored = colorama.Fore.GREEN if response['status'] == 'success' else colorama.Fore.RED + response['message'] + response = sandbox.run(code) + + if response['status'] == 'success': + colored = colorama.Fore.GREEN + response['message'] + else: + colored = colorama.Fore.RED + response['message'] + + response = response['message'] else: colored = colorama.Fore.YELLOW + response diff --git a/violet/model.txt b/violet/model.txt index 7d2d565..ed886f0 100644 --- a/violet/model.txt +++ b/violet/model.txt @@ -39,4 +39,10 @@ print("Done.") Human: What's 3 to the power of 4 plus 6 multiplied by 5? AI: ``` print(f"{3**4+6*5}") -``` \ No newline at end of file +``` +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/stt.py b/violet/stt.py new file mode 100644 index 0000000..0b6d2bc --- /dev/null +++ b/violet/stt.py @@ -0,0 +1,16 @@ +import speech_recognition as sr + +recognizer = sr.Recognizer() + +def ask() -> str: + try: + with sr.Microphone() as source2: + recognizer.adjust_for_ambient_noise(source2, duration=1) + recording = recognizer.listen(source2, timeout=5) + return recognizer.recognize_google(recording, language='de-DE') + + except sr.RequestError: + return 'I did not understand what you said.' + + except sr.UnknownValueError: + return 'There was an error processing what you said.' diff --git a/violet/tts.py b/violet/tts.py new file mode 100644 index 0000000..06a09b4 --- /dev/null +++ b/violet/tts.py @@ -0,0 +1,10 @@ +import os +import gtts +import playsound + +def say(text): + tts = gtts.gTTS(text=text, lang='de') #, lang='en' + filename = 'tts.temp.mp3' + tts.save(filename) + playsound.playsound(filename) + os.remove(filename) diff --git a/voice.py b/voice.py index 85b08be..3d84897 100644 --- a/voice.py +++ b/voice.py @@ -1,21 +1,18 @@ import sys import colorama -import pyttsx3 +import playsound import violet colorama.init(autoreset=True) -engine = pyttsx3.init() while True: try: - prompt = input(colorama.Fore.BLUE) + prompt = violet.ask() except KeyboardInterrupt: sys.exit(0) answer = violet.respond(prompt) print(answer['colored']) - - engine.say(answer['plain']) - engine.runAndWait() + violet.say(answer['plain'])