This commit is contained in:
nsde 2022-12-26 17:12:50 +01:00
parent d8afbb17fe
commit d30d919358
9 changed files with 72 additions and 10 deletions

View file

@ -1,2 +1,7 @@
gtts
openai
colorama
pyaudio
colorama
playsound
langdetect
speechrecognition

View file

@ -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 *

View file

@ -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: ',

17
violet/lang.py Normal file
View file

@ -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'

View file

@ -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

View file

@ -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}")
```
```
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.

16
violet/stt.py Normal file
View file

@ -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.'

10
violet/tts.py Normal file
View file

@ -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)

View file

@ -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'])