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

Update index.html

Browse files
Files changed (1) hide show
  1. 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>دیباگر 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",
56
- headers: { "Content-Type": "application/json" },
57
- body: JSON.stringify({ "fn_index": fnIndex, "data": data, "session_hash": sessionHash }),
58
- });
59
-
60
- return new Promise((resolve, reject) => {
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();
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 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
 
@@ -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 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>
 
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>