SemantIQ commited on
Commit
f0ed4e5
·
verified ·
1 Parent(s): 42307aa

Upload static/app.js with huggingface_hub

Browse files
Files changed (1) hide show
  1. static/app.js +68 -0
static/app.js CHANGED
@@ -21,6 +21,74 @@ wordInput.addEventListener('keydown', (e) => {
21
  if (e.key === 'Enter') lookupWord();
22
  });
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  function selectLanguage(btn) {
25
  document.querySelectorAll('.lang-btn').forEach(b => b.classList.remove('active'));
26
  btn.classList.add('active');
 
21
  if (e.key === 'Enter') lookupWord();
22
  });
23
 
24
+ /* ========== Voice Search (Web Speech API) ========== */
25
+
26
+ const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
27
+ let recognition = null;
28
+ let isListening = false;
29
+
30
+ const voiceLangMap = {
31
+ english: 'en-US',
32
+ telugu: 'te-IN',
33
+ hindi: 'hi-IN',
34
+ sanskrit: 'sa-IN',
35
+ };
36
+
37
+ function toggleVoice() {
38
+ if (!SpeechRecognition) {
39
+ alert('Voice search is not supported in this browser. Please use Chrome or Edge.');
40
+ return;
41
+ }
42
+
43
+ if (isListening) {
44
+ recognition.stop();
45
+ return;
46
+ }
47
+
48
+ recognition = new SpeechRecognition();
49
+ recognition.lang = voiceLangMap[selectedLanguage] || 'en-US';
50
+ recognition.interimResults = true;
51
+ recognition.maxAlternatives = 1;
52
+ recognition.continuous = false;
53
+
54
+ const micBtn = document.getElementById('micBtn');
55
+
56
+ recognition.onstart = () => {
57
+ isListening = true;
58
+ micBtn.classList.add('listening');
59
+ wordInput.placeholder = 'Listening...';
60
+ };
61
+
62
+ recognition.onresult = (event) => {
63
+ const transcript = event.results[0][0].transcript;
64
+ wordInput.value = transcript;
65
+ if (event.results[0].isFinal) {
66
+ lookupWord();
67
+ }
68
+ };
69
+
70
+ recognition.onerror = (event) => {
71
+ console.error('Speech error:', event.error);
72
+ if (event.error === 'not-allowed') {
73
+ alert('Microphone access denied. Please allow microphone permission.');
74
+ }
75
+ };
76
+
77
+ recognition.onend = () => {
78
+ isListening = false;
79
+ micBtn.classList.remove('listening');
80
+ wordInput.placeholder = placeholders[selectedLanguage] || 'Type a word...';
81
+ };
82
+
83
+ recognition.start();
84
+ }
85
+
86
+ // Hide mic button if Speech API not supported
87
+ if (!SpeechRecognition) {
88
+ const micBtn = document.getElementById('micBtn');
89
+ if (micBtn) micBtn.style.display = 'none';
90
+ }
91
+
92
  function selectLanguage(btn) {
93
  document.querySelectorAll('.lang-btn').forEach(b => b.classList.remove('active'));
94
  btn.classList.add('active');