Dhanavarshitha commited on
Commit
0877ca8
·
verified ·
1 Parent(s): a9af1c1

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +87 -91
index.html CHANGED
@@ -4,7 +4,8 @@
4
  <head>
5
  <meta charset="UTF-8">
6
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
- <title>Image Classification using Hugging Face</title>
 
8
 
9
  <style>
10
  body {
@@ -28,18 +29,16 @@
28
  }
29
 
30
  input {
31
- margin: 15px;
32
- padding: 10px;
33
  }
34
 
35
  button {
36
  background: #4f46e5;
37
  color: white;
38
- border: none;
39
  padding: 12px 25px;
 
40
  border-radius: 8px;
41
  cursor: pointer;
42
- margin: 10px;
43
  }
44
 
45
  button:hover {
@@ -68,139 +67,136 @@
68
 
69
  #status {
70
  color: #444;
71
- margin-top: 15px;
72
  }
73
  </style>
74
  </head>
75
 
76
  <body>
77
 
78
- <div class="container">
79
 
80
- <h1>Image Classification</h1>
81
 
82
- <p>Upload an image to identify its contents using AI.</p>
83
 
84
- <input type="password" id="token"
85
- placeholder="Enter Hugging Face access token"
86
- style="width:85%">
87
 
88
- <br>
89
 
90
- <input type="file" id="imageInput"
91
- accept="image/*">
92
 
93
- <br>
94
 
95
- <img id="preview" alt="Image Preview">
96
 
97
- <br>
98
 
99
- <button onclick="classifyImage()">
100
- Classify Image
101
- </button>
102
 
103
- <p id="status"></p>
104
 
105
- <div id="result"></div>
106
 
107
- </div>
 
108
 
109
- <script>
 
110
 
111
- const imageInput = document.getElementById("imageInput");
112
- const preview = document.getElementById("preview");
 
 
 
113
 
114
- let selectedImage = null;
 
115
 
116
- // Preview uploaded image
117
- imageInput.addEventListener("change", function() {
118
 
119
- selectedImage = this.files[0];
120
 
121
- if (selectedImage) {
122
- preview.src = URL.createObjectURL(selectedImage);
123
- preview.style.display = "block";
124
 
125
- document.getElementById("result").innerHTML = "";
126
- document.getElementById("status").innerText = "";
127
- }
128
- });
 
 
 
 
129
 
130
- // Image Classification
131
- async function classifyImage() {
132
 
133
- const token = document.getElementById("token").value.trim();
134
 
135
- const status = document.getElementById("status");
136
- const result = document.getElementById("result");
 
 
 
 
137
 
138
- if (!token) {
139
- status.innerText = "Please enter your Hugging Face token.";
140
- return;
141
- }
142
 
143
- if (!selectedImage) {
144
- status.innerText = "Please upload an image.";
145
- return;
146
- }
147
 
148
- status.innerText = "Analyzing image... Please wait.";
149
- result.innerHTML = "";
150
 
151
- try {
152
 
153
- const response = await fetch(
154
- "https://router.huggingface.co/hf-inference/models/google/vit-base-patch16-224",
155
- {
156
- method: "POST",
157
 
158
- headers: {
159
- "Authorization": "Bearer " + token,
160
- "Content-Type": selectedImage.type
161
- },
162
 
163
- body: selectedImage
164
- }
165
- );
166
 
167
- const data = await response.json();
 
 
168
 
169
- if (!response.ok) {
170
- throw new Error(
171
- data.error || "Classification request failed. Check your token and model availability."
172
- );
173
- }
174
 
175
- if (!Array.isArray(data)) {
176
- throw new Error("Unexpected response from the model.");
177
- }
178
 
179
- status.innerText = "Classification completed!";
180
 
181
- result.innerHTML = "<h3>Classification Results</h3>";
182
 
183
- data.slice(0, 5).forEach(item => {
184
 
185
- const percentage = (item.score * 100).toFixed(2);
 
 
 
 
 
 
186
 
187
- result.innerHTML += `
188
- <div class="prediction">
189
- <strong>${item.label}</strong>
190
- <br>
191
- Confidence: ${percentage}%
192
- </div>
193
- `;
194
- });
195
 
196
- } catch (error) {
197
 
198
- status.innerText = "Error: " + error.message;
199
 
200
- }
201
- }
 
 
 
 
 
 
202
 
