fffiloni commited on
Commit
fb9bcbe
·
verified ·
1 Parent(s): aeba77b

add function for audio combination

Browse files
Files changed (1) hide show
  1. app.py +45 -9
app.py CHANGED
@@ -106,7 +106,7 @@ def infer(audio_input_path):
106
  if result.returncode == 0:
107
  print("Command executed successfully.")
108
  concat_out, file_list = concatenate_audio()
109
- return concat_out, gr.update(choices=file_list, value=file_list[0], visible=True), gr.update(visible=True), gr.update(value=file_list, visible=True)
110
  else:
111
  print("Error executing command.")
112
  raise gr.Error("Error executing command")
@@ -114,8 +114,33 @@ def infer(audio_input_path):
114
  def load_chosen_audio(audio_path):
115
  return audio_path
116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  def hide_previous():
118
- return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
119
 
120
  css="""
121
  div#col-container{
@@ -131,11 +156,16 @@ with gr.Blocks(css=css) as demo:
131
  audio_input = gr.Audio(label="Audio IN", type="filepath")
132
  submit_btn = gr.Button("Submit")
133
  output_result = gr.Audio(label="Translated result")
134
- dropdown_wav_selector = gr.Dropdown(
135
- label="Pick a generated audio to load",
136
- value = None,
137
- visible=False
138
- )
 
 
 
 
 
139
  with gr.Accordion("Downloadable audio Output list", open=False, visible=False) as result_accordion:
140
  wav_list = gr.Files(label="Output Audio List", visible=False)
141
 
@@ -155,14 +185,20 @@ with gr.Blocks(css=css) as demo:
155
  queue = False
156
  )
157
 
 
 
 
 
 
 
158
  submit_btn.click(
159
  fn = hide_previous,
160
  inputs = None,
161
- outputs = [dropdown_wav_selector, result_accordion, wav_list]
162
  ).then(
163
  fn = infer,
164
  inputs = [audio_input],
165
- outputs = [output_result, dropdown_wav_selector, result_accordion, wav_list]
166
  )
167
 
168
  demo.queue().launch(show_api=False, show_error=True)
 
106
  if result.returncode == 0:
107
  print("Command executed successfully.")
108
  concat_out, file_list = concatenate_audio()
109
+ return concat_out, gr.update(choices=file_list, value=file_list[0], visible=True), gr.update(visible=True), gr.update(value=file_list, visible=True), gr.update(visible=True)
110
  else:
111
  print("Error executing command.")
112
  raise gr.Error("Error executing command")
 
114
  def load_chosen_audio(audio_path):
115
  return audio_path
116
 
117
+ def overlay_audio(original_mp3: str, translated_wav: str) -> str:
118
+ # Load original MP3 and convert to WAV
119
+ original = AudioSegment.from_mp3(original_mp3).set_frame_rate(16000).set_channels(1)
120
+
121
+ # Lower the volume (reduce by 6 dB, which is half)
122
+ original = original - 6
123
+
124
+ # Load the translated WAV
125
+ translated = AudioSegment.from_wav(translated_wav).set_frame_rate(16000).set_channels(1)
126
+
127
+ # Overlay the translated speech over the original
128
+ combined = original.overlay(translated)
129
+
130
+ # Create a temporary file to save the output
131
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
132
+ combined.export(temp_file.name, format="wav")
133
+
134
+ print(f"Final audio saved at: {temp_file.name}")
135
+ return temp_file.name # Return the temporary file path
136
+
137
+ def process_final_combination(audio_in, chosen_translated):
138
+ audio_in = process_audio(audio_in)
139
+ temp_output_path = overlay_audio(audio_in, chosen_translated)
140
+ return gr.update(value=temp_output_path, visible=True)
141
+
142
  def hide_previous():
143
+ return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
144
 
145
  css="""
146
  div#col-container{
 
156
  audio_input = gr.Audio(label="Audio IN", type="filepath")
157
  submit_btn = gr.Button("Submit")
158
  output_result = gr.Audio(label="Translated result")
159
+
160
+ with gr.Row():
161
+ dropdown_wav_selector = gr.Dropdown(
162
+ label="Pick a generated audio to load",
163
+ value = None,
164
+ visible=False,
165
+ scale=2
166
+ )
167
+ choose_this_btn = gr.Button("Use this one", scale=1)
168
+ combined_output = gr.Audio("Combined Outpu", visible=False)
169
  with gr.Accordion("Downloadable audio Output list", open=False, visible=False) as result_accordion:
170
  wav_list = gr.Files(label="Output Audio List", visible=False)
171
 
 
185
  queue = False
186
  )
187
 
188
+ choose_this_btn.click(
189
+ fn = process_final_combination,
190
+ inputs = [audio_input, dropdown_wav_selector],
191
+ outputs = [combined_output]
192
+ )
193
+
194
  submit_btn.click(
195
  fn = hide_previous,
196
  inputs = None,
197
+ outputs = [dropdown_wav_selector, result_accordion, wav_list, choose_this_btn, combined_output]
198
  ).then(
199
  fn = infer,
200
  inputs = [audio_input],
201
+ outputs = [output_result, dropdown_wav_selector, result_accordion, wav_list, choose_this_btn]
202
  )
203
 
204
  demo.queue().launch(show_api=False, show_error=True)