lyrics / app.py
KIMOSSINO's picture
Update app.py
6fc3809 verified
import gradio as gr
import whisper
import librosa
# تحميل نموذج Whisper
whisper_model = whisper.load_model("base")
def process_music(file):
# استخراج كلمات الأغاني باستخدام Whisper
result = whisper_model.transcribe(file)
lyrics = result["text"]
# استخراج نمط الموسيقى باستخدام Librosa
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
# إنشاء واجهة Gradio
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: # تأكد أن elif في مكانها الصحيح
print("Medium")
else: # استخدم 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
# إنشاء واجهة Gradio
iface = gr.Interface(
fn=analyze_audio,
inputs=gr.Audio(type="filepath", label="Upload an MP3 File"), # استخدام "filepath" بدلاً من "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()