diff --git a/convert_mkv_wav.py b/convert_mkv_wav.py new file mode 100644 index 0000000..321588c --- /dev/null +++ b/convert_mkv_wav.py @@ -0,0 +1,7 @@ +import subprocess + +def convert(mkv_file): + command = "ffmpeg -i {0} -ab 160k -ac 2 -ar 44100 -vn audio.wav".format(mkv_file) + + subprocess.call(command, shell=True) + diff --git a/speech-to-text.py b/speech-to-text.py new file mode 100644 index 0000000..3d13c57 --- /dev/null +++ b/speech-to-text.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 + +import speech_recognition as sr +# obtain path to "english.wav" in the same folder as this script +from os import path +from convert_mkv_wav import convert +from pyChatGPT import ChatGPT +import logging +# ChatGTP Session token +logging.basicConfig(level=logging.DEBUG) +session_tocken = '' + +# convert to wav format +convert('audio.mkv') + +AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), "audio.wav") + +# use the audio file as the audio source +r = sr.Recognizer() +with sr.AudioFile(AUDIO_FILE) as source: + audio = r.record(source) # read the entire audio file + +# Using a microphone +# with sr.Microphone() as source: +# #Chama a funcao de reducao de ruido disponivel na speech_recognition +# r.adjust_for_ambient_noise(source) +# #Avisa ao usuario que esta pronto para ouvir +# print("Speak: ") +# #Armazena a informacao de audio na variavel +# audio = r.listen(source) + +# recognize speech using Google Speech Recognition +try: + # for testing purposes, we're just using the default API key + # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")` + # instead of `r.recognize_google(audio)` + msg = r.recognize_google(audio, language='pt-BR') + print("Transcript> " + msg) + # Here you can set your conversation id - OR just remove it + api = ChatGPT(session_token, conversation_id='9f02d513-c6de-4cfa-898b-cd6fba91dbd9') + enhanced_text = api.send_message('Corrija esse trecho: '+msg) + print("ChatGPT> " + enhanced_text['message']) + output = open('enhanced-text.txt', 'w') + output.write(enhanced_text['message']) + output.close() +except sr.UnknownValueError: + print("Google Speech Recognition could not understand audio") +except sr.RequestError as e: + print("Could not request results from Google Speech Recognition service; {0}".format(e)) + diff --git a/src/pyChatGPT/pyChatGPT.py b/src/pyChatGPT/pyChatGPT.py index aa0b605..3a13083 100644 --- a/src/pyChatGPT/pyChatGPT.py +++ b/src/pyChatGPT/pyChatGPT.py @@ -15,12 +15,12 @@ import re import os - cf_challenge_form = (By.ID, 'challenge-form') - chatgpt_textbox = (By.TAG_NAME, 'textarea') chatgpt_streaming = (By.CLASS_NAME, 'result-streaming') chatgpt_big_response = (By.XPATH, '//div[@class="flex-1 overflow-hidden"]//div[p]') +# NOTE: The event result-streaming does not exists anymore. Thus, now Im waiting for the loading button event +chatgpt_streaming = (By.CLASS_NAME, 'text-2xl') chatgpt_small_response = ( By.XPATH, '//div[starts-with(@class, "markdown prose w-full break-words")]', @@ -408,7 +408,7 @@ def send_message(self, message: str, stream: bool = False) -> dict: self.__ensure_cf() self.logger.debug('Sending message...') - textbox = WebDriverWait(self.driver, 5).until( + textbox = WebDriverWait(self.driver, 8).until( EC.element_to_be_clickable(chatgpt_textbox) ) textbox.click() @@ -422,16 +422,18 @@ def send_message(self, message: str, stream: bool = False) -> dict: message, ) textbox.send_keys(Keys.ENTER) - + textbox.send_keys(Keys.ENTER) + # check the result-streaming tag (it doesnt seems to work) if stream: for i in self.__stream_message(): print(i, end='') time.sleep(0.1) return print() - + time.sleep(1) self.logger.debug('Waiting for completion...') - WebDriverWait(self.driver, 120).until_not( - EC.presence_of_element_located(chatgpt_streaming) + # FIXED: new chatgpt_streaming + WebDriverWait(self.driver, 200).until_not( + EC.presence_of_element_located(chatgpt_streaming) ) self.logger.debug('Getting response...') @@ -509,3 +511,4 @@ def refresh_chat_page(self) -> None: self.driver.get(chatgpt_chat_url) self.__check_capacity(chatgpt_chat_url) self.__check_blocking_elements() +