File size: 2,898 Bytes
32d4a86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
document.addEventListener('DOMContentLoaded', function() {
    const form = document.getElementById('uploadForm');
    const imageInput = document.getElementById('imageInput');
    const submitBtn = document.getElementById('submitBtn');
    const spinner = submitBtn.querySelector('.spinner-border');
    const resultSection = document.getElementById('resultSection');
    const previewImage = document.getElementById('previewImage');
    const captionText = document.getElementById('captionText');
    const errorAlert = document.getElementById('errorAlert');
    const uploadApp = document.querySelector('.upload-app');

    // Preview image when selected
    imageInput.addEventListener('change', function(e) {
        const file = e.target.files[0];
        if (file) {
            const reader = new FileReader();
            reader.onload = function(e) {
                previewImage.src = e.target.result;
                resultSection.classList.remove('d-none');
                captionText.textContent = '';
                errorAlert.classList.add('d-none');

                // Add success animation class
                uploadApp.classList.add('file-selected');
            };
            reader.readAsDataURL(file);
        }
    });

    form.addEventListener('submit', async function(e) {
        e.preventDefault();

        const formData = new FormData();
        const file = imageInput.files[0];

        if (!file) {
            showError('Please select an image first.');
            return;
        }

        // Add file and selected model to form data
        formData.append('image', file);
        formData.append('model', document.querySelector('input[name="model"]:checked').value);

        // Show loading state
        setLoading(true);

        try {
            const response = await fetch('/upload', {
                method: 'POST',
                body: formData
            });

            const data = await response.json();

            if (!response.ok) {
                throw new Error(data.error || 'Failed to generate caption');
            }

            // Display the caption
            captionText.textContent = data.caption;
            resultSection.classList.remove('d-none');
            errorAlert.classList.add('d-none');

        } catch (error) {
            showError(error.message || 'An error occurred while generating the caption');
        } finally {
            setLoading(false);
        }
    });

    function setLoading(isLoading) {
        submitBtn.disabled = isLoading;
        spinner.classList.toggle('d-none', !isLoading);
        submitBtn.textContent = isLoading ? ' Processing...' : 'Generate Caption';
        if (isLoading) {
            submitBtn.prepend(spinner);
        }
    }

    function showError(message) {
        errorAlert.textContent = message;
        errorAlert.classList.remove('d-none');
    }
});