""" Frontend Integration Example Demonstrates how to call the API from different frontend frameworks """ # ============================================================ # VANILLA JAVASCRIPT # ============================================================ vanilla_js = """ // Vanilla JavaScript Example // Get DOM elements const fileInput = document.getElementById('fileInput'); const predictButton = document.getElementById('predictButton'); const resultsDiv = document.getElementById('results'); // Add event listener predictButton.addEventListener('click', async () => { const file = fileInput.files[0]; if (!file) { alert('Please select an image first'); return; } // Create form data const formData = new FormData(); formData.append('file', file); // Show loading state resultsDiv.innerHTML = '
Analyzing image...
'; try { // Make API request const response = await fetch('http://localhost:8000/predict', { method: 'POST', body: formData }); const data = await response.json(); if (data.success) { // Display results const svm = data.predictions.svm; const cnn = data.predictions.cnn; const consensus = data.consensus; resultsDiv.innerHTML = `Prediction: ${svm.class}
Confidence: ${(svm.confidence * 100).toFixed(2)}%
Time: ${svm.inference_time_ms.toFixed(2)}ms
Prediction: ${cnn.class}
Confidence: ${(cnn.confidence * 100).toFixed(2)}%
Time: ${cnn.inference_time_ms.toFixed(2)}ms
${consensus.predicted_class}
Models ${consensus.agreement ? 'agree' : 'disagree'}
Prediction failed
'; } } catch (error) { resultsDiv.innerHTML = `Error: ${error.message}
`; } }); """ # ============================================================ # REACT # ============================================================ react_example = """ // React Component Example import React, { useState } from 'react'; function PneumoniaDetector() { const [file, setFile] = useState(null); const [results, setResults] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const handleFileChange = (e) => { setFile(e.target.files[0]); setResults(null); setError(null); }; const handlePredict = async () => { if (!file) { setError('Please select an image'); return; } setLoading(true); setError(null); const formData = new FormData(); formData.append('file', file); try { const response = await fetch('http://localhost:8000/predict', { method: 'POST', body: formData }); const data = await response.json(); if (data.success) { setResults(data); } else { setError('Prediction failed'); } } catch (err) { setError(`Error: ${err.message}`); } finally { setLoading(false); } }; return (Prediction: {results.predictions.svm.class}
Confidence: {(results.predictions.svm.confidence * 100).toFixed(2)}%
Prediction: {results.predictions.cnn.class}
Confidence: {(results.predictions.cnn.confidence * 100).toFixed(2)}%
Models {results.consensus.agreement ? 'agree' : 'disagree'}