reach-vb HF Staff commited on
Commit
80c510e
·
verified ·
1 Parent(s): efd4ae9

man, I'm a little tired now, it still doesn't work can you please try to make it work the login doesn't work - Follow Up Deployment

Browse files
Files changed (1) hide show
  1. index.html +63 -40
index.html CHANGED
@@ -154,45 +154,65 @@
154
  });
155
 
156
  // Functions
157
- function checkAuth() {
158
- const token = localStorage.getItem('hf_token');
159
- const user = localStorage.getItem('hf_user');
160
-
161
- if (token && user) {
162
- currentUser = JSON.parse(user);
163
- setupAuthenticatedUI(token);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164
  }
165
  }
166
 
167
  async function handleLogin() {
168
- const token = prompt("Enter your Hugging Face API token (get it from https://huggingface.co/settings/tokens):");
169
-
170
- if (!token) return;
171
-
172
  try {
173
- // Test the token by making a simple request
 
 
 
174
  const testClient = new HuggingFaceInference(token);
175
- await testClient.textGeneration({
176
- model: "gpt2",
177
- inputs: "test"
178
- }, { wait_for_model: true });
 
179
 
180
- const demoUser = {
181
- name: "Hugging Face User",
182
- avatar: "https://huggingface.co/front/assets/huggingface_logo-noborder.svg"
 
 
 
 
 
183
  };
184
 
185
  localStorage.setItem('hf_token', token);
186
- localStorage.setItem('hf_user', JSON.stringify(demoUser));
187
- currentUser = demoUser;
188
  hfClient = testClient;
189
  setupAuthenticatedUI();
190
 
191
- // Show success message
192
- addMessage('assistant', "Successfully logged in! How can I help you today?");
193
  } catch (error) {
194
- console.error("Login failed:", error);
195
- alert("Invalid token. Please check your token and try again.");
196
  }
197
  }
198
 
@@ -214,23 +234,26 @@
214
  }
215
 
216
  function setupAuthenticatedUI() {
217
- const token = localStorage.getItem('hf_token');
218
- if (token) {
 
 
219
  hfClient = new HuggingFaceInference(token);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
220
  }
221
- // Update UI
222
- loginBtn.classList.add('hidden');
223
- userInfo.classList.remove('hidden');
224
- userAvatar.src = currentUser.avatar;
225
- username.textContent = currentUser.name;
226
- messageInput.disabled = false;
227
- sendBtn.disabled = false;
228
-
229
- // Show welcome message if first time
230
- if (messageCount === 0) {
231
- addMessage('assistant', "Hello! I'm an AI assistant. How can I help you today?");
232
- }
233
-
234
  }
235
 
236
  async function sendMessage() {
 
154
  });
155
 
156
  // Functions
157
+ async function checkAuth() {
158
+ try {
159
+ const token = localStorage.getItem('hf_token');
160
+ const user = localStorage.getItem('hf_user');
161
+
162
+ if (token && user) {
163
+ // Verify token is still valid
164
+ const response = await fetch('https://huggingface.co/api/whoami-v2', {
165
+ headers: {
166
+ 'Authorization': `Bearer ${token}`
167
+ }
168
+ });
169
+
170
+ if (!response.ok) {
171
+ throw new Error('Token expired or invalid');
172
+ }
173
+
174
+ currentUser = JSON.parse(user);
175
+ setupAuthenticatedUI();
176
+ }
177
+ } catch (error) {
178
+ console.error("Auth check failed:", error);
179
+ handleLogout();
180
  }
181
  }
182
 
183
  async function handleLogin() {
 
 
 
 
184
  try {
185
+ const token = prompt("Enter your Hugging Face API token (get it from https://huggingface.co/settings/tokens):");
186
+ if (!token) return;
187
+
188
+ // Test the token with a lightweight request
189
  const testClient = new HuggingFaceInference(token);
190
+ const response = await fetch('https://huggingface.co/api/whoami-v2', {
191
+ headers: {
192
+ 'Authorization': `Bearer ${token}`
193
+ }
194
+ });
195
 
196
+ if (!response.ok) {
197
+ throw new Error('Invalid token');
198
+ }
199
+
200
+ const userData = await response.json();
201
+ const user = {
202
+ name: userData.name || 'Hugging Face User',
203
+ avatar: userData.avatarUrl || 'https://huggingface.co/front/assets/huggingface_logo-noborder.svg'
204
  };
205
 
206
  localStorage.setItem('hf_token', token);
207
+ localStorage.setItem('hf_user', JSON.stringify(user));
208
+ currentUser = user;
209
  hfClient = testClient;
210
  setupAuthenticatedUI();
211
 
212
+ addMessage('assistant', `Hello ${user.name}! How can I help you today?`);
 
213
  } catch (error) {
214
+ console.error("Login error:", error);
215
+ alert(`Login failed: ${error.message}. Please check your token and try again.`);
216
  }
217
  }
218
 
 
234
  }
235
 
236
  function setupAuthenticatedUI() {
237
+ try {
238
+ const token = localStorage.getItem('hf_token');
239
+ if (!token) throw new Error('No token found');
240
+
241
  hfClient = new HuggingFaceInference(token);
242
+
243
+ // Update UI
244
+ loginBtn.classList.add('hidden');
245
+ userInfo.classList.remove('hidden');
246
+ userAvatar.src = currentUser.avatar;
247
+ username.textContent = currentUser.name;
248
+ messageInput.disabled = false;
249
+ sendBtn.disabled = messageInput.value.trim() === '';
250
+
251
+ // Scroll to bottom
252
+ chatMessages.scrollTop = chatMessages.scrollHeight;
253
+ } catch (error) {
254
+ console.error("Authentication setup failed:", error);
255
+ handleLogout();
256
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
257
  }
258
 
259
  async function sendMessage() {