Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from audiocraft.models import MusicGen
|
4 |
+
from audiocraft.data.audio import audio_write
|
5 |
+
import numpy as np
|
6 |
+
import tempfile
|
7 |
+
import os
|
8 |
+
|
9 |
+
# Load the MusicGen model
|
10 |
+
model = MusicGen.get_pretrained('small')
|
11 |
+
model.set_generation_params(duration=30) # Set maximum duration to 30 seconds
|
12 |
+
|
13 |
+
def enhance_audio(audio_file):
|
14 |
+
# Load and process the audio file
|
15 |
+
waveform = model.compression_model.encode(audio_file)
|
16 |
+
|
17 |
+
# Apply AI-based enhancement (this is a simplified example)
|
18 |
+
enhanced_waveform = model.compression_model.decode(waveform)
|
19 |
+
|
20 |
+
# Convert to numpy array and normalize
|
21 |
+
enhanced_audio = enhanced_waveform.squeeze().cpu().numpy()
|
22 |
+
enhanced_audio = enhanced_audio / np.max(np.abs(enhanced_audio))
|
23 |
+
|
24 |
+
# Save the enhanced audio to a temporary file
|
25 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file:
|
26 |
+
audio_write(temp_file.name, enhanced_audio, model.sample_rate, strategy="loudness", loudness_compressor=True)
|
27 |
+
output_path = temp_file.name
|
28 |
+
|
29 |
+
return output_path
|
30 |
+
|
31 |
+
# Create the Gradio interface
|
32 |
+
iface = gr.Interface(
|
33 |
+
fn=enhance_audio,
|
34 |
+
inputs=gr.Audio(type="filepath", label="Upload your audio file"),
|
35 |
+
outputs=gr.Audio(type="filepath", label="Enhanced Audio"),
|
36 |
+
title="AI Music Mastering and Enhancement",
|
37 |
+
description="Upload an audio file to apply AI-based mastering and enhancement.",
|
38 |
+
)
|
39 |
+
|
40 |
+
# Launch the app
|
41 |
+
iface.launch()
|