Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,85 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from pydub import AudioSegment
|
|
|
|
|
|
|
| 3 |
import tempfile
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
interface = gr.Interface(
|
| 22 |
-
fn=
|
| 23 |
-
inputs=
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
)
|
| 28 |
|
| 29 |
-
print("π Launching Gradio app...")
|
| 30 |
interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from pydub import AudioSegment
|
| 3 |
+
import noisereduce as nr
|
| 4 |
+
import numpy as np
|
| 5 |
import tempfile
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
+
# Helper: Convert AudioSegment to numpy array and back
|
| 9 |
+
def audiosegment_to_array(audio_segment):
|
| 10 |
+
return np.array(audio_segment.get_array_of_samples()), audio_segment.frame_rate
|
| 11 |
+
|
| 12 |
+
# Apply effect functions
|
| 13 |
+
def apply_normalize(audio):
|
| 14 |
+
return audio.normalize()
|
| 15 |
+
|
| 16 |
+
def apply_noise_reduction(audio):
|
| 17 |
+
samples, frame_rate = audiosegment_to_array(audio)
|
| 18 |
+
reduced_noise = nr.reduce_noise(y=samples, sr=frame_rate)
|
| 19 |
+
return AudioSegment(
|
| 20 |
+
reduced_noise.tobytes(),
|
| 21 |
+
frame_rate=frame_rate,
|
| 22 |
+
sample_width=reduced_noise.dtype.itemsize,
|
| 23 |
+
channels=audio.channels
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
def apply_compression(audio):
|
| 27 |
+
return audio.compress_dynamic_range()
|
| 28 |
+
|
| 29 |
+
def apply_reverb(audio):
|
| 30 |
+
# Simulate reverb by overlaying a delayed, quieter copy
|
| 31 |
+
reverb_audio = audio - 10 # Lower volume
|
| 32 |
+
return audio.overlay(reverb_audio, position=1000)
|
| 33 |
+
|
| 34 |
+
def apply_pitch_shift(audio, semitones=-2):
|
| 35 |
+
return audio._spawn(audio._array_type(
|
| 36 |
+
np.interp(
|
| 37 |
+
np.arange(0, len(audio), 2 ** (semitones / 12)),
|
| 38 |
+
np.arange(len(audio)),
|
| 39 |
+
audio.get_array_of_samples()
|
| 40 |
+
).astype(np.int16).tolist()
|
| 41 |
+
), overrides={"frame_rate": int(audio.frame_rate * (2 ** (semitones / 12)))})
|
| 42 |
+
|
| 43 |
+
# Main processing function
|
| 44 |
+
def process_audio(audio_file, effect):
|
| 45 |
+
audio = AudioSegment.from_file(audio_file)
|
| 46 |
+
|
| 47 |
+
if effect == "Normalize":
|
| 48 |
+
result = apply_normalize(audio)
|
| 49 |
+
elif effect == "Noise Reduction":
|
| 50 |
+
result = apply_noise_reduction(audio)
|
| 51 |
+
elif effect == "Compress Dynamic Range":
|
| 52 |
+
result = apply_compression(audio)
|
| 53 |
+
elif effect == "Add Reverb":
|
| 54 |
+
result = apply_reverb(audio)
|
| 55 |
+
elif effect == "Pitch Shift":
|
| 56 |
+
result = apply_pitch_shift(audio, semitones=-2) # Down 2 semitones
|
| 57 |
+
else:
|
| 58 |
+
result = audio # No effect
|
| 59 |
+
|
| 60 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
|
| 61 |
+
result.export(f.name, format="wav")
|
| 62 |
+
return f.name
|
| 63 |
+
|
| 64 |
+
# Gradio Interface
|
| 65 |
interface = gr.Interface(
|
| 66 |
+
fn=process_audio,
|
| 67 |
+
inputs=[
|
| 68 |
+
gr.Audio(label="Upload Audio", type="filepath"),
|
| 69 |
+
gr.Dropdown(
|
| 70 |
+
choices=[
|
| 71 |
+
"Normalize",
|
| 72 |
+
"Noise Reduction",
|
| 73 |
+
"Compress Dynamic Range",
|
| 74 |
+
"Add Reverb",
|
| 75 |
+
"Pitch Shift"
|
| 76 |
+
],
|
| 77 |
+
label="Select Effect"
|
| 78 |
+
)
|
| 79 |
+
],
|
| 80 |
+
outputs=gr.Audio(label="Processed Audio", type="filepath"),
|
| 81 |
+
title="Fix My Recording - Advanced Edition",
|
| 82 |
+
description="Upload your audio to clean, master, and add creative effects with AI!"
|
| 83 |
)
|
| 84 |
|
|
|
|
| 85 |
interface.launch()
|