import streamlit as st
import openai
import os
from io import BytesIO
# Set your API Key
openai.api_key = os.getenv("GROQ_API_KEY")
# Inject Custom CSS and JavaScript for the UI
st.markdown(
"""
""",
unsafe_allow_html=True,
)
# Add moving balls to the background
st.markdown(
"""
""",
unsafe_allow_html=True,
)
# Application title
st.title("✨ Text-to-Speech Generator ✨")
st.write("Enter text and convert it to speech with OpenAI's TTS model!")
# Input text and voice options
text_input = st.text_area("🎤 Enter Text Below", "Hello world! This is a streaming test.")
voice_option = st.selectbox("🎵 Choose a Voice", ["alloy", "default"]) # Expand as needed
if st.button("Generate Speech 🎶"):
with st.spinner("✨ Generating... Please wait ✨"):
try:
# API call to OpenAI
response = openai.Audio.create(
model="tts-1",
voice=voice_option,
input=text_input,
)
# Load audio into memory
audio_bytes = BytesIO(response["audio"])
# Audio playback in Streamlit
st.audio(audio_bytes, format="audio/mp3")
st.success("✅ Speech generated successfully!")
except Exception as e:
st.error(f"❌ An error occurred: {str(e)}")