richardorama commited on
Commit
cbd867d
·
verified ·
1 Parent(s): 7b9b888

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -19
app.py CHANGED
@@ -130,7 +130,8 @@ else:
130
 
131
 
132
  from transformers import pipeline
133
- # import sounddevice as sd # Import for audio playback (optional)
 
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
- # Text to generate speech from
148
- text = STATEMENT # Use the user input from STATEMENT
149
-
150
  # Generate speech
151
  speech = tts(text)
152
 
153
- # st.sidebar.write(speech.keys())
154
-
155
- # Access the audio waveform from the dictionary (assuming key name is 'waveform')
156
- audio_data = speech['audio'] # speech['waveform']
157
-
158
- # Optional: Save the audio to a file (uncomment if needed)
159
- sd.write(audio_data, samplerate=speech['sampling_rate']) # Adjust samplerate if necessary
160
- with open("sample_tts.wav", "wb") as f:
161
- f.write(audio_data)
162
-
163
- # Optional: Play the audio directly in Streamlit (uncomment if needed)
164
- # Huggingface does not have direct access to the generated audio data
165
- # sd.play(audio_data, samplerate=speech['sampling_rate']) # Adjust samplerate if necessary
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!')