Update index.html
Browse files- index.html +58 -101
index.html
CHANGED
|
@@ -94,7 +94,7 @@
|
|
| 94 |
<div class="container">
|
| 95 |
<h2>ساخت QR Code</h2>
|
| 96 |
<label for="qr-text">متن مورد نظر را وارد کنید:</label>
|
| 97 |
-
<textarea id="qr-text" rows="4" placeholder="مثال: https://google.com"
|
| 98 |
<button id="generate-btn" onclick="generateQRCode()">ساخت QR Code</button>
|
| 99 |
<div id="generate-result-box" class="result-box">
|
| 100 |
<span class="loader">تصویر QR Code اینجا نمایش داده میشود.</span>
|
|
@@ -113,7 +113,8 @@
|
|
| 113 |
</div>
|
| 114 |
|
| 115 |
<script>
|
| 116 |
-
|
|
|
|
| 117 |
|
| 118 |
// گرفتن ارجاع به عناصر HTML
|
| 119 |
const generateBtn = document.getElementById('generate-btn');
|
|
@@ -121,36 +122,12 @@
|
|
| 121 |
const qrTextInput = document.getElementById('qr-text');
|
| 122 |
const qrFileInput = document.getElementById('qr-file');
|
| 123 |
const generateResultBox = document.getElementById('generate-result-box');
|
| 124 |
-
const readResultBox = document.getElementById('read-result-box');
|
| 125 |
const readResultP = document.getElementById('read-result');
|
| 126 |
|
| 127 |
// تابع برای تولید یک رشته تصادفی برای session_hash
|
| 128 |
function generateSessionHash() {
|
| 129 |
return Math.random().toString(36).substring(2, 15);
|
| 130 |
}
|
| 131 |
-
|
| 132 |
-
// تابع برای گوش دادن به نتایج از طریق EventSource
|
| 133 |
-
function listenForData(sessionHash, onComplete, onError) {
|
| 134 |
-
const eventSource = new EventSource(`${API_BASE_URL}/queue/data?session_hash=${sessionHash}`);
|
| 135 |
-
|
| 136 |
-
eventSource.onmessage = function(event) {
|
| 137 |
-
const data = JSON.parse(event.data);
|
| 138 |
-
|
| 139 |
-
if (data.msg === 'process_completed') {
|
| 140 |
-
eventSource.close();
|
| 141 |
-
onComplete(data.output);
|
| 142 |
-
} else if (data.msg === 'process_generating' || data.msg === 'process_starts') {
|
| 143 |
-
// در حال پردازش...
|
| 144 |
-
console.log('Processing...');
|
| 145 |
-
}
|
| 146 |
-
};
|
| 147 |
-
|
| 148 |
-
eventSource.onerror = function(err) {
|
| 149 |
-
console.error("EventSource failed:", err);
|
| 150 |
-
eventSource.close();
|
| 151 |
-
onError("خطا در برقراری ارتباط با سرور.");
|
| 152 |
-
};
|
| 153 |
-
}
|
| 154 |
|
| 155 |
// تابع برای ساخت QR Code
|
| 156 |
async function generateQRCode() {
|
|
@@ -163,41 +140,36 @@
|
|
| 163 |
generateBtn.disabled = true;
|
| 164 |
generateResultBox.innerHTML = '<span class="loader">در حال ساخت... لطفاً صبر کنید.</span>';
|
| 165 |
|
| 166 |
-
const sessionHash = generateSessionHash();
|
| 167 |
const payload = {
|
| 168 |
-
"data": [text],
|
| 169 |
-
"event_data": null,
|
| 170 |
"fn_index": 0, // ایندکس تابع ساخت QR
|
| 171 |
-
"
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
| 173 |
};
|
| 174 |
|
| 175 |
try {
|
| 176 |
-
|
| 177 |
-
const response = await fetch(`${API_BASE_URL}/queue/join?`, {
|
| 178 |
method: 'POST',
|
| 179 |
headers: { 'Content-Type': 'application/json' },
|
| 180 |
body: JSON.stringify(payload)
|
| 181 |
});
|
| 182 |
|
| 183 |
-
if (!response.ok)
|
|
|
|
|
|
|
| 184 |
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
generateBtn.disabled = false;
|
| 191 |
-
},
|
| 192 |
-
(errorMsg) => {
|
| 193 |
-
generateResultBox.innerHTML = `<span style="color:red;">${errorMsg}</span>`;
|
| 194 |
-
generateBtn.disabled = false;
|
| 195 |
-
}
|
| 196 |
-
);
|
| 197 |
|
| 198 |
} catch (error) {
|
| 199 |
console.error('Error generating QR code:', error);
|
| 200 |
generateResultBox.innerHTML = `<span style="color:red;">خطا: ${error.message}</span>`;
|
|
|
|
| 201 |
generateBtn.disabled = false;
|
| 202 |
}
|
| 203 |
}
|
|
@@ -211,72 +183,57 @@
|
|
| 211 |
}
|
| 212 |
|
| 213 |
readBtn.disabled = true;
|
| 214 |
-
readResultP.textContent = 'در حال
|
| 215 |
readResultP.classList.add('loader');
|
| 216 |
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
const uploadResponse = await fetch(`${API_BASE_URL}/upload?`, {
|
| 223 |
-
method: 'POST',
|
| 224 |
-
body: formData
|
| 225 |
-
});
|
| 226 |
-
|
| 227 |
-
if (!uploadResponse.ok) throw new Error('خطا در آپلود فایل.');
|
| 228 |
|
| 229 |
-
const uploadResult = await uploadResponse.json();
|
| 230 |
-
const serverFilePath = uploadResult[0];
|
| 231 |
-
|
| 232 |
-
// 2. ارسال درخواست برای خواندن QR کد با مسیر فایل آپلود شده
|
| 233 |
-
const sessionHash = generateSessionHash();
|
| 234 |
const payload = {
|
| 235 |
-
"data": [{
|
| 236 |
-
"path": serverFilePath,
|
| 237 |
-
"url": `${API_BASE_URL.replace('/gradio_api', '')}/file=${serverFilePath}`,
|
| 238 |
-
"orig_name": file.name,
|
| 239 |
-
"size": file.size,
|
| 240 |
-
"mime_type": file.type,
|
| 241 |
-
"meta": { "_type": "gradio.FileData" }
|
| 242 |
-
}],
|
| 243 |
-
"event_data": null,
|
| 244 |
"fn_index": 1, // ایندکس تابع خواندن QR
|
| 245 |
-
"
|
| 246 |
-
|
|
|
|
|
|
|
|
|
|
| 247 |
};
|
| 248 |
-
|
| 249 |
-
const joinResponse = await fetch(`${API_BASE_URL}/queue/join?`, {
|
| 250 |
-
method: 'POST',
|
| 251 |
-
headers: { 'Content-Type': 'application/json' },
|
| 252 |
-
body: JSON.stringify(payload)
|
| 253 |
-
});
|
| 254 |
|
| 255 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
(output) => {
|
| 260 |
-
const decodedText = output.data[0];
|
| 261 |
-
readResultP.textContent = decodedText || "متنی در QR Code یافت نشد.";
|
| 262 |
-
readResultP.classList.remove('loader');
|
| 263 |
-
readBtn.disabled = false;
|
| 264 |
-
},
|
| 265 |
-
(errorMsg) => {
|
| 266 |
-
readResultP.textContent = errorMsg;
|
| 267 |
-
readResultP.style.color = 'red';
|
| 268 |
-
readBtn.disabled = false;
|
| 269 |
}
|
| 270 |
-
);
|
| 271 |
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
readResultP.textContent = `خطا: ${error.message}`;
|
| 275 |
-
readResultP.style.color = 'red';
|
| 276 |
-
readBtn.disabled = false;
|
| 277 |
-
}
|
| 278 |
-
}
|
| 279 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
</script>
|
| 281 |
</body>
|
| 282 |
</html>
|
|
|
|
| 94 |
<div class="container">
|
| 95 |
<h2>ساخت QR Code</h2>
|
| 96 |
<label for="qr-text">متن مورد نظر را وارد کنید:</label>
|
| 97 |
+
<textarea id="qr-text" rows="4" placeholder="مثال: https://google.com">سلام</textarea>
|
| 98 |
<button id="generate-btn" onclick="generateQRCode()">ساخت QR Code</button>
|
| 99 |
<div id="generate-result-box" class="result-box">
|
| 100 |
<span class="loader">تصویر QR Code اینجا نمایش داده میشود.</span>
|
|
|
|
| 113 |
</div>
|
| 114 |
|
| 115 |
<script>
|
| 116 |
+
// آدرس API برای اسپیس Elias207/QRcode
|
| 117 |
+
const API_URL = "https://elias207-qrcode.hf.space/run/predict";
|
| 118 |
|
| 119 |
// گرفتن ارجاع به عناصر HTML
|
| 120 |
const generateBtn = document.getElementById('generate-btn');
|
|
|
|
| 122 |
const qrTextInput = document.getElementById('qr-text');
|
| 123 |
const qrFileInput = document.getElementById('qr-file');
|
| 124 |
const generateResultBox = document.getElementById('generate-result-box');
|
|
|
|
| 125 |
const readResultP = document.getElementById('read-result');
|
| 126 |
|
| 127 |
// تابع برای تولید یک رشته تصادفی برای session_hash
|
| 128 |
function generateSessionHash() {
|
| 129 |
return Math.random().toString(36).substring(2, 15);
|
| 130 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
// تابع برای ساخت QR Code
|
| 133 |
async function generateQRCode() {
|
|
|
|
| 140 |
generateBtn.disabled = true;
|
| 141 |
generateResultBox.innerHTML = '<span class="loader">در حال ساخت... لطفاً صبر کنید.</span>';
|
| 142 |
|
|
|
|
| 143 |
const payload = {
|
|
|
|
|
|
|
| 144 |
"fn_index": 0, // ایندکس تابع ساخت QR
|
| 145 |
+
"data": [
|
| 146 |
+
text, // ورودی اول: متن
|
| 147 |
+
null // ورودی دوم: تصویر (برای این تابع استفاده نمیشود)
|
| 148 |
+
],
|
| 149 |
+
"session_hash": generateSessionHash()
|
| 150 |
};
|
| 151 |
|
| 152 |
try {
|
| 153 |
+
const response = await fetch(API_URL, {
|
|
|
|
| 154 |
method: 'POST',
|
| 155 |
headers: { 'Content-Type': 'application/json' },
|
| 156 |
body: JSON.stringify(payload)
|
| 157 |
});
|
| 158 |
|
| 159 |
+
if (!response.ok) {
|
| 160 |
+
throw new Error(`خطای شبکه: ${response.status}`);
|
| 161 |
+
}
|
| 162 |
|
| 163 |
+
const result = await response.json();
|
| 164 |
+
// تصویر به صورت Base64 در پاسخ میآید
|
| 165 |
+
const base64Image = result.data[0];
|
| 166 |
+
|
| 167 |
+
generateResultBox.innerHTML = `<img id="qr-image" src="${base64Image}" alt="Generated QR Code">`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
} catch (error) {
|
| 170 |
console.error('Error generating QR code:', error);
|
| 171 |
generateResultBox.innerHTML = `<span style="color:red;">خطا: ${error.message}</span>`;
|
| 172 |
+
} finally {
|
| 173 |
generateBtn.disabled = false;
|
| 174 |
}
|
| 175 |
}
|
|
|
|
| 183 |
}
|
| 184 |
|
| 185 |
readBtn.disabled = true;
|
| 186 |
+
readResultP.textContent = 'در حال خواندن تصویر...';
|
| 187 |
readResultP.classList.add('loader');
|
| 188 |
|
| 189 |
+
// تبدیل فایل تصویر به Base64
|
| 190 |
+
const reader = new FileReader();
|
| 191 |
+
reader.readAsDataURL(file);
|
| 192 |
+
reader.onload = async () => {
|
| 193 |
+
const base64Image = reader.result;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
const payload = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
"fn_index": 1, // ایندکس تابع خواندن QR
|
| 197 |
+
"data": [
|
| 198 |
+
null, // ورودی اول: متن (برای این تابع استفاده نمیشود)
|
| 199 |
+
base64Image // ورودی دوم: تصویر به صورت Base64
|
| 200 |
+
],
|
| 201 |
+
"session_hash": generateSessionHash()
|
| 202 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
+
try {
|
| 205 |
+
const response = await fetch(API_URL, {
|
| 206 |
+
method: 'POST',
|
| 207 |
+
headers: { 'Content-Type': 'application/json' },
|
| 208 |
+
body: JSON.stringify(payload)
|
| 209 |
+
});
|
| 210 |
|
| 211 |
+
if (!response.ok) {
|
| 212 |
+
throw new Error(`خطای شبکه: ${response.status}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
}
|
|
|
|
| 214 |
|
| 215 |
+
const result = await response.json();
|
| 216 |
+
const decodedText = result.data[0];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
|
| 218 |
+
readResultP.textContent = decodedText || "متنی در QR Code یافت نشد.";
|
| 219 |
+
readResultP.classList.remove('loader');
|
| 220 |
+
|
| 221 |
+
} catch (error) {
|
| 222 |
+
console.error('Error reading QR code:', error);
|
| 223 |
+
readResultP.textContent = `خطا: ${error.message}`;
|
| 224 |
+
readResultP.style.color = 'red';
|
| 225 |
+
} finally {
|
| 226 |
+
readBtn.disabled = false;
|
| 227 |
+
}
|
| 228 |
+
};
|
| 229 |
+
|
| 230 |
+
reader.onerror = (error) => {
|
| 231 |
+
console.error('Error reading file:', error);
|
| 232 |
+
readResultP.textContent = 'خطا در خواندن فایل.';
|
| 233 |
+
readResultP.style.color = 'red';
|
| 234 |
+
readBtn.disabled = false;
|
| 235 |
+
};
|
| 236 |
+
}
|
| 237 |
</script>
|
| 238 |
</body>
|
| 239 |
</html>
|