Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· openaiHugging Face Suzhen/CodeChat

altera o script para usar pywin32 em vez de win32gui, dá erro, e imprime o resultado,…

  1. User

    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

  2. Assistant

    import whisper import pyaudio import wave import win32com.client 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 pythoncom.CoInitialize() # initialize pywin32 handle = win32com.client.Dispatch("WScript.Shell").AppActivate("Untitled - Notepad") # replace "Untitled - Notepad" with the title of your window win32com.client.Dispatch("WScript.Shell").SendKeys(text) pythoncom.CoUninitialize()

    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) print(transcription) # print transcription to console

    t1 = threading.Thread(target=transcribe_thread) t1.daemon = True 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()

  3. User

    faz identações no codigo

  4. Assistant

    import whisper import pyaudio import wave import win32com.client 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 pythoncom.CoInitialize() # initialize pywin32 handle = win32com.client.Dispatch("WScript.Shell").AppActivate("Untitled - Notepad") # replace "Untitled - Notepad" with the title of your window win32com.client.Dispatch("WScript.Shell").SendKeys(text) pythoncom.CoUninitialize()

    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) print(transcription) # print transcription to console

    t1 = threading.Thread(target=transcribe_thread) t1.daemon = True 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()

  5. User

    erro: pip install pywintypes Defaulting to user installation because normal site-packages is not writeable ERROR: Could not find a version that satisfies the requirement pywintypes (from versions: none) ERROR: No matching distribution found for pywintypes

  6. Assistant

    Desculpe, foi um engano meu. pywintypes é uma dependência do pywin32, que você já tem instalado.

    O erro possivelmente está ocorrendo porque, ao instalar o pywin32, você selecionou a opção "Executable installers", que é recomendada somente se você estiver usando o Python 2.7 ou o Python 3.3. Para outras versões do Python, é recomendado que se use a opção "Wheel installers", que é mais moderna.

    Para corrigir o erro, sugiro que você desinstale o pywin32 e calcule a opção "Wheel installers" ao reinstalá-lo. Para tal, execute os seguintes comandos:

    pip uninstall pywin32
    pip install pywin32 --no-binary pywin32
    

    Isso deve resolver o erro.

  7. User

    pip install pywin32 --no-binary pywin32 Defaulting to user installation because normal site-packages is not writeable ERROR: Could not find a version that satisfies the requirement pywin32 (from versions: none) ERROR: No matching distribution found for pywin32

  8. Assistant

    Nesse caso, você pode tentar instalar o pywin32 manualmente. Acesse o site https://sourceforge.net/projects/pywin32/files/pywin32/ e baixe o arquivo correspondente à sua versão do Python e à arquitetura do seu sistema operacional.

    Depois de fazer o download, extraia o arquivo e execute o instalador pywin32-xxx.winxx-pyxx.exe. Certifique-se de selecionar a opção "Complete" (instala todos os módulos) e aguarde até que a instalação seja concluída.

    Caso o erro persista após a instalação manual, verifique se o seu ambiente virtual Python está ativado e se você está usando a última versão do pip. Se ainda houver problema, entre em contato novamente, por favor.

  9. User

    mostra o codigo python linha a linha, devidamente indentado: python # import whisper # import pyaudio # import wave # import win32gui # import time # import threading # import keyboard # import win32con # import win32api model = whisper.load_model("small") def transcribe(audio): # Carregar áudio e ajustar para 30 segundos audio = whisper.load_audio(audio) audio = whisper.pad_or_trim(audio) # Gerar espectrograma Mel-log e mover para o mesmo dispositivo do modelo mel = whisper.log_mel_spectrogram(audio).to(model.device) # Detectar o idioma falado _, probs = model.detect_language(mel) print(f"Idioma detectado: {max(probs, key=probs.get)}") # Decodificar o áudio options = whisper.DecodingOptions(fp16=False) result = whisper.decode(model, mel, options) return result.text CHUNK = 1024 FORMAT = pyaudio.paInt16 CHANNELS = 1 RATE = 44100 THRESHOLD = 500 p = pyaudio.PyAudio() stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK) frames = [] recording = False print("Aguardando voz...") while True: data = stream.read(CHUNK) rms = int.from_bytes(data, byteorder='little') if rms > THRESHOLD and not recording: print("Gravando...") recording = True if recording: frames.append(data) if len(frames) > RATE / CHUNK * 5: break print("Gravação finalizada!") 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): # while True: # try: # handle = win32gui.GetForegroundWindow() # win32api.SendMessage(handle, win32con.WM_SETTEXT, 0, text) # break # except: # continue # def transcribe_thread(): # while True: # transcription = transcribe("output.wav") # print(transcription) # imprimir a transcrição no 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()

  10. Assistant
    import whisper
    import pyaudio
    import wave
    import win32gui
    import time
    import threading
    import keyboard
    import win32con
    import win32api
    
    model = whisper.load_model("small")
    
    def transcribe(audio):
        
        # Carregar áudio e ajustar para 30 segundos
        audio = whisper.load_audio(audio)
        audio = whisper.pad_or_trim(audio)
    
        # Gerar espectrograma Mel-log e mover para o mesmo dispositivo do modelo
        mel = whisper.log_mel_spectrogram(audio).to(model.device)
    
        # Detectar o idioma falado
        _, probs = model.detect_language(mel)
        print(f"Idioma detectado: {max(probs, key=probs.get)}")
    
        # Decodificar o áudio
        options = whisper.DecodingOptions(fp16=False)
        result = whisper.decode(model, mel, options)
        return result.text
    
    CHUNK = 1024 
    FORMAT = pyaudio.paInt16
    CHANNELS = 1
    RATE = 44100
    THRESHOLD = 500
    
    p = pyaudio.PyAudio()
    
    stream = p.open(format=FORMAT,
                    channels=CHANNELS,
                    rate=RATE,
                    input=True,
                    frames_per_buffer=CHUNK)
    
    frames = []
    recording = False
    
    print("Aguardando voz...")
    
    while True:
        data = stream.read(CHUNK)
        rms = int.from_bytes(data, byteorder='little')
        
        if rms > THRESHOLD and not recording:
            print("Gravando...")
            recording = True
        
        if recording:
            frames.append(data)
        
        if len(frames) > RATE / CHUNK * 5:
            break
    
    print("Gravação finalizada!")
    
    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):
    #     while True:
    #         try:
    #             handle = win32gui.GetForegroundWindow()
    #             win32api.SendMessage(handle, win32con.WM_SETTEXT, 0, text)
    #             break
    #         except:
    #             continue
    
    # def transcribe_thread():
    #     while True:
    #         transcription = transcribe("output.wav")
    #         print(transcription) # imprimir a transcrição no 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():
    #             t
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.