hardik90 commited on
Commit
8dc5e27
1 Parent(s): f86bee5

I've maintained the structure and functionality of the code while making it more compact and readable.

Browse files
Files changed (1) hide show
  1. index.html +31 -85
index.html CHANGED
@@ -1,120 +1,71 @@
1
  <!DOCTYPE html>
2
- <html>
3
  <head>
4
- <meta charset="UTF-8"/>
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
6
  <script src="https://cdn.tailwindcss.com"></script>
7
- <!-- polyfill for firefox + import maps -->
8
  <script src="https://unpkg.com/es-module-shims@1.7.0/dist/es-module-shims.js"></script>
9
  <script type="importmap">
10
- {
11
- "imports": {
12
- "@huggingface/inference": "https://cdn.jsdelivr.net/npm/@huggingface/inference@2.1.1/+esm"
13
- }
14
- }
15
  </script>
16
  </head>
17
  <body>
18
  <form class="w-[90%] mx-auto pt-8" onsubmit="launch(); return false;">
19
  <h1 class="text-3xl font-bold">
20
- <span
21
- class="bg-clip-text text-transparent bg-gradient-to-r from-pink-500 to-violet-500"
22
- >
23
- Document & visual question answering demo with
24
- <a href="https://github.com/huggingface/huggingface.js">
25
- <kbd>@huggingface/inference</kbd>
26
- </a>
27
- </span>
28
  </h1>
29
 
30
- <p class="mt-8">
31
- First, input your token if you have one! Otherwise, you may encounter
32
- rate limiting. You can create a token for free at
33
- <a
34
- target="_blank"
35
- href="https://huggingface.co/settings/tokens"
36
- class="underline text-blue-500"
37
- >hf.co/settings/tokens</a
38
- >
39
  </p>
40
 
41
- <input
42
- type="text"
43
- id="token"
44
- class="rounded border-2 border-blue-500 shadow-md px-3 py-2 w-96 mt-6"
45
- placeholder="token (optional)"
46
- />
47
-
48
- <p class="mt-8">
49
- Pick the model type and the model you want to run. Check out models for
50
- <a
51
- href="https://huggingface.co/tasks/document-question-answering"
52
- class="underline text-blue-500"
53
- target="_blank"
54
- >
55
- document</a
56
- > and
57
- <a
58
- href="https://huggingface.co/tasks/visual-question-answering"
59
- class="underline text-blue-500"
60
- target="_blank"
61
- >image</a> question answering.
62
  </p>
63
 
64
  <div class="space-x-2 flex text-sm mt-8">
65
  <label>
66
- <input class="sr-only peer" name="type" type="radio" value="document" onclick="update_model(this.value)" checked />
67
  <div class="px-3 py-3 rounded-lg shadow-md flex items-center justify-center text-slate-700 bg-gradient-to-r peer-checked:font-semibold peer-checked:from-pink-500 peer-checked:to-violet-500 peer-checked:text-white">
68
  Document
69
  </div>
70
  </label>
71
  <label>
72
- <input class="sr-only peer" name="type" type="radio" value="image" onclick="update_model(this.value)" />
73
  <div class="px-3 py-3 rounded-lg shadow-md flex items-center justify-center text-slate-700 bg-gradient-to-r peer-checked:font-semibold peer-checked:from-pink-500 peer-checked:to-violet-500 peer-checked:text-white">
74
  Image
75
  </div>
76
  </label>
77
  </div>
78
 
79
- <input
80
- id="model"
81
- class="rounded border-2 border-blue-500 shadow-md px-3 py-2 w-96 mt-6"
82
- value="impira/layoutlm-document-qa"
83
- required
84
- />
85
 
86
  <p class="mt-8">The input image</p>
87
 
88
- <input type="file" required accept="image/*"
89
- class="rounded border-blue-500 shadow-md px-3 py-2 w-96 mt-6 block"
90
- rows="5"
91
- id="image"
92
- />
93
 
94
  <p class="mt-8">The question</p>
95
 
96
- <input
97
- type="text"
98
- id="question"
99
- class="rounded border-2 border-blue-500 shadow-md px-3 py-2 w-96 mt-6"
100
- required
101
- />
102
 
103
- <button
104
- id="submit"
105
- class="my-8 bg-green-500 rounded py-3 px-5 text-white shadow-md disabled:bg-slate-300"
106
- >
107
- Run
108
- </button>
109
 
110
  <p class="text-gray-400 text-sm">Output logs</p>
111
- <div id="logs" class="bg-gray-100 rounded p-3 mb-8 text-sm">
112
- Output will be here
113
- </div>
114
 
115
- <p>Check out the <a class="underline text-blue-500"
116
- href="https://huggingface.co/spaces/huggingfacejs/doc-vis-qa/blob/main/index.html"
117
- target="_blank">source code</a></p>
118
  </form>
119
 
120
  <script type="module">
@@ -133,9 +84,7 @@
133
  }
134
  running = true;
