Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
-
import
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
#
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
if uploaded_file is not None:
|
| 26 |
-
st.session_state.uploaded_file = uploaded_file.getvalue() # store content, not the file object
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
| 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")
|