File size: 12,846 Bytes
80cb407
 
 
6b6d018
80cb407
 
 
0c67b3e
bc3627b
 
 
 
 
0c67b3e
 
 
 
 
 
 
 
 
bc3627b
0c67b3e
 
 
 
 
 
 
 
 
 
bc3627b
0c67b3e
bc3627b
6b6d018
 
0c67b3e
6b6d018
 
 
 
0c67b3e
6b6d018
0c67b3e
 
 
 
 
 
 
 
 
6b6d018
 
0c67b3e
 
 
 
 
 
 
 
 
6b6d018
80cb407
0c67b3e
6b6d018
80cb407
6b6d018
80cb407
6b6d018
0c67b3e
6b6d018
 
 
 
 
 
 
 
 
0c67b3e
6b6d018
 
 
0c67b3e
6b6d018
 
 
 
 
 
0c67b3e
6b6d018
 
 
 
 
0c67b3e
6b6d018
 
0c67b3e
6b6d018
 
 
80cb407
 
6b6d018
 
0c67b3e
6b6d018
80cb407
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b243a3c
 
 
80cb407
b243a3c
 
 
80cb407
 
b243a3c
 
 
80cb407
 
 
 
6b6d018
 
 
 
499cbc6
 
 
6b6d018
 
0c67b3e
6b6d018
 
 
 
 
 
 
 
 
 
80cb407
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b243a3c
 
 
 
80cb407
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b243a3c
 
 
 
80cb407
 
 
 
 
 
 
 
 
 
 
6b6d018
 
 
 
 
 
80cb407
 
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
{% extends 'base.html' %}

{% block head %}
<title>Indonesian Lyrics Classification By Age Group - Index</title>
{% endblock %}

{% block body %}
{% include 'navbar.html' %}
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% for category, message in messages %}
<div id="toast-success"
    class="fixed top-2 left-1/2 transform -translate-x-1/2 z-50 flex items-center w-full max-w-xs p-4 mb-4 text-gray-500 bg-white rounded-lg shadow-sm"
    role="alert">
    <div class="inline-flex items-center justify-center shrink-0 w-8 h-8 text-green-500 bg-green-100 rounded-lg">
        <svg class="w-5 h-5" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor"
            viewBox="0 0 20 20">
            <path
                d="M10 .5a9.5 9.5 0 1 0 9.5 9.5A9.51 9.51 0 0 0 10 .5Zm3.707 8.207-4 4a1 1 0 0 1-1.414 0l-2-2a1 1 0 0 1 1.414-1.414L9 10.586l3.293-3.293a1 1 0 0 1 1.414 1.414Z" />
        </svg>
        <span class="sr-only">Check icon</span>
    </div>
    <div class="ms-3 text-sm font-normal">You are logged in as <b>{{ current_user.email }}</b>!</div>
    <button type="button"
        class="ms-auto -mx-1.5 -my-1.5 bg-white text-gray-400 hover:text-gray-900 rounded-lg focus:ring-2 focus:ring-gray-300 p-1.5 hover:bg-gray-100 inline-flex items-center justify-center h-8 w-8"
        data-dismiss-target="#toast-success" aria-label="Close">
        <span class="sr-only">Close</span>
        <svg class="w-3 h-3" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 14 14">
            <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                d="m1 1 6 6m0 0 6 6M7 7l6-6M7 7l-6 6" />
        </svg>
    </button>
