File size: 2,693 Bytes
af44387
 
b415c64
af44387
b415c64
 
af44387
b415c64
 
 
 
 
 
 
af44387
b415c64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fc3809
 
 
 
 
 
 
 
af44387
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
395e016
af44387
 
 
82a37fd
af44387
 
 
 
 
 
 
 
 
1
2
3
4
5
6
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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()