tigorsinaga commited on
Commit
e8e46ca
·
verified ·
1 Parent(s): bd45a40

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -0
app.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import librosa
3
+ import numpy as np
4
+ import tensorflow as tf
5
+ import pickle
6
+ from pydub import AudioSegment
7
+ import os
8
+
9
+
10
+ # === Load Model dan Label Encoder ===
11
+ model = tf.keras.models.load_model('/content/drive/MyDrive/AI CHORD RECOGNITION/Final/final_model.h5')
12
+ with open('/content/drive/MyDrive/AI CHORD RECOGNITION/Final/label_chord.pkl', 'rb') as f:
13
+ label_encoder = pickle.load(f)
14
+
15
+ # === Konversi MP3 ke WAV ===
16
+ def convert_mp3_to_wav(mp3_path):
17
+ sound = AudioSegment.from_mp3(mp3_path)
18
+ wav_path = mp3_path.replace('.mp3', '.wav')
19
+ sound.export(wav_path, format="wav")
20
+ return wav_path
21
+
22
+ # === Konversi Audio ke Mel Spectrogram ===
23
+ def audio_to_mel_spectrogram(y, sr):
24
+ y = librosa.util.normalize(y)
25
+ mel_spectrogram = librosa.feature.melspectrogram(y=y, sr=sr, n_fft=2048, hop_length=512, n_mels=128)
26
+ mel_spectrogram_db = librosa.power_to_db(mel_spectrogram, ref=np.max)
27
+ mel_spectrogram_db = (mel_spectrogram_db + 80) / 80 # Normalisasi ke 0-1
28
+ mel_spectrogram_db = tf.image.resize(mel_spectrogram_db[..., np.newaxis], (128, 128)).numpy()
29
+ mel_spectrogram_db = np.repeat(mel_spectrogram_db, 3, axis=-1)
30
+ return np.expand_dims(mel_spectrogram_db, axis=0)
31
+
32
+ # === Prediksi Chord ===
33
+ def predict_chords(audio_path):
34
+ if audio_path.endswith('.mp3'):
35
+ audio_path = convert_mp3_to_wav(audio_path)
36
+
37
+ y, sr = librosa.load(audio_path, sr=22050)
38
+ duration = librosa.get_duration(y=y, sr=sr)
39
+ chords = []
40
+ previous_chord = None
41
+
42
+ for i in range(0, int(duration)):
43
+ start_sample = i * sr
44
+ end_sample = (i + 1) * sr
45
+ y_segment = y[start_sample:end_sample]
46
+ if len(y_segment) == 0:
47
+ continue
48
+
49
+ mel_spectrogram = audio_to_mel_spectrogram(y_segment, sr)
50
+ prediction = model.predict(mel_spectrogram)
51
+ predicted_index = np.argmax(prediction)
52
+ predicted_chord = label_encoder.classes_[predicted_index]
53
+ predicted_chord = predicted_chord.replace('_', '')
54
+
55
+ if predicted_chord != previous_chord:
56
+ chords.append(predicted_chord)
57
+ previous_chord = predicted_chord
58
+
59
+ return f"Predicted Chords: {' - '.join(chords)}"
60
+
61
+ # === Gradio Interface ===
62
+ css = """
63
+ body {
64
+ font-family: sans-serif;
65
+ }
66
+ .gradio-container {
67
+ border-radius: 10px;
68
+ box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
69
+ padding: 20px;
70
+ }
71
+ .gr-button {
72
+ background-color: #4CAF50;
73
+ color: white;
74
+ border: none;
75
+ padding: 6px 12px;
76
+ font-size: 14px;
77
+ cursor: pointer;
78
+ border-radius: 5px;
79
+ }
80
+ .gr-button:hover {
81
+ background-color: #4a894c;
82
+ }
83
+ .description {
84
+ margin-top: 40px;
85
+ margin-bottom:-10px;
86
+ }
87
+ """
88
+
89
+ sample_audio_path = "/content/drive/MyDrive/AI CHORD RECOGNITION/Final/example.mp3"
90
+
91
+ title = f"AI Chord Recognition"
92
+ description = """
93
+ <div class='description'>
94
+ Upload an audio file (<strong>.mp3</strong> or <strong>.wav</strong>) and let the AI predict the chord progression.
95
+ You can also try my sample audio.
96
+ </div>
97
+ """
98
+
99
+ with gr.Blocks(css=css) as interface:
100
+ gr.Markdown(f"<h1 style='text-align: center;'>{title}</h1>")
101
+ gr.Markdown("<h3 style='text-align: center;'>Hello! I'm Tigor Neilson Sinaga, with NIM 22.11.4725.<br>Welcome to my AI Chord Recognition project!</h3>")
102
+ gr.Markdown(f"<h3 style='text-align: center;'>{description}</p>")
103
+
104
+ audio_input = gr.Audio(type="filepath", label="Upload Audio (MP3/WAV)")
105
+ use_sample_button = gr.Button("Use Sample Audio", size="sm")
106
+ predict_button = gr.Button("Predict Chords") # Changed line
107
+ output_text = gr.Textbox(label="Predicted Chords", lines=5, placeholder="Chord predictions will appear here...")
108
+
109
+ use_sample_button.click(fn=lambda: sample_audio_path, inputs=[], outputs=audio_input)
110
+ predict_button.click(fn=predict_chords, inputs=audio_input, outputs=output_text, queue=True)
111
+ gr.Markdown("<p style='text-align: center; font-size: 12px; color: grey;'>This project is still under development and has not yet reached high accuracy.</p>")
112
+
113
+ interface.launch(share=True)