Spaces:
Running
Running
The login button doesn't work anymore - Follow Up Deployment
Browse files- index.html +47 -32
index.html
CHANGED
@@ -128,7 +128,10 @@
|
|
128 |
let messageCount = 0;
|
129 |
|
130 |
// Initialize
|
131 |
-
|
|
|
|
|
|
|
132 |
|
133 |
// Event Listeners
|
134 |
loginBtn.addEventListener('click', handleLogin);
|
@@ -161,23 +164,35 @@
|
|
161 |
}
|
162 |
}
|
163 |
|
164 |
-
function handleLogin() {
|
165 |
-
|
166 |
-
// For demo purposes, we'll simulate it with a prompt
|
167 |
-
const token = prompt("Enter your Hugging Face token:");
|
168 |
|
169 |
-
if (token)
|
170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
171 |
const demoUser = {
|
172 |
-
name: "
|
173 |
-
avatar: "https://
|
174 |
};
|
175 |
|
176 |
localStorage.setItem('hf_token', token);
|
177 |
localStorage.setItem('hf_user', JSON.stringify(demoUser));
|
178 |
currentUser = demoUser;
|
|
|
|
|
179 |
|
180 |
-
|
|
|
|
|
|
|
|
|
181 |
}
|
182 |
}
|
183 |
|
@@ -198,21 +213,24 @@
|
|
198 |
messageCount = 0;
|
199 |
}
|
200 |
|
201 |
-
function setupAuthenticatedUI(
|
202 |
-
|
203 |
-
|
204 |
-
|
|
|
205 |
// Update UI
|
206 |
loginBtn.classList.add('hidden');
|
207 |
userInfo.classList.remove('hidden');
|
208 |
userAvatar.src = currentUser.avatar;
|
209 |
username.textContent = currentUser.name;
|
210 |
messageInput.disabled = false;
|
|
|
211 |
|
212 |
// Show welcome message if first time
|
213 |
if (messageCount === 0) {
|
214 |
addMessage('assistant', "Hello! I'm an AI assistant. How can I help you today?");
|
215 |
}
|
|
|
216 |
}
|
217 |
|
218 |
async function sendMessage() {
|
@@ -231,39 +249,36 @@
|
|
231 |
try {
|
232 |
isGenerating = true;
|
233 |
|
234 |
-
|
235 |
-
|
|
|
|
|
236 |
|
237 |
-
//
|
238 |
-
|
|
|
|
|
239 |
|
240 |
-
|
241 |
const response = await hfClient.conversational({
|
242 |
model: model,
|
243 |
inputs: {
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
}
|
248 |
});
|
249 |
|
250 |
updateMessage(loadingId, response.generated_text);
|
251 |
chatMessages.scrollTop = chatMessages.scrollHeight;
|
252 |
-
|
253 |
-
// Update token count (approximate)
|
254 |
-
const tokens = Math.ceil(response.generated_text.length / 4);
|
255 |
-
tokenCounter.textContent = `${tokens} tokens`;
|
256 |
-
|
257 |
-
// Update token count (approximate)
|
258 |
-
const tokens = Math.ceil(fullResponse.length / 4);
|
259 |
-
tokenCounter.textContent = `${tokens} tokens`;
|
260 |
|
261 |
} catch (error) {
|
262 |
console.error("Error:", error);
|
263 |
-
updateMessage(loadingId,
|
264 |
} finally {
|
265 |
isGenerating = false;
|
266 |
-
|
267 |
}
|
268 |
}
|
269 |
|
|
|
128 |
let messageCount = 0;
|
129 |
|
130 |
// Initialize
|
131 |
+
document.addEventListener('DOMContentLoaded', () => {
|
132 |
+
checkAuth();
|
133 |
+
messageInput.disabled = false; // Always enable input
|
134 |
+
});
|
135 |
|
136 |
// Event Listeners
|
137 |
loginBtn.addEventListener('click', handleLogin);
|
|
|
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 |
|
|
|
213 |
messageCount = 0;
|
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() {
|
|
|
249 |
try {
|
250 |
isGenerating = true;
|
251 |
|
252 |
+
const token = localStorage.getItem('hf_token');
|
253 |
+
if (!token || !hfClient) {
|
254 |
+
throw new Error("Not authenticated. Please login first.");
|
255 |
+
}
|
256 |
|
257 |
+
// Initialize client if not already done
|
258 |
+
if (!hfClient) {
|
259 |
+
hfClient = new HuggingFaceInference(token);
|
260 |
+
}
|
261 |
|
262 |
+
const model = modelSelect.value;
|
263 |
const response = await hfClient.conversational({
|
264 |
model: model,
|
265 |
inputs: {
|
266 |
+
text: message,
|
267 |
+
past_user_inputs: [],
|
268 |
+
generated_responses: []
|
269 |
}
|
270 |
});
|
271 |
|
272 |
updateMessage(loadingId, response.generated_text);
|
273 |
chatMessages.scrollTop = chatMessages.scrollHeight;
|
274 |
+
tokenCounter.textContent = `${response.conversation.generated_responses[0].length} tokens`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
275 |
|
276 |
} catch (error) {
|
277 |
console.error("Error:", error);
|
278 |
+
updateMessage(loadingId, `Error: ${error.message}`);
|
279 |
} finally {
|
280 |
isGenerating = false;
|
281 |
+
sendBtn.disabled = false;
|
282 |
}
|
283 |
}
|
284 |
|