Spaces:
Paused
Paused
Update templates/index.html
Browse files- templates/index.html +97 -39
templates/index.html
CHANGED
|
@@ -3,36 +3,106 @@
|
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8" />
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
| 6 |
-
<title>Upload
|
| 7 |
<script src="https://cdn.tailwindcss.com"></script>
|
| 8 |
</head>
|
| 9 |
-
<body class="bg-gray-100
|
| 10 |
-
<div class="
|
| 11 |
-
<h1 class="text-
|
| 12 |
-
<
|
| 13 |
-
<
|
| 14 |
-
<
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
</div>
|
| 20 |
|
| 21 |
<script>
|
| 22 |
-
const form = document.getElementById('uploadForm');
|
| 23 |
const fileInput = document.getElementById('fileInput');
|
| 24 |
-
const
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
return;
|
| 32 |
}
|
| 33 |
-
formData
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
try {
|
| 37 |
const response = await fetch('/upload', {
|
| 38 |
method: 'POST',
|
|
@@ -40,27 +110,15 @@
|
|
| 40 |
});
|
| 41 |
const data = await response.json();
|
| 42 |
if (response.ok && data.result) {
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
| 44 |
} else {
|
| 45 |
-
|
| 46 |
}
|
| 47 |
} catch (error) {
|
| 48 |
-
|
| 49 |
-
}
|
| 50 |
-
});
|
| 51 |
-
|
| 52 |
-
// Listen for paste events (Ctrl+V) to upload an image.
|
| 53 |
-
window.addEventListener('paste', (event) => {
|
| 54 |
-
const items = event.clipboardData.items;
|
| 55 |
-
for (let item of items) {
|
| 56 |
-
if (item.type.indexOf("image") !== -1) {
|
| 57 |
-
const file = item.getAsFile();
|
| 58 |
-
const dt = new DataTransfer();
|
| 59 |
-
dt.items.add(file);
|
| 60 |
-
fileInput.files = dt.files;
|
| 61 |
-
form.dispatchEvent(new Event('submit'));
|
| 62 |
-
break;
|
| 63 |
-
}
|
| 64 |
}
|
| 65 |
});
|
| 66 |
</script>
|
|
|
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8" />
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
| 6 |
+
<title>Upload Images for Generative AI</title>
|
| 7 |
<script src="https://cdn.tailwindcss.com"></script>
|
| 8 |
</head>
|
| 9 |
+
<body class="bg-gray-100">
|
| 10 |
+
<div class="container mx-auto p-4">
|
| 11 |
+
<h1 class="text-3xl font-bold text-center mb-6">Upload Images</h1>
|
| 12 |
+
<div class="flex flex-col items-center">
|
| 13 |
+
<!-- Upload/Paste Area -->
|
| 14 |
+
<div id="upload-area" class="border-2 border-dashed border-gray-400 p-8 rounded w-full max-w-lg bg-white">
|
| 15 |
+
<p class="mb-4 text-center">
|
| 16 |
+
Paste images with <strong>Ctrl + V</strong> or click "Add Image".
|
| 17 |
+
</p>
|
| 18 |
+
<input type="file" id="fileInput" name="files" accept="image/*" multiple class="hidden" />
|
| 19 |
+
<button id="addImageBtn" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded mb-4">
|
| 20 |
+
Add Image
|
| 21 |
+
</button>
|
| 22 |
+
<!-- Thumbnails container -->
|
| 23 |
+
<div id="thumbnails" class="flex flex-wrap gap-4"></div>
|
| 24 |
+
</div>
|
| 25 |
+
|
| 26 |
+
<!-- Send Button -->
|
| 27 |
+
<div class="mt-6">
|
| 28 |
+
<button id="sendBtn" class="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded">
|
| 29 |
+
Send
|
| 30 |
+
</button>
|
| 31 |
+
</div>
|
| 32 |
+
|
| 33 |
+
<!-- Result Display -->
|
| 34 |
+
<div id="resultArea" class="mt-6 w-full max-w-lg"></div>
|
| 35 |
+
</div>
|
| 36 |
</div>
|
| 37 |
|
| 38 |
<script>
|
|
|
|
| 39 |
const fileInput = document.getElementById('fileInput');
|
| 40 |
+
const addImageBtn = document.getElementById('addImageBtn');
|
| 41 |
+
const thumbnails = document.getElementById('thumbnails');
|
| 42 |
+
const sendBtn = document.getElementById('sendBtn');
|
| 43 |
+
const resultArea = document.getElementById('resultArea');
|
| 44 |
|
| 45 |
+
// Array to hold selected image files.
|
| 46 |
+
let selectedFiles = [];
|
| 47 |
+
|
| 48 |
+
// Trigger file input when Add Image button is clicked.
|
| 49 |
+
addImageBtn.addEventListener('click', () => {
|
| 50 |
+
fileInput.click();
|
| 51 |
+
});
|
| 52 |
+
|
| 53 |
+
// Handle file selection.
|
| 54 |
+
fileInput.addEventListener('change', () => {
|
| 55 |
+
for (let file of fileInput.files) {
|
| 56 |
+
selectedFiles.push(file);
|
| 57 |
+
addThumbnail(file);
|
| 58 |
+
}
|
| 59 |
+
fileInput.value = ""; // reset input
|
| 60 |
+
});
|
| 61 |
+
|
| 62 |
+
// Function to add image thumbnail.
|
| 63 |
+
function addThumbnail(file) {
|
| 64 |
+
const reader = new FileReader();
|
| 65 |
+
reader.onload = function(e) {
|
| 66 |
+
const thumbContainer = document.createElement('div');
|
| 67 |
+
thumbContainer.classList.add('relative');
|
| 68 |
+
thumbContainer.innerHTML = `
|
| 69 |
+
<img src="${e.target.result}" alt="${file.name}" class="w-24 h-24 object-cover rounded border" />
|
| 70 |
+
<button class="removeBtn absolute -top-2 -right-2 bg-red-500 text-white rounded-full px-1 text-xs">x</button>
|
| 71 |
+
`;
|
| 72 |
+
thumbnails.appendChild(thumbContainer);
|
| 73 |
+
|
| 74 |
+
// Remove image on click.
|
| 75 |
+
thumbContainer.querySelector('.removeBtn').addEventListener('click', () => {
|
| 76 |
+
selectedFiles = selectedFiles.filter(f => f !== file);
|
| 77 |
+
thumbContainer.remove();
|
| 78 |
+
});
|
| 79 |
+
}
|
| 80 |
+
reader.readAsDataURL(file);
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
// Listen for paste events (Ctrl+V) to add images.
|
| 84 |
+
window.addEventListener('paste', (event) => {
|
| 85 |
+
const items = event.clipboardData.items;
|
| 86 |
+
for (let item of items) {
|
| 87 |
+
if (item.type.indexOf("image") !== -1) {
|
| 88 |
+
const file = item.getAsFile();
|
| 89 |
+
selectedFiles.push(file);
|
| 90 |
+
addThumbnail(file);
|
| 91 |
+
}
|
| 92 |
+
}
|
| 93 |
+
});
|
| 94 |
+
|
| 95 |
+
// Handle Send button click.
|
| 96 |
+
sendBtn.addEventListener('click', async () => {
|
| 97 |
+
if (selectedFiles.length === 0) {
|
| 98 |
+
alert("Please add at least one image.");
|
| 99 |
return;
|
| 100 |
}
|
| 101 |
+
const formData = new FormData();
|
| 102 |
+
selectedFiles.forEach(file => {
|
| 103 |
+
formData.append('files', file);
|
| 104 |
+
});
|
| 105 |
+
resultArea.innerHTML = `<p class="text-gray-500">Processing...</p>`;
|
| 106 |
try {
|
| 107 |
const response = await fetch('/upload', {
|
| 108 |
method: 'POST',
|
|
|
|
| 110 |
});
|
| 111 |
const data = await response.json();
|
| 112 |
if (response.ok && data.result) {
|
| 113 |
+
resultArea.innerHTML = `<pre class="bg-gray-200 p-4 rounded whitespace-pre-wrap">${data.result}</pre>`;
|
| 114 |
+
// Reset the UI.
|
| 115 |
+
selectedFiles = [];
|
| 116 |
+
thumbnails.innerHTML = "";
|
| 117 |
} else {
|
| 118 |
+
resultArea.innerHTML = `<p class="text-red-500">${data.detail || 'No result received.'}</p>`;
|
| 119 |
}
|
| 120 |
} catch (error) {
|
| 121 |
+
resultArea.innerHTML = `<p class="text-red-500">Error processing the images: ${error.message}</p>`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
}
|
| 123 |
});
|
| 124 |
</script>
|