File size: 4,327 Bytes
9fd2c00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<script>
    // ... (Keep your existing Drag & Drop and File variables) ...
    const chatSection = document.getElementById('chat-section');
    const chatBox = document.getElementById('chat-box');

    // --- UPDATED SHOW RESULTS ---
    function showResults(data, file) {
        scanner.style.display = 'none';
        const reader = new FileReader();
        reader.onload = (e) => { document.getElementById('result-img-preview').src = e.target.result; }
        reader.readAsDataURL(file);

        const title = document.getElementById('pred-title');
        title.innerText = data.prediction.toUpperCase();
        
        if(data.prediction === 'malignant') title.style.color = '#ff0055';
        else if(data.prediction === 'benign') title.style.color = '#00ff9d';
        else title.style.color = '#00f2ff';

        document.getElementById('conf-label').innerText = `Confidence Index: ${(data.confidence * 100).toFixed(2)}%`;

        const container = document.getElementById('stats-container');
        container.innerHTML = '';
        const sorted = Object.entries(data.probabilities).sort(([,a],[,b]) => b - a);
        
        sorted.forEach(([label, score]) => {
            let color = label === 'malignant' ? '#ff0055' : (label === 'benign' ? '#00ff9d' : '#00f2ff');
            const pct = (score * 100).toFixed(1);
            const html = `<div class="stat-row">

                <div class="stat-header"><span>${label.toUpperCase()}</span><span>${pct}%</span></div>

                <div class="progress-track"><div class="progress-fill" style="width: ${pct}%; background-color: ${color}; box-shadow: 0 0 10px ${color}"></div></div>

            </div>`;
            container.insertAdjacentHTML('beforeend', html);
        });

        // Toggle Views
        uploadView.style.display = 'none';
        resultsView.style.display = 'block';
        
        // Wake up Chatbot
        chatSection.style.display = 'block';
        loadSuggestions();
    }

    // --- CHATBOT LOGIC ---
    function loadSuggestions() {
        fetch('/chat', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ message: "" })
        })
        .then(res => res.json())
        .then(data => {
            const area = document.getElementById('suggestions-area');
            area.innerHTML = '';
            data.suggestions.forEach(s => {
                const btn = document.createElement('button');
                btn.className = 'suggestion-btn';
                btn.innerText = s;
                btn.onclick = () => sendChat(s);
                area.appendChild(btn);
            });
        });
    }

    async function sendChat(manualMsg = null) {
        const input = document.getElementById('user-input');
        const message = manualMsg || input.value.trim();
        if (!message) return;

        if(!manualMsg) input.value = '';

        chatBox.innerHTML += `<div class="chat-msg user-msg"><b>YOU:</b> ${message}</div>`;
        
        const response = await fetch('/chat', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ message: message })
        });
        const data = await response.json();
        
        chatBox.innerHTML += `<div class="chat-msg bot-msg"><b>BOT:</b> ${data.reply}</div>`;
        chatBox.scrollTop = chatBox.scrollHeight;
    }

    // Fix for Reset Logic (Browse Trigger)
    function resetUI() {
        form.reset();
        uploadView.style.display = 'block';
        resultsView.style.display = 'none';
        chatSection.style.display = 'none';
        chatBox.innerHTML = '<div class="chat-msg bot-msg"><b>SYSTEM:</b> Analysis complete. How can I assist you with these results?</div>';
        submitBtn.innerText = "ANALYZE SCAN";
        submitBtn.disabled = true;
        scanner.style.display = 'none';
        
        document.querySelector('.upload-text-main').innerText = "Upload Medical Imagery";
        document.querySelector('.upload-icon').classList.replace('fa-check-circle', 'fa-cloud-arrow-up');
        document.querySelector('.upload-icon').style.color = 'var(--accent-cyan)';
    }
</script>