Update index.html
Browse files- index.html +160 -110
index.html
CHANGED
|
@@ -3,103 +3,169 @@
|
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
-
<title
|
| 7 |
<style>
|
| 8 |
-
body {
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
</style>
|
| 19 |
</head>
|
| 20 |
<body>
|
| 21 |
|
| 22 |
-
<!-- بخش ساخت QR Code -->
|
| 23 |
<div class="container">
|
| 24 |
-
<h2
|
| 25 |
-
<textarea id="text-input" placeholder="
|
| 26 |
-
<button id="generate-btn" onclick="generateQRCode()"
|
| 27 |
-
<div id="generate-result" class="result-
|
| 28 |
-
<
|
| 29 |
-
<
|
| 30 |
-
<
|
| 31 |
</div>
|
| 32 |
-
<div id="generate-loader" class="loader" hidden>در حال ارتباط با سرور...</div>
|
| 33 |
</div>
|
| 34 |
|
| 35 |
-
<!-- بخش خواندن QR Code -->
|
| 36 |
<div class="container">
|
| 37 |
-
<h2
|
| 38 |
<input type="file" id="file-input" accept="image/*">
|
| 39 |
-
<button id="read-btn" onclick="readQRCode()"
|
| 40 |
-
<div id="read-result" class="result-
|
| 41 |
-
|
| 42 |
-
<
|
| 43 |
-
<button class="copy-btn" onclick="copyToClipboard('read-json-output', this)">کپی کردن JSON</button>
|
| 44 |
</div>
|
| 45 |
-
<div id="read-loader" class="loader" hidden>در حال آپلود و پردازش...</div>
|
| 46 |
</div>
|
| 47 |
|
| 48 |
<script>
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
try {
|
| 63 |
-
const response = await fetch(API_BASE_URL + `queue/data?session_hash=${sessionHash}`);
|
| 64 |
-
if (!response.ok) { clearInterval(interval); reject(new Error("خطا در شبکه با کد: " + response.status)); return; }
|
| 65 |
-
|
| 66 |
-
const responseText = await response.text();
|
| 67 |
-
const lastLine = responseText.trim().split('\n').pop();
|
| 68 |
-
|
| 69 |
-
if (lastLine && lastLine.startsWith('data:')) {
|
| 70 |
-
const result = JSON.parse(lastLine.substring(5).trim());
|
| 71 |
-
if (result.msg === "process_completed") {
|
| 72 |
-
clearInterval(interval);
|
| 73 |
-
resolve(result);
|
| 74 |
-
} else if (["queue_full", "unexpected_error"].includes(result.msg)) {
|
| 75 |
-
clearInterval(interval);
|
| 76 |
-
reject(new Error("خطای API: " + result.msg));
|
| 77 |
-
}
|
| 78 |
-
}
|
| 79 |
-
} catch (error) {
|
| 80 |
-
if (!(error instanceof SyntaxError)) { clearInterval(interval); reject(error); }
|
| 81 |
-
}
|
| 82 |
-
}, 1000);
|
| 83 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
}
|
| 85 |
|
| 86 |
async function generateQRCode() {
|
|
|
|
|
|
|
|
|
|
| 87 |
const btn = document.getElementById('generate-btn'), loader = document.getElementById('generate-loader');
|
| 88 |
-
const
|
|
|
|
|
|
|
| 89 |
|
| 90 |
-
btn.disabled = true; loader.hidden = false; resultDiv.hidden = true;
|
| 91 |
try {
|
| 92 |
-
const
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
} catch (error) {
|
| 98 |
alert("خطا در ساخت کد: " + error.message);
|
| 99 |
-
|
| 100 |
-
resultDiv.hidden = false;
|
| 101 |
} finally {
|
| 102 |
-
btn.disabled = false; loader.hidden
|
| 103 |
}
|
| 104 |
}
|
| 105 |
|
|
@@ -109,51 +175,35 @@
|
|
| 109 |
const file = fileInput.files[0];
|
| 110 |
|
| 111 |
const btn = document.getElementById('read-btn'), loader = document.getElementById('read-loader');
|
| 112 |
-
const
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
btn.disabled = true; loader.hidden = false; resultDiv.hidden = true;
|
| 115 |
try {
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
const
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
"
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
"mime_type": file.type,
|
| 134 |
-
"meta": {"_type": "gradio.FileData"}
|
| 135 |
-
}];
|
| 136 |
-
|
| 137 |
-
const result = await callGradioApi(1, fileData, sessionHash);
|
| 138 |
-
outputPre.textContent = JSON.stringify(result, null, 2);
|
| 139 |
-
resultDiv.hidden = false;
|
| 140 |
} catch (error) {
|
| 141 |
alert("خطا در خواندن کد: " + error.message);
|
| 142 |
-
|
| 143 |
-
resultDiv.hidden = false;
|
| 144 |
} finally {
|
| 145 |
-
btn.disabled = false; loader.hidden
|
| 146 |
}
|
| 147 |
}
|
| 148 |
-
|
| 149 |
-
function copyToClipboard(elementId, buttonElement) {
|
| 150 |
-
const text = document.getElementById(elementId).textContent;
|
| 151 |
-
navigator.clipboard.writeText(text).then(() => {
|
| 152 |
-
const originalText = buttonElement.textContent;
|
| 153 |
-
buttonElement.textContent = 'کپی شد!';
|
| 154 |
-
setTimeout(() => { buttonElement.textContent = originalText; }, 2000);
|
| 155 |
-
});
|
| 156 |
-
}
|
| 157 |
</script>
|
| 158 |
</body>
|
| 159 |
</html>
|
|
|
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>کلاینت نهایی QR Code</title>
|
| 7 |
<style>
|
| 8 |
+
body {
|
| 9 |
+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
| 10 |
+
background-color: #f0f2f5;
|
| 11 |
+
color: #1c1e21;
|
| 12 |
+
display: flex;
|
| 13 |
+
justify-content: center;
|
| 14 |
+
align-items: flex-start;
|
| 15 |
+
padding: 20px;
|
| 16 |
+
gap: 30px;
|
| 17 |
+
flex-wrap: wrap;
|
| 18 |
+
}
|
| 19 |
+
.container {
|
| 20 |
+
background-color: #fff;
|
| 21 |
+
padding: 25px;
|
| 22 |
+
border-radius: 8px;
|
| 23 |
+
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
|
| 24 |
+
width: 400px;
|
| 25 |
+
display: flex;
|
| 26 |
+
flex-direction: column;
|
| 27 |
+
gap: 15px;
|
| 28 |
+
}
|
| 29 |
+
h2 {
|
| 30 |
+
margin: 0 0 10px 0;
|
| 31 |
+
color: #0056b3;
|
| 32 |
+
border-bottom: 2px solid #0056b3;
|
| 33 |
+
padding-bottom: 5px;
|
| 34 |
+
}
|
| 35 |
+
textarea, input[type="file"] {
|
| 36 |
+
width: 100%;
|
| 37 |
+
padding: 10px;
|
| 38 |
+
border: 1px solid #ccc;
|
| 39 |
+
border-radius: 4px;
|
| 40 |
+
box-sizing: border-box;
|
| 41 |
+
resize: vertical;
|
| 42 |
+
}
|
| 43 |
+
button {
|
| 44 |
+
background-color: #007bff;
|
| 45 |
+
color: white;
|
| 46 |
+
padding: 12px 20px;
|
| 47 |
+
border: none;
|
| 48 |
+
border-radius: 4px;
|
| 49 |
+
cursor: pointer;
|
| 50 |
+
font-size: 16px;
|
| 51 |
+
transition: background-color 0.3s;
|
| 52 |
+
}
|
| 53 |
+
button:disabled {
|
| 54 |
+
background-color: #a0c9ff;
|
| 55 |
+
cursor: not-allowed;
|
| 56 |
+
}
|
| 57 |
+
button:hover:not(:disabled) {
|
| 58 |
+
background-color: #0056b3;
|
| 59 |
+
}
|
| 60 |
+
.result-box {
|
| 61 |
+
margin-top: 15px;
|
| 62 |
+
padding: 15px;
|
| 63 |
+
border: 1px dashed #ddd;
|
| 64 |
+
border-radius: 4px;
|
| 65 |
+
min-height: 50px;
|
| 66 |
+
display: flex;
|
| 67 |
+
justify-content: center;
|
| 68 |
+
align-items: center;
|
| 69 |
+
background-color: #fafafa;
|
| 70 |
+
word-break: break-all;
|
| 71 |
+
}
|
| 72 |
+
#qr-image {
|
| 73 |
+
max-width: 100%;
|
| 74 |
+
height: auto;
|
| 75 |
+
}
|
| 76 |
+
.loader {
|
| 77 |
+
border: 4px solid #f3f3f3;
|
| 78 |
+
border-radius: 50%;
|
| 79 |
+
border-top: 4px solid #3498db;
|
| 80 |
+
width: 30px;
|
| 81 |
+
height: 30px;
|
| 82 |
+
animation: spin 1s linear infinite;
|
| 83 |
+
}
|
| 84 |
+
@keyframes spin {
|
| 85 |
+
0% { transform: rotate(0deg); }
|
| 86 |
+
100% { transform: rotate(360deg); }
|
| 87 |
+
}
|
| 88 |
+
.hidden {
|
| 89 |
+
display: none;
|
| 90 |
+
}
|
| 91 |
</style>
|
| 92 |
</head>
|
| 93 |
<body>
|
| 94 |
|
|
|
|
| 95 |
<div class="container">
|
| 96 |
+
<h2>ساخت QR Code</h2>
|
| 97 |
+
<textarea id="text-input" placeholder="متن مورد نظر را اینجا وارد کنید..." rows="4">آزمایش نهایی</textarea>
|
| 98 |
+
<button id="generate-btn" onclick="generateQRCode()">ساخت کد</button>
|
| 99 |
+
<div id="generate-result" class="result-box">
|
| 100 |
+
<div id="generate-loader" class="loader hidden"></div>
|
| 101 |
+
<img id="qr-image" src="" alt="QR Code" class="hidden" />
|
| 102 |
+
<span id="generate-placeholder">تصویر QR Code اینجا نمایش داده میشود.</span>
|
| 103 |
</div>
|
|
|
|
| 104 |
</div>
|
| 105 |
|
|
|
|
| 106 |
<div class="container">
|
| 107 |
+
<h2>خواندن QR Code</h2>
|
| 108 |
<input type="file" id="file-input" accept="image/*">
|
| 109 |
+
<button id="read-btn" onclick="readQRCode()">خواندن کد</button>
|
| 110 |
+
<div id="read-result" class="result-box">
|
| 111 |
+
<div id="read-loader" class="loader hidden"></div>
|
| 112 |
+
<p id="read-text">نتیجه خواندن کد اینجا نمایش داده میشود.</p>
|
|
|
|
| 113 |
</div>
|
|
|
|
| 114 |
</div>
|
| 115 |
|
| 116 |
<script>
|
| 117 |
+
// *** مهمترین اصلاح: آدرس API به /api/ تغییر کرد ***
|
| 118 |
+
const API_ROOT_URL = "https://elias207-qrcode.hf.space/";
|
| 119 |
+
const API_BASE_URL = API_ROOT_URL + "api/";
|
| 120 |
+
|
| 121 |
+
// اندپوینتهای predict معمولا یک نام خاص دارند. بیایید predict/ را امتحان کنیم
|
| 122 |
+
const GENERATE_ENDPOINT = API_BASE_URL + "predict/";
|
| 123 |
+
const READ_ENDPOINT = API_BASE_URL + "predict_1/"; // معمولا تابع دوم _1 میگیرد
|
| 124 |
+
|
| 125 |
+
async function uploadFile(file) {
|
| 126 |
+
const fileData = await new Promise(resolve => {
|
| 127 |
+
const reader = new FileReader();
|
| 128 |
+
reader.onload = e => resolve(e.target.result);
|
| 129 |
+
reader.readAsDataURL(file);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
});
|
| 131 |
+
return {
|
| 132 |
+
data: fileData,
|
| 133 |
+
name: file.name
|
| 134 |
+
};
|
| 135 |
}
|
| 136 |
|
| 137 |
async function generateQRCode() {
|
| 138 |
+
const textInput = document.getElementById('text-input').value;
|
| 139 |
+
if (!textInput.trim()) { alert("لطفاً متنی وارد کنید."); return; }
|
| 140 |
+
|
| 141 |
const btn = document.getElementById('generate-btn'), loader = document.getElementById('generate-loader');
|
| 142 |
+
const placeholder = document.getElementById('generate-placeholder'), qrImage = document.getElementById('qr-image');
|
| 143 |
+
|
| 144 |
+
btn.disabled = true; loader.classList.remove('hidden'); placeholder.classList.add('hidden'); qrImage.classList.add('hidden');
|
| 145 |
|
|
|
|
| 146 |
try {
|
| 147 |
+
const response = await fetch(GENERATE_ENDPOINT, {
|
| 148 |
+
method: "POST",
|
| 149 |
+
headers: { 'Content-Type': 'application/json' },
|
| 150 |
+
body: JSON.stringify({ "data": [textInput] }),
|
| 151 |
+
});
|
| 152 |
+
|
| 153 |
+
if (!response.ok) throw new Error("خطای سرور: " + response.status);
|
| 154 |
+
|
| 155 |
+
const result = await response.json();
|
| 156 |
+
|
| 157 |
+
if (result.data?.[0]?.name) {
|
| 158 |
+
const imageUrl = API_ROOT_URL + "file=" + result.data[0].name;
|
| 159 |
+
qrImage.src = imageUrl;
|
| 160 |
+
qrImage.classList.remove('hidden');
|
| 161 |
+
} else {
|
| 162 |
+
throw new Error("پاسخ دریافتی از سرور معتبر نیست.");
|
| 163 |
+
}
|
| 164 |
} catch (error) {
|
| 165 |
alert("خطا در ساخت کد: " + error.message);
|
| 166 |
+
placeholder.classList.remove('hidden');
|
|
|
|
| 167 |
} finally {
|
| 168 |
+
btn.disabled = false; loader.classList.add('hidden');
|
| 169 |
}
|
| 170 |
}
|
| 171 |
|
|
|
|
| 175 |
const file = fileInput.files[0];
|
| 176 |
|
| 177 |
const btn = document.getElementById('read-btn'), loader = document.getElementById('read-loader');
|
| 178 |
+
const readText = document.getElementById('read-text');
|
| 179 |
+
|
| 180 |
+
btn.disabled = true; loader.classList.remove('hidden'); readText.textContent = "در حال پردازش...";
|
| 181 |
|
|
|
|
| 182 |
try {
|
| 183 |
+
const uploadedFileObject = await uploadFile(file);
|
| 184 |
+
|
| 185 |
+
const response = await fetch(READ_ENDPOINT, {
|
| 186 |
+
method: "POST",
|
| 187 |
+
headers: { 'Content-Type': 'application/json' },
|
| 188 |
+
body: JSON.stringify({ "data": [uploadedFileObject] }),
|
| 189 |
+
});
|
| 190 |
+
|
| 191 |
+
if (!response.ok) throw new Error("خطای سرور: " + response.status);
|
| 192 |
+
|
| 193 |
+
const result = await response.json();
|
| 194 |
+
|
| 195 |
+
if (result.data?.[0]) {
|
| 196 |
+
readText.textContent = "نتیجه: " + result.data[0];
|
| 197 |
+
} else {
|
| 198 |
+
readText.textContent = "نتیجهای یافت نشد.";
|
| 199 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
} catch (error) {
|
| 201 |
alert("خطا در خواندن کد: " + error.message);
|
| 202 |
+
readText.textContent = "خطا در خواندن کد.";
|
|
|
|
| 203 |
} finally {
|
| 204 |
+
btn.disabled = false; loader.classList.add('hidden');
|
| 205 |
}
|
| 206 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
</script>
|
| 208 |
</body>
|
| 209 |
</html>
|