Upload 2 files
Browse files- app.py +93 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
import os
|
4 |
+
import librosa
|
5 |
+
import librosa.display
|
6 |
+
import matplotlib.pyplot as plt
|
7 |
+
from audiosr import build_model, super_resolution, save_wave
|
8 |
+
import tempfile
|
9 |
+
import numpy as np
|
10 |
+
|
11 |
+
# Set MPS device if available (for Mac M-Series GPUs)
|
12 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
+
|
14 |
+
# Title and Description
|
15 |
+
st.title("AudioSR: Versatile Audio Super-Resolution")
|
16 |
+
st.write("""
|
17 |
+
Upload your low-resolution audio files, and AudioSR will enhance them to high fidelity!
|
18 |
+
Supports all types of audio (music, speech, sound effects, etc.) with arbitrary sampling rates.
|
19 |
+
""")
|
20 |
+
|
21 |
+
# Upload audio file
|
22 |
+
uploaded_file = st.file_uploader("Upload an audio file (WAV format)", type=["wav"])
|
23 |
+
|
24 |
+
# Model Parameters
|
25 |
+
st.sidebar.title("Model Parameters")
|
26 |
+
model_name = st.sidebar.selectbox("Select Model", ["basic", "speech"], index=0)
|
27 |
+
ddim_steps = st.sidebar.slider("DDIM Steps", min_value=10, max_value=100, value=50)
|
28 |
+
guidance_scale = st.sidebar.slider("Guidance Scale", min_value=1.0, max_value=10.0, value=3.5)
|
29 |
+
random_seed = st.sidebar.number_input("Random Seed", min_value=0, value=42, step=1)
|
30 |
+
latent_t_per_second = 12.8
|
31 |
+
|
32 |
+
# Helper function to plot spectrogram
|
33 |
+
def plot_spectrogram(audio_path, title):
|
34 |
+
y, sr = librosa.load(audio_path, sr=None)
|
35 |
+
S = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128, fmax=sr // 2)
|
36 |
+
S_dB = librosa.power_to_db(S, ref=np.max)
|
37 |
+
|
38 |
+
plt.figure(figsize=(10, 4))
|
39 |
+
librosa.display.specshow(S_dB, sr=sr, x_axis='time', y_axis='mel', fmax=sr // 2, cmap='viridis')
|
40 |
+
plt.colorbar(format='%+2.0f dB')
|
41 |
+
plt.title(title)
|
42 |
+
plt.tight_layout()
|
43 |
+
return plt
|
44 |
+
|
45 |
+
# Process Button
|
46 |
+
if uploaded_file and st.button("Enhance Audio"):
|
47 |
+
st.write("Processing audio...")
|
48 |
+
|
49 |
+
# Create temp directory for saving files
|
50 |
+
with tempfile.TemporaryDirectory() as tmp_dir:
|
51 |
+
input_path = os.path.join(tmp_dir, "input.wav")
|
52 |
+
output_path = os.path.join(tmp_dir, "output.wav")
|
53 |
+
|
54 |
+
# Save uploaded file locally
|
55 |
+
with open(input_path, "wb") as f:
|
56 |
+
f.write(uploaded_file.read())
|
57 |
+
|
58 |
+
# Plot input spectrogram
|
59 |
+
st.write("Input Audio Spectrogram:")
|
60 |
+
input_spectrogram = plot_spectrogram(input_path, title="Input Audio Spectrogram")
|
61 |
+
st.pyplot(input_spectrogram)
|
62 |
+
|
63 |
+
# Build and load the model
|
64 |
+
audiosr = build_model(model_name=model_name, device=device)
|
65 |
+
|
66 |
+
# Perform super-resolution
|
67 |
+
waveform = super_resolution(
|
68 |
+
audiosr,
|
69 |
+
input_path,
|
70 |
+
seed=random_seed,
|
71 |
+
guidance_scale=guidance_scale,
|
72 |
+
ddim_steps=ddim_steps,
|
73 |
+
latent_t_per_second=latent_t_per_second,
|
74 |
+
)
|
75 |
+
|
76 |
+
# Save enhanced audio
|
77 |
+
save_wave(waveform, inputpath=input_path, savepath=tmp_dir, name="output", samplerate=48000)
|
78 |
+
|
79 |
+
# Plot output spectrogram
|
80 |
+
st.write("Enhanced Audio Spectrogram:")
|
81 |
+
output_spectrogram = plot_spectrogram(output_path, title="Enhanced Audio Spectrogram")
|
82 |
+
st.pyplot(output_spectrogram)
|
83 |
+
|
84 |
+
# Display audio players and download link
|
85 |
+
st.audio(input_path, format="audio/wav")
|
86 |
+
st.write("Original Audio:")
|
87 |
+
|
88 |
+
st.audio(output_path, format="audio/wav")
|
89 |
+
st.write("Enhanced Audio:")
|
90 |
+
st.download_button("Download Enhanced Audio", data=open(output_path, "rb").read(), file_name="enhanced_audio.wav")
|
91 |
+
|
92 |
+
# Footer
|
93 |
+
st.write("Built with [Streamlit](https://streamlit.io) and [AudioSR](https://audioldm.github.io/audiosr)")
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
matplotlib
|
3 |
+
git+https://github.com/haoheliu/versatile_audio_super_resolution.git
|
4 |
+
streamlit
|