135
  try {
136
- const hf = new HfInference(
137
- document.getElementById("token").value.trim() || undefined
138
- );
139
  const model = document.getElementById("model").value.trim();
140
  const model_type = document.querySelector("[name=type]:checked").value;
141
  const image = document.getElementById("image").files[0];
@@ -143,10 +92,7 @@
143
  document.getElementById("logs").textContent = "";
144
 
145
  const method = model_type === "document" ? hf.documentQuestionAnswering : hf.visualQuestionAnswering;
146
- const {answer, score} = await method({model, inputs: {
147
- image, question
148
- }});
149
-
150
  document.getElementById("logs").textContent = answer + ": " + score;
151
  } catch (err) {
152
  alert("Error: " + err.message);
 
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
  <script src="https://cdn.tailwindcss.com"></script>
 
7
  <script src="https://unpkg.com/es-module-shims@1.7.0/dist/es-module-shims.js"></script>
8
  <script type="importmap">
9
+ {
10
+ "imports": {
11
+ "@huggingface/inference": "https://cdn.jsdelivr.net/npm/@huggingface/inference@2.1.1/+esm"
12
+ }
13
+ }
14
  </script>
15
  </head>
16
  <body>
17
  <form class="w-[90%] mx-auto pt-8" onsubmit="launch(); return false;">
18
  <h1 class="text-3xl font-bold">
19
+ <span class="bg-clip-text text-transparent bg-gradient-to-r from-pink-500 to-violet-500">
20
+ Document & visual question answering demo with
21
+ <a href="https://github.com/huggingface/huggingface.js">
22
+ <kbd>@huggingface/inference</kbd>
23
+ </a>
24
+ </span>
 
 
25
  </h1>
26
 
27
+ <p class="mt-8">First, input your token if you have one! Otherwise, you may encounter rate limiting. You can create a token for free at
28
+ <a href="https://huggingface.co/settings/tokens" target="_blank" class="underline text-blue-500">hf.co/settings/tokens</a>
 
 
 
 
 
 
 
29
  </p>
30
 
31
+ <input type="text" id="token" class="rounded border-2 border-blue-500 shadow-md px-3 py-2 w-96 mt-6" placeholder="token (optional)">
32
+
33
+ <p class="mt-8">Pick the model type and the model you want to run. Check out models for
34
+ <a href="https://huggingface.co/tasks/document-question-answering" class="underline text-blue-500" target="_blank">document</a> and
35
+ <a href="https://huggingface.co/tasks/visual-question-answering" class="underline text-blue-500" target="_blank">image</a> question answering.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  </p>
37
 
38
  <div class="space-x-2 flex text-sm mt-8">
39
  <label>
40
+ <input class="sr-only peer" name="type" type="radio" value="document" onclick="update_model(this.value)" checked>
41
  <div class="px-3 py-3 rounded-lg shadow-md flex items-center justify-center text-slate-700 bg-gradient-to-r peer-checked:font-semibold peer-checked:from-pink-500 peer-checked:to-violet-500 peer-checked:text-white">
42
  Document
43
  </div>
44
  </label>
45
  <label>
46
+ <input class="sr-only peer" name="type" type="radio" value="image" onclick="update_model(this.value)">
47
  <div class="px-3 py-3 rounded-lg shadow-md flex items-center justify-center text-slate-700 bg-gradient-to-r peer-checked:font-semibold peer-checked:from-pink-500 peer-checked:to-violet-500 peer-checked:text-white">
48
  Image
49
  </div>
50
  </label>
51
  </div>
52
 
53
+ <input id="model" class="rounded border-2 border-blue-500 shadow-md px-3 py-2 w-96 mt-6" value="impira/layoutlm-document-qa" required>
 
 
 
 
 
54
 
55
  <p class="mt-8">The input image</p>
56
 
57
+ <input type="file" required accept="image/*" class="rounded border-blue-500 shadow-md px-3 py-2 w-96 mt-6 block" rows="5" id="image">
 
 
 
 
58
 
59
  <p class="mt-8">The question</p>
60
 
61
+ <input type="text" id="question" class="rounded border-2 border-blue-500 shadow-md px-3 py-2 w-96 mt-6" required>
 
 
 
 
 
62
 
63
+ <button id="submit" class="my-8 bg-green-500 rounded py-3 px-5 text-white shadow-md disabled:bg-slate-300">Run</button>
 
 
 
 
 
64
 
65
  <p class="text-gray-400 text-sm">Output logs</p>
66
+ <div id="logs" class="bg-gray-100 rounded p-3 mb-8 text-sm">Output will be here</div>
 
 
67
 
68
+ <p>Check out the <a class="underline text-blue-500" href="https://huggingface.co/spaces/huggingfacejs/doc-vis-qa/blob/main/index.html" target="_blank">source code</a></p>
 
 
69
  </form>
70
 
71
  <script type="module">
 
84
  }
85
  running = true;
86
  try {
87
+ const hf = new HfInference(document.getElementById("token").value.trim() || undefined);
 
 
88
  const model = document.getElementById("model").value.trim();
89
  const model_type = document.querySelector("[name=type]:checked").value;
90
  const image = document.getElementById("image").files[0];
 
92
  document.getElementById("logs").textContent = "";
93
 
94
  const method = model_type === "document" ? hf.documentQuestionAnswering : hf.visualQuestionAnswering;
95
+ const {answer, score} = await method({model, inputs: {image, question}});
 
 
 
96
  document.getElementById("logs").textContent = answer + ": " + score;
97
  } catch (err) {
98
  alert("Error: " + err.message);