dav729 commited on
Commit
653f120
·
verified ·
1 Parent(s): 30ea2e1

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +89 -19
index.html CHANGED
@@ -1,19 +1,89 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
19
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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>