Zen-4011 commited on
Commit
0add3fa
·
verified ·
1 Parent(s): 4a322b7

Create static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +113 -0
static/script.js ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ document.addEventListener('DOMContentLoaded', function() {
2
+
3
+ const containers = document.querySelectorAll('.number-input-container');
4
+
5
+ containers.forEach(container => {
6
+ const input = container.querySelector('.number-input');
7
+ const decreaseBtn = container.querySelector('.decrease-btn');
8
+ const increaseBtn = container.querySelector('.increase-btn');
9
+
10
+ function validateAndClamp(value) {
11
+ const min = parseFloat(input.getAttribute('min')) || -Infinity;
12
+ const max = parseFloat(input.getAttribute('max')) || Infinity;
13
+
14
+ if (value > max) return max;
15
+ if (value < min) return min;
16
+ return value;
17
+ }
18
+
19
+ function updateValue(change) {
20
+ let current = parseFloat(input.value) || 0;
21
+ let step = parseFloat(input.getAttribute('step')) || 1;
22
+
23
+ let newValue = current + (change * step);
24
+ newValue = Math.round(newValue * 10000) / 10000;
25
+
26
+ const clamped = validateAndClamp(newValue);
27
+ input.value = clamped;
28
+ input.dispatchEvent(new Event('input'));
29
+ }
30
+
31
+ decreaseBtn.addEventListener('click', () => updateValue(-1));
32
+ increaseBtn.addEventListener('click', () => updateValue(1));
33
+
34
+ input.addEventListener('blur', function() {
35
+ let current = parseFloat(this.value) || 0;
36
+ const clamped = validateAndClamp(current);
37
+
38
+ if (current !== clamped) {
39
+ this.value = clamped;
40
+ this.style.color = '#f87171';
41
+ setTimeout(() => this.style.color = '#ffffff', 300);
42
+ }
43
+ });
44
+
45
+ input.addEventListener('wheel', function(e) {
46
+ if (document.activeElement === input) {
47
+ e.preventDefault();
48
+ updateValue(e.deltaY < 0 ? 1 : -1);
49
+ }
50
+ });
51
+ });
52
+ });
53
+
54
+ async function makePrediction() {
55
+ const submitBtn = document.querySelector('.submit-btn');
56
+ const originalText = submitBtn.innerText;
57
+
58
+ submitBtn.innerText = "Analyzing...";
59
+ submitBtn.disabled = true;
60
+ submitBtn.style.opacity = "0.7";
61
+
62
+ const data = {
63
+ chroma_stft_mean: Number(document.getElementById('chroma_stft_mean').value),
64
+ rms_mean: Number(document.getElementById('rms_mean').value),
65
+ spectral_centroid_mean: Number(document.getElementById('spectral_centroid_mean').value),
66
+ spectral_bandwidth_mean: Number(document.getElementById('spectral_bandwidth_mean').value),
67
+ rolloff_mean: Number(document.getElementById('rolloff_mean').value),
68
+ zero_crossing_rate_mean: Number(document.getElementById('zero_crossing_rate_mean').value),
69
+ harmony_mean: Number(document.getElementById('harmony_mean').value),
70
+ tempo: Number(document.getElementById('tempo').value)
71
+ };
72
+
73
+ try {
74
+ const response = await fetch('/predict', {
75
+ method: 'POST',
76
+ headers: { 'Content-Type': 'application/json' },
77
+ body: JSON.stringify(data)
78
+ });
79
+
80
+ const result = await response.json();
81
+
82
+ const resultCard = document.getElementById('resultCard');
83
+ const genre = result.prediction;
84
+ const confidence = result.confidence || 0;
85
+ const barColor = '#4e9e94';
86
+
87
+ resultCard.innerHTML = `
88
+ <div class="result-box">
89
+ <p style="color: #9ca3af; font-size: 0.9rem; letter-spacing: 1px; margin-bottom:10px;">PREDICTED GENRE</p>
90
+ <div style="font-size: 2.5rem; font-weight: 800; margin: 15px 0; color: #4e9e94; text-transform: capitalize;">${genre}</div>
91
+ <p style="margin-bottom: 20px; color: #e0e0e0;">The model classified the audio features successfully.</p>
92
+
93
+ <div style="text-align: left; width: 100%; background: #1c1c2e; padding: 15px; border-radius: 8px; border: 1px solid #2e2e42;">
94
+ <div style="display:flex; justify-content:space-between; font-size: 0.9rem; color: #ccc; margin-bottom: 8px;">
95
+ <span>Confidence Score</span>
96
+ <span style="color: #fff; font-weight: bold;">${confidence.toFixed(1)}%</span>
97
+ </div>
98
+ <div class="confidence-bar-bg">
99
+ <div class="confidence-bar-fill" style="width: ${confidence}%; background-color: ${barColor};"></div>
100
+ </div>
101
+ </div>
102
+ </div>
103
+ `;
104
+
105
+ } catch (error) {
106
+ console.error("Error:", error);
107
+ alert("Server Error: Make sure the Flask app is running.");
108
+ }
109
+
110
+ submitBtn.innerText = originalText;
111
+ submitBtn.disabled = false;
112
+ submitBtn.style.opacity = "1";
113
+ }