fffiloni commited on
Commit
0ac86ac
·
verified ·
1 Parent(s): 8c822aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -2
app.py CHANGED
@@ -1,7 +1,52 @@
1
  import gradio as gr
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  def infer(audio_input_path):
4
 
 
 
5
  import subprocess
6
 
7
  command = [
@@ -19,10 +64,12 @@ def infer(audio_input_path):
19
  # Check if the command was successful
20
  if result.returncode == 0:
21
  print("Command executed successfully.")
 
 
22
  else:
23
  print("Error executing command.")
24
-
25
- return "out_en-1.wav"
26
 
27
  with gr.Blocks() as demo:
28
  with gr.Column(elem_id="col-container"):
 
1
  import gradio as gr
2
 
3
+ import glob
4
+ import os
5
+ from pydub import AudioSegment
6
+
7
+ def cleanup_old_audio():
8
+ """Remove old audio files before starting a new inference."""
9
+ files_to_remove = glob.glob("out_en-*.wav") + glob.glob("final_output.wav")
10
+
11
+ if files_to_remove:
12
+ print(f"Cleaning up {len(files_to_remove)} old audio files...")
13
+ for file in files_to_remove:
14
+ try:
15
+ os.remove(file)
16
+ print(f"Deleted: {file}")
17
+ except Exception as e:
18
+ print(f"Error deleting {file}: {e}")
19
+ else:
20
+ print("No old audio files found.")
21
+
22
+ def concatenate_audio(output_filename="final_output.wav"):
23
+ # Find all WAV files matching the pattern
24
+ wav_files = glob.glob("out_en-*.wav")
25
+
26
+ # Extract numbers and sort properly
27
+ wav_files.sort(key=lambda x: int(x.split('-')[-1].split('.')[0]))
28
+
29
+ if not wav_files:
30
+ print("No audio files found.")
31
+ return
32
+
33
+ print(f"Found {len(wav_files)} audio chunks: {wav_files}")
34
+
35
+ # Load and concatenate all audio files
36
+ combined = AudioSegment.empty()
37
+ for file in wav_files:
38
+ audio = AudioSegment.from_wav(file)
39
+ combined += audio
40
+
41
+ # Export the final combined audio
42
+ combined.export(output_filename, format="wav")
43
+ print(f"Concatenated audio saved as {output_filename}")
44
+
45
+
46
  def infer(audio_input_path):
47
 
48
+ cleanup_old_audio()
49
+
50
  import subprocess
51
 
52
  command = [
 
64
  # Check if the command was successful
65
  if result.returncode == 0:
66
  print("Command executed successfully.")
67
+ concatenate_audio()
68
+ return "final_output.wav"
69
  else:
70
  print("Error executing command.")
71
+ raise gr.Error("Error executing command")
72
+
73
 
74
  with gr.Blocks() as demo:
75
  with gr.Column(elem_id="col-container"):