Update app.py
Browse files
app.py
CHANGED
|
@@ -130,7 +130,8 @@ else:
|
|
| 130 |
|
| 131 |
|
| 132 |
from transformers import pipeline
|
| 133 |
-
|
|
|
|
| 134 |
|
| 135 |
# Load the pipeline
|
| 136 |
tts = pipeline("text-to-speech")
|
|
@@ -144,27 +145,25 @@ STATEMENT = st.sidebar.text_area('Enter Text', DEFAULT_STATEMENT, height=150)
|
|
| 144 |
# Enable the button only if there is text in the TTS variable
|
| 145 |
if STATEMENT:
|
| 146 |
if st.sidebar.button('Convert Text to Speech'):
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
# Generate speech
|
| 151 |
speech = tts(text)
|
| 152 |
|
| 153 |
-
#
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
st.sidebar.write('Text converted to speech')
|
| 168 |
else:
|
| 169 |
st.sidebar.button('Convert Text to Speech', disabled=True)
|
| 170 |
# st.warning(' Please enter Statement!')
|
|
|
|
| 130 |
|
| 131 |
|
| 132 |
from transformers import pipeline
|
| 133 |
+
import soundfile as sf # For saving audio files
|
| 134 |
+
import base64 # For encoding audio data
|
| 135 |
|
| 136 |
# Load the pipeline
|
| 137 |
tts = pipeline("text-to-speech")
|
|
|
|
| 145 |
# Enable the button only if there is text in the TTS variable
|
| 146 |
if STATEMENT:
|
| 147 |
if st.sidebar.button('Convert Text to Speech'):
|
| 148 |
+
text = STATEMENT
|
| 149 |
+
|
|
|
|
| 150 |
# Generate speech
|
| 151 |
speech = tts(text)
|
| 152 |
|
| 153 |
+
# Access the audio waveform from the dictionary (assuming key name is 'audio')
|
| 154 |
+
audio_data = speech['audio']
|
| 155 |
+
|
| 156 |
+
# Convert audio data to a byte array
|
| 157 |
+
wav_data = sf.write_buffer(audio_data, samplerate=speech['sampling_rate'])
|
| 158 |
+
|
| 159 |
+
# Encode the byte array to base64
|
| 160 |
+
base64_audio = base64.b64encode(wav_data).decode("utf-8")
|
| 161 |
+
|
| 162 |
+
# Generate a download link
|
| 163 |
+
download_link = f"<a href='data:audio/wav;base64,{base64_audio}'>Download Speech</a>"
|
| 164 |
+
st.sidebar.write(download_link, unsafe_allow_html=True)
|
| 165 |
+
|
| 166 |
+
st.sidebar.write('Text converted to speech (download available)')
|
|
|
|
| 167 |
else:
|
| 168 |
st.sidebar.button('Convert Text to Speech', disabled=True)
|
| 169 |
# st.warning(' Please enter Statement!')
|