import streamlit as st from openai import OpenAI import os # Initialize OpenAI client openai_api_key = os.getenv("OPENAI_API_KEY") # Ensure you have this environment variable set client = OpenAI(api_key=openai_api_key) # Streamlit interface st.title('Text to Speech with OpenAI') # Voice selection voice_option = st.selectbox( 'Choose a voice:', ['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'] ) # Text input text_input = st.text_area("Enter the text you want to convert to speech:") # Convert to speech if st.button('Convert to Speech'): if text_input: try: response = client.audio.speech.create( model="tts-1", voice=voice_option, input=text_input, ) # Stream or save the response as needed # For demonstration, let's assume we save then provide a link for downloading audio_file_path = "output.mp3" response.stream_to_file(audio_file_path) # Display audio file to download st.audio(audio_file_path, format='audio/mp3') st.success("Conversion successful!") except Exception as e: st.error(f"An error occurred: {e}") else: st.error("Please enter some text to convert.")