Elias207 commited on
Commit
a0dc433
·
verified ·
1 Parent(s): 19f79dc

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +69 -175
index.html CHANGED
@@ -3,142 +3,70 @@
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
  <!-- بخش ساخت QR Code -->
96
  <div class="container">
97
- <h2>ساخت QR Code</h2>
98
- <textarea id="text-input" placeholder="متن مورد نظر را اینجا وارد کنید..." rows="4"></textarea>
99
- <button id="generate-btn" onclick="generateQRCode()">ساخت کد</button>
100
- <div id="generate-result" class="result-box">
101
  <div id="generate-loader" class="loader hidden"></div>
102
- <img id="qr-image" src="" alt="QR Code" class="hidden" />
103
- <span id="generate-placeholder">تصویر QR Code اینجا نمایش داده می‌شود.</span>
 
 
104
  </div>
105
  </div>
106
 
107
  <!-- بخش خواندن QR Code -->
108
  <div class="container">
109
- <h2>خواندن QR Code</h2>
110
  <input type="file" id="file-input" accept="image/*">
111
- <button id="read-btn" onclick="readQRCode()">خواندن کد</button>
112
- <div id="read-result" class="result-box">
113
- <div id="read-loader" class="loader hidden"></div>
114
- <p id="read-text">نتیجه خواندن کد اینجا نمایش داده می‌شود.</p>
 
 
 
115
  </div>
116
  </div>
117
 
118
  <script>
119
- // *** مهم: آدرس API مطابق با اسکرین شات شما تغییر کرد ***
120
  const API_BASE_URL = "https://elias207-qrcode.hf.space/";
121
 
122
- // یک session_hash تصادفی ایجاد می‌کند
123
  function generateSessionHash() {
124
  return Math.random().toString(36).substring(2, 11);
125
  }
126
 
127
- // تابع برای ارسال درخواست به API و دریافت نتیجه
128
  async function callGradioApi(fnIndex, data, sessionHash) {
129
- // Step 1: Join the queue
130
  const joinResponse = await fetch(API_BASE_URL + "queue/join?", {
131
  method: "POST",
132
  headers: { "Content-Type": "application/json" },
133
- body: JSON.stringify({
134
- "fn_index": fnIndex,
135
- "data": data,
136
- "session_hash": sessionHash,
137
- }),
138
  });
139
  if (!joinResponse.ok) throw new Error("Failed to join queue.");
140
 
141
- // Step 2: Poll for the result
142
  return new Promise((resolve, reject) => {
143
  const interval = setInterval(async () => {
144
  try {
@@ -149,16 +77,11 @@
149
  return;
150
  }
151
 
152
- // --- بخش اصلاح شده ---
153
- // پاسخ را به عنوان متن می خوانیم
154
  const responseText = await dataResponse.text();
155
-
156
- // ممکن است چندین پیام در استریم باشد، آخرین پیام را پردازش می کنیم
157
  const lines = responseText.trim().split('\n');
158
  const lastLine = lines[lines.length - 1];
159
 
160
  if (lastLine && lastLine.startsWith('data:')) {
161
- // پیشوند 'data: ' را حذف کرده و نتیجه را به JSON تبدیل می کنیم
162
  const jsonString = lastLine.substring(5).trim();
163
  const result = JSON.parse(jsonString);
164
 
@@ -170,117 +93,88 @@
170
  reject(new Error("API Error: " + result.msg));
171
  }
172
  }
173
- // --- پایان بخش اصلاح شده ---
174
-
175
  } catch (error) {
176
- // اگر خطا در حین پارس کردن رخ دهد (مثلا پیام کامل نباشد)، منتظر درخواست بعدی می مانیم
177
  if (!(error instanceof SyntaxError)) {
178
  clearInterval(interval);
179
  reject(error);
180
  }
181
  }
182
- }, 1000); // هر ۱ ثانیه چک کن
183
  });
184
  }
185
 
