File size: 5,345 Bytes
922ec85 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | // ============================================================
// Configuration
// ============================================================
const API_BASE_URL = 'http://localhost:8000';
const ASK_ENDPOINT = '/ask';
// ============================================================
// DOM Elements
// ============================================================
const questionInput = document.getElementById('questionInput');
const askBtn = document.getElementById('askBtn');
const btnText = document.getElementById('btnText');
const btnLoader = document.getElementById('btnLoader');
const resultsSection = document.getElementById('resultsSection');
const errorSection = document.getElementById('errorSection');
const answerText = document.getElementById('answerText');
const sourcesText = document.getElementById('sourcesText');
const languageDetected = document.getElementById('languageDetected');
const errorText = document.getElementById('errorText');
// ============================================================
// Event Listeners
// ============================================================
askBtn.addEventListener('click', askQuestion);
questionInput.addEventListener('keydown', (e) => {
if (e.ctrlKey && e.key === 'Enter') {
askQuestion();
}
});
// ============================================================
// Main Functions
// ============================================================
async function askQuestion() {
const question = questionInput.value.trim();
if (!question) {
showError('Please enter a question');
return;
}
// Hide previous results and errors
resultsSection.style.display = 'none';
errorSection.style.display = 'none';
// Show loading state
setLoading(true);
try {
// Make API request
const response = await fetch(API_BASE_URL + ASK_ENDPOINT, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
question: question
})
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || 'Failed to get answer');
}
const data = await response.json();
// Display results
displayResults(data);
} catch (error) {
console.error('Error:', error);
showError(`Error: ${error.message}`);
} finally {
setLoading(false);
}
}
function displayResults(data) {
// Set answer
answerText.innerHTML = formatText(data.answer);
// Set sources
sourcesText.innerHTML = formatText(data.sources);
// Set language
const langMap = {
'en': 'π¬π§ English',
'ar': 'πΈπ¦ Arabic',
'ur': 'π΅π° Urdu',
'fr': 'π«π· French',
'de': 'π©πͺ German'
};
languageDetected.textContent = langMap[data.language] || data.language.toUpperCase();
// Show results section
resultsSection.style.display = 'block';
// Scroll to results
resultsSection.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
function showError(message) {
errorText.textContent = message;
errorSection.style.display = 'block';
errorSection.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
function setLoading(loading) {
askBtn.disabled = loading;
if (loading) {
btnText.style.display = 'none';
btnLoader.style.display = 'inline-block';
} else {
btnText.style.display = 'inline';
btnLoader.style.display = 'none';
}
}
function setQuestion(text) {
questionInput.value = text;
questionInput.focus();
}
function formatText(text) {
if (!text) return '<p>No content available</p>';
// Escape HTML
text = text
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>');
// Convert newlines to line breaks
text = text.replace(/\n/g, '<br>');
// Convert links
text = text.replace(
/\b(https?:\/\/[^\s]+)/g,
'<a href="$1" target="_blank" rel="noopener noreferrer">$1</a>'
);
// Convert bold markers
text = text.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
text = text.replace(/__(.*?)__/g, '<strong>$1</strong>');
// Convert italic markers
text = text.replace(/\*(.*?)\*/g, '<em>$1</em>');
text = text.replace(/_(.*?)_/g, '<em>$1</em>');
return `<p>${text}</p>`;
}
// ============================================================
// Initialization
// ============================================================
console.log('Islamic QA - Static Frontend Loaded');
console.log(`API URL: ${API_BASE_URL}`);
// Check API connection on load
window.addEventListener('load', async () => {
try {
const response = await fetch(API_BASE_URL + '/health');
if (response.ok) {
console.log('β API connection successful');
}
} catch (error) {
console.warn('β API connection failed:', error.message);
console.warn('Make sure to run: python main.py');
}
});
|