Update index.js
Browse files
index.js
CHANGED
|
@@ -1,79 +1,48 @@
|
|
| 1 |
-
import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.10.1';
|
| 2 |
-
|
| 3 |
-
// Since we will download the model from the Hugging Face Hub, we can skip the local model check
|
| 4 |
-
env.allowLocalModels = false;
|
| 5 |
-
|
| 6 |
// Reference the elements that we will need
|
| 7 |
const status = document.getElementById('status');
|
| 8 |
-
const
|
| 9 |
-
const
|
| 10 |
-
const
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
e.preventDefault();
|
| 21 |
-
detect(EXAMPLE_URL);
|
| 22 |
-
});
|
| 23 |
-
|
| 24 |
-
fileUpload.addEventListener('change', function (e) {
|
| 25 |
-
const file = e.target.files[0];
|
| 26 |
-
if (!file) {
|
| 27 |
return;
|
| 28 |
}
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
});
|
| 37 |
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
imageContainer.innerHTML = '';
|
| 42 |
-
imageContainer.style.backgroundImage = `url(${img})`;
|
| 43 |
-
|
| 44 |
-
status.textContent = 'Analysing...';
|
| 45 |
-
const output = await detector(img, {
|
| 46 |
-
threshold: 0.5,
|
| 47 |
-
percentage: true,
|
| 48 |
-
});
|
| 49 |
-
status.textContent = '';
|
| 50 |
-
output.forEach(renderBox);
|
| 51 |
-
}
|
| 52 |
-
|
| 53 |
-
// Render a bounding box and label on the image
|
| 54 |
-
function renderBox({ box, label }) {
|
| 55 |
-
const { xmax, xmin, ymax, ymin } = box;
|
| 56 |
-
|
| 57 |
-
// Generate a random color for the box
|
| 58 |
-
const color = '#' + Math.floor(Math.random() * 0xFFFFFF).toString(16).padStart(6, 0);
|
| 59 |
-
|
| 60 |
-
// Draw the box
|
| 61 |
-
const boxElement = document.createElement('div');
|
| 62 |
-
boxElement.className = 'bounding-box';
|
| 63 |
-
Object.assign(boxElement.style, {
|
| 64 |
-
borderColor: color,
|
| 65 |
-
left: 100 * xmin + '%',
|
| 66 |
-
top: 100 * ymin + '%',
|
| 67 |
-
width: 100 * (xmax - xmin) + '%',
|
| 68 |
-
height: 100 * (ymax - ymin) + '%',
|
| 69 |
-
})
|
| 70 |
-
|
| 71 |
-
// Draw label
|
| 72 |
-
const labelElement = document.createElement('span');
|
| 73 |
-
labelElement.textContent = label;
|
| 74 |
-
labelElement.className = 'bounding-box-label';
|
| 75 |
-
labelElement.style.backgroundColor = color;
|
| 76 |
|
| 77 |
-
|
| 78 |
-
imageContainer.appendChild(boxElement);
|
| 79 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
// Reference the elements that we will need
|
| 2 |
const status = document.getElementById('status');
|
| 3 |
+
const codeInput = document.getElementById('codeInput');
|
| 4 |
+
const resultsContainer = document.getElementById('results');
|
| 5 |
+
const analyzeButton = document.querySelector('button');
|
| 6 |
+
|
| 7 |
+
// Set initial status
|
| 8 |
+
status.textContent = 'Ready to analyze code';
|
| 9 |
+
|
| 10 |
+
// Event listener for the analyze button
|
| 11 |
+
analyzeButton.addEventListener('click', async () => {
|
| 12 |
+
const code = codeInput.value.trim();
|
| 13 |
+
if (!code) {
|
| 14 |
+
status.textContent = 'Please enter some Python code.';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
return;
|
| 16 |
}
|
| 17 |
|
| 18 |
+
status.textContent = 'Analyzing...';
|
| 19 |
+
|
| 20 |
+
try {
|
| 21 |
+
const response = await fetch('/analyze', {
|
| 22 |
+
method: 'POST',
|
| 23 |
+
headers: { 'Content-Type': 'application/json' },
|
| 24 |
+
body: JSON.stringify({ code })
|
| 25 |
+
});
|
| 26 |
+
|
| 27 |
+
if (!response.ok) {
|
| 28 |
+
throw new Error('Failed to analyze code');
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
const result = await response.json();
|
| 32 |
+
displayResults(result);
|
| 33 |
+
status.textContent = 'Analysis complete';
|
| 34 |
+
} catch (error) {
|
| 35 |
+
status.textContent = 'Error analyzing code';
|
| 36 |
+
console.error(error);
|
| 37 |
+
}
|
| 38 |
});
|
| 39 |
|
| 40 |
+
// Function to display the results
|
| 41 |
+
function displayResults(results) {
|
| 42 |
+
resultsContainer.innerHTML = '';
|
| 43 |
|
| 44 |
+
const preElement = document.createElement('pre');
|
| 45 |
+
preElement.textContent = JSON.stringify(results, null, 2);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
resultsContainer.appendChild(preElement);
|
|
|
|
| 48 |
}
|