import openai
import requests
import os
import time
from gtts import gTTS
from pydub import AudioSegment
# Set up the OpenAI API credentials
openai.api_key = '<your-api-key>'
# Define the path to the audio file to be converted to text
AUDIO_FILE_PATH = '/path/to/sample.mp3'
# Define the request payload to convert audio to text using OpenAI Speech
response = openai.Completion.create(
engine="davinci",
prompt=f"Transcribe the following audio file: {AUDIO_FILE_PATH}",
audio=open(AUDIO_FILE_PATH, "rb").read(),
max_tokens=2048
)
# Extract the text output from the OpenAI Speech response
text_output = response['choices'][0]['text']
# Define the message to send to ChatGPT
message = text_output
# Define the request payload to send message to ChatGPT using Whisper
WHISPER_API_ENDPOINT = 'https://api.whisper.ai/v1'
WHISPER_ACCESS_TOKEN = '<your-whisper-access-token>'
payload = {
'access_token': WHISPER_ACCESS_TOKEN,
'message': message,
'to': 'ChatGPT'
}
# Send the request to the Whisper API to send message to ChatGPT
response = requests.post(f'{WHISPER_API_ENDPOINT}/message/send', json=payload)
# Parse the response and extract the ChatGPT response
chatgpt_response = response.json()['response']
# Define the language for the text-to-speech conversion
LANGUAGE = 'fr'
# Define the filename for the output audio file
AUDIO_FILE_NAME = 'chatgpt_output.mp3'
# Use the gTTS library to convert the ChatGPT response to speech
tts = gTTS(text=chatgpt_response, lang=LANGUAGE)
# Save the speech output to an audio file
tts.save(AUDIO_FILE_NAME)
# Load the audio file using the pydub library
audio = AudioSegment.from_file(AUDIO_FILE_NAME)
# Play the audio file using the default audio player
audio.export(out_f=os.devnull).close() # Required to avoid issue with audio playback on some systems
audio.export(f"{AUDIO_FILE_NAME}.wav", format='wav')
os.system(f"aplay {AUDIO_FILE_NAME}.wav")
# Clean up temporary audio files
os.remove(AUDIO_FILE_NAME)
os.remove(f"{AUDIO_FILE_NAME}.wav")