Spaces:
Running
Running
Upload index.js with huggingface_hub
Browse files
index.js
CHANGED
|
@@ -1,76 +1,75 @@
|
|
| 1 |
-
import { pipeline } from '
|
| 2 |
-
|
| 3 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
const status = document.getElementById('status');
|
| 5 |
-
const
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
const
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
example.addEventListener('click', (e) => {
|
| 17 |
-
e.preventDefault();
|
| 18 |
-
detect(EXAMPLE_URL);
|
| 19 |
});
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
const reader = new FileReader();
|
| 28 |
-
|
| 29 |
-
// Set up a callback when the file is loaded
|
| 30 |
-
reader.onload = e2 => detect(e2.target.result);
|
| 31 |
-
|
| 32 |
-
reader.readAsDataURL(file);
|
| 33 |
});
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
//
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
function renderBox({ box, label }) {
|
| 52 |
-
const { xmax, xmin, ymax, ymin } = box;
|
| 53 |
-
|
| 54 |
-
// Generate a random color for the box
|
| 55 |
-
const color = '#' + Math.floor(Math.random() * 0xFFFFFF).toString(16).padStart(6, 0);
|
| 56 |
-
|
| 57 |
-
// Draw the box
|
| 58 |
-
const boxElement = document.createElement('div');
|
| 59 |
-
boxElement.className = 'bounding-box';
|
| 60 |
-
Object.assign(boxElement.style, {
|
| 61 |
-
borderColor: color,
|
| 62 |
-
left: 100 * xmin + '%',
|
| 63 |
-
top: 100 * ymin + '%',
|
| 64 |
-
width: 100 * (xmax - xmin) + '%',
|
| 65 |
-
height: 100 * (ymax - ymin) + '%',
|
| 66 |
-
})
|
| 67 |
-
|
| 68 |
-
// Draw label
|
| 69 |
-
const labelElement = document.createElement('span');
|
| 70 |
-
labelElement.textContent = label;
|
| 71 |
-
labelElement.className = 'bounding-box-label';
|
| 72 |
-
labelElement.style.backgroundColor = color;
|
| 73 |
-
|
| 74 |
-
boxElement.appendChild(labelElement);
|
| 75 |
-
imageContainer.appendChild(boxElement);
|
| 76 |
-
}
|
|
|
|
| 1 |
+
import { pipeline } from '@huggingface/transformers';
|
| 2 |
+
|
| 3 |
+
// Initialize transcriber (lazy loading)
|
| 4 |
+
let transcriber;
|
| 5 |
+
const getTranscriber = async () => {
|
| 6 |
+
if (!transcriber) {
|
| 7 |
+
document.getElementById('status').textContent = 'Loading Whisper model...';
|
| 8 |
+
transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en');
|
| 9 |
+
document.getElementById('status').textContent = 'Model loaded.';
|
| 10 |
+
}
|
| 11 |
+
return transcriber;
|
| 12 |
+
};
|
| 13 |
+
|
| 14 |
+
// DOM Elements
|
| 15 |
+
const audioInput = document.getElementById('audioInput');
|
| 16 |
+
const useSampleBtn = document.getElementById('useSample');
|
| 17 |
+
const player = document.getElementById('player');
|
| 18 |
+
const output = document.getElementById('output');
|
| 19 |
const status = document.getElementById('status');
|
| 20 |
+
const modeOptions = document.querySelectorAll('input[name="mode"]');
|
| 21 |
+
|
| 22 |
+
// Set audio source from file
|
| 23 |
+
audioInput.addEventListener('change', (e) => {
|
| 24 |
+
const file = e.target.files[0];
|
| 25 |
+
if (file) {
|
| 26 |
+
const url = URL.createObjectURL(file);
|
| 27 |
+
player.src = url;
|
| 28 |
+
output.textContent = '';
|
| 29 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
});
|
| 31 |
|
| 32 |
+
// Use sample JFK audio
|
| 33 |
+
useSampleBtn.addEventListener('click', () => {
|
| 34 |
+
const sampleUrl = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav';
|
| 35 |
+
player.src = sampleUrl;
|
| 36 |
+
output.textContent = '';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
});
|
| 38 |
|
| 39 |
+
// Get selected mode
|
| 40 |
+
const getSelectedMode = () => {
|
| 41 |
+
for (const option of modeOptions) {
|
| 42 |
+
if (option.checked) return option.value;
|
| 43 |
+
}
|
| 44 |
+
return 'normal';
|
| 45 |
+
};
|
| 46 |
+
|
| 47 |
+
// Transcribe button click handler
|
| 48 |
+
player.addEventListener('play', async () => {
|
| 49 |
+
try {
|
| 50 |
+
const transcriber = await getTranscriber();
|
| 51 |
+
const mode = getSelectedMode();
|
| 52 |
+
|
| 53 |
+
// Prepare transcription options
|
| 54 |
+
let options = {};
|
| 55 |
+
if (mode === 'chunks') {
|
| 56 |
+
options.return_timestamps = true;
|
| 57 |
+
} else if (mode === 'words') {
|
| 58 |
+
options.return_timestamps = 'word';
|
| 59 |
+
}
|
| 60 |
|
| 61 |
+
// Update status
|
| 62 |
+
status.textContent = 'Transcribing...';
|
| 63 |
+
output.textContent = 'Transcribing...';
|
| 64 |
+
|
| 65 |
+
// Perform transcription
|
| 66 |
+
const result = await transcriber(player.src, options);
|
| 67 |
+
output.textContent = JSON.stringify(result, null, 2);
|
| 68 |
+
|
| 69 |
+
status.textContent = 'Transcription complete.';
|
| 70 |
+
} catch (err) {
|
| 71 |
+
console.error(err);
|
| 72 |
+
status.textContent = `Error: ${err.message}`;
|
| 73 |
+
output.textContent = '';
|
| 74 |
+
}
|
| 75 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|