Elias207 commited on
Commit
28ef445
·
verified ·
1 Parent(s): b0a55a3

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +70 -146
index.html CHANGED
@@ -3,123 +3,53 @@
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
  const API_BASE_URL = "https://elias207-qrcode.hf.space/gradio_api/";
120
 
121
  function generateSessionHash() { return Math.random().toString(36).substring(2, 11); }
122
-
123
  async function callGradioApi(fnIndex, data, sessionHash) {
124
  await fetch(API_BASE_URL + "queue/join?", {
125
  method: "POST",
@@ -131,7 +61,7 @@
131
  const interval = setInterval(async () => {
132
  try {
133
  const response = await fetch(API_BASE_URL + `queue/data?session_hash=${sessionHash}`);
134
- if (!response.ok) { clearInterval(interval); reject(new Error("خطا در شبکه")); return; }
135
 
136
  const responseText = await response.text();
137
  const lastLine = responseText.trim().split('\n').pop();
@@ -143,7 +73,7 @@
143
  resolve(result);
144
  } else if (["queue_full", "unexpected_error"].includes(result.msg)) {
145
  clearInterval(interval);
146
- reject(new Error("API Error: " + result.msg));
147
  }
148
  }
149
  } catch (error) {
@@ -154,37 +84,22 @@
154
  }
155
 
156
  async function generateQRCode() {
157
- const textInput = document.getElementById('text-input').value;
158
- if (!textInput.trim()) { alert("لطفاً متنی وارد کنید."); return; }
159
-
160
- const btn = document.getElementById('generate-btn');
161
- const loader = document.getElementById('generate-loader');
162
- const placeholder = document.getElementById('generate-placeholder');
163
- const qrImage = document.getElementById('qr-image');
164
-
165
- btn.disabled = true;
166
- loader.classList.remove('hidden');
167
- placeholder.classList.add('hidden');
168
- qrImage.classList.add('hidden');
169
 
 
170
  try {
 
171
  const sessionHash = generateSessionHash();
172
- const result = await callGradioApi(0, [textInput], sessionHash);
173
-
174
- if (result.output?.data?.[0]?.path) {
175
- // **اصلاح**: آدرس فایل هم باید از مسیر API خوانده شود
176
- const imageUrl = API_BASE_URL + "file=" + result.output.data[0].path;
177
- qrImage.src = imageUrl;
178
- qrImage.classList.remove('hidden');
179
- } else {
180
- throw new Error("پاسخ دریافتی از سرور معتبر نیست.");
181
- }
182
  } catch (error) {
183
  alert("خطا در ساخت کد: " + error.message);
184
- placeholder.classList.remove('hidden');
 
185
  } finally {
186
- btn.disabled = false;
187
- loader.classList.add('hidden');
188
  }
189
  }
190
 
@@ -193,43 +108,52 @@
193
  if (fileInput.files.length === 0) { alert("لطفا فایلی انتخاب کنید."); return; }
194
  const file = fileInput.files[0];
195
 
196
- const btn = document.getElementById('read-btn');
197
- const loader = document.getElementById('read-loader');
198
- const readText = document.getElementById('read-text');
199
-
200
- btn.disabled = true;
201
- loader.classList.remove('hidden');
202
- readText.textContent = "در حال پردازش...";
203
 
 
204
  try {
 
205
  const formData = new FormData();
206
  formData.append("files", file);
207
-
208
- // *** مهم: اینجا نقطه اصلی اصلاح بود ***
209
- // آدرس آپلود باید به API_BASE_URL اشاره کند
210
  const uploadRes = await fetch(API_BASE_URL + "upload", { method: "POST", body: formData });
211
- if (!uploadRes.ok) throw new Error("آپلود فایل ناموفق بود.");
212
-
213
  const uploadResult = await uploadRes.json();
214
  const tempFilePath = uploadResult[0];
215
 
 
216
  const sessionHash = generateSessionHash();
217
- const fileData = [{"path": tempFilePath, "url": API_BASE_URL + "file=" + tempFilePath, "orig_name": file.name}];
218
- const result = await callGradioApi(1, fileData, sessionHash);
 
 
 
 
 
 
 
219
 
220
- if (result.output?.data?.[0]) {
221
- readText.textContent = "نتیجه: " + result.output.data[0];
222
- } else {
223
- readText.textContent = "نتیجه‌ای یافت نشد.";
224
- }
225
  } catch (error) {
226
  alert("خطا در خواندن کد: " + error.message);
227
- readText.textContent = "خطا در خواندن کد.";
 
228
  } finally {
229
- btn.disabled = false;
230
- loader.classList.add('hidden');
231
  }
232
  }
 
 
 
 
 
 
 
 
 
233
  </script>
234
  </body>
235
  </html>
 
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>دیباگر API کیوآرکد</title>
7
  <style>
8
+ body { font-family: sans-serif; background-color: #f4f4f9; display: flex; justify-content: center; padding: 20px; gap: 30px; flex-wrap: wrap; }
9
+ .container { background-color: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); width: 450px; }
10
+ h2 { color: #333; border-bottom: 2px solid #6c5ce7; padding-bottom: 5px; }
11
+ textarea, input { width: 100%; padding: 8px; margin-bottom: 10px; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; }
12
+ button { background-color: #6c5ce7; color: white; padding: 10px 15px; border: none; border-radius: 4px; cursor: pointer; width: 100%; font-size: 16px; }
13
+ button:disabled { background-color: #b2aae8; }
14
+ .result-container { margin-top: 15px; }
15
+ pre { background-color: #eee; padding: 15px; border-radius: 4px; white-space: pre-wrap; word-wrap: break-word; text-align: left; direction: ltr; max-height: 300px; overflow-y: auto; border: 1px solid #ddd; }
16
+ .copy-btn { background-color: #00b894; margin-top: 10px; width: auto; padding: 5px 10px; font-size: 12px; }
17
+ .loader { text-align: center; padding: 20px; color: #555; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  </style>
19
  </head>
20
  <body>
21
 
22
  <!-- بخش ساخت QR Code -->
23
  <div class="container">
24
+ <h2>۱. تست ساخت QR Code</h2>
25
+ <textarea id="text-input" placeholder="یک متن برای تست وارد کنید..." rows="3">test</textarea>
26
+ <button id="generate-btn" onclick="generateQRCode()">دریافت JSON ساخت کد</button>
27
+ <div id="generate-result" class="result-container" hidden>
28
+ <h3>خروجی JSON:</h3>
29
+ <pre id="generate-json-output"></pre>
30
+ <button class="copy-btn" onclick="copyToClipboard('generate-json-output', this)">کپی کردن JSON</button>
31
  </div>
32
+ <div id="generate-loader" class="loader" hidden>در حال ارتباط با سرور...</div>
33
  </div>
34
 
35
  <!-- بخش خواندن QR Code -->
36
  <div class="container">
37
+ <h2>۲. تست خواندن QR Code</h2>
38
  <input type="file" id="file-input" accept="image/*">
39
+ <button id="read-btn" onclick="readQRCode()">دریافت JSON خواندن کد</button>
40
+ <div id="read-result" class="result-container" hidden>
41
+ <h3>خروجی JSON:</h3>
42
+ <pre id="read-json-output"></pre>
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
  const API_BASE_URL = "https://elias207-qrcode.hf.space/gradio_api/";
50
 
51
  function generateSessionHash() { return Math.random().toString(36).substring(2, 11); }
52
+
53
  async function callGradioApi(fnIndex, data, sessionHash) {
54
  await fetch(API_BASE_URL + "queue/join?", {
55
  method: "POST",
 
61
  const interval = setInterval(async () => {
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();
 
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) {
 
84
  }
85
 
86
  async function generateQRCode() {
87
+ const btn = document.getElementById('generate-btn'), loader = document.getElementById('generate-loader');
88
+ const resultDiv = document.getElementById('generate-result'), outputPre = document.getElementById('generate-json-output');
 
 
 
 
 
 
 
 
 
 
89
 
90
+ btn.disabled = true; loader.hidden = false; resultDiv.hidden = true;
91
  try {
92
+ const text = document.getElementById('text-input').value;
93
  const sessionHash = generateSessionHash();
94
+ const result = await callGradioApi(0, [text], sessionHash);
95
+ outputPre.textContent = JSON.stringify(result, null, 2);
96
+ resultDiv.hidden = false;
 
 
 
 
 
 
 
97
  } catch (error) {
98
  alert("خطا در ساخت کد: " + error.message);
99
+ outputPre.textContent = "خطا: " + error.message;
100
+ resultDiv.hidden = false;
101
  } finally {
102
+ btn.disabled = false; loader.hidden = true;
 
103
  }
104
  }
105
 
 
108
  if (fileInput.files.length === 0) { alert("لطفا فایلی انتخاب کنید."); return; }
109
  const file = fileInput.files[0];
110
 
111
+ const btn = document.getElementById('read-btn'), loader = document.getElementById('read-loader');
112
+ const resultDiv = document.getElementById('read-result'), outputPre = document.getElementById('read-json-output');
 
 
 
 
 
113
 
114
+ btn.disabled = true; loader.hidden = false; resultDiv.hidden = true;
115
  try {
116
+ // 1. آپلود فایل
117
  const formData = new FormData();
118
  formData.append("files", file);
 
 
 
119
  const uploadRes = await fetch(API_BASE_URL + "upload", { method: "POST", body: formData });
120
+ if (!uploadRes.ok) throw new Error("آپلود فایل ناموفق بود. کد وضعیت: " + uploadRes.status);
121
+
122
  const uploadResult = await uploadRes.json();
123
  const tempFilePath = uploadResult[0];
124
 
125
+ // 2. ارسال درخواست پردازش با اطلاعات کامل فایل
126
  const sessionHash = generateSessionHash();
127
+ // **مهم**: ساختار کامل دیتا برای فایل
128
+ const fileData = [{
129
+ "path": tempFilePath,
130
+ "url": API_BASE_URL + "file=" + tempFilePath,
131
+ "orig_name": file.name,
132
+ "size": file.size,
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
+ outputPre.textContent = "خطا: " + error.message;
143
+ resultDiv.hidden = false;
144
  } finally {
145
+ btn.disabled = false; loader.hidden = true;
 
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>