File size: 3,489 Bytes
400921b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
async function uploadImage() {
    const fileInput = document.getElementById('image');
    const versionInput = document.getElementById('version');
    const scaleInput = document.getElementById('scale');

    // Check if the file input is empty
    if (!fileInput.files[0]) {
        document.getElementById('resultContainer').innerHTML = `<p style="color: white;">Please upload an image.</p>`;
        return;
    }

    // Check if the file size is above 1500 KB
    if (fileInput.files[0].size > 1500 * 1024) {
        // Display loading spinner and estimated time
        const loadingSpinner = document.getElementById('loadingSpinner');
        loadingSpinner.style.display = 'block';
        const estimatedTime = document.getElementById('estimatedTime');
        estimatedTime.style.display = 'block';

        // Hide loading spinner and estimated time after 30 seconds
        setTimeout(() => {
            loadingSpinner.style.display = 'none';
            estimatedTime.style.display = 'none';

            // Display uploaded image after 30 seconds
            const reader = new FileReader();
            reader.onload = function(e) {
                const resultContainer = document.getElementById('resultContainer');
                const uploadedImage = document.createElement('img');
                uploadedImage.src = e.target.result;
                uploadedImage.style.maxWidth = '100%'; // Adjust this value as needed
                uploadedImage.style.maxHeight = '100%'; // Adjust this value as needed
                resultContainer.innerHTML = ''; // Clear previous content
                resultContainer.appendChild(uploadedImage);
            };
            reader.readAsDataURL(fileInput.files[0]);
        }, 30000);

        return;
    }

    // Clear the result container
    document.getElementById('resultContainer').innerHTML = "";

    const formData = new FormData();
    formData.append('file', fileInput.files[0]);
    formData.append('version', versionInput.value);
    formData.append('scale', scaleInput.value);

    // Display loading spinner
    const loadingSpinner = document.getElementById('loadingSpinner');
    loadingSpinner.style.display = 'block';
    const estimatedTime = document.getElementById('estimatedTime');
    estimatedTime.style.display = 'block';
    try {
        const response = await fetch('https://ashrafb-furdockgr1.hf.space/upload/', {
            method: 'POST',
            body: formData
        });

        // Hide loading spinner
        loadingSpinner.style.display = 'none';
        estimatedTime.style.display = 'none';
        if (response.ok) {
            const resultContainer = document.getElementById('resultContainer');
            const data = await response.json();
            const sketchImage = document.createElement('img');
            sketchImage.style.maxWidth = '100%'; // Adjust this value as needed
            sketchImage.style.maxHeight = '100%'; // Adjust this value as needed
            sketchImage.src = data.sketch_image_base64;
            resultContainer.appendChild(sketchImage);
        } else {
            document.getElementById('resultContainer').innerHTML = `<p style="color: white;">Oops! Something went wrong. Please try again later.</p>`;
        }
    } catch (error) {
        console.error('Error:', error);
        document.getElementById('resultContainer').innerHTML = `<p style="color: white;">Oops! Something went wrong. Please try again later.</p>`;
    }
}