-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
76 lines (61 loc) · 2.5 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
import speech_recognition as sr
import pyttsx3
from talk_in_code import translate
def initialize_voice_assistant(voice_enabled):
if voice_enabled:
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)
rate = engine.getProperty('rate')
engine.setProperty('rate', rate - 25)
return engine
return 'Disabled'
def say(voice_assistant, text):
print(text)
if not voice_assistant == 'Disabled':
voice_assistant.say(text)
voice_assistant.runAndWait()
def listen_and_transfer_to_text(recognizer, microphone):
audio = recognizer.listen(microphone)
try:
text = recognizer.recognize_google(audio, language='pt-BR')
except Exception as err:
print("Não entendi. ", err)
return 'Error'
return text
def write_on_script_file(line, filename='talked-code.py'):
with open(filename, 'a') as script_file:
script_file.write(line + '\n')
def start(voice_enabled=True):
voice_assistant = initialize_voice_assistant(voice_enabled)
say(voice_assistant, 'Sistema de escrita por voz inicializando...')
recognizer = sr.Recognizer()
while True:
with sr.Microphone() as mic:
recognizer.adjust_for_ambient_noise(mic)
say(voice_assistant, 'Diga algo: ')
line = listen_and_transfer_to_text(recognizer, mic)
if 'sair' in line:
say(voice_assistant, 'fechar sistema detectado, finalizando...')
break
say(voice_assistant, 'Verifique na tela se a frase está correta.')
print("Você disse: ", line, "?")
while True:
response = listen_and_transfer_to_text(recognizer, mic)
say(voice_assistant, f'Sua resposta: {response}')
if 'sim' in response or 'confirmo' in response:
line_of_code = translate.run(line, language='pt-BR')
write_on_script_file(line_of_code)
say(voice_assistant, 'Linha escrita com sucesso, vamos para outra?')
break
elif 'sair' in response:
return 'Sistema fechado.'
elif 'não' in response or 'refazer' in response:
break
return 'Sucesso total!'
def auto_pep_8(file_name: str = 'talked-code.py'):
os.system(f'autopep8 --in-place --aggressive --aggressive {file_name}')
if __name__ == '__main__':
start(False)
auto_pep_8()