Pretendo que atues como programador. Quero que faça as alterações neste script: from…
- User
Pretendo que atues como programador. Quero que faça as alterações neste script: from scipy.io.wavfile import write import sounddevice as sd import numpy as np import whisper import os import keyboard import pyperclip
tex1 = "" model = 'c:\meu\medium.pt' samplerate = 44100 blocksize = 111 threshold = 0.0125 vocals = [20, 20000] endblocks = 8 noise_blocks = 3 ### ADDED paused = False pyperclip.copy("limpezaaaa")
class StreamHandler: def init(self): self.running = True self.padding = 0 self.noise_counter = 0 ### ADDED self.prevblock = self.buffer = np.zeros((0, 1)) self.fileready = False print("\033[96mLoading Whisper Model…\033[0m", end='', flush=True) self.model = whisper.load_model(f'{model}') print("\033[90m Done.\033[0m") def check_pause_toggle(self): global paused if keyboard.is_pressed('F3'): print(" — Foi premida a tecla F3 — ") print("---------------- ") paused = not paused def process_audio_buffer(self, indata, frames, time, status): global paused
self.check_pause_toggle() if paused: return if any(indata): freq = np.argmax(np.abs(np.fft.rfft(indata[:, 0]))) * samplerate / frames if indata.max() > threshold and vocals[0] <= freq <= vocals[1]: self.noise_counter = 0 ### ADDED print('.', end='', flush=True) if self.padding < 1: self.buffer = self.prevblock.copy() self.buffer = np.concatenate((self.buffer, indata)) self.padding = endblocks else: ### CHANGED (IF-ELSE structure) if self.noise_counter < noise_blocks: self.noise_counter += 1 else: self.padding -= 1 if self.padding > 1: self.buffer = np.concatenate((self.buffer, indata)) elif self.padding < 1 and 1 < self.buffer.shape[0] > samplerate: self.fileready = True write('dictate.wav', samplerate, self.buffer) self.buffer = np.zeros((0,1)) elif self.padding < 1 and 1 < self.buffer.shape[0] < samplerate: self.buffer = np.zeros((0,1)) print("\033[2K\033[0G", end='', flush=True) else: self.prevblock = indata.copy() else: print("\033[31mNo input or device is muted.\033[0m") self.running = False def process_transcription(self): global tex1, paused self.check_pause_toggle() if paused: return if self.fileready: print("\n\033[90mTranscribing…\033[0m") result = self.model.transcribe('dictate.wav', language='pt', task='transcribe') print(f"\033[1A\033[2K\033[0G{result['text']}") tex1 = str(result['text']) print('2 Fala agora, comprimento da frase já reconhecida é ', len(tex1)) print('-----------------------------------------------') print(tex1) tex1 = " ".join([word for word in tex1.split()]) if tex1.lower() == "5": print(' ------Apaga 5--------') print('-----------------------------------------------') keyboard.press_and_release("ctrl+backspace") keyboard.press_and_release("ctrl+backspace") keyboard.press_and_release("ctrl+backspace") keyboard.press_and_release("ctrl+backspace") keyboard.press_and_release("ctrl+backspace") elif tex1.lower() == "4" or tex1.lower() == "quatro": print(' ------Apaga 4--------') print('-----------------------------------------------') keyboard.press_and_release("ctrl+backspace") keyboard.press_and_release("ctrl+backspace") keyboard.press_and_release("ctrl+backspace") elif tex1.lower() == "sim, três." or tex1.lower() == "sim, 3.": print(' ------Apaga 3--------') print('-----------------------------------------------') keyboard.press_and_release("ctrl+backspace") keyboard.press_and_release("ctrl+backspace") keyboard.press_and_release("ctrl+backspace") elif tex1.lower() == "sim, dois." : print(' ------Apaga 2--------') print('-----------------------------------------------') keyboard.press_and_release("ctrl+backspace") keyboard.press_and_release("ctrl+backspace") # Alteração: Verificar se a palavra “apaga” foi dita e apagar a última palavra escrita elif tex1.lower() == "sim" or tex1.lower() == "sim!" or tex1.lower() == "sim.": print(' ------Apaga--------') print('-----------------------------------------------') keyboard.press_and_release("ctrl+backspace") elif len(tex1) > 0: keyboard.write(tex1 + " ") tex1 = "" """ if len(tex1) > 0: keyboard.write(tex1 + " ") tex1 = "" """ self.fileready = False def listen(self): print("\033[92mListening… \033[37m(Ctrl+C to Quit)\033[0m") with sd.InputStream(channels=1, callback=self.process_audio_buffer, blocksize=int(samplerate * blocksize / 1000), samplerate=samplerate) as stream: while self.running: self.process_transcription()def main(): try: handler = StreamHandler() handler.listen() except (KeyboardInterrupt, SystemExit): pass finally: print("\n\033[93mQuitting…\033[0m") if os.path.exists('dictate.wav'): os.remove('dictate.wav')
if name == 'main': main()
Altera o código De forma que F3 cada vez que é premida alterne entre pause e continua. Embora O script detete sempre o clique na tecla F3, nem sempre consegue alternar entre pause e continua da execussão do script. Corrige esta situação . Otimiza, tambem, o codigo: if tex1.lower() == "5": print(' ------Apaga 5--------') print('-----------------------------------------------') keyboard.press_and_release("ctrl+backspace") keyboard.press_and_release("ctrl+backspace") keyboard.press_and_release("ctrl+backspace") keyboard.press_and_release("ctrl+backspace") keyboard.press_and_release("ctrl+backspace") elif tex1.lower() == "4" or tex1.lower() == "quatro": print(' ------Apaga 4--------') print('-----------------------------------------------') keyboard.press_and_release("ctrl+backspace") keyboard.press_and_release("ctrl+backspace") keyboard.press_and_release("ctrl+backspace") elif tex1.lower() == "sim, três." or tex1.lower() == "sim, 3.": print(' ------Apaga 3--------') print('-----------------------------------------------') keyboard.press_and_release("ctrl+backspace") keyboard.press_and_release("ctrl+backspace") keyboard.press_and_release("ctrl+backspace") elif tex1.lower() == "sim, dois." : print(' ------Apaga 2--------') print('-----------------------------------------------') keyboard.press_and_release("ctrl+backspace") keyboard.press_and_release("ctrl+backspace") # Alteração: Verificar se a palavra “apaga” foi dita e apagar a última palavra escrita elif tex1.lower() == "sim" or tex1.lower() == "sim!" or tex1.lower() == "sim.": print(' ------Apaga--------') print('-----------------------------------------------') keyboard.press_and_release("ctrl+backspace") elif len(tex1) > 0: keyboard.write(tex1 + " ")
tex1 = "" - Assistant
from scipy.io.wavfile import write import sounddevice as sd import numpy as np import whisper import os import keyboard import pyperclip
tex1 = "" model = 'c:\meu\medium.pt' samplerate = 44100 blocksize = 111 threshold = 0.0125 vocals = [20, 20000] endblocks = 8 noise_blocks = 3 paused = False pyperclip.copy("limpezaaaa")
class StreamHandler: def init(self): self.running = True self.padding = 0 self.noise_counter = 0 self.prevblock = self.buffer = np.zeros((0, 1)) self.fileready = False print("\033[96mLoading Whisper Model…\033[0m", end='', flush=True) self.model = whisper.load_model(f'{model}') print("\033[90m Done.\033[0m") def check_pause_toggle(self): global paused if keyboard.is_pressed('F3'): print(" — Foi premida a tecla F3 — ") print("---------------- ") paused = not paused while keyboard.is_pressed('F3'): # added to prevent toggling multiple times continue def process_audio_buffer(self, indata, frames, time, status): global paused
self.check_pause_toggle() if paused: return if any(indata): freq = np.argmax(np.abs(np.fft.rfft(indata[:, 0]))) * samplerate / frames if indata.max() > threshold and vocals[0] <= freq <= vocals[1]: self.noise_counter = 0 print('.', end='', flush=True) if self.padding < 1: self.buffer = self.prevblock.copy() self.buffer = np.concatenate((self.buffer, indata)) self.padding = endblocks else: if self.noise_counter < noise_blocks: self.noise_counter += 1 else: self.padding -= 1 if self.padding > 1: self.buffer = np.concatenate((self.buffer, indata)) elif self.padding < 1 and 1 < self.buffer.shape[0] > samplerate: self.fileready = True write('dictate.wav', samplerate, self.buffer) self.buffer = np.zeros((0,1)) elif self.padding < 1 and 1 < self.buffer.shape[0] < samplerate: self.buffer = np.zeros((0,1)) print("\033[2K\033[0G", end='', flush=True) else: self.prevblock = indata.copy() else: print("\033[31mNo input or device is muted.\033[0m") self.running = False def process_transcription(self): global tex1, paused self.check_pause_toggle() if paused: return if self.fileready: print("\n\033[90mTranscribing…\033[0m") result = self.model.transcribe('dictate.wav', language='pt', task='transcribe') print(f"\033[1A\033[2K\033[0G{result['text']}") tex1 = str(result['text']) print('2 Fala agora, comprimento da frase já reconhecida é ', len(tex1)) print('-----------------------------------------------') print(tex1) tex1 = " ".join([word for word in tex1.split()]) apagar = {'5': 5, 'quatro': 3, 'sim, três.': 3, 'sim, 3.': 3, 'sim, dois.': 2} # added if tex1.lower().startswith('sim, ') and len(tex1.split(' ')) > 1: # added word = tex1.split(' ')[1] if word in apagar: print(f' ------Apaga {word}{"-" * (7 - len(word))}--------') # optimized for _ in range(apagar[word]): keyboard.press_and_release("ctrl+backspace") # Alteração: Verificar se a palavra “apaga” foi dita e apagar a última palavra escrita elif tex1.lower() == "sim" or tex1.lower() == "sim!" or tex1.lower() == "sim.": print(' ------Apaga--------') print('-----------------------------------------------') keyboard.press_and_release("ctrl+backspace") elif len(tex1) > 0: keyboard.write(tex1 + " ") tex1 = "" self.fileready = False def listen(self): print("\033[92mListening… \033[37m(Ctrl+C to Quit)\033[0m") with sd.InputStream(channels=1, callback=self.process_audio_buffer, blocksize=int(samplerate * blocksize / 1000), samplerate=samplerate) as stream: while self.running: self.process_transcription()def main(): try: handler = StreamHandler() handler.listen() except (KeyboardInterrupt, SystemExit): pass finally: print("\n\033[93mQuitting…\033[0m") if os.path.exists('dictate.wav'): os.remove('dictate.wav')
if name == 'main': main()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.