</div>
{% endfor %}
{% endif %}
{% endwith %}
<main class="flex flex-row justify-center items-center min-h-screen">
    <div class="container flex flex-col justify-center items-center">
        <h1 class="mb-4 text-5xl font-extrabold tracking-tight text-slate-900 sm:text-7xl text-center">Indonesian Lyrics
            Classification By Age Group</h1>
        <h2 class="mt-4 mb-2 px-4 text-center text-1xl font-semibold tracking-tight text-slate-900 sm:text-2xl">
            Upload or Record Your Lyrics:</h2>
        <div
            class="mb-2 flex flex-col justify-center items-center rounded-lg bg-white shadow-xl shadow-black/5 ring-1 ring-slate-700/10">
            <div class="flex flex-row space-x-2 py-2 w-full px-2">
                <button id="uploadButton" type="button"
                    class="inline-flex items-center text-1xl sm:text-lg justify-center rounded-lg p-2 bg-blue text-slate-500 hover:text-indigo-600 hover:bg-indigo-50 transition-all duration-200">
                    <svg class="w-5 h-5 mr-2" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"
                        stroke-width="1.5" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round"
                            d="M3.75 9.776c.112-.017.227-.026.344-.026h15.812c.117 0 .232.009.344.026m-16.5 0a2.25 2.25 0 00-1.883 2.542l.857 6a2.25 2.25 0 002.227 1.932H19.05a2.25 2.25 0 002.227-1.932l.857-6a2.25 2.25 0 00-1.883-2.542m-16.5 0V6A2.25 2.25 0 016 3.75h3.879a1.5 1.5 0 011.06.44l2.122 2.12a1.5 1.5 0 001.06.44H18A2.25 2.25 0 0120.25 9v.776">
                        </path>
                    </svg>
                    From File
                </button>
                <div class="w-[1px] bg-slate-200"></div>
                <button id="recordButton" type="button"
                    class="inline-flex items-center text-1xl sm:text-lg justify-center rounded-lg p-2 bg-blue text-slate-500 hover:text-indigo-600 hover:bg-indigo-50 transition-all duration-200">
                    <svg class="w-5 h-5 mr-2" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"
                        stroke-width="1.5" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round"
                            d="M12 18.75a6 6 0 006-6v-1.5m-6 7.5a6 6 0 01-6-6v-1.5m6 7.5v3.75m-3.75 0h7.5M12 15.75a3 3 0 01-3-3V4.5a3 3 0 116 0v8.25a3 3 0 01-3 3z">
                        </path>
                    </svg>
                    <span id="recordText">Record</span>
                </button>
            </div>
            <div class="w-full bg-gray-700 rounded-full h-1">
                <div class="bg-blue-600 h-1 rounded-full transition-all duration-100" style="width: 0%;"></div>
            </div>
        </div>

        <!-- Hidden playback and transcribe sections -->
        <div id="playback" class="mb-2 flex relative z-10 p-4 w-full hidden">
            <audio id="audioPlayer" controls
                class="w-full h-14 rounded-lg bg-white shadow-xl shadow-black/5 ring-1 ring-slate-700/10">
                <source id="audioSource" type="audio/mpeg" src="">
            </audio>
        </div>
        <div id="transcribe" class="relative w-full flex justify-center items-center hidden">
            <form action="/transcribe" method="POST" enctype="multipart/form-data">
                <input type="file" name="file" id="fileInput" accept=".mp3" class="hidden">
                <button type="button" onclick="showInputText()"
                    class="text-white bg-red-500 hover:bg-red-600 font-medium rounded-lg text-1xl sm:text-lg px-5 py-2.5 text-center">
                    Cancel
                </button>
                <button type="submit"
                    class="text-white bg-blue-700 hover:bg-blue-800 font-medium rounded-lg text-1xl sm:text-lg px-5 py-2.5 text-center">
                    Predict Age Group
                </button>
            </form>
        </div>

        <!-- Text Input for Manual Lyrics Prediction -->
        <div class="mt-2 w-full" id="input-text">
            <form action="/predict-text" method="POST" class="flex flex-col space-y-4">
                <label for="lyricsInput"
                    class="mb-2 text-1xl font-semibold tracking-tight text-slate-900 sm:text-2xl">Or Input Your Lyrics
                    Here:</label>
                <textarea name="lyrics" id="lyricsInput" rows="6" required
                    class="w-full rounded-lg border border-gray-300 px-2 py-2 text-1xl sm:text-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
                    placeholder="Type or paste Indonesian lyrics..."></textarea>
                <button type="submit"
                    class="text-white bg-blue-700 hover:bg-blue-800 font-medium rounded-lg text-1xl sm:text-lg px-5 py-2.5 text-center">
                    Predict Age Group
                </button>
            </form>
        </div>
    </div>
</main>
<footer class="flex flex-row justify-center items-center">
    <div class="mb-3 text-gray-500">By Michael Natanael</div>
</footer>
{% endblock %}