186
- // --- بخش ساخت QR Code ---
187
  async function generateQRCode() {
188
  const textInput = document.getElementById('text-input').value;
189
- if (!textInput.trim()) {
190
- alert("لطفاً متنی را برای ساخت QR Code وارد کنید.");
191
- return;
192
- }
193
 
194
- const generateBtn = document.getElementById('generate-btn');
195
  const loader = document.getElementById('generate-loader');
196
- const placeholder = document.getElementById('generate-placeholder');
197
- const qrImage = document.getElementById('qr-image');
198
 
199
- generateBtn.disabled = true;
200
  loader.classList.remove('hidden');
201
- placeholder.classList.add('hidden');
202
- qrImage.classList.add('hidden');
203
 
204
  try {
205
  const sessionHash = generateSessionHash();
206
- const data = [textInput];
207
- const result = await callGradioApi(0, data, sessionHash); // fn_index=0 برای ساخت
208
-
209
- if (result.output && result.output.data && result.output.data[0]) {
210
- // Gradio آدرس فایل را به صورت نسبی برمی‌گرداند، ما آن را کامل می‌کنیم
211
- const imageUrl = API_BASE_URL + "file=" + result.output.data[0].path;
212
- qrImage.src = imageUrl;
213
- qrImage.classList.remove('hidden');
214
- } else {
215
- throw new Error("پاسخ دریافتی از سرور معتبر نیست.");
216
- }
217
-
218
  } catch (error) {
219
- console.error("Error generating QR Code:", error);
220
- alert("خطا در ساخت QR Code: " + error.message);
221
- placeholder.classList.remove('hidden');
222
  } finally {
223
- generateBtn.disabled = false;
224
  loader.classList.add('hidden');
225
  }
226
  }
227
 
228
- // --- بخش خواندن QR Code ---
229
  async function readQRCode() {
230
  const fileInput = document.getElementById('file-input');
231
- if (fileInput.files.length === 0) {
232
- alert("لطفاً یک فایل تصویر QR Code انتخاب کنید.");
233
- return;
234
- }
235
  const file = fileInput.files[0];
236
 
237
- const readBtn = document.getElementById('read-btn');
238
  const loader = document.getElementById('read-loader');
239
- const readText = document.getElementById('read-text');
 
240
 
241
- readBtn.disabled = true;
242
  loader.classList.remove('hidden');
243
- readText.textContent = "در حال پردازش...";
 
244
 
245
  try {
246
- // Step 1: Upload the file
247
  const formData = new FormData();
248
  formData.append("files", file);
249
- const uploadResponse = await fetch(API_BASE_URL + "upload", {
250
- method: "POST",
251
- body: formData,
252
- });
253
  if (!uploadResponse.ok) throw new Error("آپلود فایل با خطا مواجه شد.");
254
 
255
  const uploadResult = await uploadResponse.json();
256
  const tempFilePath = uploadResult[0];
257
 
258
- // Step 2: Call the API with the uploaded file path
259
  const sessionHash = generateSessionHash();
260
- const data = [{
261
- "path": tempFilePath,
262
- "url": API_BASE_URL + "file=" + tempFilePath,
263
- "orig_name": file.name,
264
- "size": file.size,
265
- "mime_type": file.type,
266
- "meta": {"_type": "gradio.FileData"}
267
- }];
268
- const result = await callGradioApi(1, data, sessionHash); // fn_index=1 برای خواندن
269
-
270
- if (result.output && result.output.data && typeof result.output.data[0] === 'string') {
271
- readText.textContent = "نتیجه: " + result.output.data[0];
272
- } else {
273
- readText.textContent = "نتیجه‌ای یافت نشد یا فرمت پاسخ نامعتبر است.";
274
- }
275
-
276
  } catch (error) {
277
- console.error("Error reading QR Code:", error);
278
- readText.textContent = "خطا در خواندن QR Code: " + error.message;
279
  } finally {
280
- readBtn.disabled = false;
281
  loader.classList.add('hidden');
282
  }
283
  }
 
 
 
 
 
 
 
 
 
284
  </script>
285
 
286
  </body>
 
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>دیباگر QR Code API</title>
7
  <style>
