Spaces:
Running
Running
File size: 8,370 Bytes
2c6db99 7a2cb60 2c6db99 7a2cb60 2c6db99 | 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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | // Global state
const appState = {
currentModel: 'yolov11n',
inputModality: 'image',
inputSource: 'file',
isLoading: false,
results: []
};
// Initialize the application
function initApp() {
console.log('MedVision Model Viewer initialized');
// Load default model (YOLOv11n)
loadModel('yolov11n');
// Set up event listeners
setupEventListeners();
}
// Load a model into the application
function loadModel(modelName) {
appState.isLoading = true;
document.getElementById('loading-overlay').classList.remove('hidden');
console.log(`Loading model: ${modelName}`);
// Simulate model loading
setTimeout(() => {
appState.currentModel = modelName;
appState.isLoading = false;
document.getElementById('loading-overlay').classList.add('hidden');
updateUI();
}, 1500);
}
// Run inference with the current model
function runInference(inputData) {
if (appState.isLoading) return;
appState.isLoading = true;
document.getElementById('loading-overlay').classList.remove('hidden');
console.log(`Running inference with ${appState.currentModel} on ${appState.inputModality} input`);
// Simulate inference
setTimeout(() => {
// Mock results for demo purposes
appState.results = generateMockResults();
appState.isLoading = false;
document.getElementById('loading-overlay').classList.add('hidden');
updateUI();
renderResults();
}, 2000);
}
// Generate mock results for demo
function generateMockResults() {
const classes = ['tumor', 'organ', 'lesion', 'vessel', 'bone'];
const results = [];
for (let i = 0; i < 5; i++) {
results.push({
class: classes[Math.floor(Math.random() * classes.length)],
confidence: (Math.random() * 0.9 + 0.1).toFixed(2),
bbox: {
x: Math.floor(Math.random() * 100),
y: Math.floor(Math.random() * 100),
width: Math.floor(Math.random() * 30 + 10),
height: Math.floor(Math.random() * 30 + 10)
},
color: `#${Math.floor(Math.random()*16777215).toString(16)}`
});
}
return results;
}
// Update the UI based on current state
function updateUI() {
// Update model indicator
document.querySelectorAll('[data-model-indicator]').forEach(el => {
el.textContent = appState.currentModel;
});
}
// Render results to the results table
function renderResults() {
const tbody = document.querySelector('tbody');
if (appState.results.length === 0) {
tbody.innerHTML = `
<tr>
<td colspan="4" class="px-6 py-4 whitespace-nowrap text-sm text-gray-500 text-center">No results yet</td>
</tr>
`;
return;
}
tbody.innerHTML = appState.results.map(result => `
<tr>
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900 capitalize">${result.class}</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">${result.confidence}</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">[${result.bbox.x}, ${result.bbox.y}, ${result.bbox.width}, ${result.bbox.height}]</td>
<td class="px-6 py-4 whitespace-nowrap">
<span class="inline-block w-4 h-4 rounded-full" style="background-color: ${result.color}"></span>
</td>
</tr>
`).join('');
}
// File upload handling
document.getElementById('media-upload').addEventListener('change', (e) => {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (event) => {
const outputContainer = document.getElementById('output-container');
if (file.type.startsWith('image/')) {
outputContainer.innerHTML = `<img src="${event.target.result}" class="max-w-full max-h-full" alt="Uploaded image">`;
} else if (file.type.startsWith('video/')) {
outputContainer.innerHTML = `
<video controls class="max-w-full max-h-full">
<source src="${event.target.result}" type="${file.type}">
Your browser does not support the video tag.
</video>
`;
}
};
reader.readAsDataURL(file);
}
});
// Make file upload area clickable
const fileUploadArea = document.querySelector('#file-upload-container .border-dashed');
fileUploadArea.addEventListener('click', () => {
document.getElementById('media-upload').click();
});
// Model upload handling
const modelUpload = document.getElementById('model-upload');
if (modelUpload) {
modelUpload.addEventListener('change', (e) => {
const file = e.target.files[0];
if (file) {
console.log('Model file selected:', file.name);
// Here you would typically validate and process the model file
alert(`Model file selected: ${file.name}`);
}
});
// Make model upload area clickable
const modelUploadArea = document.querySelector('#custom-model-upload .border-dashed');
if (modelUploadArea) {
modelUploadArea.addEventListener('click', () => {
modelUpload.click();
});
}
// Handle model URL input
const modelUrlInput = document.querySelector('#custom-model-upload input[type="text"]');
if (modelUrlInput) {
modelUrlInput.addEventListener('change', (e) => {
const url = e.target.value.trim();
if (url) {
console.log('Model URL entered:', url);
// Here you would typically validate and load the model from URL
alert(`Model URL entered: ${url}`);
}
});
}
}
// Drag and drop for file upload
const uploadArea = document.getElementById('file-upload-container');
uploadArea.addEventListener('dragover', (e) => {
e.preventDefault();
uploadArea.classList.add('border-primary-500', 'bg-amber-50');
});
uploadArea.addEventListener('dragleave', () => {
uploadArea.classList.remove('border-primary-500', 'bg-amber-50');
});
uploadArea.addEventListener('drop', (e) => {
e.preventDefault();
uploadArea.classList.remove('border-primary-500', 'bg-amber-50');
const file = e.dataTransfer.files[0];
if (file) {
document.getElementById('media-upload').files = e.dataTransfer.files;
const event = new Event('change');
document.getElementById('media-upload').dispatchEvent(event);
}
});
// Drag and drop for model upload
const modelUploadArea = document.querySelector('#custom-model-upload');
if (modelUploadArea) {
modelUploadArea.addEventListener('dragover', (e) => {
e.preventDefault();
const dashedBorder = modelUploadArea.querySelector('.border-dashed');
dashedBorder.classList.add('border-primary-500', 'bg-amber-50');
});
modelUploadArea.addEventListener('dragleave', (e) => {
const dashedBorder = modelUploadArea.querySelector('.border-dashed');
dashedBorder.classList.remove('border-primary-500', 'bg-amber-50');
});
modelUploadArea.addEventListener('drop', (e) => {
e.preventDefault();
const dashedBorder = modelUploadArea.querySelector('.border-dashed');
dashedBorder.classList.remove('border-primary-500', 'bg-amber-50');
const file = e.dataTransfer.files[0];
if (file) {
document.getElementById('model-upload').files = e.dataTransfer.files;
const event = new Event('change');
document.getElementById('model-upload').dispatchEvent(event);
}
});
}
}
// Initialize the app when the DOM is loaded
document.addEventListener('DOMContentLoaded', initApp); |