Spaces:
Sleeping
Sleeping
import streamlit as st | |
from audio_recorder_streamlit import audio_recorder | |
import openai | |
# Prompt the user to input their OpenAI API key | |
API_KEY = st.text_input("Enter your OpenAI API Key", type="password") | |
# Check if the API key is provided | |
if API_KEY: | |
openai.api_key = API_KEY # Set the 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']) | |
# Streamlit interface for voice assistant | |
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("You said:", text) | |
# Get AI response from GPT-3.5 | |
api_response = chat_completion_call(text) | |
st.write("AI says:", 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) | |
else: | |
st.warning("Please enter your OpenAI API key to continue.") | |