cmagganas commited on
Commit
74f66bb
·
1 Parent(s): 628521d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -12
app.py CHANGED
@@ -26,20 +26,25 @@ if uploaded_file:
26
  # Convert audio to tensor
27
  audio_tensor = torch.tensor(audio).float()
28
 
29
- with st.spinner("Inverting audio..."):
30
- # Invert the audio using the modified function
31
- inverted_audio_tensor = invert_audio(model, processor, audio_tensor, sr)
32
-
33
- # Convert tensor back to numpy
34
- inverted_audio_np = inverted_audio_tensor.numpy()
35
-
36
- # Convert numpy audio to bytes and play
37
- with io.BytesIO() as out_io:
38
- sf.write(out_io, inverted_audio_np, sr, format="wav")
39
- st.audio(out_io.getvalue(), format="audio/wav")
 
 
 
 
 
40
 
41
  # Offer a download button for the inverted audio
42
  if st.button("Download Inverted Audio"):
43
  with io.BytesIO() as out_io:
44
- sf.write(out_io, inverted_audio_np, sr, format="wav")
45
  st.download_button("Download Inverted Audio", data=out_io.getvalue(), file_name="inverted_output.wav", mime="audio/wav")
 
26
  # Convert audio to tensor
27
  audio_tensor = torch.tensor(audio).float()
28
 
29
+ # Use Streamlit's session state to prevent re-inversion
30
+ if "inverted_audio" not in st.session_state:
31
+ with st.spinner("Inverting audio..."):
32
+ # Invert the audio using the modified function
33
+ inverted_audio_tensor = invert_audio(model, processor, audio_tensor, sr)
34
+
35
+ # Convert tensor back to numpy
36
+ inverted_audio_np = inverted_audio_tensor.numpy()
37
+
38
+ # Store inverted audio in session state
39
+ st.session_state.inverted_audio = inverted_audio_np
40
+
41
+ # Play inverted audio from session state
42
+ with io.BytesIO() as out_io:
43
+ sf.write(out_io, st.session_state.inverted_audio, sr, format="wav")
44
+ st.audio(out_io.getvalue(), format="audio/wav")
45
 
46
  # Offer a download button for the inverted audio
47
  if st.button("Download Inverted Audio"):
48
  with io.BytesIO() as out_io:
49
+ sf.write(out_io, st.session_state.inverted_audio, sr, format="wav")
50
  st.download_button("Download Inverted Audio", data=out_io.getvalue(), file_name="inverted_output.wav", mime="audio/wav")