cmagganas commited on
Commit
07d370a
1 Parent(s): 7585d18

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -34
app.py CHANGED
@@ -1,6 +1,5 @@
1
- import streamlit as st
2
  import torch
3
- import julius
4
  import soundfile as sf
5
  import io
6
 
@@ -12,45 +11,87 @@ preloaded["model"], preloaded["processor"] = load_model()
12
  model = preloaded["model"]
13
  processor = preloaded["processor"]
14
 
15
- st.title("Audio Inversion with HuggingFace & Streamlit")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- # If this is the first run, create a new session state attribute for uploaded file
18
- if 'uploaded_file' not in st.session_state:
19
- st.session_state.uploaded_file = None
20
 
21
- # Get the uploaded file
22
- uploaded_file = st.file_uploader("Upload an audio file", type=["wav", "flac"])
 
 
 
23
 
24
- # Update the session state only if a new file is uploaded
25
- if uploaded_file is not None:
26
- st.session_state.uploaded_file = uploaded_file.getvalue() # store content, not the file object
27
 
28
- if st.session_state.uploaded_file:
29
- # Play the uploaded audio
30
- audio_byte_content = st.session_state.uploaded_file
31
- st.audio(audio_byte_content, format="audio/wav")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
- # Read the audio file
34
- audio, sr = sf.read(io.BytesIO(audio_byte_content))
35
 
36
- # Convert audio to tensor
37
- audio_tensor = torch.tensor(audio).float()
38
 
39
- @st.cache(allow_output_mutation=True, suppress_st_warning=True)
40
- def cache_inverted_audio(audio_tensor):
41
- return invert_audio(model, processor, audio_tensor, sr)
42
 
43
- # Use cached result
44
- inverted_audio_tensor = cache_inverted_audio(audio_tensor)
45
- inverted_audio_np = inverted_audio_tensor.numpy()
46
 
47
- # Play inverted audio
48
- with io.BytesIO() as out_io:
49
- sf.write(out_io, inverted_audio_np, sr, format="wav")
50
- st.audio(out_io.getvalue(), format="audio/wav")
51
 
52
- # Offer a download button for the inverted audio
53
- if st.button("Download Inverted Audio"):
54
- with io.BytesIO() as out_io:
55
- sf.write(out_io, inverted_audio_np, sr, format="wav")
56
- st.download_button("Download Inverted Audio", data=out_io.getvalue(), file_name="inverted_output.wav", mime="audio/wav")
 
1
+ import gradio as gr
2
  import torch
 
3
  import soundfile as sf
4
  import io
5
 
 
11
  model = preloaded["model"]
12
  processor = preloaded["processor"]
13
 
14
+ def gr_invert_audio(input_audio):
15
+ # Extract the file content and sampling rate
16
+ audio, sr = sf.read(input_audio)
17
+
18
+ # Convert audio to tensor
19
+ audio_tensor = torch.tensor(audio).float()
20
+
21
+ # Invert the audio
22
+ inverted_audio_tensor = invert_audio(model, processor, audio_tensor, sr)
23
+ inverted_audio_np = inverted_audio_tensor.numpy()
24
+
25
+ # Save the inverted audio to a temporary file and return it
26
+ with io.BytesIO() as out_io:
27
+ sf.write(out_io, inverted_audio_np, sr, format="wav")
28
+ out_io.seek(0)
29
+ return out_io.read()
30
+
31
+ # Gradio interface
32
+ iface = gr.Interface(
33
+ fn=gr_invert_audio,
34
+ inputs=gr.inputs.Audio(type="file", label="Upload an Audio File"),
35
+ outputs=gr.outputs.Audio(label="Inverted Audio"),
36
+ live=True
37
+ )
38
+
39
+ iface.launch()
40
 
 
 
 
41
 
42
+ # import streamlit as st
43
+ # import torch
44
+ # import julius
45
+ # import soundfile as sf
46
+ # import io
47
 
48
+ # from model import load_model, invert_audio
 
 
49
 
50
+ # # Load the model and processor
51
+ # preloaded = {}
52
+ # preloaded["model"], preloaded["processor"] = load_model()
53
+ # model = preloaded["model"]
54
+ # processor = preloaded["processor"]
55
+
56
+ # st.title("Audio Inversion with HuggingFace & Streamlit")
57
+
58
+ # # If this is the first run, create a new session state attribute for uploaded file
59
+ # if 'uploaded_file' not in st.session_state:
60
+ # st.session_state.uploaded_file = None
61
+
62
+ # # Get the uploaded file
63
+ # uploaded_file = st.file_uploader("Upload an audio file", type=["wav", "flac"])
64
+
65
+ # # Update the session state only if a new file is uploaded
66
+ # if uploaded_file is not None:
67
+ # st.session_state.uploaded_file = uploaded_file.getvalue() # store content, not the file object
68
+
69
+ # if st.session_state.uploaded_file:
70
+ # # Play the uploaded audio
71
+ # audio_byte_content = st.session_state.uploaded_file
72
+ # st.audio(audio_byte_content, format="audio/wav")
73
 
74
+ # # Read the audio file
75
+ # audio, sr = sf.read(io.BytesIO(audio_byte_content))
76
 
77
+ # # Convert audio to tensor
78
+ # audio_tensor = torch.tensor(audio).float()
79
 
80
+ # @st.cache(allow_output_mutation=True, suppress_st_warning=True)
81
+ # def cache_inverted_audio(audio_tensor):
82
+ # return invert_audio(model, processor, audio_tensor, sr)
83
 
84
+ # # Use cached result
85
+ # inverted_audio_tensor = cache_inverted_audio(audio_tensor)
86
+ # inverted_audio_np = inverted_audio_tensor.numpy()
87
 
88
+ # # Play inverted audio
89
+ # with io.BytesIO() as out_io:
90
+ # sf.write(out_io, inverted_audio_np, sr, format="wav")
91
+ # st.audio(out_io.getvalue(), format="audio/wav")
92
 
93
+ # # Offer a download button for the inverted audio
94
+ # if st.button("Download Inverted Audio"):
95
+ # with io.BytesIO() as out_io:
96
+ # sf.write(out_io, inverted_audio_np, sr, format="wav")
97
+ # st.download_button("Download Inverted Audio", data=out_io.getvalue(), file_name="inverted_output.wav", mime="audio/wav")