fargerm commited on
Commit
d50e22f
1 Parent(s): ca4c4e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -2
app.py CHANGED
@@ -4,6 +4,7 @@ from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
4
  from transformers import MarianMTModel, MarianTokenizer
5
  import soundfile as sf
6
  from datasets import load_dataset
 
7
 
8
  # Device setup
9
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
@@ -82,7 +83,12 @@ if st.button("Listen to Translated Audio"):
82
  speech = tts_pipe(st.session_state.translated_text, forward_params={"speaker_embeddings": speaker_embedding})
83
  st.session_state.audio_path = "translated_speech.wav"
84
  sf.write(st.session_state.audio_path, speech["audio"], samplerate=speech["sampling_rate"])
85
- st.audio(st.session_state.audio_path, format="audio/wav")
 
 
 
 
 
86
  else:
87
  st.error("Please submit the text first.")
88
 
@@ -98,4 +104,4 @@ if st.button("Reset"):
98
  if st.session_state.translated_text and not st.session_state.audio_path:
99
  st.write(f"Translated text: {st.session_state.translated_text}")
100
  elif st.session_state.audio_path:
101
- st.audio(st.session_state.audio_path, format="audio/wav")
 
4
  from transformers import MarianMTModel, MarianTokenizer
5
  import soundfile as sf
6
  from datasets import load_dataset
7
+ import os
8
 
9
  # Device setup
10
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
 
83
  speech = tts_pipe(st.session_state.translated_text, forward_params={"speaker_embeddings": speaker_embedding})
84
  st.session_state.audio_path = "translated_speech.wav"
85
  sf.write(st.session_state.audio_path, speech["audio"], samplerate=speech["sampling_rate"])
86
+
87
+ # Ensure the audio file exists and display the player
88
+ if os.path.exists(st.session_state.audio_path):
89
+ st.audio(st.session_state.audio_path, format="audio/wav")
90
+ else:
91
+ st.error("Failed to generate audio. Please try again.")
92
  else:
93
  st.error("Please submit the text first.")
94
 
 
104
  if st.session_state.translated_text and not st.session_state.audio_path:
105
  st.write(f"Translated text: {st.session_state.translated_text}")
106
  elif st.session_state.audio_path:
107
+ st.audio(st.session_state.audio_path, format="audio/wav")