File size: 5,277 Bytes
b9cb33d | 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 | 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';
});
});
|