Spaces:
Runtime error
Runtime error
Download static/app.js from SemantIQ/llm-dictionary: direct link, hf CLI and curl.
- Browser
- Download file 10.8 kB
-
https://huggingface.co/spaces/SemantIQ/llm-dictionary/resolve/main/static/app.js
- Command line
-
hf download hf://spaces/SemantIQ/llm-dictionary/static/app.js
-
curl -L -o app.js https://huggingface.co/spaces/SemantIQ/llm-dictionary/resolve/main/static/app.js
10.8 kB
| const wordInput = document.getElementById('wordInput'); | |
| const loading = document.getElementById('loading'); | |
| const errorDiv = document.getElementById('error'); | |
| const resultDiv = document.getElementById('result'); | |
| const errorResult = document.getElementById('errorResult'); | |
| let selectedLanguage = 'english'; | |
| const placeholders = { | |
| english: 'Type an English word...', | |
| telugu: 'తెలుగు పదం టైప్ చేయండి...', | |
| hindi: 'हिंदी शब्द टाइप करें...', | |
| sanskrit: 'संस्कृत शब्दं लिखतु...', | |
| }; | |
| // Load saved API key | |
| const savedKey = localStorage.getItem('groq_api_key') || ''; | |
| document.getElementById('apiKeyInput').value = savedKey; | |
| wordInput.addEventListener('keydown', (e) => { | |
| if (e.key === 'Enter') lookupWord(); | |
| }); | |
| /* ========== Voice Search (Web Speech API) ========== */ | |
| const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition; | |
| let recognition = null; | |
| let isListening = false; | |
| const voiceLangMap = { | |
| english: 'en-US', | |
| telugu: 'te-IN', | |
| hindi: 'hi-IN', | |
| sanskrit: 'sa-IN', | |
| }; | |
| function toggleVoice() { | |
| if (!SpeechRecognition) { | |
| alert('Voice search is not supported in this browser. Please use Chrome or Edge.'); | |
| return; | |
| } | |
| if (isListening) { | |
| recognition.stop(); | |
| return; | |
| } | |
| recognition = new SpeechRecognition(); | |
| recognition.lang = voiceLangMap[selectedLanguage] || 'en-US'; | |
| recognition.interimResults = true; | |
| recognition.maxAlternatives = 1; | |
| recognition.continuous = false; | |
| const micBtn = document.getElementById('micBtn'); | |
| recognition.onstart = () => { | |
| isListening = true; | |
| micBtn.classList.add('listening'); | |
| wordInput.placeholder = 'Listening...'; | |
| }; | |
| recognition.onresult = (event) => { | |
| const transcript = event.results[0][0].transcript; | |
| wordInput.value = transcript; | |
| if (event.results[0].isFinal) { | |
| lookupWord(); | |
| } | |
| }; | |
| recognition.onerror = (event) => { | |
| console.error('Speech error:', event.error); | |
| if (event.error === 'not-allowed') { | |
| alert('Microphone access denied. Please allow microphone permission.'); | |
| } | |
| }; | |
| recognition.onend = () => { | |
| isListening = false; | |
| micBtn.classList.remove('listening'); | |
| wordInput.placeholder = placeholders[selectedLanguage] || 'Type a word...'; | |
| }; | |
| recognition.start(); | |
| } | |
| // Hide mic button if Speech API not supported | |
| if (!SpeechRecognition) { | |
| const micBtn = document.getElementById('micBtn'); | |
| if (micBtn) micBtn.style.display = 'none'; | |
| } | |
| function selectLanguage(btn) { | |
| document.querySelectorAll('.lang-btn').forEach(b => b.classList.remove('active')); | |
| btn.classList.add('active'); | |
| selectedLanguage = btn.dataset.lang; | |
| wordInput.placeholder = placeholders[selectedLanguage] || 'Type a word...'; | |
| wordInput.focus(); | |
| } | |
| function toggleSettings() { | |
| const panel = document.getElementById('settingsPanel'); | |
| panel.style.display = panel.style.display === 'none' ? 'block' : 'none'; | |
| } | |
| function saveApiKey() { | |
| const key = document.getElementById('apiKeyInput').value.trim(); | |
| localStorage.setItem('groq_api_key', key); | |
| toggleSettings(); | |
| } | |
| async function lookupWord() { | |
| const word = wordInput.value.trim(); | |
| if (!word) return; | |
| // Reset UI | |
| loading.style.display = 'flex'; | |
| errorDiv.style.display = 'none'; | |
| resultDiv.style.display = 'none'; | |
| errorResult.style.display = 'none'; | |
| const apiKey = localStorage.getItem('groq_api_key') || ''; | |
| try { | |
| const resp = await fetch('/api/lookup', { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify({ word, language: selectedLanguage, api_key: apiKey }), | |
| }); | |
| loading.style.display = 'none'; | |
| if (!resp.ok) { | |
| const err = await resp.json(); | |
| errorDiv.textContent = err.detail || 'Something went wrong.'; | |
| errorDiv.style.display = 'block'; | |
| return; | |
| } | |
| const data = await resp.json(); | |
| renderResult(data.result); | |
| } catch (e) { | |
| loading.style.display = 'none'; | |
| errorDiv.textContent = 'Network error. Is the server running?'; | |
| errorDiv.style.display = 'block'; | |
| } | |
| } | |
| function renderResult(r) { | |
| // Check if LLM returned an error | |
| if (r.error) { | |
| document.getElementById('resError').textContent = r.error; | |
| errorResult.style.display = 'block'; | |
| return; | |
| } | |
| // Word + Phonetic | |
| document.getElementById('resWord').textContent = r.word || ''; | |
| document.getElementById('resPhonetic').textContent = r.phonetic || ''; | |
| // Transliteration (for non-English) | |
| const transEl = document.getElementById('resTransliteration'); | |
| if (r.transliteration) { | |
| transEl.textContent = r.transliteration; | |
| transEl.style.display = 'block'; | |
| } else { | |
| transEl.style.display = 'none'; | |
| } | |
| // Language badge | |
| document.getElementById('resLanguageBadge').textContent = r.language || selectedLanguage; | |
| // Parts of speech tags | |
| const posContainer = document.getElementById('resPOS'); | |
| posContainer.innerHTML = ''; | |
| (r.part_of_speech || []).forEach(pos => { | |
| const span = document.createElement('span'); | |
| span.className = 'pos-tag'; | |
| span.textContent = pos; | |
| posContainer.appendChild(span); | |
| }); | |
| // Definitions | |
| const defsContainer = document.getElementById('resDefinitions'); | |
| defsContainer.innerHTML = ''; | |
| (r.definitions || []).forEach(d => { | |
| const item = document.createElement('div'); | |
| item.className = 'definition-item'; | |
| item.innerHTML = ` | |
| <span class="def-pos">${escapeHtml(d.pos || '')}</span> | |
| <p class="def-meaning">${escapeHtml(d.meaning || '')}</p> | |
| ${d.example ? `<p class="def-example">${escapeHtml(d.example)}</p>` : ''} | |
| ${d.example_translation ? `<p class="def-example-translation">${escapeHtml(d.example_translation)}</p>` : ''} | |
| `; | |
| defsContainer.appendChild(item); | |
| }); | |
| // Synonyms | |
| renderTags('resSynonyms', r.synonyms || []); | |
| document.getElementById('synonymsSection').style.display = (r.synonyms && r.synonyms.length) ? '' : 'none'; | |
| // Antonyms | |
| renderTags('resAntonyms', r.antonyms || []); | |
| document.getElementById('antonymsSection').style.display = (r.antonyms && r.antonyms.length) ? '' : 'none'; | |
| // Etymology | |
| document.getElementById('resEtymology').textContent = r.etymology || ''; | |
| document.getElementById('etymologySection').style.display = r.etymology ? '' : 'none'; | |
| // Usage notes | |
| document.getElementById('resUsage').textContent = r.usage_notes || ''; | |
| document.getElementById('usageSection').style.display = r.usage_notes ? '' : 'none'; | |
| resultDiv.style.display = 'block'; | |
| } | |
| function renderTags(containerId, words) { | |
| const container = document.getElementById(containerId); | |
| container.innerHTML = ''; | |
| words.forEach(w => { | |
| const span = document.createElement('span'); | |
| span.className = 'tag'; | |
| span.textContent = w; | |
| span.onclick = () => { | |
| wordInput.value = w; | |
| lookupWord(); | |
| }; | |
| container.appendChild(span); | |
| }); | |
| } | |
| function escapeHtml(text) { | |
| const div = document.createElement('div'); | |
| div.appendChild(document.createTextNode(text)); | |
| return div.innerHTML; | |
| } | |
| /* ========== Community: Like & Comments ========== */ | |
| function getSessionId() { | |
| let id = localStorage.getItem('dict_session_id'); | |
| if (!id) { | |
| id = crypto.randomUUID(); | |
| localStorage.setItem('dict_session_id', id); | |
| } | |
| return id; | |
| } | |
| async function loadCommunity() { | |
| try { | |
| const sid = getSessionId(); | |
| const resp = await fetch(`/api/community?session_id=${encodeURIComponent(sid)}`); | |
| if (!resp.ok) return; | |
| const data = await resp.json(); | |
| document.getElementById('likeCount').textContent = data.likes || 0; | |
| const count = data.comments ? data.comments.length : 0; | |
| document.getElementById('commentCount').textContent = `${count} Comment${count !== 1 ? 's' : ''}`; | |
| const btn = document.getElementById('likeBtn'); | |
| btn.classList.toggle('liked', !!data.liked); | |
| document.getElementById('likeIcon').textContent = data.liked ? '❤️' : '🤍'; | |
| renderComments(data.comments || []); | |
| } catch (_) {} | |
| } | |
| async function toggleLike() { | |
| const btn = document.getElementById('likeBtn'); | |
| const isLiked = btn.classList.contains('liked'); | |
| try { | |
| const resp = await fetch('/api/like', { | |
| method: isLiked ? 'DELETE' : 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify({ session_id: getSessionId() }), | |
| }); | |
| if (resp.ok) loadCommunity(); | |
| } catch (_) {} | |
| } | |
| function toggleComments() { | |
| const panel = document.getElementById('commentsPanel'); | |
| panel.style.display = panel.style.display === 'none' ? 'block' : 'none'; | |
| } | |
| async function submitComment() { | |
| const nameEl = document.getElementById('commentName'); | |
| const textEl = document.getElementById('commentText'); | |
| const name = nameEl.value.trim(); | |
| const text = textEl.value.trim(); | |
| if (!text) return; | |
| try { | |
| const resp = await fetch('/api/comment', { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify({ name: name || 'Anonymous', text }), | |
| }); | |
| if (resp.ok) { | |
| textEl.value = ''; | |
| loadCommunity(); | |
| } | |
| } catch (_) {} | |
| } | |
| function renderComments(comments) { | |
| const list = document.getElementById('commentsList'); | |
| if (!comments.length) { | |
| list.innerHTML = '<div class="no-comments">No comments yet. Be the first!</div>'; | |
| return; | |
| } | |
| list.innerHTML = comments.map(c => ` | |
| <div class="comment-item"> | |
| <div class="comment-meta"> | |
| <span class="comment-name">${escapeHtml(c.name)}</span> | |
| <span class="comment-time">${escapeHtml(c.time)}</span> | |
| </div> | |
| <div class="comment-text">${escapeHtml(c.text)}</div> | |
| </div> | |
| `).join(''); | |
| } | |
| // Load community data on page load | |
| loadCommunity(); | |