203
- </script>
204
 
205
  </body>
206
  </html>
 
4
  <head>
5
  <meta charset="UTF-8">
6
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
+
8
+ <title>Hugging Face Image Classification</title>
9
 
10
  <style>
11
  body {
 
29
  }
30
 
31
  input {
32
+ margin: 20px;
 
33
  }
34
 
35
  button {
36
  background: #4f46e5;
37
  color: white;
 
38
  padding: 12px 25px;
39
+ border: none;
40
  border-radius: 8px;
41
  cursor: pointer;
 
42
  }
43
 
44
  button:hover {
 
67
 
68
  #status {
69
  color: #444;
70
+ margin: 20px;
71
  }
72
  </style>
73
  </head>
74
 
75
  <body>
76
 
77
+ <div class="container">
78
 
79
+ <h1>Image Classification</h1>
80
 
81
+ <p>Upload an image to identify its contents using AI.</p>
82
 
83
+ <input type="file" id="imageInput" accept="image/*">
 
 
84
 
85
+ <br>
86
 
87
+ <img id="preview" alt="Image Preview">
 
88
 
89
+ <br>
90
 
91
+ <button id="classifyBtn">Classify Image</button>
92
 
93
+ <p id="status">Loading AI library...</p>
94
 
95
+ <div id="result"></div>
 
 
96
 
97
+ </div>
98
 
99
+ <script type="module">
100
 
101
+ import { pipeline } from
102
+ 'https://cdn.jsdelivr.net/npm/@huggingface/transformers';
103
 
104
+ let classifier = null;
105
+ let selectedImage = null;
106
 
107
+ const imageInput = document.getElementById("imageInput");
108
+ const preview = document.getElementById("preview");
109
+ const status = document.getElementById("status");
110
+ const result = document.getElementById("result");
111
+ const button = document.getElementById("classifyBtn");
112
 
113
+ // Upload image and preview
114
+ imageInput.addEventListener("change", function() {
115
 
116
+ selectedImage = this.files[0];
 
117
 
118
+ if (selectedImage) {
119
 
120
+ preview.src = URL.createObjectURL(selectedImage);
121
+ preview.style.display = "block";
 
122
 
123
+ result.innerHTML = "";
124
+ status.innerText = "Image uploaded successfully!";
125
+
126
+ }
127
+ });
128
+
129
+ // Load AI model
130
+ async function loadModel() {
131
 
132
+ if (!classifier) {
 
133
 
134
+ status.innerText = "Loading AI model. Please wait...";
135
 
136
+ classifier = await pipeline(
137
+ "image-classification",
138
+ "Xenova/vit-base-patch16-224"
139
+ );
140
+ }
141
+ }
142
 
143
+ // Classify image
144
+ button.addEventListener("click", async function() {
 
 
145
 
146
+ if (!selectedImage) {
147
+ status.innerText = "Please upload an image first!";
148
+ return;
149
+ }
150
 
151
+ button.disabled = true;
152
+ result.innerHTML = "";
153
 
154
+ try {
155
 
156
+ await loadModel();
 
 
 
157
 
158
+ status.innerText = "Analyzing image...";
 
 
 
159
 
160
+ const imageURL = URL.createObjectURL(selectedImage);
 
 
161
 
162
+ const predictions = await classifier(imageURL, {
163
+ top_k: 5
164
+ });
165
 
166
+ URL.revokeObjectURL(imageURL);
 
 
 
 
167
 
168
+ status.innerText = "Image classification completed!";
 
 
169
 
170
+ result.innerHTML = "<h3>Classification Results</h3>";
171
 
172
+ predictions.forEach(item => {
173
 
174
+ const percentage = (item.score * 100).toFixed(2);
175
 
176
+ result.innerHTML += `
177
+ <div class="prediction">
178
+ <strong>${item.label}</strong>
179
+ <br>
180
+ Confidence: ${percentage}%
181
+ </div>
182
+ `;
183
 
184
+ });
 
 
 
 
 
 
 
185
 
186
+ } catch (error) {
187
 
188
+ status.innerText = "Error: " + error.message;
189
 
190
+ }
191
+
192
+ button.disabled = false;
193
+
194
+ });
195
+
196
+ // Initial status
197
+ status.innerText = "Upload an image to get started.";
198
 
199
+ </script>
200
 
201
  </body>
202
  </html>