Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,45 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from model import load_model, invert_audio
|
| 3 |
-
import os
|
| 4 |
|
|
|
|
| 5 |
preloaded = {}
|
| 6 |
preloaded["model"], preloaded["processor"] = load_model()
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
for input_audio_path in data_dir.glob('*.wav'):
|
| 10 |
-
print(os.path.basename(input_audio_path))
|
| 11 |
-
output_audio_path = os.path.join(output_dir, "inverted-" + os.path.basename(input_audio_path))
|
| 12 |
-
output_file = invert_audio(
|
| 13 |
-
preloaded["model"], preloaded["processor"],
|
| 14 |
-
input_audio_path, output_audio_path)
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
# HuggingFace UI
|
| 18 |
-
import streamlit as st
|
| 19 |
-
import torch
|
| 20 |
-
import julius
|
| 21 |
|
| 22 |
st.title("Audio Inversion with HuggingFace & Streamlit")
|
| 23 |
|
| 24 |
uploaded_file = st.file_uploader("Upload an audio file", type=["wav", "flac"])
|
| 25 |
|
| 26 |
if uploaded_file:
|
|
|
|
| 27 |
st.audio(uploaded_file, format="audio/wav")
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
if st.button("Download Inverted Audio"):
|
| 34 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
import julius
|
| 4 |
+
import soundfile as sf
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
from model import load_model, invert_audio
|
|
|
|
| 8 |
|
| 9 |
+
# Load the model and processor
|
| 10 |
preloaded = {}
|
| 11 |
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 |
uploaded_file = st.file_uploader("Upload an audio file", type=["wav", "flac"])
|
| 18 |
|
| 19 |
if uploaded_file:
|
| 20 |
+
# Play the uploaded audio
|
| 21 |
st.audio(uploaded_file, format="audio/wav")
|
| 22 |
+
|
| 23 |
+
# Read the audio file
|
| 24 |
+
audio, sr = sf.read(io.BytesIO(uploaded_file.getvalue()))
|
| 25 |
+
|
| 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")
|