Spaces:
Sleeping
Sleeping
Amina commited on
Commit ·
e122ef4
1
Parent(s): 18611fc
Fix
Browse files- front/index.html +84 -129
front/index.html
CHANGED
|
@@ -1,139 +1,94 @@
|
|
| 1 |
-
<
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
#results li { background-color: #e9ecef; padding: 10px; margin-bottom: 8px; border-radius: 4px; }
|
| 24 |
-
#results .confidence { color: #28a745; font-weight: bold; }
|
| 25 |
-
</style>
|
| 26 |
-
</head>
|
| 27 |
-
<body>
|
| 28 |
-
|
| 29 |
-
<div class="container">
|
| 30 |
-
<h1>🚗 Car Damage Detector</h1>
|
| 31 |
-
<p>Upload a photo to automatically check for scratches, dents, or rust.</p>
|
| 32 |
-
|
| 33 |
-
<div class="upload-area" id="upload-area">
|
| 34 |
-
<p>Click here or drag & drop an image</p>
|
| 35 |
-
</div>
|
| 36 |
-
|
| 37 |
-
<input type="file" id="image-upload" accept="image/*" hidden>
|
| 38 |
-
|
| 39 |
-
<div class="button-group">
|
| 40 |
-
<button id="check-button" disabled>Check for Damage</button>
|
| 41 |
-
<button id="delete-button">Delete</button>
|
| 42 |
-
</div>
|
| 43 |
-
|
| 44 |
-
<img id="image-preview" src="" alt="Your uploaded image">
|
| 45 |
-
|
| 46 |
-
<div id="results"></div>
|
| 47 |
-
</div>
|
| 48 |
-
|
| 49 |
-
<script>
|
| 50 |
-
// ⚠️ IMPORTANT: REPLACE THIS URL with your actual Hugging Face Space URL!
|
| 51 |
-
const API_URL = "https://amn23-car-image-classification.hf.space/detect";
|
| 52 |
-
|
| 53 |
-
const uploadArea = document.getElementById('upload-area');
|
| 54 |
-
const imageUpload = document.getElementById('image-upload');
|
| 55 |
-
const checkButton = document.getElementById('check-button');
|
| 56 |
-
const imagePreview = document.getElementById('image-preview');
|
| 57 |
-
const resultsDiv = document.getElementById('results');
|
| 58 |
-
const deleteButton = document.getElementById('delete-button');
|
| 59 |
-
let selectedFile = null;
|
| 60 |
-
|
| 61 |
-
uploadArea.addEventListener('click', () => imageUpload.click());
|
| 62 |
-
imageUpload.addEventListener('change', handleFileSelect);
|
| 63 |
-
|
| 64 |
-
function handleFileSelect(event) {
|
| 65 |
-
selectedFile = event.target.files[0];
|
| 66 |
-
if (selectedFile) {
|
| 67 |
-
imagePreview.src = URL.createObjectURL(selectedFile);
|
| 68 |
-
imagePreview.style.display = 'block';
|
| 69 |
-
checkButton.disabled = false;
|
| 70 |
-
// --- THIS IS THE FIXED LINE ---
|
| 71 |
-
deleteButton.style.display = 'inline-block'; // Show delete button
|
| 72 |
-
// --- END OF FIX ---
|
| 73 |
-
resultsDiv.innerHTML = '';
|
| 74 |
-
}
|
| 75 |
-
}
|
| 76 |
-
|
| 77 |
-
deleteButton.addEventListener('click', () => {
|
| 78 |
-
selectedFile = null;
|
| 79 |
-
imageUpload.value = '';
|
| 80 |
-
imagePreview.src = '';
|
| 81 |
-
imagePreview.style.display = 'none';
|
| 82 |
resultsDiv.innerHTML = '';
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
body: formData,
|
| 104 |
-
});
|
| 105 |
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
}
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
console.error("Error:", error);
|
| 116 |
-
} finally {
|
| 117 |
-
checkButton.disabled = false;
|
| 118 |
-
deleteButton.disabled = false;
|
| 119 |
}
|
| 120 |
-
});
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
resultsDiv.innerHTML = "<p>✅ No damage detected!</p>";
|
| 125 |
-
return;
|
| 126 |
-
}
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
}
|
| 136 |
-
</script>
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script>
|
| 2 |
+
// ⚠️ IMPORTANT: Make sure this URL is correct
|
| 3 |
+
const API_URL = "https://your-username-your-spacename.hf.space/detect";
|
| 4 |
+
|
| 5 |
+
const uploadArea = document.getElementById('upload-area');
|
| 6 |
+
const imageUpload = document.getElementById('image-upload');
|
| 7 |
+
const checkButton = document.getElementById('check-button');
|
| 8 |
+
const imagePreview = document.getElementById('image-preview');
|
| 9 |
+
const resultsDiv = document.getElementById('results');
|
| 10 |
+
const deleteButton = document.getElementById('delete-button');
|
| 11 |
+
let selectedFile = null;
|
| 12 |
+
|
| 13 |
+
uploadArea.addEventListener('click', () => imageUpload.click());
|
| 14 |
+
imageUpload.addEventListener('change', handleFileSelect);
|
| 15 |
+
|
| 16 |
+
function handleFileSelect(event) {
|
| 17 |
+
selectedFile = event.target.files[0];
|
| 18 |
+
if (selectedFile) {
|
| 19 |
+
imagePreview.src = URL.createObjectURL(selectedFile);
|
| 20 |
+
imagePreview.style.display = 'block';
|
| 21 |
+
checkButton.disabled = false;
|
| 22 |
+
deleteButton.style.display = 'inline-block';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
resultsDiv.innerHTML = '';
|
| 24 |
+
}
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
deleteButton.addEventListener('click', () => {
|
| 28 |
+
selectedFile = null;
|
| 29 |
+
imageUpload.value = '';
|
| 30 |
+
imagePreview.src = '';
|
| 31 |
+
imagePreview.style.display = 'none';
|
| 32 |
+
resultsDiv.innerHTML = '';
|
| 33 |
+
checkButton.disabled = true;
|
| 34 |
+
deleteButton.style.display = 'none';
|
| 35 |
+
});
|
| 36 |
+
|
| 37 |
+
checkButton.addEventListener('click', async () => {
|
| 38 |
+
if (!selectedFile) {
|
| 39 |
+
alert("Please select an image first!");
|
| 40 |
+
return;
|
| 41 |
+
}
|
| 42 |
|
| 43 |
+
resultsDiv.innerHTML = "<p>Analyzing, please wait... 🧐</p>";
|
| 44 |
+
checkButton.disabled = true;
|
| 45 |
+
deleteButton.disabled = true;
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
const formData = new FormData();
|
| 48 |
+
formData.append('file', selectedFile);
|
|
|
|
| 49 |
|
| 50 |
+
try {
|
| 51 |
+
const response = await fetch(API_URL, {
|
| 52 |
+
method: 'POST',
|
| 53 |
+
body: formData,
|
| 54 |
+
});
|
| 55 |
|
| 56 |
+
if (!response.ok) {
|
| 57 |
+
throw new Error(`Server error: ${response.statusText}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
}
|
|
|
|
| 59 |
|
| 60 |
+
const data = await response.json();
|
| 61 |
+
displayResults(data.conditions);
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
} catch (error) {
|
| 64 |
+
resultsDiv.innerHTML = `<p style="color: red;">An error occurred: ${error.message}</p>`;
|
| 65 |
+
console.error("Error:", error);
|
| 66 |
+
} finally {
|
| 67 |
+
checkButton.disabled = false;
|
| 68 |
+
deleteButton.disabled = false;
|
| 69 |
+
}
|
| 70 |
+
});
|
| 71 |
+
|
| 72 |
+
// --- UPDATED FUNCTION ---
|
| 73 |
+
// This function now correctly handles your new backend messages
|
| 74 |
+
function displayResults(conditions) {
|
| 75 |
+
if (!conditions || conditions.length === 0) {
|
| 76 |
+
resultsDiv.innerHTML = "<p>❓ Could not determine condition.</p>";
|
| 77 |
+
return;
|
| 78 |
}
|
|
|
|
| 79 |
|
| 80 |
+
// Check for the specific "no damage" message
|
| 81 |
+
if (conditions.length === 1 && conditions[0] === 'No damage detected.') {
|
| 82 |
+
resultsDiv.innerHTML = "<p>✅ No damage detected!</p>";
|
| 83 |
+
return;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
// Display "Broken", "Dirty", etc.
|
| 87 |
+
let html = "<h3>Determined Conditions:</h3><ul>";
|
| 88 |
+
conditions.forEach(condition => {
|
| 89 |
+
html += `<li><strong>${condition}</strong></li>`;
|
| 90 |
+
});
|
| 91 |
+
html += "</ul>";
|
| 92 |
+
resultsDiv.innerHTML = html;
|
| 93 |
+
}
|
| 94 |
+
</script>
|