shukdevdatta123 commited on
Commit
ac8753d
·
verified ·
1 Parent(s): 7d9b87d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -17
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import os
2
  import numpy as np
 
3
  from pydub import AudioSegment
4
  import streamlit as st
5
  from io import BytesIO
@@ -108,24 +109,35 @@ def main():
108
  # Process and save files
109
  if st.button("Process Audio"):
110
  if uploaded_files:
111
- noisy_audios = {}
112
- for uploaded_file in uploaded_files:
113
- st.write(f"Processing {uploaded_file.name}...")
114
- audio_data = BytesIO(uploaded_file.read())
115
-
116
- noisy_audio = add_noise(audio_data, noise_type, noise_level, start_time, end_time)
117
- if noisy_audio:
118
- # Save to in-memory buffer
119
- buffer = BytesIO()
120
- noisy_audio.export(buffer, format="wav")
121
- buffer.seek(0)
122
- noisy_audios[uploaded_file.name] = buffer
123
- st.success(f"Added {noise_type} noise to {uploaded_file.name}.")
 
 
 
 
 
 
 
124
 
125
- if noisy_audios:
126
- st.write("### Download Processed Files")
127
- for name, buffer in noisy_audios.items():
128
- st.download_button(label=f"Download {name}", data=buffer, file_name=f"noisy_{name}", mime="audio/wav")
 
 
 
 
129
  else:
130
  st.warning("Please upload at least one audio file.")
131
 
 
1
  import os
2
  import numpy as np
3
+ import zipfile
4
  from pydub import AudioSegment
5
  import streamlit as st
6
  from io import BytesIO
 
109
  # Process and save files
110
  if st.button("Process Audio"):
111
  if uploaded_files:
112
+ zip_buffer = BytesIO()
113
+ with zipfile.ZipFile(zip_buffer, "w") as zf:
114
+ for uploaded_file in uploaded_files:
115
+ st.write(f"Processing {uploaded_file.name}...")
116
+ audio_data = BytesIO(uploaded_file.read())
117
+
118
+ noisy_audio = add_noise(audio_data, noise_type, noise_level, start_time, end_time)
119
+ if noisy_audio:
120
+ # Rename file based on noise type
121
+ base_name, ext = os.path.splitext(uploaded_file.name)
122
+ output_name = f"{noise_type}_{base_name}.wav"
123
+
124
+ # Save to in-memory buffer
125
+ buffer = BytesIO()
126
+ noisy_audio.export(buffer, format="wav")
127
+ buffer.seek(0)
128
+
129
+ # Add to zip archive
130
+ zf.writestr(output_name, buffer.read())
131
+ st.success(f"Added {noise_type} noise to {uploaded_file.name}.")
132
 
133
+ zip_buffer.seek(0)
134
+ st.write("### Download All Processed Files")
135
+ st.download_button(
136
+ label="Download All as ZIP",
137
+ data=zip_buffer,
138
+ file_name="processed_audio_files.zip",
139
+ mime="application/zip"
140
+ )
141
  else:
142
  st.warning("Please upload at least one audio file.")
143