altera o script para usar pywin32 em vez de win32gui, dá erro, e imprime o resultado, além da janela windows, também no prompt:



import whisper
import pyaudio
import wave
import win32gui
import time
import threading
import keyboard

model = whisper.load_model("small")

def transcribe(audio):
    
    # load audio and pad/trim it to fit 30 seconds
    audio = whisper.load_audio(audio)
    audio = whisper.pad_or_trim(audio)

    # make log-Mel spectrogram and move to the same device as the model
    mel = whisper.log_mel_spectrogram(audio).to(model.device)

    # detect the spoken language
    _, probs = model.detect_language(mel)
    print(f"Detected language: {max(probs, key=probs.get)}")

    # decode the audio
    options = whisper.DecodingOptions(fp16 = False)
    result = whisper.decode(model, mel, options)
    return result.text

CHUNK = 1024 
FORMAT = pyaudio.paInt16 # Audio format
CHANNELS = 1 # Number of channels
RATE = 44100 # Sampling rate (number of samples per second)
THRESHOLD = 500

p = pyaudio.PyAudio()

stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                frames_per_buffer=CHUNK)

frames = []
recording = False

print("Waiting for voice...")

while True:
    data = stream.read(CHUNK)
    rms = int.from_bytes(data, byteorder='little')
    
    if rms > THRESHOLD and not recording:
        print("Recording...")
        recording = True
    
    if recording:
        frames.append(data)
    
    if len(frames) > RATE / CHUNK * 5:
        break

print("Recording finished!")

stream.stop_stream()
stream.close()
p.terminate()

wf = wave.open("output.wav", "wb")
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b"".join(frames))
wf.close()

def write_in_window(text):
    # get the handle of the current window
    handle = win32gui.GetForegroundWindow()
    # write text in the window
    win32gui.SendMessage(handle, 0x000C, 0, text)

def transcribe_thread():
    while True:
        transcription = transcribe("output.wav")
        print(transcription) # print transcription to console
        write_in_window(transcription)
        time.sleep(5)

t1 = threading.Thread(target=transcribe_thread)
t1.start()

while True:
    if keyboard.is_pressed('ctrl+end'):
        break
    elif keyboard.is_pressed('ctrl+home'):
        if t1.is_alive():
            t1.join()
        else:
            t1.start()
Here’s an example of how you could modify the transcribe_thread function to continuously record and transcribe new audio:

def transcribe_thread():
    while True:
        # Record new audio
        frames = []
        recording = False
        print("Waiting for voice...")
        while True:
            data = stream.read(CHUNK)
            rms = int.from_bytes(data, byteorder='little')
            if rms > THRESHOLD and not recording:
                print("Recording...")
                recording = True
            if recording:
                frames.append(data)
            if len(frames) > RATE / CHUNK * 5:
                break
        print("Recording finished!")
        # Save recorded audio to file
        wf = wave.open("output.wav", "wb")
        wf.setnchannels(CHANNELS)
        wf.setsampwidth(p.get_sample_size(FORMAT))
        wf.setframerate(RATE)
        wf.writeframes(b"".join(frames))
        wf.close()
        # Transcribe recorded audio
        transcription = transcribe("output.wav")
        print(transcription) # print transcription to console
        write_in_window(transcription)

adapta o codigo anterior a esta alteração