Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import openai
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
|
| 5 |
+
# Set your API Key
|
| 6 |
+
openai.api_key = "your-groq-api-key"
|
| 7 |
+
|
| 8 |
+
# Inject Custom CSS and JavaScript for the UI
|
| 9 |
+
st.markdown(
|
| 10 |
+
"""
|
| 11 |
+
<style>
|
| 12 |
+
/* Black background with animated moving balls */
|
| 13 |
+
body {
|
| 14 |
+
background-color: black;
|
| 15 |
+
color: white;
|
| 16 |
+
overflow: hidden;
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
/* Animation for the background */
|
| 20 |
+
@keyframes move {
|
| 21 |
+
0% {
|
| 22 |
+
transform: translateY(0) translateX(0);
|
| 23 |
+
}
|
| 24 |
+
50% {
|
| 25 |
+
transform: translateY(-20px) translateX(20px);
|
| 26 |
+
}
|
| 27 |
+
100% {
|
| 28 |
+
transform: translateY(0) translateX(0);
|
| 29 |
+
}
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
.background {
|
| 33 |
+
position: absolute;
|
| 34 |
+
width: 100%;
|
| 35 |
+
height: 100%;
|
| 36 |
+
z-index: -1;
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
.ball {
|
| 40 |
+
position: absolute;
|
| 41 |
+
width: 20px;
|
| 42 |
+
height: 20px;
|
| 43 |
+
background-color: #FF4500;
|
| 44 |
+
border-radius: 50%;
|
| 45 |
+
animation: move 3s infinite;
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
.ball:nth-child(1) {
|
| 49 |
+
top: 10%;
|
| 50 |
+
left: 20%;
|
| 51 |
+
}
|
| 52 |
+
.ball:nth-child(2) {
|
| 53 |
+
top: 30%;
|
| 54 |
+
left: 70%;
|
| 55 |
+
animation-delay: 1s;
|
| 56 |
+
}
|
| 57 |
+
.ball:nth-child(3) {
|
| 58 |
+
top: 50%;
|
| 59 |
+
left: 40%;
|
| 60 |
+
animation-delay: 2s;
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
/* Styled input box */
|
| 64 |
+
textarea, select {
|
| 65 |
+
background-color: black;
|
| 66 |
+
color: white;
|
| 67 |
+
border: 2px solid #FF4500;
|
| 68 |
+
border-radius: 8px;
|
| 69 |
+
padding: 10px;
|
| 70 |
+
width: 100%;
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
textarea:focus, select:focus {
|
| 74 |
+
outline: none;
|
| 75 |
+
border-color: #FFD700;
|
| 76 |
+
box-shadow: 0 0 15px #FFD700;
|
| 77 |
+
transition: 0.3s ease-in-out;
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
/* Styled button */
|
| 81 |
+
.stButton button {
|
| 82 |
+
background: linear-gradient(45deg, #FF4500, #FFD700);
|
| 83 |
+
color: white;
|
| 84 |
+
font-size: 16px;
|
| 85 |
+
font-weight: bold;
|
| 86 |
+
border: none;
|
| 87 |
+
border-radius: 12px;
|
| 88 |
+
padding: 10px 20px;
|
| 89 |
+
transition: transform 0.3s, box-shadow 0.3s;
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
.stButton button:hover {
|
| 93 |
+
transform: scale(1.1);
|
| 94 |
+
box-shadow: 0 0 15px #FFD700;
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
</style>
|
| 98 |
+
""",
|
| 99 |
+
unsafe_allow_html=True,
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
# Add moving balls to the background
|
| 103 |
+
st.markdown(
|
| 104 |
+
"""
|
| 105 |
+
<div class="background">
|
| 106 |
+
<div class="ball"></div>
|
| 107 |
+
<div class="ball"></div>
|
| 108 |
+
<div class="ball"></div>
|
| 109 |
+
</div>
|
| 110 |
+
""",
|
| 111 |
+
unsafe_allow_html=True,
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
# Application title
|
| 115 |
+
st.title("✨ Text-to-Speech Generator ✨")
|
| 116 |
+
st.write("Enter text and convert it to speech with OpenAI's TTS model!")
|
| 117 |
+
|
| 118 |
+
# Input text and voice options
|
| 119 |
+
text_input = st.text_area("🎤 Enter Text Below", "Hello world! This is a streaming test.")
|
| 120 |
+
voice_option = st.selectbox("🎵 Choose a Voice", ["alloy", "default"]) # Expand as needed
|
| 121 |
+
|
| 122 |
+
if st.button("Generate Speech 🎶"):
|
| 123 |
+
with st.spinner("✨ Generating... Please wait ✨"):
|
| 124 |
+
try:
|
| 125 |
+
# API call to OpenAI
|
| 126 |
+
response = openai.Audio.create(
|
| 127 |
+
model="tts-1",
|
| 128 |
+
voice=voice_option,
|
| 129 |
+
input=text_input,
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
# Load audio into memory
|
| 133 |
+
audio_bytes = BytesIO(response["audio"])
|
| 134 |
+
|
| 135 |
+
# Audio playback in Streamlit
|
| 136 |
+
st.audio(audio_bytes, format="audio/mp3")
|
| 137 |
+
st.success("✅ Speech generated successfully!")
|
| 138 |
+
except Exception as e:
|
| 139 |
+
st.error(f"❌ An error occurred: {str(e)}")
|