Skip to content
This repository was archived by the owner on Dec 7, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions convert_mkv_wav.py
Original file line number Diff line number Diff line change
@@ -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)

50 changes: 50 additions & 0 deletions speech-to-text.py
Original file line number Diff line number Diff line change
@@ -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 = '<YOUR SESSION TOKEN>'

# 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))

17 changes: 10 additions & 7 deletions src/pyChatGPT/pyChatGPT.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")]',
Expand Down Expand Up @@ -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()
Expand All @@ -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...')
Expand Down Expand Up @@ -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()