import streamlit as st from audio_recorder_streamlit import audio_recorder import openai # Corrected import API_KEY = 'enter-openai-api-key-here' # Set the API key openai.api_key = API_KEY def transcribe_text_to_voice(audio_location): # Transcribe audio to text using Whisper API with open(audio_location, "rb") as audio_file: transcript = openai.Audio.transcriptions.create( model="whisper-1", file=audio_file ) return transcript['text'] def chat_completion_call(text): # Send the text to GPT-3.5 for chat completion response = openai.ChatCompletion.create( model="gpt-3.5-turbo-1106", messages=[{"role": "user", "content": text}] ) return response['choices'][0]['message']['content'] def text_to_speech_ai(speech_file_path, api_response): # Convert the text to speech using OpenAI's TTS API response = openai.Audio.speech.create( model="text-to-speech-1", # TTS model name may differ voice="nova", # Specify a voice (choose one that is available) input=api_response ) # Save the speech response to a file with open(speech_file_path, "wb") as f: f.write(response['audio']) st.title("🧑‍💻 Skolo Online 💬 Talking Assistant") """ Hi🤖 just click on the voice recorder and let me know how I can help you today? """ audio_bytes = audio_recorder() if audio_bytes: # Save the Recorded Audio File audio_location = "audio_file.wav" with open(audio_location, "wb") as f: f.write(audio_bytes) # Transcribe the saved file to text text = transcribe_text_to_voice(audio_location) st.write(text) # Get AI response from GPT-3.5 api_response = chat_completion_call(text) st.write(api_response) # Convert the response to speech and save it as a file speech_file_path = 'audio_response.mp3' text_to_speech_ai(speech_file_path, api_response) # Play the audio response in the app st.audio(speech_file_path)