Ashrafb commited on
Commit
07245cf
1 Parent(s): cb4762a

Update static/index.html

Browse files
Files changed (1) hide show
  1. static/index.html +130 -53
static/index.html CHANGED
@@ -316,68 +316,145 @@ input[type="number"]#scale:focus {
316
  <button type="button" onclick="uploadImage()">Enhance Image</button>
317
  </form>
318
 
319
- <div id="resultImage">
320
- <div id="estimatedTime" style="display: none;">Estimated time: 90s</div>
321
- <div id="loadingSpinner" style="display: none;"></div>
322
- <div id="resultContainer"></div>
323
- </div>
 
 
324
 
325
  <script>
326
- function previewImage(event) {
327
- const file = event.target.files[0];
328
- const imagePreview = document.getElementById('image-preview');
329
- imagePreview.innerHTML = `<img src="${URL.createObjectURL(file)}" alt="Uploaded Image" style="max-width: 300px; max-height: 300px;">`;
330
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
331
 
332
  async function uploadImage() {
333
- const fileInput = document.getElementById('image');
334
- const versionInput = document.getElementById('version');
335
- const scaleInput = document.getElementById('scale');
336
-
337
- // Check if the file input is empty
338
- if (!fileInput.files[0]) {
339
- document.getElementById('resultContainer').innerHTML = `<p style="color: white;">Please upload an image.</p>`;
340
- return;
341
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
342
 
343
- // Clear the result container
344
- document.getElementById('resultContainer').innerHTML = "";
345
-
346
- const formData = new FormData();
347
- formData.append('file', fileInput.files[0]);
348
- formData.append('version', versionInput.value);
349
- formData.append('scale', scaleInput.value);
350
-
351
- // Display loading spinner
352
- const loadingSpinner = document.getElementById('loadingSpinner');
353
- loadingSpinner.style.display = 'block';
354
- const estimatedTime = document.getElementById('estimatedTime');
355
- estimatedTime.style.display = 'block';
356
- try {
357
- const response = await fetch('/upload/', {
358
- method: 'POST',
359
- body: formData
360
- });
361
-
362
- // Hide loading spinner
363
- loadingSpinner.style.display = 'none';
364
- estimatedTime.style.display = 'none';
365
- if (response.ok) {
366
- const resultContainer = document.getElementById('resultContainer');
367
- const data = await response.json();
368
- const sketchImage = document.createElement('img');
369
- sketchImage.style.maxWidth = '100%'; // Adjust this value as needed
370
- sketchImage.style.maxHeight = '100%'; // Adjust this value as needed
371
- sketchImage.src = data.sketch_image_base64;
372
- resultContainer.appendChild(sketchImage);
373
  } else {
374
- document.getElementById('resultContainer').innerHTML = `<p style="color: white;">Oops! Something went wrong. Please try again later.</p>`;
 
 
 
375
  }
376
- } catch (error) {
377
- console.error('Error:', error);
378
- document.getElementById('resultContainer').innerHTML = `<p style="color: white;">Oops! Something went wrong. Please try again later.</p>`;
 
 
 
 
 
 
 
 
 
379
  }
380
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
381
  </script>
382
  </body>
383
  </html>
 
316
  <button type="button" onclick="uploadImage()">Enhance Image</button>
317
  </form>
318
 
319
+ <div id="resultImage">
320
+ <div id="estimatedTime" style="display: none;">Estimated time: 90s</div>
321
+ <div id="loadingSpinner" style="display: none;"></div>
322
+ <div id="resultContainer"></div>
323
+
324
+ </div>
325
+ <button id="downloadButton" style="display: block ;" onclick="downloadResultImage()">Download </button>
326
 
327
  <script>
328
+ function previewImage(event) {
329
+ const file = event.target.files[0];
330
+ const imagePreview = document.getElementById('image-preview');
331
+ const uploadButton = document.querySelector('button');
332
+
333
+ // Check if file size is larger than 500 MB (500 * 1024 * 1024 bytes)
334
+ if (file.size > 1000 * 1024) {
335
+ imagePreview.innerHTML = `<p style="color: white;">Please upload an image smaller than 1000 KB.</p>`;
336
+ uploadButton.disabled = true; // Disable the button
337
+ return;
338
+ }
339
+
340
+ // If file size is within limit, enable the button and display image preview
341
+ imagePreview.innerHTML = `<img src="${URL.createObjectURL(file)}" alt="Uploaded Image" style="max-width: 300px; max-height: 300px;">`;
342
+ uploadButton.disabled = false; // Enable the button
343
+ }
344
+
345
+
346
 
347
  async function uploadImage() {
348
+ const fileInput = document.getElementById('image');
349
+ const versionInput = document.getElementById('version');
350
+ const scaleInput = document.getElementById('scale');
351
+
352
+ // Check if the file input is empty
353
+ if (!fileInput.files[0]) {
354
+ document.getElementById('resultContainer').innerHTML = `<p style="color: white;">Please upload an image.</p>`;
355
+ return;
356
+ }
357
+
358
+ // Clear the result container
359
+ document.getElementById('resultContainer').innerHTML = "";
360
+
361
+ // Compress the image to 500 KB before sending
362
+ const compressedFile = await compressImage(fileInput.files[0]);
363
+
364
+ const formData = new FormData();
365
+ formData.append('file', compressedFile);
366
+ formData.append('version', versionInput.value);
367
+ formData.append('scale', scaleInput.value);
368
+
369
+ // Display loading spinner
370
+ const loadingSpinner = document.getElementById('loadingSpinner');
371
+ loadingSpinner.style.display = 'block';
372
+ const estimatedTime = document.getElementById('estimatedTime');
373
+ estimatedTime.style.display = 'block';
374
+ try {
375
+ const response = await fetch('https://ashrafb-furdockgrtest.hf.space/upload/', {
376
+ method: 'POST',
377
+ body: formData
378
+ });
379
+
380
+ // Hide loading spinner
381
+ loadingSpinner.style.display = 'none';
382
+ estimatedTime.style.display = 'none';
383
+ if (response.ok) {
384
+ const resultContainer = document.getElementById('resultContainer');
385
+ const data = await response.json();
386
+ const sketchImage = document.createElement('img');
387
+ sketchImage.style.maxWidth = '100%'; // Adjust this value as needed
388
+ sketchImage.style.maxHeight = '100%'; // Adjust this value as needed
389
+ sketchImage.src = data.sketch_image_base64;
390
+ resultContainer.appendChild(sketchImage);
391
+ } else {
392
+ document.getElementById('resultContainer').innerHTML = `<p style="color: white;">Oops! Something went wrong. Please try again later.</p>`;
393
+ }
394
+ } catch (error) {
395
+ console.error('Error:', error);
396
+ document.getElementById('resultContainer').innerHTML = `<p style="color: white;">Oops! Something went wrong. Please try again later.</p>`;
397
+ }
398
+ }
399
 
400
+ async function compressImage(file) {
401
+ return new Promise((resolve, reject) => {
402
+ const reader = new FileReader();
403
+ reader.onload = function(event) {
404
+ const img = new Image();
405
+ img.src = event.target.result;
406
+
407
+ img.onload = function() {
408
+ const canvas = document.createElement('canvas');
409
+ const ctx = canvas.getContext('2d');
410
+
411
+ // Calculate the new dimensions to resize the image while maintaining aspect ratio
412
+ const maxWidth = 800;
413
+ const maxHeight = 600;
414
+ let width = img.width;
415
+ let height = img.height;
416
+
417
+ if (width > height) {
418
+ if (width > maxWidth) {
419
+ height *= maxWidth / width;
420
+ width = maxWidth;
421
+ }
 
 
 
 
 
 
 
 
422
  } else {
423
+ if (height > maxHeight) {
424
+ width *= maxHeight / height;
425
+ height = maxHeight;
426
+ }
427
  }
428
+
429
+ // Set the canvas dimensions
430
+ canvas.width = width;
431
+ canvas.height = height;
432
+
433
+ // Draw the image on the canvas with the new dimensions
434
+ ctx.drawImage(img, 0, 0, width, height);
435
+
436
+ // Convert canvas content to a blob
437
+ canvas.toBlob((blob) => {
438
+ resolve(blob);
439
+ }, 'image/jpeg', 0.7); // Adjust quality as needed (0.7 is 70% quality)
440
  }
441
  }
442
+
443
+ // Read the file as data URL
444
+ reader.readAsDataURL(file);
445
+ });
446
+ }
447
+
448
+ function downloadResultImage() {
449
+ const resultImage = document.getElementById('resultContainer').querySelector('img');
450
+ const link = document.createElement('a');
451
+ link.href = resultImage.src;
452
+ link.download = 'result_image.png';
453
+ document.body.appendChild(link);
454
+ link.click();
455
+ document.body.removeChild(link);
456
+ }
457
+
458
  </script>
459
  </body>
460
  </html>