-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTranscribe.html
More file actions
214 lines (183 loc) · 10.1 KB
/
Transcribe.html
File metadata and controls
214 lines (183 loc) · 10.1 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Audio to Text Transcriber</title>
<!-- Tailwind CSS for styling -->
<script src="https://cdn.tailwindcss.com"></script>
<!-- Google Fonts: Inter -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<!-- Custom Styles -->
<style>
body {
font-family: 'Inter', sans-serif;
background-color: #111827; /* Tailwind gray-900 */
}
/* Simple spinner animation */
.spinner {
border: 4px solid rgba(255, 255, 255, 0.2);
border-left-color: #3b82f6; /* Tailwind blue-500 */
border-radius: 50%;
width: 24px;
height: 24px;
animation: spin 1s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
</style>
</head>
<body class="text-gray-200 flex items-center justify-center min-h-screen p-4">
<div class="w-full max-w-2xl mx-auto p-6 md:p-8 bg-gray-800 rounded-2xl shadow-2xl space-y-6">
<!-- Header -->
<div class="text-center">
<h1 class="text-3xl font-bold text-white">Audio Transcriber</h1>
<p class="text-gray-400 mt-2">Upload an audio file to convert its speech into text.</p>
</div>
<!-- Error Message Area -->
<div id="error-message" class="hidden text-center text-red-400 p-4 bg-red-900/50 rounded-lg"></div>
<!-- File Upload Section -->
<div id="upload-section">
<label for="audio-upload" class="flex flex-col items-center justify-center w-full h-64 border-2 border-gray-600 border-dashed rounded-lg cursor-pointer bg-gray-700 hover:bg-gray-600 transition-colors duration-300">
<div class="flex flex-col items-center justify-center pt-5 pb-6">
<svg class="w-10 h-10 mb-4 text-gray-400" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 20 16">
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 13h3a3 3 0 0 0 0-6h-.025A5.56 5.56 0 0 0 16 6.5 5.5 5.5 0 0 0 5.207 5.021C5.137 5.017 5.071 5 5 5a4 4 0 0 0 0 8h2.167M10 15V6m0 0L8 8m2-2 2 2"/>
</svg>
<p class="mb-2 text-sm text-gray-400"><span class="font-semibold">Click to upload</span> or drag and drop</p>
<p class="text-xs text-gray-500">MP3, WAV, M4A, etc.</p>
</div>
<input id="audio-upload" type="file" class="hidden" accept="audio/*" />
</label>
<div id="file-info" class="mt-4 text-center text-sm text-gray-400 hidden">
Selected file: <span id="file-name" class="font-medium text-white"></span>
</div>
</div>
<!-- Transcribe Button -->
<div id="transcribe-button-container" class="text-center">
<button id="transcribe-button" class="w-full sm:w-auto bg-blue-600 hover:bg-blue-700 disabled:bg-blue-800 disabled:cursor-not-allowed disabled:text-gray-400 text-white font-bold py-3 px-8 rounded-lg transition-all duration-300 shadow-lg focus:outline-none focus:ring-4 focus:ring-blue-500/50" disabled>
Transcribe Audio
</button>
</div>
<!-- Processing State -->
<div id="processing-section" class="hidden flex flex-col items-center justify-center text-center space-y-4 py-8">
<div class="spinner"></div>
<p class="text-lg font-medium text-white">Processing your audio...</p>
<p id="status-text" class="text-gray-400">Please wait, this may take a few moments.</p>
</div>
<!-- Result Section -->
<div id="result-section" class="hidden space-y-4">
<div>
<h2 class="text-2xl font-semibold text-white">Transcription Result</h2>
<p class="text-gray-400 mt-1">Review the generated text below.</p>
</div>
<textarea id="transcription-output" rows="12" class="w-full p-4 bg-gray-900 border border-gray-600 rounded-lg text-gray-300 focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-colors" readonly></textarea>
<div class="flex flex-col sm:flex-row gap-4">
<button id="download-button" class="w-full sm:w-auto flex-grow bg-green-600 hover:bg-green-700 text-white font-bold py-3 px-8 rounded-lg transition-all duration-300 shadow-lg focus:outline-none focus:ring-4 focus:ring-green-500/50">
Download .txt File
</button>
<button id="reset-button" class="w-full sm:w-auto flex-grow bg-gray-600 hover:bg-gray-700 text-white font-bold py-3 px-8 rounded-lg transition-all duration-300 shadow-lg focus:outline-none focus:ring-4 focus:ring-gray-500/50">
Transcribe Another File
</button>
</div>
</div>
</div>
<script type="module">
// --- Element Selectors ---
const audioUploadInput = document.getElementById('audio-upload');
const transcribeButton = document.getElementById('transcribe-button');
const downloadButton = document.getElementById('download-button');
const resetButton = document.getElementById('reset-button');
const fileInfoDiv = document.getElementById('file-info');
const fileNameSpan = document.getElementById('file-name');
const uploadSection = document.getElementById('upload-section');
const transcribeButtonContainer = document.getElementById('transcribe-button-container');
const processingSection = document.getElementById('processing-section');
const resultSection = document.getElementById('result-section');
const transcriptionOutput = document.getElementById('transcription-output');
const statusText = document.getElementById('status-text');
const errorMessageDiv = document.getElementById('error-message');
let selectedFile = null;
let transcriber = null;
// --- UI Helper Functions ---
function showUI(state) {
uploadSection.classList.toggle('hidden', state !== 'upload');
transcribeButtonContainer.classList.toggle('hidden', state !== 'upload');
processingSection.classList.toggle('hidden', state !== 'processing');
resultSection.classList.toggle('hidden', state !== 'result');
processingSection.classList.toggle('flex', state === 'processing');
errorMessageDiv.classList.add('hidden'); // Hide error on state change
}
function showError(message) {
errorMessageDiv.textContent = message;
errorMessageDiv.classList.remove('hidden');
showUI('upload'); // Go back to upload state
}
// --- Event Listeners ---
audioUploadInput.addEventListener('change', (event) => {
selectedFile = event.target.files[0];
if (selectedFile) {
fileNameSpan.textContent = selectedFile.name;
fileInfoDiv.classList.remove('hidden');
transcribeButton.disabled = false;
errorMessageDiv.classList.add('hidden');
} else {
fileInfoDiv.classList.add('hidden');
transcribeButton.disabled = true;
}
});
transcribeButton.addEventListener('click', async () => {
if (!selectedFile) {
showError("Please select an audio file first.");
return;
}
showUI('processing');
try {
// Lazily import the pipeline and load the model
if (!transcriber) {
statusText.textContent = 'Loading AI model... (This may take a minute on first use)';
const { pipeline } = await import('https://cdn.jsdelivr.net/npm/@xenova/transformers@2.17.1');
transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en');
}
statusText.textContent = 'Transcribing audio... Please wait.';
// Create a URL for the file to pass to the transcriber
const audioUrl = URL.createObjectURL(selectedFile);
const output = await transcriber(audioUrl, {
chunk_length_s: 30,
stride_length_s: 5,
});
URL.revokeObjectURL(audioUrl); // Clean up the object URL
transcriptionOutput.value = output.text.trim();
showUI('result');
} catch (error) {
console.error('Transcription failed:', error);
showError('An error occurred during transcription. Please try another file.');
}
});
downloadButton.addEventListener('click', () => {
const text = transcriptionOutput.value;
if (!text) return;
const blob = new Blob([text], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
const originalFileName = selectedFile.name.split('.').slice(0, -1).join('.') || 'transcription';
a.download = `${originalFileName}.txt`;
a.href = url;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
resetButton.addEventListener('click', () => {
audioUploadInput.value = ''; // Clear the file input
selectedFile = null;
transcriptionOutput.value = '';
fileInfoDiv.classList.add('hidden');
transcribeButton.disabled = true;
showUI('upload');
});
</script>
</body>
</html>