Spaces:
Running
Running
shukdevdatta123
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,7 @@ import openai
|
|
3 |
from kokoro import KPipeline
|
4 |
import soundfile as sf
|
5 |
import io
|
6 |
-
import time
|
7 |
|
8 |
# Streamlit App UI Setup
|
9 |
st.title("Text-to-Speech Translator with Kokoro")
|
@@ -59,71 +59,6 @@ st.sidebar.markdown("""
|
|
59 |
- Enjoy exploring different languages, voices, and speeds with the text-to-speech conversion!
|
60 |
""")
|
61 |
|
62 |
-
# Custom HTML and CSS for Spinner
|
63 |
-
st.markdown("""
|
64 |
-
<style>
|
65 |
-
.lds-ellipsis,
|
66 |
-
.lds-ellipsis div {
|
67 |
-
box-sizing: border-box;
|
68 |
-
}
|
69 |
-
.lds-ellipsis {
|
70 |
-
display: inline-block;
|
71 |
-
position: relative;
|
72 |
-
width: 80px;
|
73 |
-
height: 80px;
|
74 |
-
}
|
75 |
-
.lds-ellipsis div {
|
76 |
-
position: absolute;
|
77 |
-
top: 33.33333px;
|
78 |
-
width: 13.33333px;
|
79 |
-
height: 13.33333px;
|
80 |
-
border-radius: 50%;
|
81 |
-
background: currentColor;
|
82 |
-
animation-timing-function: cubic-bezier(0, 1, 1, 0);
|
83 |
-
}
|
84 |
-
.lds-ellipsis div:nth-child(1) {
|
85 |
-
left: 8px;
|
86 |
-
animation: lds-ellipsis1 0.6s infinite;
|
87 |
-
}
|
88 |
-
.lds-ellipsis div:nth-child(2) {
|
89 |
-
left: 8px;
|
90 |
-
animation: lds-ellipsis2 0.6s infinite;
|
91 |
-
}
|
92 |
-
.lds-ellipsis div:nth-child(3) {
|
93 |
-
left: 32px;
|
94 |
-
animation: lds-ellipsis2 0.6s infinite;
|
95 |
-
}
|
96 |
-
.lds-ellipsis div:nth-child(4) {
|
97 |
-
left: 56px;
|
98 |
-
animation: lds-ellipsis3 0.6s infinite;
|
99 |
-
}
|
100 |
-
@keyframes lds-ellipsis1 {
|
101 |
-
0% {
|
102 |
-
transform: scale(0);
|
103 |
-
}
|
104 |
-
100% {
|
105 |
-
transform: scale(1);
|
106 |
-
}
|
107 |
-
}
|
108 |
-
@keyframes lds-ellipsis3 {
|
109 |
-
0% {
|
110 |
-
transform: scale(1);
|
111 |
-
}
|
112 |
-
100% {
|
113 |
-
transform: scale(0);
|
114 |
-
}
|
115 |
-
}
|
116 |
-
@keyframes lds-ellipsis2 {
|
117 |
-
0% {
|
118 |
-
transform: translate(0, 0);
|
119 |
-
}
|
120 |
-
100% {
|
121 |
-
transform: translate(24px, 0);
|
122 |
-
}
|
123 |
-
}
|
124 |
-
</style>
|
125 |
-
""", unsafe_allow_html=True)
|
126 |
-
|
127 |
# User input for text, language, and voice settings
|
128 |
input_text = st.text_area("Enter your text here", placeholder="The sky above the port was the color of television...")
|
129 |
lang_code = st.selectbox("Select Language", ['a', 'b', 'e', 'f', 'h', 'i', 'p', 'z', 'j'])
|
@@ -181,27 +116,30 @@ def translate_to_english(api_key, text, lang_code):
|
|
181 |
# Generate Audio function
|
182 |
def generate_audio(text, lang_code, voice, speed):
|
183 |
generator = pipeline(text, voice=voice, speed=speed, split_pattern=r'\n+')
|
|
|
184 |
for i, (gs, ps, audio) in enumerate(generator):
|
185 |
audio_data = audio
|
186 |
-
#
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
|
|
192 |
|
193 |
# Generate and display the audio file
|
194 |
if st.button('Generate Audio'):
|
195 |
-
# Display custom spinner while generating audio
|
196 |
-
spinner_placeholder = st.empty() # Create a placeholder for the spinner
|
197 |
-
spinner_placeholder.markdown('<div class="lds-ellipsis"><div></div><div></div><div></div><div></div></div>', unsafe_allow_html=True)
|
198 |
|
199 |
-
#
|
200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
201 |
|
202 |
-
# Remove the spinner once audio generation is complete
|
203 |
-
spinner_placeholder.empty()
|
204 |
-
|
205 |
# Display Audio player for the original language
|
206 |
st.audio(audio_buffer, format='audio/wav')
|
207 |
|
@@ -231,4 +169,4 @@ if st.button('Generate Audio'):
|
|
231 |
data=translated_audio_buffer,
|
232 |
file_name="generated_speech_translated.wav",
|
233 |
mime="audio/wav"
|
234 |
-
)
|
|
|
3 |
from kokoro import KPipeline
|
4 |
import soundfile as sf
|
5 |
import io
|
6 |
+
import time
|
7 |
|
8 |
# Streamlit App UI Setup
|
9 |
st.title("Text-to-Speech Translator with Kokoro")
|
|
|
59 |
- Enjoy exploring different languages, voices, and speeds with the text-to-speech conversion!
|
60 |
""")
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
# User input for text, language, and voice settings
|
63 |
input_text = st.text_area("Enter your text here", placeholder="The sky above the port was the color of television...")
|
64 |
lang_code = st.selectbox("Select Language", ['a', 'b', 'e', 'f', 'h', 'i', 'p', 'z', 'j'])
|
|
|
116 |
# Generate Audio function
|
117 |
def generate_audio(text, lang_code, voice, speed):
|
118 |
generator = pipeline(text, voice=voice, speed=speed, split_pattern=r'\n+')
|
119 |
+
audio_data = None
|
120 |
for i, (gs, ps, audio) in enumerate(generator):
|
121 |
audio_data = audio
|
122 |
+
time.sleep(1) # Simulate processing delay for the spinner (this can be removed or adjusted)
|
123 |
+
# Save audio to in-memory buffer
|
124 |
+
buffer = io.BytesIO()
|
125 |
+
# Explicitly specify format as WAV
|
126 |
+
sf.write(buffer, audio_data, 24000, format='WAV') # Add 'format="WAV"'
|
127 |
+
buffer.seek(0)
|
128 |
+
return buffer
|
129 |
|
130 |
# Generate and display the audio file
|
131 |
if st.button('Generate Audio'):
|
|
|
|
|
|
|
132 |
|
133 |
+
# Show the spinner with percentage count
|
134 |
+
with st.spinner("Generating audio... 0%"):
|
135 |
+
for i in range(1, 101, 10):
|
136 |
+
time.sleep(0.5) # Simulate work being done, you can adjust timing here
|
137 |
+
st.spinner(f"Generating audio... {i}%")
|
138 |
+
|
139 |
+
# Generate audio for the original text after the spinner
|
140 |
+
st.write("Generating speech for the original text...")
|
141 |
+
audio_buffer = generate_audio(input_text, lang_code, voice, speed)
|
142 |
|
|
|
|
|
|
|
143 |
# Display Audio player for the original language
|
144 |
st.audio(audio_buffer, format='audio/wav')
|
145 |
|
|
|
169 |
data=translated_audio_buffer,
|
170 |
file_name="generated_speech_translated.wav",
|
171 |
mime="audio/wav"
|
172 |
+
)
|