File size: 6,278 Bytes
be6a95f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// This function runs once the HTML document is fully loaded.
document.addEventListener("DOMContentLoaded", () => {

    // --- 1. Get all our HTML elements ---
    const analyzeBtn = document.getElementById("analyze_btn");
    const loadingSpinner = document.getElementById("loading_spinner");
    const resultsSection = document.getElementById("results_section");

    const textInput = document.getElementById("text_input");
    const imageInput = document.getElementById("image_input");
    const audioInput = document.getElementById("audio_input"); // <-- ADD THIS

    const finalPrediction = document.getElementById("final_prediction");

    // Get the 'canvas' elements for our charts
    const textChartCtx = document.getElementById('text_chart').getContext('2d');
    const imageChartCtx = document.getElementById('image_chart').getContext('2d');
    const audioChartCtx = document.getElementById('audio_chart').getContext('2d'); // <-- ADD THIS

    // We'll store our chart objects here so we can update them
    let textChart, imageChart, audioChart; // <-- ADD audioChart

    // --- 2. Add "click" listener to the Analyze button ---
    analyzeBtn.addEventListener("click", async () => {

        // Get the values from the input fields
        const text = textInput.value;
        const imageFile = imageInput.files[0];
        const audioFile = audioInput.files[0]; // <-- ADD THIS

        // Basic validation
        if (!text || !imageFile || !audioFile) { // <-- ADD !audioFile
            alert("Please provide text, an image, and an audio file."); // <-- UPDATE TEXT
            return;
        }

        // Show the loading spinner and hide old results
        loadingSpinner.classList.remove("hidden");
        resultsSection.classList.add("hidden");
        analyzeBtn.disabled = true;

        // --- 3. Prepare the data to send to the API ---
        const formData = new FormData();
        formData.append("text_input", text);
        formData.append("image_input", imageFile);
        formData.append("audio_input", audioFile); // <-- ADD THIS

        try {
            // --- 4. Send the data to our FastAPI backend ---
            const response = await fetch("http://127.0.0.1:8000/predict", {
                method: "POST",
                body: formData,
            });

            if (!response.ok) {
                throw new Error(`HTTP error! Status: ${response.status}`);
            }

            const results = await response.json();

            // --- 5. Update the UI with the results ---
            updateUI(results);

        } catch (error) {
            console.error("Error during analysis:", error);
            alert("An error occurred while analyzing. Please check the console.");
        } finally {
            // Hide loading spinner and re-enable button
            loadingSpinner.classList.add("hidden");
            analyzeBtn.disabled = false;
        }
    });

    // --- 6. Helper function to update all the charts and text ---
    function updateUI(results) {
        // Update the final prediction text
        finalPrediction.textContent = results.final_prediction;

        // Get the labels and data from our results
        const labels = Object.keys(results.final_probabilities);
        const textData = Object.values(results.text_probabilities);
        const imageData = Object.values(results.image_probabilities);
        const audioData = Object.values(results.audio_probabilities); // <-- ADD THIS

        // Update the charts
        textChart = createOrUpdateChart(textChart, textChartCtx, labels, textData, 'Text Probabilities');
        imageChart = createOrUpdateChart(imageChart, imageChartCtx, labels, imageData, 'Image Probabilities');
        audioChart = createOrUpdateChart(audioChart, audioChartCtx, labels, audioData, 'Audio Probabilities'); // <-- ADD THIS

        // Show the results section
        resultsSection.classList.remove("hidden");
        // Scroll to the results card
        resultsSection.scrollIntoView({ behavior: 'smooth' });
    }

    // --- 7. Helper function to create or update a bar chart ---
    function createOrUpdateChart(chartInstance, context, labels, data, title) {
        // If the chart already exists, destroy it before creating a new one
        if (chartInstance) {
            chartInstance.destroy();
        }

        // Create a new Chart.js bar chart
        return new Chart(context, {
            type: 'bar',
            data: {
                labels: labels,
                datasets: [{
                    label: title,
                    data: data,
                    backgroundColor: [
                        'rgba(255, 99, 132, 0.2)',
                        'rgba(54, 162, 235, 0.2)',
                        'rgba(255, 206, 86, 0.2)',
                        'rgba(75, 192, 192, 0.2)',
                        'rgba(153, 102, 255, 0.2)',
                        'rgba(255, 159, 64, 0.2)',
                        'rgba(199, 199, 199, 0.2)'
                    ],
                    borderColor: [
                        'rgba(255, 99, 132, 1)',
                        'rgba(54, 162, 235, 1)',
                        'rgba(255, 206, 86, 1)',
                        'rgba(75, 192, 192, 1)',
                        'rgba(153, 102, 255, 1)',
                        'rgba(255, 159, 64, 1)',
                        'rgba(199, 199, 199, 1)'
                    ],
                    borderWidth: 1
                }]
            },
            options: {
                // --- THIS IS THE FIX ---
                maintainAspectRatio: false,
                // -----------------------
                indexAxis: 'y', // Makes it a horizontal bar chart
                scales: {
                    x: {
                        beginAtZero: true,
                        max: 1.0 // Probabilities are from 0 to 1
                    }
                },
                plugins: {
                    legend: {
                        display: false // Hides the "Text Probabilities" label
                    }
                }
            }
        });
    }
});