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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
let currentDomain = 'general';
|
| 2 |
+
let currentLang = 'en';
|
| 3 |
+
let currentAnswer = '';
|
| 4 |
+
let mediaRecorder = null;
|
| 5 |
+
let audioChunks = [];
|
| 6 |
+
|
| 7 |
+
function setDomain(domain) {
|
| 8 |
+
currentDomain = domain;
|
| 9 |
+
document.getElementById('phone-home').style.display = 'none';
|
| 10 |
+
document.getElementById('phone-detail').style.display = 'block';
|
| 11 |
+
|
| 12 |
+
const domainNames = {
|
| 13 |
+
'islamic': 'Islamic law',
|
| 14 |
+
'harassment': 'Harassment law',
|
| 15 |
+
'inheritance': 'Inheritance law',
|
| 16 |
+
'verify': 'Law verification',
|
| 17 |
+
'general': 'General law'
|
| 18 |
+
};
|
| 19 |
+
document.getElementById('detail-domain').textContent = domainNames[domain];
|
| 20 |
+
document.getElementById('detail-question').textContent = 'Ask your question...';
|
| 21 |
+
document.getElementById('detail-answer').textContent = 'Your answer will appear here.';
|
| 22 |
+
|
| 23 |
+
// Focus search
|
| 24 |
+
document.getElementById('search-input').focus();
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
function showHome() {
|
| 28 |
+
document.getElementById('phone-home').style.display = 'block';
|
| 29 |
+
document.getElementById('phone-detail').style.display = 'none';
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
function focusSearch() {
|
| 33 |
+
document.getElementById('search-input').focus();
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
function setLang(lang, btn) {
|
| 37 |
+
currentLang = lang;
|
| 38 |
+
document.querySelectorAll('.lang-btn').forEach(b => b.classList.remove('active'));
|
| 39 |
+
btn.classList.add('active');
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
function setAnswerLang(lang, btn) {
|
| 43 |
+
// Re-generate answer in new language
|
| 44 |
+
if (currentAnswer && currentAnswer.original_query) {
|
| 45 |
+
askAPI(currentAnswer.original_query, currentDomain, lang, currentAnswer.mode || 'qa');
|
| 46 |
+
}
|
| 47 |
+
document.querySelectorAll('#phone-detail .lang-btn').forEach(b => b.classList.remove('active'));
|
| 48 |
+
btn.classList.add('active');
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
function loadRecent(question, domain) {
|
| 52 |
+
setDomain(domain);
|
| 53 |
+
document.getElementById('detail-question').textContent = question;
|
| 54 |
+
askAPI(question, domain, currentLang, 'qa');
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
function searchQuery() {
|
| 58 |
+
const query = document.getElementById('search-input').value.trim();
|
| 59 |
+
if (!query) return;
|
| 60 |
+
|
| 61 |
+
setDomain(currentDomain);
|
| 62 |
+
document.getElementById('detail-question').textContent = query;
|
| 63 |
+
askAPI(query, currentDomain, currentLang, 'qa');
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
async function askAPI(query, domain, lang, mode) {
|
| 67 |
+
showLoading(true);
|
| 68 |
+
|
| 69 |
+
try {
|
| 70 |
+
const res = await fetch('/api/ask', {
|
| 71 |
+
method: 'POST',
|
| 72 |
+
headers: {'Content-Type': 'application/json'},
|
| 73 |
+
body: JSON.stringify({query, domain, lang, mode})
|
| 74 |
+
});
|
| 75 |
+
|
| 76 |
+
const data = await res.json();
|
| 77 |
+
currentAnswer = data;
|
| 78 |
+
|
| 79 |
+
// Update UI
|
| 80 |
+
document.getElementById('detail-answer').textContent = data.plain_answer || 'No answer found.';
|
| 81 |
+
document.getElementById('detail-law').textContent = data.law_cited || 'N/A';
|
| 82 |
+
document.getElementById('detail-verdict').textContent =
|
| 83 |
+
(data.verdict === 'VERIFIED' ? '✓ Verified: ' : '⚠ ') +
|
| 84 |
+
(data.verdict || 'Unknown');
|
| 85 |
+
document.getElementById('detail-dept').textContent = data.where_to_file || 'N/A';
|
| 86 |
+
document.getElementById('detail-action').textContent = data.department || 'Consult local court.';
|
| 87 |
+
document.getElementById('detail-source').textContent =
|
| 88 |
+
data.law_cited ? `Source: ${data.law_cited.split(',')[0]}` : 'Source: Database search';
|
| 89 |
+
|
| 90 |
+
// Color code verdict
|
| 91 |
+
const verifyBox = document.getElementById('detail-verify');
|
| 92 |
+
if (data.verdict === 'FARCE') {
|
| 93 |
+
verifyBox.style.background = '#FCEBEB';
|
| 94 |
+
verifyBox.querySelector('.verify-icon').style.background = '#E24B4A';
|
| 95 |
+
verifyBox.querySelector('.verify-text').style.color = '#A32D2D';
|
| 96 |
+
} else if (data.verdict === 'NOT IN DATABASE') {
|
| 97 |
+
verifyBox.style.background = '#FFF3E0';
|
| 98 |
+
verifyBox.querySelector('.verify-icon').style.background = '#F57C00';
|
| 99 |
+
verifyBox.querySelector('.verify-text').style.color = '#E65100';
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
} catch (err) {
|
| 103 |
+
document.getElementById('detail-answer').textContent = 'Error: Could not connect to server.';
|
| 104 |
+
console.error(err);
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
showLoading(false);
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
async function startVoice() {
|
| 111 |
+
try {
|
| 112 |
+
const stream = await navigator.mediaDevices.getUserMedia({audio: true});
|
| 113 |
+
mediaRecorder = new MediaRecorder(stream);
|
| 114 |
+
audioChunks = [];
|
| 115 |
+
|
| 116 |
+
mediaRecorder.ondataavailable = e => audioChunks.push(e.data);
|
| 117 |
+
mediaRecorder.onstop = async () => {
|
| 118 |
+
const audioBlob = new Blob(audioChunks, {type: 'audio/wav'});
|
| 119 |
+
await sendVoice(audioBlob);
|
| 120 |
+
};
|
| 121 |
+
|
| 122 |
+
mediaRecorder.start();
|
| 123 |
+
alert('🎙️ Recording... Speak now! Click OK to stop.');
|
| 124 |
+
mediaRecorder.stop();
|
| 125 |
+
stream.getTracks().forEach(t => t.stop());
|
| 126 |
+
|
| 127 |
+
} catch (err) {
|
| 128 |
+
alert('Microphone access denied or not available.');
|
| 129 |
+
}
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
async function sendVoice(audioBlob) {
|
| 133 |
+
showLoading(true);
|
| 134 |
+
|
| 135 |
+
const formData = new FormData();
|
| 136 |
+
formData.append('audio', audioBlob);
|
| 137 |
+
formData.append('domain', currentDomain);
|
| 138 |
+
formData.append('lang', currentLang);
|
| 139 |
+
|
| 140 |
+
try {
|
| 141 |
+
const res = await fetch('/api/voice', {
|
| 142 |
+
method: 'POST',
|
| 143 |
+
body: formData
|
| 144 |
+
});
|
| 145 |
+
|
| 146 |
+
const data = await res.json();
|
| 147 |
+
document.getElementById('detail-question').textContent = data.transcript || 'Voice input';
|
| 148 |
+
currentAnswer = data;
|
| 149 |
+
|
| 150 |
+
document.getElementById('detail-answer').textContent = data.plain_answer || 'No answer.';
|
| 151 |
+
document.getElementById('detail-law').textContent = data.law_cited || 'N/A';
|
| 152 |
+
document.getElementById('detail-verdict').textContent = data.verdict || 'Unknown';
|
| 153 |
+
document.getElementById('detail-dept').textContent = data.where_to_file || 'N/A';
|
| 154 |
+
|
| 155 |
+
showHome();
|
| 156 |
+
document.getElementById('phone-detail').style.display = 'block';
|
| 157 |
+
|
| 158 |
+
} catch (err) {
|
| 159 |
+
alert('Error processing voice.');
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
showLoading(false);
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
async function speakAnswer() {
|
| 166 |
+
if (!currentAnswer || !currentAnswer.plain_answer) return;
|
| 167 |
+
|
| 168 |
+
const text = currentAnswer.plain_answer;
|
| 169 |
+
const lang = currentLang === 'urdu' ? 'urdu' : 'en';
|
| 170 |
+
|
| 171 |
+
try {
|
| 172 |
+
const res = await fetch(`/api/tts?text=${encodeURIComponent(text)}&lang=${lang}`);
|
| 173 |
+
const blob = await res.blob();
|
| 174 |
+
const url = URL.createObjectURL(blob);
|
| 175 |
+
|
| 176 |
+
const audio = document.getElementById('tts-audio');
|
| 177 |
+
audio.src = url;
|
| 178 |
+
audio.style.display = 'block';
|
| 179 |
+
audio.play();
|
| 180 |
+
|
| 181 |
+
} catch (err) {
|
| 182 |
+
console.error('TTS error:', err);
|
| 183 |
+
}
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
function showHistory() {
|
| 187 |
+
alert('History feature coming soon!');
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
function showLoading(show) {
|
| 191 |
+
document.getElementById('loading').classList.toggle('active', show);
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
// Clock
|
| 195 |
+
setInterval(() => {
|
| 196 |
+
const now = new Date();
|
| 197 |
+
const time = now.getHours() + ':' + String(now.getMinutes()).padStart(2, '0');
|
| 198 |
+
document.getElementById('clock').textContent = time;
|
| 199 |
+
document.getElementById('clock2').textContent = time;
|
| 200 |
+
}, 1000);
|