Spaces:
Running
Running
Create script.js
Browse files
script.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Grabbing elements
|
| 2 |
+
const imageInput = document.getElementById('imageInput');
|
| 3 |
+
const uploadButton = document.getElementById('uploadButton');
|
| 4 |
+
const resultContainer = document.getElementById('result-container');
|
| 5 |
+
const resultText = document.getElementById('resultText');
|
| 6 |
+
const uploadedImage = document.getElementById('uploadedImage');
|
| 7 |
+
|
| 8 |
+
// Mock prediction function (replace this with Hugging Face API call if needed)
|
| 9 |
+
function mockPrediction(image) {
|
| 10 |
+
const classes = ['Cat', 'Dog'];
|
| 11 |
+
const randomIndex = Math.floor(Math.random() * classes.length);
|
| 12 |
+
return classes[randomIndex];
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
// Handle upload button click
|
| 16 |
+
uploadButton.addEventListener('click', () => {
|
| 17 |
+
const file = imageInput.files[0];
|
| 18 |
+
if (!file) {
|
| 19 |
+
alert('Please upload an image first!');
|
| 20 |
+
return;
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
// Display the image
|
| 24 |
+
const reader = new FileReader();
|
| 25 |
+
reader.onload = function (e) {
|
| 26 |
+
uploadedImage.src = e.target.result;
|
| 27 |
+
uploadedImage.alt = file.name;
|
| 28 |
+
};
|
| 29 |
+
reader.readAsDataURL(file);
|
| 30 |
+
|
| 31 |
+
// Mock prediction
|
| 32 |
+
const prediction = mockPrediction(file);
|
| 33 |
+
resultText.textContent = `This looks like a ${prediction}!`;
|
| 34 |
+
|
| 35 |
+
// Show result container
|
| 36 |
+
resultContainer.classList.remove('hidden');
|
| 37 |
+
});
|