abdurrehmansaeed's picture
Upload 7 files
b9cb33d verified
Raw History Blame Contribute Delete
5.28 kB
document.addEventListener('DOMContentLoaded', () => {
const reviewForm = document.getElementById('review-form');
const reviewTextarea = document.getElementById('review-text');
const imageInput = document.getElementById('review-image');
const imagePreview = document.getElementById('image-preview');
const analyzeBtn = document.getElementById('analyze-btn');
const loader = document.getElementById('loader');
const resultContainer = document.getElementById('result-container');
const classificationBadge = document.getElementById('classification-badge');
const classificationText = document.getElementById('classification-text');
// const confidenceValue = document.getElementById('confidence-value');
const explanationText = document.getElementById('explanation-text');
const uploadedImageContainer = document.getElementById('uploaded-image-container');
const resultImage = document.getElementById('result-image');
// Handle image preview
imageInput.addEventListener('change', (e) => {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (event) => {
imagePreview.src = event.target.result;
imagePreview.style.display = 'block';
};
reader.readAsDataURL(file);
} else {
imagePreview.src = '';
imagePreview.style.display = 'none';
}
});
// Handle form submission
reviewForm.addEventListener('submit', async (e) => {
e.preventDefault();
// Basic validation
if (!reviewTextarea.value.trim()) {
alert('Please enter a review text to analyze.');
return;
}
// Show loader, hide results
loader.style.display = 'flex';
resultContainer.style.display = 'none';
analyzeBtn.disabled = true;
try {
// Create form data
const formData = new FormData();
formData.append('review_text', reviewTextarea.value.trim());
if (imageInput.files.length > 0) {
formData.append('review_image', imageInput.files[0]);
}
// Send the request
const response = await fetch('/analyze', {
method: 'POST',
body: formData
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || 'An error occurred during analysis');
}
const result = await response.json();
// Update the UI with the results
classificationText.textContent = result.classification;
classificationBadge.className = 'classification-badge';
classificationBadge.classList.add(result.classification.toLowerCase());
// confidenceValue.textContent = result.confidence;
explanationText.textContent = result.explanation;
// Handle image display
if (result.image) {
resultImage.src = 'data:image/jpeg;base64,' + result.image;
uploadedImageContainer.style.display = 'block';
} else {
uploadedImageContainer.style.display = 'none';
}
// Show results
resultContainer.style.display = 'block';
} catch (error) {
alert(error.message || 'An error occurred. Please try again.');
console.error('Error:', error);
} finally {
// Hide loader, re-enable button
loader.style.display = 'none';
analyzeBtn.disabled = false;
}
});
// Reset preview when form is reset
reviewForm.addEventListener('reset', () => {
imagePreview.src = '';
imagePreview.style.display = 'none';
resultContainer.style.display = 'none';
});
// Add a character counter to the textarea
reviewTextarea.addEventListener('input', function() {
const maxLength = 500; // You can adjust this
const currentLength = this.value.length;
if (this.nextElementSibling && this.nextElementSibling.classList.contains('char-counter')) {
this.nextElementSibling.textContent = `${currentLength}/${maxLength} characters`;
} else {
const counter = document.createElement('div');
counter.classList.add('char-counter');
counter.style.textAlign = 'right';
counter.style.fontSize = '0.8rem';
counter.style.color = 'var(--light-text)';
counter.style.marginTop = '0.25rem';
counter.textContent = `${currentLength}/${maxLength} characters`;
this.parentNode.insertBefore(counter, this.nextSibling);
}
if (currentLength > maxLength) {
this.nextElementSibling.style.color = 'var(--danger-color)';
} else {
this.nextElementSibling.style.color = 'var(--light-text)';
}
});
// Add animations for smooth transitions
document.querySelectorAll('.form-group, .result-container').forEach(el => {
el.style.transition = 'all 0.3s ease';
});
});