| <!DOCTYPE html> |
| <html lang="fa" dir="rtl"> |
| <head> |
| <meta charset="UTF-8"> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| <title>کلاینت نهایی QR Code</title> |
| <style> |
| body { |
| font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; |
| background-color: #f0f2f5; |
| color: #1c1e21; |
| display: flex; |
| justify-content: center; |
| align-items: flex-start; |
| padding: 20px; |
| gap: 30px; |
| flex-wrap: wrap; |
| } |
| .container { |
| background-color: #fff; |
| padding: 25px; |
| border-radius: 8px; |
| box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1); |
| width: 400px; |
| display: flex; |
| flex-direction: column; |
| gap: 15px; |
| } |
| h2 { |
| margin: 0 0 10px 0; |
| color: #0056b3; |
| border-bottom: 2px solid #0056b3; |
| padding-bottom: 5px; |
| } |
| textarea, input[type="file"] { |
| width: 100%; |
| padding: 10px; |
| border: 1px solid #ccc; |
| border-radius: 4px; |
| box-sizing: border-box; |
| resize: vertical; |
| } |
| button { |
| background-color: #007bff; |
| color: white; |
| padding: 12px 20px; |
| border: none; |
| border-radius: 4px; |
| cursor: pointer; |
| font-size: 16px; |
| transition: background-color 0.3s; |
| } |
| button:disabled { |
| background-color: #a0c9ff; |
| cursor: not-allowed; |
| } |
| button:hover:not(:disabled) { |
| background-color: #0056b3; |
| } |
| .result-box { |
| margin-top: 15px; |
| padding: 15px; |
| border: 1px dashed #ddd; |
| border-radius: 4px; |
| min-height: 50px; |
| display: flex; |
| justify-content: center; |
| align-items: center; |
| background-color: #fafafa; |
| word-break: break-all; |
| } |
| #qr-image { |
| max-width: 100%; |
| height: auto; |
| } |
| .loader { |
| border: 4px solid #f3f3f3; |
| border-radius: 50%; |
| border-top: 4px solid #3498db; |
| width: 30px; |
| height: 30px; |
| animation: spin 1s linear infinite; |
| } |
| @keyframes spin { |
| 0% { transform: rotate(0deg); } |
| 100% { transform: rotate(360deg); } |
| } |
| .hidden { |
| display: none; |
| } |
| </style> |
| </head> |
| <body> |
|
|
| |
| <div class="container"> |
| <h2>ساخت QR Code</h2> |
| <textarea id="text-input" placeholder="متن مورد نظر را اینجا وارد کنید..." rows="4">این بار درست کار میکند!</textarea> |
| <button id="generate-btn" onclick="generateQRCode()">ساخت کد</button> |
| <div id="generate-result" class="result-box"> |
| <div id="generate-loader" class="loader hidden"></div> |
| <img id="qr-image" src="" alt="QR Code" class="hidden" /> |
| <span id="generate-placeholder">تصویر QR Code اینجا نمایش داده میشود.</span> |
| </div> |
| </div> |
|
|
| |
| <div class="container"> |
| <h2>خواندن QR Code</h2> |
| <input type="file" id="file-input" accept="image/*"> |
| <button id="read-btn" onclick="readQRCode()">خواندن کد</button> |
| <div id="read-result" class="result-box"> |
| <div id="read-loader" class="loader hidden"></div> |
| <p id="read-text">نتیجه خواندن کد اینجا نمایش داده میشود.</p> |
| </div> |
| </div> |
|
|
| <script> |
| const API_BASE_URL = "https://elias207-qrcode.hf.space/gradio_api/"; |
| |
| function generateSessionHash() { return Math.random().toString(36).substring(2, 11); } |
| |
| async function callGradioApi(fnIndex, data, sessionHash) { |
| await fetch(API_BASE_URL + "queue/join?", { |
| method: "POST", |
| headers: { "Content-Type": "application/json" }, |
| body: JSON.stringify({ "fn_index": fnIndex, "data": data, "session_hash": sessionHash }), |
| }); |
| |
| return new Promise((resolve, reject) => { |
| const interval = setInterval(async () => { |
| try { |
| const response = await fetch(API_BASE_URL + `queue/data?session_hash=${sessionHash}`); |
| if (!response.ok) { clearInterval(interval); reject(new Error("خطا در شبکه")); return; } |
| |
| const responseText = await response.text(); |
| const lastLine = responseText.trim().split('\n').pop(); |
| |
| if (lastLine && lastLine.startsWith('data:')) { |
| const result = JSON.parse(lastLine.substring(5).trim()); |
| if (result.msg === "process_completed") { |
| clearInterval(interval); |
| resolve(result); |
| } else if (["queue_full", "unexpected_error"].includes(result.msg)) { |
| clearInterval(interval); |
| reject(new Error("API Error: " + result.msg)); |
| } |
| } |
| } catch (error) { |
| if (!(error instanceof SyntaxError)) { clearInterval(interval); reject(error); } |
| } |
| }, 1000); |
| }); |
| } |
| |
| async function generateQRCode() { |
| const textInput = document.getElementById('text-input').value; |
| if (!textInput.trim()) { alert("لطفاً متنی وارد کنید."); return; } |
| |
| const btn = document.getElementById('generate-btn'); |
| const loader = document.getElementById('generate-loader'); |
| const placeholder = document.getElementById('generate-placeholder'); |
| const qrImage = document.getElementById('qr-image'); |
| |
| btn.disabled = true; |
| loader.classList.remove('hidden'); |
| placeholder.classList.add('hidden'); |
| qrImage.classList.add('hidden'); |
| |
| try { |
| const sessionHash = generateSessionHash(); |
| const result = await callGradioApi(0, [textInput], sessionHash); |
| |
| if (result.output?.data?.[0]?.path) { |
| |
| const imageUrl = API_BASE_URL + "file=" + result.output.data[0].path; |
| qrImage.src = imageUrl; |
| qrImage.classList.remove('hidden'); |
| } else { |
| throw new Error("پاسخ دریافتی از سرور معتبر نیست."); |
| } |
| } catch (error) { |
| alert("خطا در ساخت کد: " + error.message); |
| placeholder.classList.remove('hidden'); |
| } finally { |
| btn.disabled = false; |
| loader.classList.add('hidden'); |
| } |
| } |
| |
| async function readQRCode() { |
| const fileInput = document.getElementById('file-input'); |
| if (fileInput.files.length === 0) { alert("لطفا فایلی انتخاب کنید."); return; } |
| const file = fileInput.files[0]; |
| |
| const btn = document.getElementById('read-btn'); |
| const loader = document.getElementById('read-loader'); |
| const readText = document.getElementById('read-text'); |
| |
| btn.disabled = true; |
| loader.classList.remove('hidden'); |
| readText.textContent = "در حال پردازش..."; |
| |
| try { |
| const formData = new FormData(); |
| formData.append("files", file); |
| |
| |
| |
| const uploadRes = await fetch(API_BASE_URL + "upload", { method: "POST", body: formData }); |
| if (!uploadRes.ok) throw new Error("آپلود فایل ناموفق بود."); |
| |
| const uploadResult = await uploadRes.json(); |
| const tempFilePath = uploadResult[0]; |
| |
| const sessionHash = generateSessionHash(); |
| const fileData = [{"path": tempFilePath, "url": API_BASE_URL + "file=" + tempFilePath, "orig_name": file.name}]; |
| const result = await callGradioApi(1, fileData, sessionHash); |
| |
| if (result.output?.data?.[0]) { |
| readText.textContent = "نتیجه: " + result.output.data[0]; |
| } else { |
| readText.textContent = "نتیجهای یافت نشد."; |
| } |
| } catch (error) { |
| alert("خطا در خواندن کد: " + error.message); |
| readText.textContent = "خطا در خواندن کد."; |
| } finally { |
| btn.disabled = false; |
| loader.classList.add('hidden'); |
| } |
| } |
| </script> |
| </body> |
| </html> |