8
+ body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; background-color: #f0f2f5; display: flex; justify-content: center; padding: 20px; gap: 30px; flex-wrap: wrap; }
9
+ .container { background-color: #fff; padding: 25px; border-radius: 8px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); width: 450px; display: flex; flex-direction: column; gap: 15px; }
10
+ h2 { margin: 0 0 10px 0; color: #0056b3; border-bottom: 2px solid #0056b3; padding-bottom: 5px; }
11
+ textarea, input[type="file"] { width: 100%; padding: 10px; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; }
12
+ button { background-color: #007bff; color: white; padding: 10px 15px; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; transition: background-color 0.3s; }
13
+ button:disabled { background-color: #a0c9ff; cursor: not-allowed; }
14
+ button:hover:not(:disabled) { background-color: #0056b3; }
15
+ .result-container { display: flex; flex-direction: column; gap: 10px; }
16
+ .result-box { margin-top: 10px; background-color: #f8f9fa; border: 1px solid #dee2e6; border-radius: 4px; padding: 10px; min-height: 100px; }
17
+ pre { white-space: pre-wrap; word-break: break-all; margin: 0; font-size: 13px; text-align: left; direction: ltr; }
18
+ .copy-btn { background-color: #28a745; }
19
+ .copy-btn:hover { background-color: #218838; }
20
+ .loader { border: 4px solid #f3f3f3; border-radius: 50%; border-top: 4px solid #3498db; width: 30px; height: 30px; animation: spin 1s linear infinite; margin: 10px auto; }
21
+ @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
22
+ .hidden { display: none; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  </style>
24
  </head>
25
  <body>
26
 
27
  <!-- بخش ساخت QR Code -->
28
  <div class="container">
29
+ <h2>۱. ساخت QR Code (دیباگ)</h2>
30
+ <textarea id="text-input" placeholder="یک متن مثل 'salam' وارد کنید" rows="3">salam</textarea>
31
+ <button id="generate-btn" onclick="generateQRCode()">دریافت جیسون ساخت</button>
32
+ <div class="result-container">
33
  <div id="generate-loader" class="loader hidden"></div>
34
+ <div class="result-box">
35
+ <pre id="generate-json-output">خروجی JSON اینجا نمایش داده می‌شود.</pre>
36
+ </div>
37
+ <button id="copy-generate-btn" class="copy-btn hidden" onclick="copyResult('generate-json-output', this)">کپی جیسون خروجی</button>
38
  </div>
39
  </div>
40
 
41
  <!-- بخش خواندن QR Code -->
42
  <div class="container">
43
+ <h2>۲. خواندن QR Code (دیباگ)</h2>
44
  <input type="file" id="file-input" accept="image/*">
45
+ <button id="read-btn" onclick="readQRCode()">دریافت جیسون خواندن</button>
46
+ <div class="result-container">
47
+ <div id="read-loader" class="loader hidden"></div>
48
+ <div class="result-box">
49
+ <pre id="read-json-output">خروجی JSON اینجا نمایش داده می‌شود.</pre>
50
+ </div>
51
+ <button id="copy-read-btn" class="copy-btn hidden" onclick="copyResult('read-json-output', this)">کپی جیسون خروجی</button>
52
  </div>
53
  </div>
54
 
55
  <script>
 
56
  const API_BASE_URL = "https://elias207-qrcode.hf.space/";
57
 
 
58
  function generateSessionHash() {
59
  return Math.random().toString(36).substring(2, 11);
60
  }
61
 
 
62
  async function callGradioApi(fnIndex, data, sessionHash) {
 
63
  const joinResponse = await fetch(API_BASE_URL + "queue/join?", {
64
  method: "POST",
65
  headers: { "Content-Type": "application/json" },
66
+ body: JSON.stringify({ "fn_index": fnIndex, "data": data, "session_hash": sessionHash }),
 
 
 
 
67
  });
68
  if (!joinResponse.ok) throw new Error("Failed to join queue.");
69
 
 
70
  return new Promise((resolve, reject) => {
71
  const interval = setInterval(async () => {
72
  try {
 
77
  return;
78
  }
79
 
 
 
80
  const responseText = await dataResponse.text();
 
 
81
  const lines = responseText.trim().split('\n');
82
  const lastLine = lines[lines.length - 1];
83
 
84
  if (lastLine && lastLine.startsWith('data:')) {
 
85
  const jsonString = lastLine.substring(5).trim();
86
  const result = JSON.parse(jsonString);
87
 
 
93
  reject(new Error("API Error: " + result.msg));
94
  }
95
  }
 
 
96
  } catch (error) {
 
97
  if (!(error instanceof SyntaxError)) {
98
  clearInterval(interval);
99
  reject(error);
100
  }
101
  }
102
+ }, 1000);
103
  });
104
  }
105
 
 
106
  async function generateQRCode() {
107
  const textInput = document.getElementById('text-input').value;
108
+ if (!textInput.trim()) { alert("لطفاً متنی را وارد کنید."); return; }
 
 
 
109
 
110
+ const btn = document.getElementById('generate-btn');
111
  const loader = document.getElementById('generate-loader');
112
+ const outputPre = document.getElementById('generate-json-output');
113
+ const copyBtn = document.getElementById('copy-generate-btn');
114
 
115
+ btn.disabled = true;
116
  loader.classList.remove('hidden');
117
+ copyBtn.classList.add('hidden');
118
+ outputPre.textContent = "در حال ارتباط با سرور...";
119
 
120
  try {
121
  const sessionHash = generateSessionHash();
122
+ const result = await callGradioApi(0, [textInput], sessionHash);
123
+ outputPre.textContent = JSON.stringify(result, null, 2); // نمایش جیسون خام و مرتب
124
+ copyBtn.classList.remove('hidden');
 
 
 
 
 
 
 
 
 
125
  } catch (error) {
126
+ outputPre.textContent = "خطا: " + error.message;
 
 
127
  } finally {
128
+ btn.disabled = false;
129
  loader.classList.add('hidden');
130
  }
131
  }
132
 
 
133
  async function readQRCode() {
134
  const fileInput = document.getElementById('file-input');
135
+ if (fileInput.files.length === 0) { alert("لطفاً یک فایل تصویر انتخاب کنید."); return; }
 
 
 
136
  const file = fileInput.files[0];
137
 
138
+ const btn = document.getElementById('read-btn');
139
  const loader = document.getElementById('read-loader');
140
+ const outputPre = document.getElementById('read-json-output');
141
+ const copyBtn = document.getElementById('copy-read-btn');
142
 
143
+ btn.disabled = true;
144
  loader.classList.remove('hidden');
145
+ copyBtn.classList.add('hidden');
146
+ outputPre.textContent = "در حال آپلود و پردازش...";
147
 
148
  try {
 
149
  const formData = new FormData();
150
  formData.append("files", file);
151
+ const uploadResponse = await fetch(API_BASE_URL + "upload", { method: "POST", body: formData });
 
 
 
152
  if (!uploadResponse.ok) throw new Error("آپلود فایل با خطا مواجه شد.");
153
 
154
  const uploadResult = await uploadResponse.json();
155
  const tempFilePath = uploadResult[0];
156
 
 
157
  const sessionHash = generateSessionHash();
158
+ const data = [{ "path": tempFilePath, "url": API_BASE_URL + "file=" + tempFilePath, "orig_name": file.name }];
159
+ const result = await callGradioApi(1, [data], sessionHash);
160
+ outputPre.textContent = JSON.stringify(result, null, 2); // نمایش جیسون خام و مرتب
161
+ copyBtn.classList.remove('hidden');
 
 
 
 
 
 
 
 
 
 
 
 
162
  } catch (error) {
163
+ outputPre.textContent = "خطا: " + error.message;
 
164
  } finally {
165
+ btn.disabled = false;
166
  loader.classList.add('hidden');
167
  }
168
  }
169
+
170
+ function copyResult(elementId, buttonElement) {
171
+ const pre = document.getElementById(elementId);
172
+ navigator.clipboard.writeText(pre.textContent).then(() => {
173
+ const originalText = buttonElement.textContent;
174
+ buttonElement.textContent = 'کپی شد!';
175
+ setTimeout(() => { buttonElement.textContent = originalText; }, 2000);
176
+ }).catch(err => console.error('Failed to copy: ', err));
177
+ }
178
  </script>
179
 
180
  </body>