trysem commited on
Commit
9d18449
·
verified ·
1 Parent(s): 0f463d2

Create MLonnx

Browse files
Files changed (1) hide show
  1. MLonnx +599 -0
MLonnx ADDED
@@ -0,0 +1,599 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import React, { useState, useEffect, useRef } from 'react';
2
+ import { Mic, Square, Settings, Loader2, AlertCircle, Copy, CheckCircle2, ChevronDown, ChevronUp, Upload } from 'lucide-react';
3
+
4
+ // --- Feature Extraction: Log-Mel Spectrogram ---
5
+ // This model requires 80-dim log-mel spectrogram features, standard for Conformer models.
6
+ const computeLogMelSpectrogram = (audioData) => {
7
+ const sr = 16000;
8
+ const n_fft = 512;
9
+ const win_length = 400; // 25ms
10
+ const hop_length = 160; // 10ms
11
+ const n_mels = 80;
12
+ const preemph = 0.97;
13
+
14
+ // 1. Preemphasis
15
+ const preemphasized = new Float32Array(audioData.length);
16
+ preemphasized[0] = audioData[0];
17
+ for (let i = 1; i < audioData.length; i++) {
18
+ preemphasized[i] = audioData[i] - preemph * audioData[i - 1];
19
+ }
20
+
21
+ // 2. Window (Hann)
22
+ const window = new Float32Array(win_length);
23
+ for (let i = 0; i < win_length; i++) {
24
+ window[i] = 0.5 - 0.5 * Math.cos((2 * Math.PI * i) / (win_length - 1));
25
+ }
26
+
27
+ // 3. Mel Filterbank
28
+ const fmin = 0;
29
+ const fmax = 8000;
30
+ const melMin = 2595 * Math.log10(1 + fmin / 700);
31
+ const melMax = 2595 * Math.log10(1 + fmax / 700);
32
+ const melPoints = Array.from({length: n_mels + 2}, (_, i) => melMin + i * (melMax - melMin) / (n_mels + 1));
33
+ const hzPoints = melPoints.map(m => 700 * (Math.pow(10, m / 2595) - 1));
34
+ const fftFreqs = Array.from({length: n_fft / 2 + 1}, (_, i) => (i * sr) / n_fft);
35
+
36
+ const fbank = [];
37
+ for (let i = 0; i < n_mels; i++) {
38
+ const row = new Float32Array(n_fft / 2 + 1);
39
+ const f_left = hzPoints[i];
40
+ const f_center = hzPoints[i + 1];
41
+ const f_right = hzPoints[i + 2];
42
+ for (let j = 0; j < fftFreqs.length; j++) {
43
+ const f = fftFreqs[j];
44
+ if (f >= f_left && f <= f_center) {
45
+ row[j] = (f - f_left) / (f_center - f_left);
46
+ } else if (f >= f_center && f <= f_right) {
47
+ row[j] = (f_right - f) / (f_right - f_center);
48
+ }
49
+ }
50
+ fbank.push(row);
51
+ }
52
+
53
+ // 4. STFT & Log-Mel Computation
54
+ const numFrames = Math.floor((preemphasized.length - win_length) / hop_length) + 1;
55
+ if (numFrames <= 0) return { melSpec: new Float32Array(0), numFrames: 0 };
56
+
57
+ const melSpec = new Float32Array(n_mels * numFrames);
58
+
59
+ for (let frame = 0; frame < numFrames; frame++) {
60
+ const start = frame * hop_length;
61
+ const real = new Float32Array(n_fft);
62
+ const imag = new Float32Array(n_fft);
63
+
64
+ for (let i = 0; i < win_length; i++) {
65
+ real[i] = preemphasized[start + i] * window[i];
66
+ }
67
+
68
+ // Cooley-Tukey FFT
69
+ let j = 0;
70
+ for (let i = 0; i < n_fft - 1; i++) {
71
+ if (i < j) {
72
+ let tr = real[i]; real[i] = real[j]; real[j] = tr;
73
+ let ti = imag[i]; imag[i] = imag[j]; imag[j] = ti;
74
+ }
75
+ let m = n_fft >> 1;
76
+ while (m >= 1 && j >= m) { j -= m; m >>= 1; }
77
+ j += m;
78
+ }
79
+
80
+ for (let l = 2; l <= n_fft; l <<= 1) {
81
+ let l2 = l >> 1;
82
+ let u1 = 1.0, u2 = 0.0;
83
+ let c1 = Math.cos(Math.PI / l2), c2 = -Math.sin(Math.PI / l2);
84
+ for (let j = 0; j < l2; j++) {
85
+ for (let i = j; i < n_fft; i += l) {
86
+ let i1 = i + l2;
87
+ let t1 = u1 * real[i1] - u2 * imag[i1];
88
+ let t2 = u1 * imag[i1] + u2 * real[i1];
89
+ real[i1] = real[i] - t1;
90
+ imag[i1] = imag[i] - t2;
91
+ real[i] += t1;
92
+ imag[i] += t2;
93
+ }
94
+ let z = u1 * c1 - u2 * c2;
95
+ u2 = u1 * c2 + u2 * c1;
96
+ u1 = z;
97
+ }
98
+ }
99
+
100
+ // Apply Mel Filterbank & Log
101
+ for (let m = 0; m < n_mels; m++) {
102
+ let melEnergy = 0;
103
+ for (let i = 0; i <= n_fft / 2; i++) {
104
+ const power = real[i] * real[i] + imag[i] * imag[i];
105
+ melEnergy += power * fbank[m][i];
106
+ }
107
+ const logMel = Math.log(Math.max(melEnergy, 1e-9));
108
+ melSpec[m * numFrames + frame] = logMel;
109
+ }
110
+ }
111
+
112
+ // 5. Feature Standardization (per-instance mean/var normalization)
113
+ for (let m = 0; m < n_mels; m++) {
114
+ let sum = 0;
115
+ for (let f = 0; f < numFrames; f++) {
116
+ sum += melSpec[m * numFrames + f];
117
+ }
118
+ const mean = sum / numFrames;
119
+ let sumSq = 0;
120
+ for (let f = 0; f < numFrames; f++) {
121
+ const diff = melSpec[m * numFrames + f] - mean;
122
+ sumSq += diff * diff;
123
+ }
124
+ const std = Math.sqrt(sumSq / numFrames) + 1e-9;
125
+ for (let f = 0; f < numFrames; f++) {
126
+ melSpec[m * numFrames + f] = (melSpec[m * numFrames + f] - mean) / std;
127
+ }
128
+ }
129
+
130
+ return { melSpec, numFrames };
131
+ };
132
+
133
+
134
+ export default function App() {
135
+ // App State
136
+ const [modelUrl, setModelUrl] = useState("https://huggingface.co/sulabhkatiyar/indicconformer-120m-onnx/resolve/main/ml/model.onnx");
137
+ const [vocabUrl, setVocabUrl] = useState("https://huggingface.co/sulabhkatiyar/indicconformer-120m-onnx/resolve/main/ml/vocab.json");
138
+
139
+ const [isOrtReady, setIsOrtReady] = useState(false);
140
+ const [session, setSession] = useState(null);
141
+ const [vocab, setVocab] = useState([]);
142
+ const [isLoading, setIsLoading] = useState(false);
143
+
144
+ const [isRecording, setIsRecording] = useState(false);
145
+ const [status, setStatus] = useState("Please load the model to begin.");
146
+ const [transcript, setTranscript] = useState("");
147
+ const [copiedMessage, setCopiedMessage] = useState("");
148
+
149
+ const [showSettings, setShowSettings] = useState(false);
150
+ const [errorMessage, setErrorMessage] = useState("");
151
+
152
+ // Refs for Audio Recording
153
+ const mediaRecorderRef = useRef(null);
154
+ const audioChunksRef = useRef([]);
155
+ const fileInputRef = useRef(null);
156
+
157
+ // Load onnxruntime-web script dynamically
158
+ useEffect(() => {
159
+ if (window.ort) {
160
+ setIsOrtReady(true);
161
+ return;
162
+ }
163
+ const script = document.createElement('script');
164
+ script.src = "https://cdn.jsdelivr.net/npm/onnxruntime-web/dist/ort.min.js";
165
+ script.async = true;
166
+ script.onload = () => setIsOrtReady(true);
167
+ script.onerror = () => setErrorMessage("Failed to load onnxruntime-web library.");
168
+ document.body.appendChild(script);
169
+ }, []);
170
+
171
+ const loadVocab = async (url) => {
172
+ const res = await fetch(url);
173
+ if (!res.ok) throw new Error(`Failed to load vocab from ${url}`);
174
+
175
+ try {
176
+ // First attempt to parse as JSON
177
+ const data = await res.json();
178
+ if (Array.isArray(data)) {
179
+ return data; // Simple array of tokens
180
+ } else if (typeof data === 'object') {
181
+ // Handle format {"token": index}
182
+ const vocabArray = [];
183
+ for (const [token, index] of Object.entries(data)) {
184
+ vocabArray[index] = token;
185
+ }
186
+ return vocabArray;
187
+ }
188
+ } catch (e) {
189
+ // Fallback to text-based parsing if JSON fails (e.g. for vocab.txt)
190
+ const text = await res.text();
191
+ return text.split('\n').map(line => line.trim()).filter(line => line.length > 0);
192
+ }
193
+ throw new Error("Invalid vocabulary format");
194
+ };
195
+
196
+ const initModel = async () => {
197
+ if (!isOrtReady || !window.ort) {
198
+ setErrorMessage("ONNX Runtime is not ready yet.");
199
+ return;
200
+ }
201
+
202
+ setIsLoading(true);
203
+ setErrorMessage("");
204
+ setStatus("Downloading Vocabulary...");
205
+
206
+ try {
207
+ const loadedVocab = await loadVocab(vocabUrl);
208
+ setVocab(loadedVocab);
209
+
210
+ setStatus("Downloading ONNX Model (100MB+). This may take a while...");
211
+ // Create Inference Session using the WASM execution provider
212
+ const sess = await window.ort.InferenceSession.create(modelUrl, {
213
+ executionProviders: ['wasm']
214
+ });
215
+
216
+ setSession(sess);
217
+ setStatus("Model Loaded & Ready. Press the microphone to speak.");
218
+ } catch (err) {
219
+ console.error(err);
220
+ setErrorMessage(`Initialization Error: ${err.message}. Please check the URLs in Settings.`);
221
+ setStatus("Failed to load model.");
222
+ } finally {
223
+ setIsLoading(false);
224
+ }
225
+ };
226
+
227
+ const startRecording = async () => {
228
+ try {
229
+ const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
230
+ const mediaRecorder = new MediaRecorder(stream);
231
+ audioChunksRef.current = [];
232
+
233
+ mediaRecorder.ondataavailable = (e) => {
234
+ if (e.data.size > 0) audioChunksRef.current.push(e.data);
235
+ };
236
+
237
+ mediaRecorder.onstop = processAndInfer;
238
+ mediaRecorderRef.current = mediaRecorder;
239
+ mediaRecorder.start();
240
+
241
+ setIsRecording(true);
242
+ setStatus("Recording... Speak in Malayalam.");
243
+ setErrorMessage("");
244
+ } catch (err) {
245
+ console.error(err);
246
+ setErrorMessage("Microphone permission denied or an error occurred.");
247
+ }
248
+ };
249
+
250
+ const stopRecording = () => {
251
+ if (mediaRecorderRef.current && isRecording) {
252
+ mediaRecorderRef.current.stop();
253
+ setIsRecording(false);
254
+ // Stops all microphone tracks
255
+ mediaRecorderRef.current.stream.getTracks().forEach(track => track.stop());
256
+ }
257
+ };
258
+
259
+ const processAndInfer = async () => {
260
+ setStatus("Processing Audio...");
261
+ try {
262
+ // Decode audio and resample to 16kHz Mono Float32
263
+ const blob = new Blob(audioChunksRef.current);
264
+ const arrayBuffer = await blob.arrayBuffer();
265
+ const audioCtx = new (window.AudioContext || window.webkitAudioContext)({ sampleRate: 16000 });
266
+ const decodedData = await audioCtx.decodeAudioData(arrayBuffer);
267
+ const float32Data = decodedData.getChannelData(0); // Mono channel
268
+
269
+ setStatus("Running Inference...");
270
+ await runInference(float32Data);
271
+ } catch (err) {
272
+ console.error(err);
273
+ setErrorMessage(`Audio Processing Error: ${err.message}`);
274
+ setStatus("Ready.");
275
+ }
276
+ };
277
+
278
+ const handleFileUpload = async (e) => {
279
+ const file = e.target.files[0];
280
+ if (!file) return;
281
+
282
+ setStatus("Processing Uploaded Audio...");
283
+ setErrorMessage("");
284
+ setIsLoading(true);
285
+
286
+ try {
287
+ const arrayBuffer = await file.arrayBuffer();
288
+ const audioCtx = new (window.AudioContext || window.webkitAudioContext)({ sampleRate: 16000 });
289
+ const decodedData = await audioCtx.decodeAudioData(arrayBuffer);
290
+ const float32Data = decodedData.getChannelData(0); // Mono channel
291
+
292
+ setStatus("Running Inference on File...");
293
+ await runInference(float32Data);
294
+ } catch (err) {
295
+ console.error(err);
296
+ setErrorMessage(`Audio Upload Error: ${err.message}`);
297
+ setStatus("Ready.");
298
+ } finally {
299
+ setIsLoading(false);
300
+ e.target.value = null; // Reset input to allow re-uploading the same file
301
+ }
302
+ };
303
+
304
+ const runInference = async (float32Data) => {
305
+ try {
306
+ const inputNames = session.inputNames;
307
+ const feeds = {};
308
+
309
+ // Attempt 1: Raw Waveform tensor
310
+ if (inputNames.includes('audio_signal')) {
311
+ feeds['audio_signal'] = new window.ort.Tensor('float32', float32Data, [1, float32Data.length]);
312
+ } else {
313
+ throw new Error(`The model expects inputs: ${inputNames.join(', ')}.`);
314
+ }
315
+
316
+ if (inputNames.includes('length')) {
317
+ feeds['length'] = new window.ort.Tensor('int64', new BigInt64Array([BigInt(float32Data.length)]), [1]);
318
+ }
319
+
320
+ let results;
321
+ try {
322
+ results = await session.run(feeds);
323
+ } catch (runError) {
324
+ // Attempt 2: Feature-extracted Log-Mel Spectrogram (Catches "Expected: 3" or "Expected: 80" errors)
325
+ if (runError.message && (runError.message.includes("Expected: 3") || runError.message.includes("Expected: 80"))) {
326
+ console.warn("Raw audio tensor failed. Model likely lacks a feature extractor. Computing 80-bin Log-Mel Spectrogram natively...");
327
+
328
+ const { melSpec, numFrames } = computeLogMelSpectrogram(float32Data);
329
+ if (numFrames <= 0) throw new Error("Audio sample is too short to process.");
330
+
331
+ feeds['audio_signal'] = new window.ort.Tensor('float32', melSpec, [1, 80, numFrames]);
332
+
333
+ if (inputNames.includes('length')) {
334
+ feeds['length'] = new window.ort.Tensor('int64', new BigInt64Array([BigInt(numFrames)]), [1]);
335
+ }
336
+
337
+ results = await session.run(feeds);
338
+ } else {
339
+ throw runError; // Unhandled error
340
+ }
341
+ }
342
+
343
+ // Assume the first output contains the logprobs/logits
344
+ const outputName = session.outputNames[0];
345
+ const outputTensor = results[outputName];
346
+ const logits = outputTensor.data;
347
+ let dims = outputTensor.dims;
348
+
349
+ // Standardize dims to [batch, time, vocab]
350
+ if (dims.length === 2) dims = [1, dims[0], dims[1]];
351
+
352
+ const text = decodeCTC(logits, dims, vocab);
353
+ setTranscript(prev => prev + (prev ? " " : "") + text);
354
+ setStatus("Transcription Complete. Ready for next.");
355
+ } catch (err) {
356
+ console.error(err);
357
+ setErrorMessage(`Inference Error: ${err.message}`);
358
+ setStatus("Ready.");
359
+ }
360
+ };
361
+
362
+ const decodeCTC = (logits, dims, vocabList) => {
363
+ const T = dims[1]; // Time frames
364
+ const V = dims[2]; // Vocab size emitted by model
365
+ let result = [];
366
+ let prev_id = -1;
367
+
368
+ // In typical NeMo models, the blank token is the last index
369
+ const blankId = V - 1;
370
+
371
+ for (let t = 0; t < T; t++) {
372
+ let max_val = -Infinity;
373
+ let max_id = -1;
374
+
375
+ for (let v = 0; v < V; v++) {
376
+ const val = logits[t * V + v];
377
+ if (val > max_val) {
378
+ max_val = val;
379
+ max_id = v;
380
+ }
381
+ }
382
+
383
+ if (max_id !== prev_id && max_id !== blankId) {
384
+ let token = "";
385
+ if (max_id < vocabList.length) {
386
+ token = vocabList[max_id];
387
+ }
388
+
389
+ // Ignore standard special tokens
390
+ if (token && token !== '<blank>' && token !== '<pad>' && token !== '<s>' && token !== '</s>') {
391
+ result.push(token);
392
+ }
393
+ }
394
+ prev_id = max_id;
395
+ }
396
+
397
+ // Clean up SentencePiece artifacts (e.g., '_' or ' ')
398
+ let decodedText = result.join('');
399
+ decodedText = decodedText.replace(/ /g, ' ').replace(/_/g, ' ').trim();
400
+ return decodedText.replace(/\s+/g, ' '); // Remove redundant spaces
401
+ };
402
+
403
+ const handleCopy = () => {
404
+ const textArea = document.createElement("textarea");
405
+ textArea.value = transcript;
406
+ document.body.appendChild(textArea);
407
+ textArea.select();
408
+ try {
409
+ document.execCommand('copy');
410
+ setCopiedMessage("Copied to clipboard!");
411
+ setTimeout(() => setCopiedMessage(""), 2000);
412
+ } catch (err) {
413
+ setCopiedMessage("Failed to copy");
414
+ setTimeout(() => setCopiedMessage(""), 2000);
415
+ }
416
+ document.body.removeChild(textArea);
417
+ };
418
+
419
+ return (
420
+ <div className="min-h-screen bg-neutral-50 dark:bg-neutral-900 text-neutral-900 dark:text-neutral-100 p-4 sm:p-8 font-sans selection:bg-blue-200 dark:selection:bg-blue-900">
421
+ <div className="max-w-3xl mx-auto space-y-6">
422
+
423
+ {/* Header */}
424
+ <div className="text-center space-y-2">
425
+ <h1 className="text-3xl sm:text-4xl font-extrabold tracking-tight bg-clip-text text-transparent bg-gradient-to-r from-blue-600 to-indigo-600 dark:from-blue-400 dark:to-indigo-400">
426
+ Malayalam Speech-to-Text
427
+ </h1>
428
+ <p className="text-neutral-500 dark:text-neutral-400 text-sm sm:text-base">
429
+ Powered by IndicConformer-120M & ONNX Runtime Web
430
+ </p>
431
+ </div>
432
+
433
+ {/* Main Interface Card */}
434
+ <div className="bg-white dark:bg-neutral-800 rounded-2xl shadow-xl border border-neutral-100 dark:border-neutral-700 overflow-hidden">
435
+
436
+ {/* Status Bar */}
437
+ <div className="bg-neutral-100 dark:bg-neutral-700/50 px-6 py-3 flex items-center justify-between">
438
+ <div className="flex items-center space-x-2 text-sm font-medium text-neutral-600 dark:text-neutral-300">
439
+ {isLoading ? (
440
+ <Loader2 size={16} className="animate-spin text-blue-500" />
441
+ ) : session ? (
442
+ <CheckCircle2 size={16} className="text-emerald-500" />
443
+ ) : (
444
+ <AlertCircle size={16} className="text-amber-500" />
445
+ )}
446
+ <span>{status}</span>
447
+ </div>
448
+
449
+ <button
450
+ onClick={() => setShowSettings(!showSettings)}
451
+ className="text-neutral-400 hover:text-neutral-600 dark:hover:text-neutral-200 transition-colors"
452
+ title="Settings"
453
+ >
454
+ <Settings size={18} />
455
+ </button>
456
+ </div>
457
+
458
+ {/* Settings Panel */}
459
+ {showSettings && (
460
+ <div className="px-6 py-4 bg-neutral-50 dark:bg-neutral-800/80 border-b border-neutral-100 dark:border-neutral-700 space-y-4">
461
+ <h3 className="text-sm font-semibold uppercase tracking-wider text-neutral-500 dark:text-neutral-400">
462
+ Model Configuration
463
+ </h3>
464
+ <div className="space-y-3 text-sm">
465
+ <div>
466
+ <label className="block text-neutral-700 dark:text-neutral-300 mb-1 font-medium">ONNX Model URL</label>
467
+ <input
468
+ type="text"
469
+ value={modelUrl}
470
+ onChange={e => setModelUrl(e.target.value)}
471
+ className="w-full p-2.5 border border-neutral-300 dark:border-neutral-600 rounded-lg bg-white dark:bg-neutral-900 focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none transition-all"
472
+ />
473
+ </div>
474
+ <div>
475
+ <label className="block text-neutral-700 dark:text-neutral-300 mb-1 font-medium">Vocabulary URL (.txt)</label>
476
+ <input
477
+ type="text"
478
+ value={vocabUrl}
479
+ onChange={e => setVocabUrl(e.target.value)}
480
+ className="w-full p-2.5 border border-neutral-300 dark:border-neutral-600 rounded-lg bg-white dark:bg-neutral-900 focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none transition-all"
481
+ />
482
+ </div>
483
+ <div className="flex items-center justify-between pt-2">
484
+ <span className="text-xs text-neutral-500 dark:text-neutral-400 flex items-center">
485
+ <AlertCircle size={12} className="inline mr-1" /> Re-initialize model after changing URLs.
486
+ </span>
487
+ <button
488
+ onClick={initModel}
489
+ disabled={isLoading}
490
+ className="px-4 py-2 bg-neutral-200 dark:bg-neutral-700 hover:bg-neutral-300 dark:hover:bg-neutral-600 rounded-lg font-medium transition-colors text-sm"
491
+ >
492
+ Load / Refresh Model
493
+ </button>
494
+ </div>
495
+ </div>
496
+ </div>
497
+ )}
498
+
499
+ {/* Action Area */}
500
+ <div className="p-8 flex flex-col items-center justify-center space-y-6">
501
+
502
+ {/* Error Message Display */}
503
+ {errorMessage && (
504
+ <div className="w-full p-4 bg-red-50 dark:bg-red-900/20 text-red-600 dark:text-red-400 rounded-xl text-sm border border-red-100 dark:border-red-900/50 flex items-start">
505
+ <AlertCircle size={18} className="mr-2 flex-shrink-0 mt-0.5" />
506
+ <span>{errorMessage}</span>
507
+ </div>
508
+ )}
509
+
510
+ {!session && !isLoading && !errorMessage && (
511
+ <button
512
+ onClick={initModel}
513
+ className="px-8 py-4 bg-blue-600 hover:bg-blue-700 text-white rounded-xl font-bold shadow-lg hover:shadow-blue-600/30 transition-all transform hover:scale-105 active:scale-95"
514
+ >
515
+ Initialize Model
516
+ </button>
517
+ )}
518
+
519
+ {/* Input Controls */}
520
+ <div className="flex items-center space-x-6">
521
+ {/* Microphone Button */}
522
+ <button
523
+ onClick={isRecording ? stopRecording : startRecording}
524
+ disabled={!session || isLoading}
525
+ className={`p-8 rounded-full transition-all duration-300 group ${
526
+ !session || isLoading
527
+ ? 'bg-neutral-200 dark:bg-neutral-800 text-neutral-400 dark:text-neutral-600 cursor-not-allowed'
528
+ : isRecording
529
+ ? 'bg-red-500 hover:bg-red-600 animate-pulse text-white shadow-[0_0_40px_rgba(239,68,68,0.5)]'
530
+ : 'bg-blue-600 hover:bg-blue-700 text-white shadow-lg hover:shadow-[0_0_30px_rgba(37,99,235,0.4)] transform hover:scale-105 active:scale-95'
531
+ }`}
532
+ title="Record Audio"
533
+ >
534
+ {isRecording ? <Square size={40} className="fill-current" /> : <Mic size={40} />}
535
+ </button>
536
+
537
+ {/* Upload Button */}
538
+ <button
539
+ onClick={() => fileInputRef.current?.click()}
540
+ disabled={!session || isLoading || isRecording}
541
+ className={`p-8 rounded-full transition-all duration-300 group ${
542
+ !session || isLoading || isRecording
543
+ ? 'bg-neutral-200 dark:bg-neutral-800 text-neutral-400 dark:text-neutral-600 cursor-not-allowed'
544
+ : 'bg-indigo-600 hover:bg-indigo-700 text-white shadow-lg hover:shadow-[0_0_30px_rgba(79,70,229,0.4)] transform hover:scale-105 active:scale-95'
545
+ }`}
546
+ title="Upload Audio File"
547
+ >
548
+ <Upload size={40} />
549
+ </button>
550
+ <input
551
+ type="file"
552
+ ref={fileInputRef}
553
+ onChange={handleFileUpload}
554
+ accept="audio/*"
555
+ className="hidden"
556
+ />
557
+ </div>
558
+
559
+ <p className="text-neutral-500 dark:text-neutral-400 font-medium text-center">
560
+ {isRecording ? "Tap to Stop & Transcribe" : (session ? "Tap Mic to Record or Upload an Audio File" : "Model required to process audio")}
561
+ </p>
562
+ </div>
563
+
564
+ {/* Transcript Area */}
565
+ <div className="border-t border-neutral-100 dark:border-neutral-700 p-6 bg-neutral-50 dark:bg-neutral-800/50">
566
+ <div className="flex items-center justify-between mb-3">
567
+ <h3 className="font-semibold text-neutral-700 dark:text-neutral-300">Transcript</h3>
568
+
569
+ {/* Copy Tools */}
570
+ <div className="flex items-center space-x-3">
571
+ {copiedMessage && <span className="text-xs text-green-500 font-medium animate-fade-in">{copiedMessage}</span>}
572
+ <button
573
+ onClick={handleCopy}
574
+ disabled={!transcript}
575
+ className="p-2 text-neutral-400 hover:text-blue-500 disabled:opacity-50 disabled:cursor-not-allowed transition-colors rounded-lg hover:bg-blue-50 dark:hover:bg-blue-900/20"
576
+ title="Copy Transcript"
577
+ >
578
+ <Copy size={18} />
579
+ </button>
580
+ <button
581
+ onClick={() => setTranscript("")}
582
+ disabled={!transcript}
583
+ className="text-xs font-medium px-3 py-1.5 rounded-lg text-neutral-500 hover:text-red-500 hover:bg-red-50 dark:hover:bg-red-900/20 transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
584
+ >
585
+ Clear
586
+ </button>
587
+ </div>
588
+ </div>
589
+
590
+ <div className="w-full min-h-[120px] p-4 bg-white dark:bg-neutral-900 border border-neutral-200 dark:border-neutral-700 rounded-xl text-neutral-800 dark:text-neutral-200 font-medium text-lg leading-relaxed whitespace-pre-wrap">
591
+ {transcript || <span className="text-neutral-400 dark:text-neutral-600 italic">Transcription will appear here...</span>}
592
+ </div>
593
+ </div>
594
+
595
+ </div>
596
+ </div>
597
+ </div>
598
+ );
599
+ }