|
import gradio as gr |
|
import whisper |
|
import librosa |
|
|
|
|
|
whisper_model = whisper.load_model("base") |
|
|
|
def process_music(file): |
|
|
|
result = whisper_model.transcribe(file) |
|
lyrics = result["text"] |
|
|
|
|
|
y, sr = librosa.load(file) |
|
tempo, _ = librosa.beat.beat_track(y=y, sr=sr) |
|
music_style = f"Tempo: {tempo:.2f} BPM" |
|
|
|
return lyrics, music_style |
|
|
|
|
|
app = gr.Interface( |
|
fn=process_music, |
|
inputs=gr.Audio(type="filepath", label="Upload Music File"), |
|
outputs=[ |
|
gr.Textbox(label="Extracted Lyrics"), |
|
gr.Textbox(label="Music Style"), |
|
], |
|
title="Lyrics & Music Style Extractor", |
|
description="Upload a music file to extract lyrics and detect the music style." |
|
) |
|
|
|
if __name__ == "__main__": |
|
app.launch() |
|
|
|
if mean_mfcc > 50: |
|
print("High") |
|
elif mean_mfcc > 30: |
|
print("Medium") |
|
else: |
|
print("Low") |
|
|
|
|
|
|
|
def plot_waveform(y, sr): |
|
plt.figure(figsize=(10, 4)) |
|
librosa.display.waveshow(y, sr=sr) |
|
plt.title("Waveform") |
|
plt.xlabel("Time (s)") |
|
plt.ylabel("Amplitude") |
|
buf = BytesIO() |
|
plt.savefig(buf, format="png") |
|
buf.seek(0) |
|
plt.close() |
|
return buf |
|
|
|
|
|
def plot_spectrogram(y, sr): |
|
plt.figure(figsize=(10, 4)) |
|
D = librosa.amplitude_to_db(np.abs(librosa.stft(y)), ref=np.max) |
|
librosa.display.specshow(D, sr=sr, x_axis='time', y_axis='log', cmap='coolwarm') |
|
plt.colorbar(format="%+2.0f dB") |
|
plt.title("Spectrogram") |
|
plt.xlabel("Time (s)") |
|
plt.ylabel("Frequency (Hz)") |
|
buf = BytesIO() |
|
plt.savefig(buf, format="png") |
|
buf.seek(0) |
|
plt.close() |
|
return buf |
|
|
|
|
|
iface = gr.Interface( |
|
fn=analyze_audio, |
|
inputs=gr.Audio(type="filepath", label="Upload an MP3 File"), |
|
outputs=[ |
|
"text", |
|
"text", |
|
gr.Image(type="file", label="Waveform"), |
|
gr.outputs.Image(type="file", label="Spectrogram") |
|
], |
|
title="Music Analyzer", |
|
description="Upload a song to extract lyrics, classify the genre, and visualize the audio features.", |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
iface.launch() |
|
|