import gradio as gr import openai from decouple import config from gtts import gTTS import io import pydub import config openai.api_key = config.API_KEYS['openai'] # The Models Job or role messages = [ {"role": "system", "content": "You are a helpful assistant."}, ] # Main method goes here def decipher(audio): global messages # Using openAI's speech to text model transcript = openai.Audio.transcribe("whisper-1", audio) messages.append({"role": "user", "content": transcript["text"]}) response = openai.Completion.create( model="text-davinci-002", prompt="Conversation:\n" + "\n".join([f"{m['role']}: {m['content']}" for m in messages]), temperature=0.7, max_tokens=1024, ) system_message = response.choices[0].text messages.append({"role": "assistant", "content": system_message}) # Convert the text to audio using gTTS tts = gTTS(text=system_message, lang='en') audio_data = io.BytesIO() tts.write_to_fp(audio_data) # Convert the audio to a playable format using pydub audio_data.seek(0) audio = pydub.AudioSegment.from_file(io.BytesIO(audio_data.getvalue())) # Play the audio using VLC player = pydub.playback.play player(audio) chat_transcript = "" for message in messages: if message['role'] != 'system': chat_transcript += message['role'] + ": " + message['content'] + "\n\n" return chat_transcript # Using Gradio's audio Interface interface = gr.Interface(fn=decipher, inputs=gr.inputs.Audio( type="filepath", label="Record a voice message"), outputs="text", title="AI Assistant", description="An AI assistant that can transcribe and respond to voice messages", theme="compact") if __name__ == "__main__": interface.launch()