bram4627 commited on
Commit
18479c9
1 Parent(s): d11ffb4

Upload 6 files

Browse files
.gitattributes CHANGED
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ model_code1.keras filter=lfs diff=lfs merge=lfs -text
37
+ static/uploads/That's[[:space:]]the[[:space:]]One[[:space:]]-[[:space:]]Ryan[[:space:]]McCaffreyGo[[:space:]]By[[:space:]]Ocean.mp3 filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request, redirect, url_for
2
+ import os
3
+ import librosa
4
+ import numpy as np
5
+ import tensorflow as tf
6
+ from sklearn.preprocessing import StandardScaler
7
+ import pickle
8
+ import subprocess # Untuk menjalankan perintah FFmpeg
9
+ import threading # Untuk menjalankan penghapusan otomatis file setelah delay
10
+
11
+ app = Flask(__name__)
12
+
13
+ # Path folder untuk menyimpan file yang diunggah
14
+ UPLOAD_FOLDER = 'static/uploads'
15
+ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
16
+
17
+ # Load model dan scaler
18
+ model = tf.keras.models.load_model('model_code1.keras')
19
+ with open('scaler.pkl', 'rb') as f:
20
+ scaler = pickle.load(f)
21
+
22
+ # Label genre musik
23
+ genres = ['blues', 'classical', 'country', 'disco', 'hiphop', 'jazz', 'metal', 'pop', 'reggae', 'rock']
24
+
25
+ # Fungsi untuk menghapus file setelah delay
26
+ def delete_file_after_delay(file_path, delay=3600):
27
+ def delete_file():
28
+ try:
29
+ if os.path.exists(file_path):
30
+ os.remove(file_path)
31
+ print(f"File {file_path} berhasil dihapus setelah {delay} detik.")
32
+ except Exception as e:
33
+ print(f"Gagal menghapus file {file_path}: {e}")
34
+
35
+ # Jalankan penghapusan file dalam thread baru
36
+ threading.Timer(delay, delete_file).start()
37
+
38
+ # Fungsi untuk mengonversi MP3 ke WAV menggunakan FFmpeg
39
+ def convert_mp3_to_wav(mp3_path):
40
+ wav_path = mp3_path.replace('.mp3', '.wav') # Ubah ekstensi ke .wav
41
+ try:
42
+ # Jalankan perintah FFmpeg untuk konversi
43
+ subprocess.run(['ffmpeg', '-i', mp3_path, wav_path], check=True)
44
+ # Hapus file MP3 setelah berhasil dikonversi
45
+ os.remove(mp3_path)
46
+ return wav_path
47
+ except subprocess.CalledProcessError as e:
48
+ print(f"Error converting MP3 to WAV: {e}")
49
+ return None
50
+ except OSError as e:
51
+ print(f"Error deleting MP3 file: {e}")
52
+ return None
53
+
54
+ # Fungsi untuk ekstraksi fitur dari file musik
55
+ def extract_features(file_path):
56
+ try:
57
+ y, sr = librosa.load(file_path, duration=30, sr=22050)
58
+
59
+ # Ekstraksi fitur
60
+ mfccs = np.mean(librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13).T, axis=0)
61
+ chroma = np.mean(librosa.feature.chroma_stft(y=y, sr=sr).T, axis=0)
62
+ spectral_contrast = np.mean(librosa.feature.spectral_contrast(y=y, sr=sr).T, axis=0)
63
+ zero_crossings = np.mean(librosa.feature.zero_crossing_rate(y).T, axis=0)
64
+ tempo, _ = librosa.beat.beat_track(y=y, sr=sr)
65
+
66
+ # Menggabungkan semua fitur
67
+ features = np.hstack([mfccs, chroma, spectral_contrast, zero_crossings, tempo])
68
+ return features
69
+ except Exception as e:
70
+ print(f"Error extracting features: {e}")
71
+ return None
72
+
73
+
74
+ @app.route('/', methods=['GET', 'POST'])
75
+ def index():
76
+ if request.method == 'POST':
77
+ # Periksa apakah file diunggah
78
+ if 'file' not in request.files:
79
+ return redirect(request.url)
80
+ file = request.files['file']
81
+ if file.filename == '':
82
+ return redirect(request.url)
83
+
84
+ # Simpan file ke folder yang ditentukan
85
+ file_path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
86
+ file.save(file_path)
87
+
88
+ # Periksa format file
89
+ if file.filename.lower().endswith('.mp3'):
90
+ # Konversi MP3 ke WAV
91
+ file_path_wav = convert_mp3_to_wav(file_path)
92
+ if file_path_wav is None:
93
+ return "Konversi MP3 ke WAV gagal. Pastikan file yang diunggah valid."
94
+ file_path = file_path_wav # Gunakan file WAV untuk proses berikutnya
95
+
96
+ # Ekstraksi fitur dari file yang diunggah
97
+ features = extract_features(file_path)
98
+ if features is None:
99
+ return "Ekstraksi fitur gagal. Coba unggah file lain."
100
+
101
+ # Normalisasi fitur menggunakan scaler
102
+ features_scaled = scaler.transform([features])
103
+
104
+ # Prediksi genre menggunakan model
105
+ prediction = model.predict(features_scaled)
106
+ predicted_genre = genres[np.argmax(prediction)]
107
+
108
+ # Hapus file WAV setelah 30 detik
109
+ delete_file_after_delay(file_path, delay=30)
110
+
111
+ # Kembalikan hasil prediksi
112
+ return render_template('index.html', file_path=file_path, prediction=predicted_genre)
113
+
114
+ return render_template('index.html')
115
+
116
+
117
+ if __name__ == '__main__':
118
+ app.run(debug=True)
model_code1.keras ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f48886d66143f95bb2316e1df8b9d94367f7d5f14761ed8f55cccb883ad90af0
3
+ size 8825044
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ Flask==3.1.0
2
+ librosa==0.10.2.post1
3
+ numpy==2.2.0
4
+ scikit_learn==1.6.0
5
+ tensorflow==2.18.0
6
+ tensorflow_intel==2.18.0
scaler.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3ff5d059ce0b0a9600bf26b2d5b673e47a18a2101fb01d18cf0ec150181c4ac0
3
+ size 1274
static/uploads/That's the One - Ryan McCaffreyGo By Ocean.mp3 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a44051ade58e941318b3ba736b5a82e6db2c00d7ab7bb88c9c7de7326e34a4ff
3
+ size 8322649
templates/index.html ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Music Genre Classification</title>
7
+ <!-- Bootstrap CSS -->
8
+ <link
9
+ href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css"
10
+ rel="stylesheet"
11
+ >
12
+ </head>
13
+ <body class="bg-light">
14
+
15
+ <div class="container mt-5">
16
+ <!-- Header -->
17
+ <div class="text-center mb-4">
18
+ <h1 class="display-5 fw-bold">Music Genre Classification</h1>
19
+ <p class="lead text-secondary">Upload your music file (wav/mp3) and discover its genre!</p>
20
+ </div>
21
+
22
+ <!-- Upload Form -->
23
+ <div class="card shadow p-4">
24
+ <form action="/" method="post" enctype="multipart/form-data">
25
+ <div class="mb-3">
26
+ <label for="file" class="form-label fw-bold">Upload a music file</label>
27
+ <input type="file" id="file" name="file" class="form-control" accept=".wav,.mp3" onchange="previewAudio(event)">
28
+ </div>
29
+
30
+ <!-- Audio Player -->
31
+ <div class="mb-3" id="audioPlayerContainer" style="display: none;">
32
+ <label class="form-label fw-bold">Preview:</label>
33
+ <audio id="audioPlayer" controls class="w-100">
34
+ <source id="audioSource" type="audio/wav">
35
+ Your browser does not support the audio element.
36
+ </audio>
37
+ </div>
38
+
39
+ <!-- Submit Button -->
40
+ <div class="d-grid">
41
+ <button type="submit" class="btn btn-primary btn-lg">Submit</button>
42
+ </div>
43
+ </form>
44
+ </div>
45
+
46
+ <!-- Prediction Result -->
47
+ {% if file_path %}
48
+ <div class="card shadow mt-4 p-4">
49
+ <h4 class="fw-bold">Uploaded File:</h4>
50
+ <audio controls class="w-100 mb-3">
51
+ <source src="{{ file_path }}" type="audio/wav">
52
+ Your browser does not support the audio element.
53
+ </audio>
54
+ <h4 class="fw-bold">Predicted Genre:</h4>
55
+ <p class="fs-4 text-success"><strong>{{ prediction }}</strong></p>
56
+ </div>
57
+ {% endif %}
58
+ </div>
59
+
60
+ <!-- Bootstrap JS Bundle with Popper -->
61
+ <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
62
+ <script>
63
+ function previewAudio(event) {
64
+ const file = event.target.files[0];
65
+ if (file) {
66
+ const audioPlayer = document.getElementById('audioPlayer');
67
+ const audioSource = document.getElementById('audioSource');
68
+ const audioPlayerContainer = document.getElementById('audioPlayerContainer');
69
+
70
+ audioSource.src = URL.createObjectURL(file);
71
+ audioPlayerContainer.style.display = 'block';
72
+ audioPlayer.load();
73
+ }
74
+ }
75
+ </script>
76
+ </body>
77
+ </html>