gaur3009 commited on
Commit
ab523da
·
verified ·
1 Parent(s): 567afb4

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +76 -64
index.html CHANGED
@@ -318,7 +318,7 @@
318
  const historyGrid = document.getElementById('history-grid');
319
 
320
  // API Endpoint
321
- const API_URL = 'https://multimodalart-cosxl.hf.space/run/predict';
322
 
323
  // Current state
324
  let uploadedImage = null;
@@ -405,52 +405,65 @@
405
  generateBtn.disabled = true;
406
 
407
  try {
408
- // Prepare the form data
409
- const formData = new FormData();
410
- formData.append('data', JSON.stringify({
411
- 'fn_index': 1,
412
- 'data': [
413
- uploadedImage, // image
414
- colorPrompt.value, // prompt
415
- negativePrompt.value, // negative prompt
416
- Number(guidanceScale.value), // guidance scale
417
- Number(steps.value) // steps
418
- ]
419
- }));
420
-
421
- // Call the CosXL API
422
- const response = await fetch(API_URL, {
423
- method: 'POST',
424
- body: formData
425
- });
426
-
427
- if (!response.ok) {
428
- throw new Error(`API request failed with status ${response.status}`);
429
- }
430
-
431
- const result = await response.json();
432
-
433
- if (result.error) {
434
- throw new Error(result.error);
435
- }
436
-
437
- // Display results
438
- originalImage.src = uploadedImage;
439
- resultImage.src = `data:image/png;base64,${result.data[0]}`;
440
- emptyState.classList.add('hidden');
441
- resultContainer.classList.remove('hidden');
442
-
443
- // Add to history
444
- addToHistory(uploadedImage, `data:image/png;base64,${result.data[0]}`);
445
- historySection.classList.remove('hidden');
446
- } catch (error) {
447
- console.error('Error:', error);
448
- alert(`An error occurred: ${error.message}`);
449
- } finally {
450
- // Reset button state
451
- generateIcon.classList.remove('hidden');
452
- loadingIcon.classList.add('hidden');
453
- generateBtn.disabled = false;
 
 
 
 
 
 
 
 
 
 
 
 
 
454
  }
455
  });
456
 
@@ -466,24 +479,23 @@
466
  });
467
 
468
  function addToHistory(originalUrl, resultUrl) {
469
- const historyItem = document.createElement('div');
470
- historyItem.className = 'history-item bg-white p-2 rounded-lg shadow-sm cursor-pointer';
471
- historyItem.innerHTML = `
472
- <img src="${resultUrl}" class="w-full h-auto rounded" alt="History item">
473
- `;
474
- historyItem.addEventListener('click', () => {
475
- originalImage.src = originalUrl;
476
- resultImage.src = resultUrl;
477
- emptyState.classList.add('hidden');
478
- resultContainer.classList.remove('hidden');
479
- });
480
- historyGrid.insertBefore(historyItem, historyGrid.firstChild);
481
-
482
- // Limit history to 6 items
483
- if (historyGrid.children.length > 6) {
484
- historyGrid.removeChild(historyGrid.lastChild);
485
- }
486
  }
 
487
  });
488
  </script>
489
  </body>
 
318
  const historyGrid = document.getElementById('history-grid');
319
 
320
  // API Endpoint
321
+ const API_URL = 'https://multimodalart-cosxl.hf.space/api/predict';
322
 
323
  // Current state
324
  let uploadedImage = null;
 
405
  generateBtn.disabled = true;
406
 
407
  try {
408
+ // Convert image to base64
409
+ const imageBase64 = await fileToBase64(uploadedFile);
410
+
411
+ // Construct API payload
412
+ const payload = {
413
+ data: [
414
+ imageBase64, // Image data
415
+ colorPrompt.value, // Positive prompt
416
+ negativePrompt.value, // Negative prompt
417
+ Number(guidanceScale.value), // Guidance scale
418
+ Number(steps.value) // Inference steps
419
+ ]
420
+ };
421
+
422
+ // API request
423
+ const response = await fetch(API_URL, {
424
+ method: 'POST',
425
+ headers: {
426
+ 'Content-Type': 'application/json',
427
+ },
428
+ body: JSON.stringify(payload)
429
+ });
430
+
431
+ // Handle response errors
432
+ if (!response.ok) {
433
+ const errorText = await response.text();
434
+ throw new Error(`API Error: ${response.status} - ${errorText}`);
435
+ }
436
+
437
+ const result = await response.json();
438
+
439
+ // Handle different response formats
440
+ let resultImageData;
441
+ if (result.data && result.data[0]) {
442
+ resultImageData = result.data[0];
443
+ } else if (result.output) {
444
+ resultImageData = result.output;
445
+ } else {
446
+ throw new Error('Unexpected API response format');
447
+ }
448
+
449
+ // Display results
450
+ originalImage.src = URL.createObjectURL(uploadedFile);
451
+ resultImage.src = `data:image/png;base64,${resultImageData}`;
452
+ emptyState.classList.add('hidden');
453
+ resultContainer.classList.remove('hidden');
454
+
455
+ // Add to history
456
+ addToHistory(URL.createObjectURL(uploadedFile), `data:image/png;base64,${resultImageData}`);
457
+ historySection.classList.remove('hidden');
458
+
459
+ } catch (error) {
460
+ console.error('API Error:', error);
461
+ alert(`Error processing image: ${error.message}`);
462
+ } finally {
463
+ generateIcon.classList.remove('hidden');
464
+ loadingIcon.classList.add('hidden');
465
+ generateBtn.disabled = false;
466
+
467
  }
468
  });
469
 
 
479
  });
480
 
481
  function addToHistory(originalUrl, resultUrl) {
482
+ const historyItem = document.createElement('div');
483
+ historyItem.className = 'history-item bg-white p-2 rounded-lg shadow-sm cursor-pointer';
484
+ historyItem.innerHTML = `
485
+ <img src="${resultUrl}" class="w-full h-auto rounded" alt="History item">
486
+ `;
487
+ historyItem.addEventListener('click', () => {
488
+ originalImage.src = originalUrl;
489
+ resultImage.src = resultUrl;
490
+ emptyState.classList.add('hidden');
491
+ resultContainer.classList.remove('hidden');
492
+ });
493
+ historyGrid.insertBefore(historyItem, historyGrid.firstChild);
494
+
495
+ if (historyGrid.children.length > 6) {
496
+ historyGrid.removeChild(historyGrid.lastChild);
 
 
497
  }
498
+ }
499
  });
500
  </script>
501
  </body>