Elias207 commited on
Commit
185845e
·
verified ·
1 Parent(s): 4d00960

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +282 -19
index.html CHANGED
@@ -1,19 +1,282 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
19
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="fa" dir="rtl">
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: 20px;
17
+ flex-wrap: wrap;
18
+ }
19
+ .container {
20
+ background-color: #ffffff;
21
+ padding: 25px;
22
+ border-radius: 8px;
23
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
24
+ width: 100%;
25
+ max-width: 450px;
26
+ display: flex;
27
+ flex-direction: column;
28
+ gap: 15px;
29
+ }
30
+ h2 {
31
+ text-align: center;
32
+ color: #0d6efd;
33
+ margin-top: 0;
34
+ border-bottom: 2px solid #eee;
35
+ padding-bottom: 10px;
36
+ }
37
+ label, button, input {
38
+ font-size: 16px;
39
+ }
40
+ textarea, input[type="file"] {
41
+ width: 100%;
42
+ padding: 10px;
43
+ border-radius: 6px;
44
+ border: 1px solid #ddd;
45
+ box-sizing: border-box;
46
+ resize: vertical;
47
+ }
48
+ button {
49
+ background-color: #0d6efd;
50
+ color: white;
51
+ border: none;
52
+ padding: 12px 20px;
53
+ border-radius: 6px;
54
+ cursor: pointer;
55
+ transition: background-color 0.3s;
56
+ width: 100%;
57
+ }
58
+ button:hover {
59
+ background-color: #0b5ed7;
60
+ }
61
+ button:disabled {
62
+ background-color: #6c757d;
63
+ cursor: not-allowed;
64
+ }
65
+ .result-box {
66
+ margin-top: 15px;
67
+ padding: 15px;
68
+ border: 1px dashed #ccc;
69
+ border-radius: 6px;
70
+ min-height: 50px;
71
+ display: flex;
72
+ justify-content: center;
73
+ align-items: center;
74
+ text-align: center;
75
+ }
76
+ #qr-image {
77
+ max-width: 100%;
78
+ height: auto;
79
+ border-radius: 4px;
80
+ }
81
+ .loader {
82
+ font-style: italic;
83
+ color: #666;
84
+ }
85
+ #read-result {
86
+ word-break: break-all;
87
+ white-space: pre-wrap;
88
+ }
89
+ </style>
90
+ </head>
91
+ <body>
92
+
93
+ <!-- بخش ساخت QR Code -->
94
+ <div class="container">
95
+ <h2>ساخت QR Code</h2>
96
+ <label for="qr-text">متن مورد نظر را وارد کنید:</label>
97
+ <textarea id="qr-text" rows="4" placeholder="مثال: https://google.com"></textarea>
98
+ <button id="generate-btn" onclick="generateQRCode()">ساخت QR Code</button>
99
+ <div id="generate-result-box" class="result-box">
100
+ <span class="loader">تصویر QR Code اینجا نمایش داده می‌شود.</span>
101
+ </div>
102
+ </div>
103
+
104
+ <!-- بخش خواندن QR Code -->
105
+ <div class="container">
106
+ <h2>خواندن QR Code</h2>
107
+ <label for="qr-file">فایل تصویر QR Code را انتخاب کنید:</label>
108
+ <input type="file" id="qr-file" accept="image/*">
109
+ <button id="read-btn" onclick="readQRCode()">خواندن از تصویر</button>
110
+ <div id="read-result-box" class="result-box">
111
+ <p id="read-result" class="loader">متن خوانده شده اینجا نمایش داده می‌شود.</p>
112
+ </div>
113
+ </div>
114
+
115
+ <script>
116
+ const API_BASE_URL = "https://cultrix-qrcode-read-generate.hf.space/gradio_api";
117
+
118
+ // گرفتن ارجاع به عناصر HTML
119
+ const generateBtn = document.getElementById('generate-btn');
120
+ const readBtn = document.getElementById('read-btn');
121
+ const qrTextInput = document.getElementById('qr-text');
122
+ const qrFileInput = document.getElementById('qr-file');
123
+ const generateResultBox = document.getElementById('generate-result-box');
124
+ const readResultBox = document.getElementById('read-result-box');
125
+ const readResultP = document.getElementById('read-result');
126
+
127
+ // تابع برای تولید یک رشته تصادفی برای session_hash
128
+ function generateSessionHash() {
129
+ return Math.random().toString(36).substring(2, 15);
130
+ }
131
+
132
+ // تابع برای گوش دادن به نتایج از طریق EventSource
133
+ function listenForData(sessionHash, onComplete, onError) {
134
+ const eventSource = new EventSource(`${API_BASE_URL}/queue/data?session_hash=${sessionHash}`);
135
+
136
+ eventSource.onmessage = function(event) {
137
+ const data = JSON.parse(event.data);
138
+
139
+ if (data.msg === 'process_completed') {
140
+ eventSource.close();
141
+ onComplete(data.output);
142
+ } else if (data.msg === 'process_generating' || data.msg === 'process_starts') {
143
+ // در حال پردازش...
144
+ console.log('Processing...');
145
+ }
146
+ };
147
+
148
+ eventSource.onerror = function(err) {
149
+ console.error("EventSource failed:", err);
150
+ eventSource.close();
151
+ onError("خطا در برقراری ارتباط با سرور.");
152
+ };
153
+ }
154
+
155
+ // تابع برای ساخت QR Code
156
+ async function generateQRCode() {
157
+ const text = qrTextInput.value.trim();
158
+ if (!text) {
159
+ alert("لطفاً متنی را برای ساخت QR Code وارد کنید.");
160
+ return;
161
+ }
162
+
163
+ generateBtn.disabled = true;
164
+ generateResultBox.innerHTML = '<span class="loader">در حال ساخت... لطفاً صبر کنید.</span>';
165
+
166
+ const sessionHash = generateSessionHash();
167
+ const payload = {
168
+ "data": [text],
169
+ "event_data": null,
170
+ "fn_index": 0, // ایندکس تابع ساخت QR
171
+ "trigger_id": 5, // این مقادیر از لاگ‌ها گرفته شده
172
+ "session_hash": sessionHash
173
+ };
174
+
175
+ try {
176
+ // 1. ارسال درخواست برای شروع پردازش
177
+ const response = await fetch(`${API_BASE_URL}/queue/join?`, {
178
+ method: 'POST',
179
+ headers: { 'Content-Type': 'application/json' },
180
+ body: JSON.stringify(payload)
181
+ });
182
+
183
+ if (!response.ok) throw new Error('خطا در ارسال درخواست به صف.');
184
+
185
+ // 2. گوش دادن برای دریافت نتیجه
186
+ listenForData(sessionHash,
187
+ (output) => {
188
+ const imageUrl = `${API_BASE_URL.replace('/gradio_api', '')}/file=${output.data[0].path}`;
189
+ generateResultBox.innerHTML = `<img id="qr-image" src="${imageUrl}" alt="Generated QR Code">`;
190
+ generateBtn.disabled = false;
191
+ },
192
+ (errorMsg) => {
193
+ generateResultBox.innerHTML = `<span style="color:red;">${errorMsg}</span>`;
194
+ generateBtn.disabled = false;
195
+ }
196
+ );
197
+
198
+ } catch (error) {
199
+ console.error('Error generating QR code:', error);
200
+ generateResultBox.innerHTML = `<span style="color:red;">خطا: ${error.message}</span>`;
201
+ generateBtn.disabled = false;
202
+ }
203
+ }
204
+
205
+ // تابع برای خواندن QR Code
206
+ async function readQRCode() {
207
+ const file = qrFileInput.files[0];
208
+ if (!file) {
209
+ alert("لطفاً یک فایل تصویر را انتخاب کنید.");
210
+ return;
211
+ }
212
+
213
+ readBtn.disabled = true;
214
+ readResultP.textContent = 'در حال آپلود و پردازش...';
215
+ readResultP.classList.add('loader');
216
+
217
+ try {
218
+ // 1. آپلود فایل تصویر
219
+ const formData = new FormData();
220
+ formData.append('files', file);
221
+
222
+ const uploadResponse = await fetch(`${API_BASE_URL}/upload?`, {
223
+ method: 'POST',
224
+ body: formData
225
+ });
226
+
227
+ if (!uploadResponse.ok) throw new Error('خطا در آپلود فایل.');
228
+
229
+ const uploadResult = await uploadResponse.json();
230
+ const serverFilePath = uploadResult[0];
231
+
232
+ // 2. ارسال درخواست برای خواندن QR کد با مسیر فایل آپلود شده
233
+ const sessionHash = generateSessionHash();
234
+ const payload = {
235
+ "data": [{
236
+ "path": serverFilePath,
237
+ "url": `${API_BASE_URL.replace('/gradio_api', '')}/file=${serverFilePath}`,
238
+ "orig_name": file.name,
239
+ "size": file.size,
240
+ "mime_type": file.type,
241
+ "meta": { "_type": "gradio.FileData" }
242
+ }],
243
+ "event_data": null,
244
+ "fn_index": 1, // ایندکس تابع خواندن QR
245
+ "trigger_id": 13,
246
+ "session_hash": sessionHash
247
+ };
248
+
249
+ const joinResponse = await fetch(`${API_BASE_URL}/queue/join?`, {
250
+ method: 'POST',
251
+ headers: { 'Content-Type': 'application/json' },
252
+ body: JSON.stringify(payload)
253
+ });
254
+
255
+ if (!joinResponse.ok) throw new Error('خطا در ارسال درخواست خواندن به صف.');
256
+
257
+ // 3. گوش دادن برای دریافت نتیجه
258
+ listenForData(sessionHash,
259
+ (output) => {
260
+ const decodedText = output.data[0];
261
+ readResultP.textContent = decodedText || "متنی در QR Code یافت نشد.";
262
+ readResultP.classList.remove('loader');
263
+ readBtn.disabled = false;
264
+ },
265
+ (errorMsg) => {
266
+ readResultP.textContent = errorMsg;
267
+ readResultP.style.color = 'red';
268
+ readBtn.disabled = false;
269
+ }
270
+ );
271
+
272
+ } catch (error) {
273
+ console.error('Error reading QR code:', error);
274
+ readResultP.textContent = `خطا: ${error.message}`;
275
+ readResultP.style.color = 'red';
276
+ readBtn.disabled = false;
277
+ }
278
+ }
279
+
280
+ </script>
281
+ </body>
282
+ </html>