{% block script %}
<script>
    let mediaRecorder;
    let audioChunks = [];
    let recordingInterval;
    let seconds = 0;

    // Handle file upload
    document.getElementById("uploadButton").addEventListener("click", function () {
        document.getElementById("fileInput").click();
    });

    document.getElementById("fileInput").addEventListener("change", function (event) {
        if (event.target.files.length > 0) {
            let file = event.target.files[0];
            let fileURL = URL.createObjectURL(file);
            updateAudioPlayer(fileURL);
        }
    });

    // Handle audio recording
    document.getElementById("recordButton").addEventListener("click", async function () {
        const submitButtons = document.querySelectorAll('form button[type="submit"]');
        const uploadButton = document.getElementById('uploadButton');
        const textArea = document.getElementById('lyricsInput');
        if (!mediaRecorder || mediaRecorder.state === "inactive") {
            submitButtons.forEach(btn => btn.disabled = true);
            uploadButton.disabled = true;
            textArea.disabled = true;
            startRecording();
        } else {
            submitButtons.forEach(btn => btn.disabled = false);
            uploadButton.disabled = false;
            textArea.disabled = false;
            stopRecording();
        }
    });

    document.querySelectorAll('form').forEach(form => {
        form.addEventListener('submit', function (e) {
            const cancelBtn = this.querySelector('[type="button"]');
            const submitBtn = this.querySelector('[type="submit"]');
            if (cancelBtn) {
                cancelBtn.disabled = true;
            }
            submitBtn.disabled = true;
            submitBtn.innerHTML = `
                <svg aria-hidden="true" role="status" class="inline w-5 h-5 me-1 text-white animate-spin" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
                        fill="#E5E7EB" />
                    <path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
                        fill="currentColor" />
                </svg>
                Loading...
            `;
        });
    });

    async function startRecording() {
        const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
        mediaRecorder = new MediaRecorder(stream);

        mediaRecorder.ondataavailable = event => {
            audioChunks.push(event.data);
        };

        mediaRecorder.onstop = () => {
            clearInterval(recordingInterval); // Stop timer
            seconds = 0; // Reset timer

            const audioBlob = new Blob(audioChunks, { type: 'audio/mpeg' });
            const audioUrl = URL.createObjectURL(audioBlob);

            // Update the file input with recorded audio
            const file = new File([audioBlob], "recording.mp3", { type: "audio/mpeg" });
            const fileInput = document.getElementById("fileInput");
            const dataTransfer = new DataTransfer();
            dataTransfer.items.add(file);
            fileInput.files = dataTransfer.files;

            updateAudioPlayer(audioUrl);
            audioChunks = []; // Reset chunks
        };

        mediaRecorder.start();
        startTimer();

        // Highlight recording
        const recordBtn = document.getElementById("recordButton");
        recordBtn.classList.add("recording-active");
        document.getElementById("recordText").innerHTML = "Stop Recording (00:00)";
    }

    function startTimer() {
        recordingInterval = setInterval(() => {
            seconds++;
            let minutes = Math.floor(seconds / 60);
            let remainingSeconds = seconds % 60;
            let timeString = `${String(minutes).padStart(2, '0')}:${String(remainingSeconds).padStart(2, '0')}`;

            document.getElementById("recordText").innerHTML = `Stop Recording (${timeString})`;
        }, 1000);
    }

    function stopRecording() {
        mediaRecorder.stop();

        // Remove highlight
        const recordBtn = document.getElementById("recordButton");
        recordBtn.classList.remove("recording-active");
        document.getElementById("recordText").innerHTML = "Record";
    }

    // Function to update audio player and show playback & transcribe sections
    function updateAudioPlayer(audioUrl) {
        document.getElementById("audioSource").src = audioUrl;
        document.getElementById("audioPlayer").load(); // Refresh the audio player
        document.getElementById("playback").classList.remove("hidden");
        document.getElementById("transcribe").classList.remove("hidden");
        document.getElementById("input-text").classList.add("hidden");
    }

    function showInputText() {
        document.getElementById("playback").classList.add("hidden");
        document.getElementById("transcribe").classList.add("hidden");
        document.getElementById("input-text").classList.remove("hidden");
    }
</script>
{% endblock %}