Spaces:
Running
Running
Update index.html
Browse files- index.html +89 -19
index.html
CHANGED
|
@@ -1,19 +1,89 @@
|
|
| 1 |
-
<!
|
| 2 |
-
<html>
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Image Caption Prototype</title>
|
| 7 |
+
</head>
|
| 8 |
+
|
| 9 |
+
<body>
|
| 10 |
+
<h1>Image Caption Prototype</h1>
|
| 11 |
+
|
| 12 |
+
<input type="file" id="imageInput" accept="image/*">
|
| 13 |
+
|
| 14 |
+
<br><br>
|
| 15 |
+
|
| 16 |
+
<img
|
| 17 |
+
id="preview"
|
| 18 |
+
style="max-width: 500px; display: none;"
|
| 19 |
+
alt="Selected image"
|
| 20 |
+
>
|
| 21 |
+
|
| 22 |
+
<br><br>
|
| 23 |
+
|
| 24 |
+
<button id="captionButton" disabled>
|
| 25 |
+
Generate caption
|
| 26 |
+
</button>
|
| 27 |
+
|
| 28 |
+
<p id="status"></p>
|
| 29 |
+
|
| 30 |
+
<h2>Caption</h2>
|
| 31 |
+
<p id="caption"></p>
|
| 32 |
+
|
| 33 |
+
<script type="module">
|
| 34 |
+
import { pipeline }
|
| 35 |
+
from "https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.8.1";
|
| 36 |
+
|
| 37 |
+
const imageInput = document.getElementById("imageInput");
|
| 38 |
+
const preview = document.getElementById("preview");
|
| 39 |
+
const captionButton = document.getElementById("captionButton");
|
| 40 |
+
const status = document.getElementById("status");
|
| 41 |
+
const caption = document.getElementById("caption");
|
| 42 |
+
|
| 43 |
+
let selectedImage = null;
|
| 44 |
+
let captioner = null;
|
| 45 |
+
|
| 46 |
+
imageInput.addEventListener("change", (event) => {
|
| 47 |
+
selectedImage = event.target.files[0];
|
| 48 |
+
|
| 49 |
+
if (!selectedImage) {
|
| 50 |
+
captionButton.disabled = true;
|
| 51 |
+
preview.style.display = "none";
|
| 52 |
+
return;
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
preview.src = URL.createObjectURL(selectedImage);
|
| 56 |
+
preview.style.display = "block";
|
| 57 |
+
captionButton.disabled = false;
|
| 58 |
+
caption.textContent = "";
|
| 59 |
+
});
|
| 60 |
+
|
| 61 |
+
captionButton.addEventListener("click", async () => {
|
| 62 |
+
try {
|
| 63 |
+
captionButton.disabled = true;
|
| 64 |
+
status.textContent = "Loading model...";
|
| 65 |
+
|
| 66 |
+
if (!captioner) {
|
| 67 |
+
captioner = await pipeline(
|
| 68 |
+
"image-to-text",
|
| 69 |
+
"Xenova/vit-gpt2-image-captioning"
|
| 70 |
+
);
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
status.textContent = "Generating caption...";
|
| 74 |
+
|
| 75 |
+
const result = await captioner(selectedImage);
|
| 76 |
+
|
| 77 |
+
caption.textContent = result[0].generated_text;
|
| 78 |
+
|
| 79 |
+
status.textContent = "Done.";
|
| 80 |
+
} catch (error) {
|
| 81 |
+
console.error(error);
|
| 82 |
+
status.textContent = "Error: " + error.message;
|
| 83 |
+
} finally {
|
| 84 |
+
captionButton.disabled = false;
|
| 85 |
+
}
|
| 86 |
+
});
|
| 87 |
+
</script>
|
| 88 |
+
</body>
|
| 89 |
+
</html>
|