File size: 4,614 Bytes
7505319
55acd9b
7505319
55acd9b
 
7505319
55acd9b
 
 
 
 
 
 
 
7505319
55acd9b
 
7505319
 
55acd9b
 
7505319
55acd9b
 
 
 
 
 
 
 
 
 
 
7505319
 
55acd9b
 
 
7505319
55acd9b
7505319
55acd9b
7505319
 
55acd9b
 
7505319
 
 
 
 
 
 
55acd9b
 
7505319
55acd9b
7505319
 
 
55acd9b
 
7505319
55acd9b
 
7505319
 
 
 
 
 
55acd9b
 
 
 
 
 
7505319
55acd9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7505319
55acd9b
7505319
 
 
55acd9b
 
 
 
 
 
 
 
 
 
 
 
 
 
7505319
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
document.addEventListener('DOMContentLoaded', () => {
    // Model selection buttons and sections
    const sentimentBtn = document.getElementById('sentiment-btn');
    const catdogBtn = document.getElementById('catdog-btn');
    const housingBtn = document.getElementById('housing-btn');

    const sentimentSection = document.getElementById('sentiment-section');
    const catdogSection = document.getElementById('catdog-section');
    const housingSection = document.getElementById('housing-section');

    const sections = [sentimentSection, catdogSection, housingSection];
    const buttons = [sentimentBtn, catdogBtn, housingBtn];

    // Forms and result displays
    const sentimentForm = document.getElementById('sentiment-form');
    const catdogForm = document.getElementById('catdog-form');
    const housingForm = document.getElementById('housing-form');

    const sentimentResult = document.getElementById('sentiment-result');
    const catdogResult = document.getElementById('catdog-result');
    const housingResult = document.getElementById('housing-result');

    const imagePreview = document.getElementById('image-preview');
    const catdogFileInput = document.getElementById('catdog-file');

    // --- Event Listeners for Model Switching ---
    function switchTab(activeIndex) {
        buttons.forEach((button, index) => {
            button.classList.toggle('active', index === activeIndex);
        });
        sections.forEach((section, index) => {
            section.classList.toggle('active', index === activeIndex);
        });
    }

    sentimentBtn.addEventListener('click', () => switchTab(0));
    catdogBtn.addEventListener('click', () => switchTab(1));
    housingBtn.addEventListener('click', () => switchTab(2));

    // --- Form Submission Handlers ---

    // Sentiment Form
    sentimentForm.addEventListener('submit', async (e) => {
        e.preventDefault();
        const text = document.getElementById('sentiment-text').value;
        sentimentResult.textContent = 'Analyzing...';
        try {
            const response = await fetch('/predict/sentiment', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ text })
            });
            const data = await response.json();
            if (!response.ok) throw new Error(data.detail || 'An unknown error occurred.');
            sentimentResult.textContent = `Prediction: ${data.prediction}`;
        } catch (error) {
            sentimentResult.textContent = `Error: ${error.message}`;
        }
    });

    // Cat & Dog Form
    catdogForm.addEventListener('submit', async (e) => {
        e.preventDefault();
        const formData = new FormData(catdogForm);
        catdogResult.textContent = 'Classifying...';
        try {
            const response = await fetch('/predict/catdog', {
                method: 'POST',
                body: formData
            });
            const data = await response.json();
            if (!response.ok) throw new Error(data.detail || 'An unknown error occurred.');
            catdogResult.textContent = `Prediction: ${data.prediction}`;
        } catch (error) {
            catdogResult.textContent = `Error: ${error.message}`;
        }
    });

    // Housing Form
    housingForm.addEventListener('submit', async (e) => {
        e.preventDefault();
        const formData = new FormData(housingForm);
        const data = Object.fromEntries(formData.entries());
        housingResult.textContent = 'Predicting...';
        try {
            const response = await fetch('/predict/housing', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify(data)
            });
            const result = await response.json();
            if (!response.ok) throw new Error(result.detail || 'An unknown error occurred.');
            housingResult.textContent = `Predicted Price: ${result.prediction}`;
        } catch (error) {
            housingResult.textContent = `Error: ${error.message}`;
        }
    });

    // Image Preview Handler
    catdogFileInput.addEventListener('change', () => {
        const file = catdogFileInput.files[0];
        if (file) {
            const reader = new FileReader();
            reader.onload = (e) => {
                imagePreview.src = e.target.result;
                imagePreview.style.display = 'block';
            };
            reader.readAsDataURL(file);
        } else {
            imagePreview.style.display = 'none';
        }
    });
});