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

Create MLONNX-FIXED

Browse files
Files changed (1) hide show
  1. MLONNX-FIXED +651 -0
MLONNX-FIXED ADDED
@@ -0,0 +1,651 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ const [modelSource, setModelSource] = useState('url'); // 'url' | 'local'
139
+ const [localModel, setLocalModel] = useState(null);
140
+ const [localVocab, setLocalVocab] = useState(null);
141
+
142
+ const [isOrtReady, setIsOrtReady] = useState(false);
143
+ const [session, setSession] = useState(null);
144
+ const [vocab, setVocab] = useState([]);
145
+ const [isLoading, setIsLoading] = useState(false);
146
+
147
+ const [isRecording, setIsRecording] = useState(false);
148
+ const [status, setStatus] = useState("Please load the model to begin.");
149
+ const [transcript, setTranscript] = useState("");
150
+ const [copiedMessage, setCopiedMessage] = useState("");
151
+
152
+ const [showSettings, setShowSettings] = useState(false);
153
+ const [errorMessage, setErrorMessage] = useState("");
154
+
155
+ // Refs for Audio Recording
156
+ const mediaRecorderRef = useRef(null);
157
+ const audioChunksRef = useRef([]);
158
+ const fileInputRef = useRef(null);
159
+
160
+ // Load onnxruntime-web script dynamically
161
+ useEffect(() => {
162
+ if (window.ort) {
163
+ setIsOrtReady(true);
164
+ return;
165
+ }
166
+ const script = document.createElement('script');
167
+ script.src = "https://cdn.jsdelivr.net/npm/onnxruntime-web/dist/ort.min.js";
168
+ script.async = true;
169
+ script.onload = () => setIsOrtReady(true);
170
+ script.onerror = () => setErrorMessage("Failed to load onnxruntime-web library.");
171
+ document.body.appendChild(script);
172
+ }, []);
173
+
174
+ const loadVocabData = (text) => {
175
+ try {
176
+ const data = JSON.parse(text);
177
+ if (Array.isArray(data)) return data;
178
+ if (typeof data === 'object') {
179
+ const vocabArray = [];
180
+ for (const [token, index] of Object.entries(data)) {
181
+ vocabArray[index] = token;
182
+ }
183
+ return vocabArray;
184
+ }
185
+ } catch (e) {
186
+ return text.split('\n').map(line => line.trim()).filter(line => line.length > 0);
187
+ }
188
+ throw new Error("Invalid vocabulary format");
189
+ };
190
+
191
+ const initModel = async () => {
192
+ if (!isOrtReady || !window.ort) {
193
+ setErrorMessage("ONNX Runtime is not ready yet.");
194
+ return;
195
+ }
196
+
197
+ setIsLoading(true);
198
+ setErrorMessage("");
199
+ setStatus("Downloading/Reading Vocabulary...");
200
+
201
+ try {
202
+ let loadedVocab;
203
+ let sess;
204
+
205
+ if (modelSource === 'url') {
206
+ const res = await fetch(vocabUrl);
207
+ if (!res.ok) throw new Error(`Failed to load vocab from ${vocabUrl}`);
208
+ const text = await res.text();
209
+ loadedVocab = loadVocabData(text);
210
+
211
+ setStatus("Downloading ONNX Model (100MB+). This may take a while...");
212
+ sess = await window.ort.InferenceSession.create(modelUrl, {
213
+ executionProviders: ['wasm']
214
+ });
215
+ } else {
216
+ if (!localModel || !localVocab) {
217
+ throw new Error("Please select both the ONNX Model and Vocabulary files.");
218
+ }
219
+ const vocabText = await localVocab.text();
220
+ loadedVocab = loadVocabData(vocabText);
221
+
222
+ setStatus("Reading Local ONNX Model...");
223
+ const modelBuffer = await localModel.arrayBuffer();
224
+ sess = await window.ort.InferenceSession.create(new Uint8Array(modelBuffer), {
225
+ executionProviders: ['wasm']
226
+ });
227
+ }
228
+
229
+ setVocab(loadedVocab);
230
+ setSession(sess);
231
+ setStatus("Model Loaded & Ready. Press the microphone to speak.");
232
+ } catch (err) {
233
+ console.error(err);
234
+ setErrorMessage(`Initialization Error: ${err.message}`);
235
+ setStatus("Failed to load model.");
236
+ } finally {
237
+ setIsLoading(false);
238
+ }
239
+ };
240
+
241
+ const startRecording = async () => {
242
+ try {
243
+ const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
244
+ const mediaRecorder = new MediaRecorder(stream);
245
+ audioChunksRef.current = [];
246
+
247
+ mediaRecorder.ondataavailable = (e) => {
248
+ if (e.data.size > 0) audioChunksRef.current.push(e.data);
249
+ };
250
+
251
+ mediaRecorder.onstop = processAndInfer;
252
+ mediaRecorderRef.current = mediaRecorder;
253
+ mediaRecorder.start();
254
+
255
+ setIsRecording(true);
256
+ setStatus("Recording... Speak in Malayalam.");
257
+ setErrorMessage("");
258
+ } catch (err) {
259
+ console.error(err);
260
+ setErrorMessage("Microphone permission denied or an error occurred.");
261
+ }
262
+ };
263
+
264
+ const stopRecording = () => {
265
+ if (mediaRecorderRef.current && isRecording) {
266
+ mediaRecorderRef.current.stop();
267
+ setIsRecording(false);
268
+ // Stops all microphone tracks
269
+ mediaRecorderRef.current.stream.getTracks().forEach(track => track.stop());
270
+ }
271
+ };
272
+
273
+ const processAndInfer = async () => {
274
+ setStatus("Processing Audio...");
275
+ try {
276
+ // Decode audio and resample to 16kHz Mono Float32
277
+ const blob = new Blob(audioChunksRef.current);
278
+ const arrayBuffer = await blob.arrayBuffer();
279
+ const audioCtx = new (window.AudioContext || window.webkitAudioContext)({ sampleRate: 16000 });
280
+ const decodedData = await audioCtx.decodeAudioData(arrayBuffer);
281
+ const float32Data = decodedData.getChannelData(0); // Mono channel
282
+
283
+ setStatus("Running Inference...");
284
+ await runInference(float32Data);
285
+ } catch (err) {
286
+ console.error(err);
287
+ setErrorMessage(`Audio Processing Error: ${err.message}`);
288
+ setStatus("Ready.");
289
+ }
290
+ };
291
+
292
+ const handleFileUpload = async (e) => {
293
+ const file = e.target.files[0];
294
+ if (!file) return;
295
+
296
+ setStatus("Processing Uploaded Audio...");
297
+ setErrorMessage("");
298
+ setIsLoading(true);
299
+
300
+ try {
301
+ const arrayBuffer = await file.arrayBuffer();
302
+ const audioCtx = new (window.AudioContext || window.webkitAudioContext)({ sampleRate: 16000 });
303
+ const decodedData = await audioCtx.decodeAudioData(arrayBuffer);
304
+ const float32Data = decodedData.getChannelData(0); // Mono channel
305
+
306
+ setStatus("Running Inference on File...");
307
+ await runInference(float32Data);
308
+ } catch (err) {
309
+ console.error(err);
310
+ setErrorMessage(`Audio Upload Error: ${err.message}`);
311
+ setStatus("Ready.");
312
+ } finally {
313
+ setIsLoading(false);
314
+ e.target.value = null; // Reset input to allow re-uploading the same file
315
+ }
316
+ };
317
+
318
+ const runInference = async (float32Data) => {
319
+ try {
320
+ const inputNames = session.inputNames;
321
+ const feeds = {};
322
+
323
+ // Attempt 1: Raw Waveform tensor
324
+ if (inputNames.includes('audio_signal')) {
325
+ feeds['audio_signal'] = new window.ort.Tensor('float32', float32Data, [1, float32Data.length]);
326
+ } else {
327
+ throw new Error(`The model expects inputs: ${inputNames.join(', ')}.`);
328
+ }
329
+
330
+ if (inputNames.includes('length')) {
331
+ feeds['length'] = new window.ort.Tensor('int64', new BigInt64Array([BigInt(float32Data.length)]), [1]);
332
+ }
333
+
334
+ let results;
335
+ try {
336
+ results = await session.run(feeds);
337
+ } catch (runError) {
338
+ // Attempt 2: Feature-extracted Log-Mel Spectrogram (Catches "Expected: 3" or "Expected: 80" errors)
339
+ if (runError.message && (runError.message.includes("Expected: 3") || runError.message.includes("Expected: 80"))) {
340
+ console.warn("Raw audio tensor failed. Model likely lacks a feature extractor. Computing 80-bin Log-Mel Spectrogram natively...");
341
+
342
+ const { melSpec, numFrames } = computeLogMelSpectrogram(float32Data);
343
+ if (numFrames <= 0) throw new Error("Audio sample is too short to process.");
344
+
345
+ feeds['audio_signal'] = new window.ort.Tensor('float32', melSpec, [1, 80, numFrames]);
346
+
347
+ if (inputNames.includes('length')) {
348
+ feeds['length'] = new window.ort.Tensor('int64', new BigInt64Array([BigInt(numFrames)]), [1]);
349
+ }
350
+
351
+ results = await session.run(feeds);
352
+ } else {
353
+ throw runError; // Unhandled error
354
+ }
355
+ }
356
+
357
+ // Assume the first output contains the logprobs/logits
358
+ const outputName = session.outputNames[0];
359
+ const outputTensor = results[outputName];
360
+ const logits = outputTensor.data;
361
+ let dims = outputTensor.dims;
362
+
363
+ // Standardize dims to [batch, time, vocab]
364
+ if (dims.length === 2) dims = [1, dims[0], dims[1]];
365
+
366
+ const text = decodeCTC(logits, dims, vocab);
367
+ setTranscript(prev => prev + (prev ? " " : "") + text);
368
+ setStatus("Transcription Complete. Ready for next.");
369
+ } catch (err) {
370
+ console.error(err);
371
+ setErrorMessage(`Inference Error: ${err.message}`);
372
+ setStatus("Ready.");
373
+ }
374
+ };
375
+
376
+ const decodeCTC = (logits, dims, vocabList) => {
377
+ const T = dims[1]; // Time frames
378
+ const V = dims[2]; // Vocab size emitted by model
379
+ let result = [];
380
+ let prev_id = -1;
381
+
382
+ // In typical NeMo models, the blank token is the last index
383
+ const blankId = V - 1;
384
+
385
+ for (let t = 0; t < T; t++) {
386
+ let max_val = -Infinity;
387
+ let max_id = -1;
388
+
389
+ for (let v = 0; v < V; v++) {
390
+ const val = logits[t * V + v];
391
+ if (val > max_val) {
392
+ max_val = val;
393
+ max_id = v;
394
+ }
395
+ }
396
+
397
+ if (max_id !== prev_id && max_id !== blankId) {
398
+ let token = "";
399
+ if (max_id < vocabList.length) {
400
+ token = vocabList[max_id];
401
+ }
402
+
403
+ // Ignore standard special tokens
404
+ if (token && token !== '<blank>' && token !== '<pad>' && token !== '<s>' && token !== '</s>') {
405
+ result.push(token);
406
+ }
407
+ }
408
+ prev_id = max_id;
409
+ }
410
+
411
+ // Clean up SentencePiece artifacts (e.g., '_' or ' ') and Malayalam spacing character
412
+ let decodedText = result.join('');
413
+ decodedText = decodedText.replace(/ /g, ' ').replace(/_/g, ' ').replace(/▁/g, ' ').trim();
414
+ return decodedText.replace(/\s+/g, ' '); // Remove redundant spaces
415
+ };
416
+
417
+ const handleCopy = () => {
418
+ const textArea = document.createElement("textarea");
419
+ textArea.value = transcript;
420
+ document.body.appendChild(textArea);
421
+ textArea.select();
422
+ try {
423
+ document.execCommand('copy');
424
+ setCopiedMessage("Copied to clipboard!");
425
+ setTimeout(() => setCopiedMessage(""), 2000);
426
+ } catch (err) {
427
+ setCopiedMessage("Failed to copy");
428
+ setTimeout(() => setCopiedMessage(""), 2000);
429
+ }
430
+ document.body.removeChild(textArea);
431
+ };
432
+
433
+ return (
434
+ <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">
435
+ <div className="max-w-3xl mx-auto space-y-6">
436
+
437
+ {/* Header */}
438
+ <div className="text-center space-y-2">
439
+ <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">
440
+ Malayalam Speech-to-Text
441
+ </h1>
442
+ <p className="text-neutral-500 dark:text-neutral-400 text-sm sm:text-base">
443
+ Powered by IndicConformer-120M & ONNX Runtime Web
444
+ </p>
445
+ </div>
446
+
447
+ {/* Main Interface Card */}
448
+ <div className="bg-white dark:bg-neutral-800 rounded-2xl shadow-xl border border-neutral-100 dark:border-neutral-700 overflow-hidden">
449
+
450
+ {/* Status Bar */}
451
+ <div className="bg-neutral-100 dark:bg-neutral-700/50 px-6 py-3 flex items-center justify-between">
452
+ <div className="flex items-center space-x-2 text-sm font-medium text-neutral-600 dark:text-neutral-300">
453
+ {isLoading ? (
454
+ <Loader2 size={16} className="animate-spin text-blue-500" />
455
+ ) : session ? (
456
+ <CheckCircle2 size={16} className="text-emerald-500" />
457
+ ) : (
458
+ <AlertCircle size={16} className="text-amber-500" />
459
+ )}
460
+ <span>{status}</span>
461
+ </div>
462
+
463
+ <button
464
+ onClick={() => setShowSettings(!showSettings)}
465
+ className="text-neutral-400 hover:text-neutral-600 dark:hover:text-neutral-200 transition-colors"
466
+ title="Settings"
467
+ >
468
+ <Settings size={18} />
469
+ </button>
470
+ </div>
471
+
472
+ {/* Settings Panel */}
473
+ {showSettings && (
474
+ <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">
475
+ <h3 className="text-sm font-semibold uppercase tracking-wider text-neutral-500 dark:text-neutral-400">
476
+ Model Configuration
477
+ </h3>
478
+
479
+ <div className="flex items-center space-x-4 mb-2">
480
+ <label className="flex items-center space-x-2 text-sm text-neutral-700 dark:text-neutral-300">
481
+ <input type="radio" value="url" checked={modelSource === 'url'} onChange={() => setModelSource('url')} className="text-blue-600 focus:ring-blue-500" />
482
+ <span>Use URLs</span>
483
+ </label>
484
+ <label className="flex items-center space-x-2 text-sm text-neutral-700 dark:text-neutral-300">
485
+ <input type="radio" value="local" checked={modelSource === 'local'} onChange={() => setModelSource('local')} className="text-blue-600 focus:ring-blue-500" />
486
+ <span>Local Files</span>
487
+ </label>
488
+ </div>
489
+
490
+ <div className="space-y-3 text-sm">
491
+ {modelSource === 'url' ? (
492
+ <>
493
+ <div>
494
+ <label className="block text-neutral-700 dark:text-neutral-300 mb-1 font-medium">ONNX Model URL</label>
495
+ <input
496
+ type="text"
497
+ value={modelUrl}
498
+ onChange={e => setModelUrl(e.target.value)}
499
+ 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"
500
+ />
501
+ </div>
502
+ <div>
503
+ <label className="block text-neutral-700 dark:text-neutral-300 mb-1 font-medium">Vocabulary URL (.txt / .json)</label>
504
+ <input
505
+ type="text"
506
+ value={vocabUrl}
507
+ onChange={e => setVocabUrl(e.target.value)}
508
+ 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"
509
+ />
510
+ </div>
511
+ </>
512
+ ) : (
513
+ <>
514
+ <div>
515
+ <label className="block text-neutral-700 dark:text-neutral-300 mb-1 font-medium">ONNX Model File (.onnx)</label>
516
+ <input
517
+ type="file"
518
+ accept=".onnx"
519
+ onChange={e => setLocalModel(e.target.files[0])}
520
+ className="w-full text-neutral-700 dark:text-neutral-300 file:mr-4 file:py-2 file:px-4 file:rounded-lg file:border-0 file:text-sm file:font-semibold file:bg-blue-50 file:text-blue-700 hover:file:bg-blue-100 dark:file:bg-blue-900/30 dark:file:text-blue-400"
521
+ />
522
+ </div>
523
+ <div>
524
+ <label className="block text-neutral-700 dark:text-neutral-300 mb-1 font-medium">Vocabulary File (.json / .txt)</label>
525
+ <input
526
+ type="file"
527
+ accept=".json,.txt"
528
+ onChange={e => setLocalVocab(e.target.files[0])}
529
+ className="w-full text-neutral-700 dark:text-neutral-300 file:mr-4 file:py-2 file:px-4 file:rounded-lg file:border-0 file:text-sm file:font-semibold file:bg-blue-50 file:text-blue-700 hover:file:bg-blue-100 dark:file:bg-blue-900/30 dark:file:text-blue-400"
530
+ />
531
+ </div>
532
+ </>
533
+ )}
534
+
535
+ <div className="flex items-center justify-between pt-2">
536
+ <span className="text-xs text-neutral-500 dark:text-neutral-400 flex items-center">
537
+ <AlertCircle size={12} className="inline mr-1" /> Re-initialize model after changing configuration.
538
+ </span>
539
+ <button
540
+ onClick={initModel}
541
+ disabled={isLoading}
542
+ 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"
543
+ >
544
+ Load / Refresh Model
545
+ </button>
546
+ </div>
547
+ </div>
548
+ </div>
549
+ )}
550
+
551
+ {/* Action Area */}
552
+ <div className="p-8 flex flex-col items-center justify-center space-y-6">
553
+
554
+ {/* Error Message Display */}
555
+ {errorMessage && (
556
+ <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">
557
+ <AlertCircle size={18} className="mr-2 flex-shrink-0 mt-0.5" />
558
+ <span>{errorMessage}</span>
559
+ </div>
560
+ )}
561
+
562
+ {!session && !isLoading && !errorMessage && (
563
+ <button
564
+ onClick={initModel}
565
+ 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"
566
+ >
567
+ Initialize Model
568
+ </button>
569
+ )}
570
+
571
+ {/* Input Controls */}
572
+ <div className="flex items-center space-x-6">
573
+ {/* Microphone Button */}
574
+ <button
575
+ onClick={isRecording ? stopRecording : startRecording}
576
+ disabled={!session || isLoading}
577
+ className={`p-8 rounded-full transition-all duration-300 group ${
578
+ !session || isLoading
579
+ ? 'bg-neutral-200 dark:bg-neutral-800 text-neutral-400 dark:text-neutral-600 cursor-not-allowed'
580
+ : isRecording
581
+ ? 'bg-red-500 hover:bg-red-600 animate-pulse text-white shadow-[0_0_40px_rgba(239,68,68,0.5)]'
582
+ : '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'
583
+ }`}
584
+ title="Record Audio"
585
+ >
586
+ {isRecording ? <Square size={40} className="fill-current" /> : <Mic size={40} />}
587
+ </button>
588
+
589
+ {/* Upload Button */}
590
+ <button
591
+ onClick={() => fileInputRef.current?.click()}
592
+ disabled={!session || isLoading || isRecording}
593
+ className={`p-8 rounded-full transition-all duration-300 group ${
594
+ !session || isLoading || isRecording
595
+ ? 'bg-neutral-200 dark:bg-neutral-800 text-neutral-400 dark:text-neutral-600 cursor-not-allowed'
596
+ : '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'
597
+ }`}
598
+ title="Upload Audio File"
599
+ >
600
+ <Upload size={40} />
601
+ </button>
602
+ <input
603
+ type="file"
604
+ ref={fileInputRef}
605
+ onChange={handleFileUpload}
606
+ accept="audio/*"
607
+ className="hidden"
608
+ />
609
+ </div>
610
+
611
+ <p className="text-neutral-500 dark:text-neutral-400 font-medium text-center">
612
+ {isRecording ? "Tap to Stop & Transcribe" : (session ? "Tap Mic to Record or Upload an Audio File" : "Model required to process audio")}
613
+ </p>
614
+ </div>
615
+
616
+ {/* Transcript Area */}
617
+ <div className="border-t border-neutral-100 dark:border-neutral-700 p-6 bg-neutral-50 dark:bg-neutral-800/50">
618
+ <div className="flex items-center justify-between mb-3">
619
+ <h3 className="font-semibold text-neutral-700 dark:text-neutral-300">Transcript</h3>
620
+
621
+ {/* Copy Tools */}
622
+ <div className="flex items-center space-x-3">
623
+ {copiedMessage && <span className="text-xs text-green-500 font-medium animate-fade-in">{copiedMessage}</span>}
624
+ <button
625
+ onClick={handleCopy}
626
+ disabled={!transcript}
627
+ 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"
628
+ title="Copy Transcript"
629
+ >
630
+ <Copy size={18} />
631
+ </button>
632
+ <button
633
+ onClick={() => setTranscript("")}
634
+ disabled={!transcript}
635
+ 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"
636
+ >
637
+ Clear
638
+ </button>
639
+ </div>
640
+ </div>
641
+
642
+ <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 break-words">
643
+ {transcript || <span className="text-neutral-400 dark:text-neutral-600 italic">Transcription will appear here...</span>}
644
+ </div>
645
+ </div>
646
+
647
+ </div>
648
+ </div>
649
+ </div>
650
+ );
651
+ }