Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,107 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
from pydub import AudioSegment
|
3 |
-
import base64
|
4 |
-
import tempfile
|
5 |
import numpy as np
|
6 |
-
import
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from flask import Flask, request, jsonify
|
3 |
from pydub import AudioSegment
|
|
|
|
|
4 |
import numpy as np
|
5 |
+
import tempfile
|
6 |
+
import os
|
7 |
+
import noisereduce as nr
|
8 |
+
import torch
|
9 |
+
from demucs import pretrained
|
10 |
+
from demucs.apply import apply_model
|
11 |
+
import torchaudio
|
12 |
+
import librosa
|
13 |
+
import soundfile as sf
|
14 |
+
import json
|
15 |
+
import warnings
|
16 |
+
|
17 |
+
warnings.filterwarnings("ignore")
|
18 |
+
|
19 |
+
app = Flask(__name__)
|
20 |
+
|
21 |
+
# Helper Functions
|
22 |
+
def audiosegment_to_array(audio):
|
23 |
+
return np.array(audio.get_array_of_samples()), audio.frame_rate
|
24 |
+
|
25 |
+
def array_to_audiosegment(samples, frame_rate, channels=1):
|
26 |
+
return AudioSegment(
|
27 |
+
samples.tobytes(),
|
28 |
+
frame_rate=int(frame_rate),
|
29 |
+
sample_width=samples.dtype.itemsize,
|
30 |
+
channels=channels
|
31 |
+
)
|
32 |
+
|
33 |
+
def save_audiosegment_to_temp(audio, suffix=".wav"):
|
34 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as f:
|
35 |
+
audio.export(f.name, format=suffix.lstrip('.'))
|
36 |
+
return f.name
|
37 |
+
|
38 |
+
def load_audiofile_to_numpy(path):
|
39 |
+
samples, sr = sf.read(path, dtype="int16")
|
40 |
+
if samples.ndim > 1 and samples.shape[1] > 2:
|
41 |
+
samples = samples[:, :2]
|
42 |
+
return samples, sr
|
43 |
+
|
44 |
+
# Effect Functions
|
45 |
+
def apply_normalize(audio):
|
46 |
+
return audio.normalize()
|
47 |
+
|
48 |
+
def apply_noise_reduction(audio):
|
49 |
+
samples, sr = audiosegment_to_array(audio)
|
50 |
+
reduced = nr.reduce_noise(y=samples, sr=sr)
|
51 |
+
return array_to_audiosegment(reduced, sr, channels=audio.channels)
|
52 |
+
|
53 |
+
# Add other effect functions here...
|
54 |
+
|
55 |
+
# API Endpoints
|
56 |
+
@app.route('/process_audio', methods=['POST'])
|
57 |
+
def process_audio_endpoint():
|
58 |
+
if 'audio' not in request.files:
|
59 |
+
return jsonify({'error': 'No audio file provided'}), 400
|
60 |
+
|
61 |
+
audio_file = request.files['audio']
|
62 |
+
audio_path = save_audiosegment_to_temp(AudioSegment.from_file(audio_file))
|
63 |
+
|
64 |
+
# Process the audio file here
|
65 |
+
audio = AudioSegment.from_file(audio_path)
|
66 |
+
audio = apply_normalize(audio)
|
67 |
+
audio = apply_noise_reduction(audio)
|
68 |
+
|
69 |
+
# Save the processed audio
|
70 |
+
processed_audio_path = save_audiosegment_to_temp(audio)
|
71 |
+
samples, sr = load_audiofile_to_numpy(processed_audio_path)
|
72 |
+
|
73 |
+
return jsonify({
|
74 |
+
'samples': samples.tolist(),
|
75 |
+
'sample_rate': sr,
|
76 |
+
'message': 'Audio processed successfully'
|
77 |
+
})
|
78 |
+
|
79 |
+
@app.route('/apply_effect', methods=['POST'])
|
80 |
+
def apply_effect_endpoint():
|
81 |
+
data = request.get_json()
|
82 |
+
effect = data.get('effect')
|
83 |
+
audio_file = data.get('audio_file')
|
84 |
+
|
85 |
+
if not audio_file:
|
86 |
+
return jsonify({'error': 'No audio file provided'}), 400
|
87 |
+
|
88 |
+
audio = AudioSegment.from_file(audio_file)
|
89 |
+
|
90 |
+
# Apply the selected effect
|
91 |
+
if effect == 'normalize':
|
92 |
+
audio = apply_normalize(audio)
|
93 |
+
elif effect == 'noise_reduction':
|
94 |
+
audio = apply_noise_reduction(audio)
|
95 |
+
# Add other effects here...
|
96 |
+
|
97 |
+
processed_audio_path = save_audiosegment_to_temp(audio)
|
98 |
+
samples, sr = load_audiofile_to_numpy(processed_audio_path)
|
99 |
+
|
100 |
+
return jsonify({
|
101 |
+
'samples': samples.tolist(),
|
102 |
+
'sample_rate': sr,
|
103 |
+
'message': f'{effect} applied successfully'
|
104 |
+
})
|
105 |
+
|
106 |
+
if __name__ == '__main__':
|
107 |
+
app.run(host='0.0.0.0', port=5000)
|