nitikdias commited on
Commit
27df5d6
·
verified ·
1 Parent(s): ab5dda0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -15
app.py CHANGED
@@ -1,31 +1,24 @@
1
  import gradio as gr
2
  from pydub import AudioSegment
 
3
  import os
4
 
5
- # Folder to save converted WAV files
6
- output_dir = "converted_audio"
7
- os.makedirs(output_dir, exist_ok=True)
8
-
9
  def convert_mp3_to_wav(mp3_file):
10
- # Load the MP3 file
11
  audio = AudioSegment.from_mp3(mp3_file.name)
12
 
13
- # Construct output path
14
- base_name = os.path.splitext(os.path.basename(mp3_file.name))[0]
15
- output_path = os.path.join(output_dir, f"{base_name}.wav")
16
-
17
- # Export as WAV
18
- audio.export(output_path, format="wav")
19
-
20
- return f"Saved WAV file to: {output_path}"
21
 
22
  # Gradio Interface
23
  iface = gr.Interface(
24
  fn=convert_mp3_to_wav,
25
  inputs=gr.File(label="Upload MP3 File", file_types=[".mp3"]),
26
- outputs="text",
27
  title="MP3 to WAV Converter",
28
- description="Upload an MP3 file and it will be converted to WAV and saved in the 'converted_audio' folder."
29
  )
30
 
31
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  from pydub import AudioSegment
3
+ import tempfile
4
  import os
5
 
 
 
 
 
6
  def convert_mp3_to_wav(mp3_file):
7
+ # Load the MP3 file using Pydub
8
  audio = AudioSegment.from_mp3(mp3_file.name)
9
 
10
+ # Create a temporary file for the WAV output
11
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_wav:
12
+ audio.export(tmp_wav.name, format="wav")
13
+ return tmp_wav.name # Return path to temporary WAV file
 
 
 
 
14
 
15
  # Gradio Interface
16
  iface = gr.Interface(
17
  fn=convert_mp3_to_wav,
18
  inputs=gr.File(label="Upload MP3 File", file_types=[".mp3"]),
19
+ outputs=gr.File(label="Download WAV File"),
20
  title="MP3 to WAV Converter",
21
+ description="Upload an MP3 file and download the converted WAV file."
22
  )
23
 
24
  if __name__ == "__main__":