File size: 7,988 Bytes
b9cb33d
 
 
 
 
405a3f0
 
 
 
 
b9cb33d
 
405a3f0
b9cb33d
405a3f0
b9cb33d
 
405a3f0
 
 
 
 
 
 
b9cb33d
 
 
405a3f0
 
b9cb33d
405a3f0
 
 
b9cb33d
405a3f0
 
 
 
b9cb33d
 
 
405a3f0
 
 
 
 
b9cb33d
 
405a3f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9cb33d
 
 
405a3f0
 
 
b9cb33d
 
 
405a3f0
 
b9cb33d
405a3f0
b9cb33d
 
 
 
405a3f0
b9cb33d
 
 
 
 
405a3f0
b9cb33d
 
 
 
 
 
 
405a3f0
b9cb33d
 
405a3f0
 
 
 
b9cb33d
405a3f0
 
 
c26f85e
 
 
 
405a3f0
 
 
 
 
 
 
 
b9cb33d
405a3f0
 
 
b9cb33d
 
 
 
 
405a3f0
 
b9cb33d
 
405a3f0
 
 
b9cb33d
 
 
 
 
 
405a3f0
b9cb33d
405a3f0
b9cb33d
 
405a3f0
 
 
b9cb33d
 
405a3f0
 
b9cb33d
405a3f0
b9cb33d
 
405a3f0
 
 
 
