Spaces:
Sleeping
Sleeping
Create templates/index.html
Browse files- templates/index.html +120 -0
templates/index.html
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8" />
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
| 6 |
+
<title>Image to Question Generator</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">
|
| 12 |
+
Image to Question Generator
|
| 13 |
+
</h1>
|
| 14 |
+
<div class="flex flex-col items-center">
|
| 15 |
+
<!-- Upload / Paste Area -->
|
| 16 |
+
<div id="upload-area" class="border-2 border-dashed border-gray-400 p-8 rounded w-full max-w-lg text-center bg-white">
|
| 17 |
+
<p class="mb-4">
|
| 18 |
+
Paste an image with <strong>Ctrl + V</strong> or click to select an image.
|
| 19 |
+
</p>
|
| 20 |
+
<input type="file" id="fileInput" accept="image/*" class="hidden" />
|
| 21 |
+
<button id="selectBtn" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
|
| 22 |
+
Select Image
|
| 23 |
+
</button>
|
| 24 |
+
</div>
|
| 25 |
+
|
| 26 |
+
<!-- Result Display -->
|
| 27 |
+
<div id="resultArea" class="mt-6 w-full max-w-lg"></div>
|
| 28 |
+
|
| 29 |
+
<!-- History Section -->
|
| 30 |
+
<div id="historyArea" class="mt-6 w-full max-w-lg">
|
| 31 |
+
<h2 class="text-xl font-bold mb-2">History</h2>
|
| 32 |
+
<ul id="historyList" class="list-disc pl-5"></ul>
|
| 33 |
+
</div>
|
| 34 |
+
</div>
|
| 35 |
+
</div>
|
| 36 |
+
|
| 37 |
+
<script>
|
| 38 |
+
const uploadArea = document.getElementById('upload-area');
|
| 39 |
+
const fileInput = document.getElementById('fileInput');
|
| 40 |
+
const selectBtn = document.getElementById('selectBtn');
|
| 41 |
+
const resultArea = document.getElementById('resultArea');
|
| 42 |
+
const historyList = document.getElementById('historyList');
|
| 43 |
+
|
| 44 |
+
// Load history from localStorage
|
| 45 |
+
let history = JSON.parse(localStorage.getItem('history')) || [];
|
| 46 |
+
|
| 47 |
+
function renderHistory() {
|
| 48 |
+
historyList.innerHTML = '';
|
| 49 |
+
history.forEach((item) => {
|
| 50 |
+
const li = document.createElement('li');
|
| 51 |
+
li.textContent = item;
|
| 52 |
+
li.classList.add('cursor-pointer', 'hover:text-blue-500');
|
| 53 |
+
li.addEventListener('click', () => {
|
| 54 |
+
resultArea.innerHTML = `<pre class="bg-gray-200 p-4 rounded whitespace-pre-wrap">${item}</pre>`;
|
| 55 |
+
});
|
| 56 |
+
historyList.appendChild(li);
|
| 57 |
+
});
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
renderHistory();
|
| 61 |
+
|
| 62 |
+
// Trigger file input on button click
|
| 63 |
+
selectBtn.addEventListener('click', () => {
|
| 64 |
+
fileInput.click();
|
| 65 |
+
});
|
| 66 |
+
|
| 67 |
+
// When a file is selected, upload it
|
| 68 |
+
fileInput.addEventListener('change', () => {
|
| 69 |
+
if (fileInput.files.length > 0) {
|
| 70 |
+
uploadFile(fileInput.files[0]);
|
| 71 |
+
}
|
| 72 |
+
});
|
| 73 |
+
|
| 74 |
+
// Listen for paste events to handle Ctrl+V image pasting
|
| 75 |
+
window.addEventListener('paste', (event) => {
|
| 76 |
+
const items = event.clipboardData.items;
|
| 77 |
+
for (let i = 0; i < items.length; i++) {
|
| 78 |
+
if (items[i].type.indexOf("image") !== -1) {
|
| 79 |
+
const file = items[i].getAsFile();
|
| 80 |
+
uploadFile(file);
|
| 81 |
+
break;
|
| 82 |
+
}
|
| 83 |
+
}
|
| 84 |
+
});
|
| 85 |
+
|
| 86 |
+
// Upload the file via fetch
|
| 87 |
+
async function uploadFile(file) {
|
| 88 |
+
const formData = new FormData();
|
| 89 |
+
formData.append('file', file);
|
| 90 |
+
resultArea.innerHTML = `<p class="text-gray-500">Processing...</p>`;
|
| 91 |
+
try {
|
| 92 |
+
const response = await fetch('/upload', {
|
| 93 |
+
method: 'POST',
|
| 94 |
+
body: formData
|
| 95 |
+
});
|
| 96 |
+
const data = await response.json();
|
| 97 |
+
if (data.result) {
|
| 98 |
+
resultArea.innerHTML = `<pre class="bg-gray-200 p-4 rounded whitespace-pre-wrap">${data.result}</pre>`;
|
| 99 |
+
// Save result to history (only last 10 entries)
|
| 100 |
+
history.unshift(data.result);
|
| 101 |
+
if (history.length > 10) history = history.slice(0, 10);
|
| 102 |
+
localStorage.setItem('history', JSON.stringify(history));
|
| 103 |
+
renderHistory();
|
| 104 |
+
} else {
|
| 105 |
+
resultArea.innerHTML = `<p class="text-red-500">No result received.</p>`;
|
| 106 |
+
}
|
| 107 |
+
} catch (error) {
|
| 108 |
+
resultArea.innerHTML = `<p class="text-red-500">Error processing the image.</p>`;
|
| 109 |
+
}
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
// Example keyboard support: pressing Enter triggers file selection
|
| 113 |
+
document.addEventListener('keydown', (event) => {
|
| 114 |
+
if (event.key === 'Enter') {
|
| 115 |
+
fileInput.click();
|
| 116 |
+
}
|
| 117 |
+
});
|
| 118 |
+
</script>
|
| 119 |
+
</body>
|
| 120 |
+
</html>
|