b9cb33d
405a3f0
b9cb33d
 
 
405a3f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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 previewWrapper = document.getElementById('preview-wrapper');
    const uploadPrompt = document.getElementById('upload-prompt');
    const removeImgBtn = document.getElementById('remove-img-btn');
    const dropZone = document.getElementById('drop-zone');
    
    const analyzeBtn = document.getElementById('analyze-btn');
    const loader = document.getElementById('loader');
    const resultsEmpty = document.getElementById('results-empty');
    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 metricSentiment = document.getElementById('metric-sentiment');
    const metricLength = document.getElementById('metric-length');
    const metricCapital = document.getElementById('metric-capital');
    const metricPunctuation = document.getElementById('metric-punctuation');
    
    const explanationText = document.getElementById('explanation-text');
    const uploadedImageContainer = document.getElementById('uploaded-image-container');
    const resultImage = document.getElementById('result-image');
    
    const btnCopyExplanation = document.getElementById('btn-copy-explanation');

    // 1. File Upload Preview Handler
    const handleFileSelect = (file) => {
        if (file && file.type.startsWith('image/')) {
            const reader = new FileReader();
            reader.onload = (e) => {
                imagePreview.src = e.target.result;
                previewWrapper.style.display = 'block';
                uploadPrompt.style.display = 'none';
            };
            reader.readAsDataURL(file);
        }
    };

    imageInput.addEventListener('change', (e) => {
        const file = e.target.files[0];
        handleFileSelect(file);
    });

    removeImgBtn.addEventListener('click', (e) => {
        e.stopPropagation(); // Prevent triggering dropZone click
        imageInput.value = '';
        imagePreview.src = '';
        previewWrapper.style.display = 'none';
        uploadPrompt.style.display = 'block';
    });

    // 2. Drag & Drop Zone Handlers
    ['dragenter', 'dragover'].forEach(eventName => {
        dropZone.addEventListener(eventName, (e) => {
            e.preventDefault();
            dropZone.classList.add('dragover');
        }, false);
    });

    ['dragleave', 'drop'].forEach(eventName => {
        dropZone.addEventListener(eventName, (e) => {
            e.preventDefault();
            dropZone.classList.remove('dragover');
        }, false);
    });

    dropZone.addEventListener('drop', (e) => {
        const dt = e.dataTransfer;
        const file = dt.files[0];
        if (file && file.type.startsWith('image/')) {
            imageInput.files = dt.files;
            handleFileSelect(file);
        }
    });

    // 3. Form Submission Audit Runner
    reviewForm.addEventListener('submit', async (e) => {
        e.preventDefault();

        const textValue = reviewTextarea.value.trim();
        if (!textValue) {
            alert('Please enter a review text to audit.');
            return;
        }

        // Configure active loader display
        resultsEmpty.style.display = 'none';
        resultContainer.style.display = 'none';
        loader.style.display = 'flex';
        analyzeBtn.disabled = true;

        try {
            const formData = new FormData();
            formData.append('review_text', textValue);
            
            if (imageInput.files.length > 0) {
                formData.append('review_image', imageInput.files[0]);
            }

            // Post review to Flask API endpoint
            const response = await fetch('/analyze', {
                method: 'POST',
                body: formData
            });

            if (!response.ok) {
                const errorData = await response.json();
                throw new Error(errorData.error || 'Review audit analysis failed.');
            }

            const data = await response.json();

            // Render Classification summary
            classificationText.textContent = data.classification.toUpperCase();
            classificationBadge.className = 'classification-badge';
            classificationBadge.classList.add(data.classification.toLowerCase());
            confidenceValue.textContent = data.confidence;

            // Toggle dynamic dashboard visual themes (green for verified, red for suspicious)
            resultContainer.className = 'editorial-report-wrapper';
            resultContainer.classList.add('status-' + data.classification.toLowerCase());

            // Render Extracted Vector Metrics
            metricSentiment.textContent = data.metrics.sentiment_score;
            metricLength.textContent = data.metrics.length;
            metricCapital.textContent = `${(data.metrics.capitalization_ratio * 100).toFixed(1)}%`;
            metricPunctuation.textContent = data.metrics.punctuation_count;

            // Render LLM explanation text
            explanationText.textContent = data.explanation;

            // Handle audit visual image preview attachment
            if (data.image) {
                resultImage.src = 'data:image/jpeg;base64,' + data.image;
                uploadedImageContainer.style.display = 'block';
            } else {
                uploadedImageContainer.style.display = 'none';
            }

            // Trigger result sheet fade-in
            resultContainer.style.display = 'flex';

        } catch (error) {
            alert(error.message || 'An error occurred during auditing.');
            console.error('[Audit Error]', error);
            resultsEmpty.style.display = 'flex';
        } finally {
            loader.style.display = 'none';
            analyzeBtn.disabled = false;
        }
    });

    // 4. Character Limit Counter
    reviewTextarea.addEventListener('input', function() {
        const maxLength = 1000;
        const currentLength = this.value.length;
        
        let counter = this.parentNode.querySelector('.char-counter');
        if (!counter) {
            counter = document.createElement('div');
            counter.classList.add('char-counter');
            counter.style.textAlign = 'right';
            counter.style.fontSize = '0.75rem';
            counter.style.color = 'var(--text-muted)';
            counter.style.marginTop = '0.25rem';
            this.parentNode.appendChild(counter);
        }
        
        counter.textContent = `${currentLength}/${maxLength} CHARACTER VECTORS`;
        
        if (currentLength >= maxLength) {
            counter.style.color = 'var(--suspicious-color)';
        } else {
            counter.style.color = 'var(--text-muted)';
        }
    });

    // 5. Copy Log Clipboard Handler
    btnCopyExplanation.addEventListener('click', () => {
        const text = explanationText.textContent;
        navigator.clipboard.writeText(text).then(() => {
            const originalText = btnCopyExplanation.querySelector('.copy-text').textContent;
            btnCopyExplanation.querySelector('.copy-text').textContent = 'COPIED!';
            btnCopyExplanation.style.color = 'var(--verified-color)';
            
            setTimeout(() => {
                btnCopyExplanation.querySelector('.copy-text').textContent = originalText;
                btnCopyExplanation.style.color = 'var(--accent-blue)';
            }, 1500);
        }).catch(err => {
            console.error('Failed to copy to clipboard', err);
        });
    });
});