iThors commited on
Commit
72153b1
·
verified ·
1 Parent(s): 512efb5

Update frontend/index.html

Browse files
Files changed (1) hide show
  1. frontend/index.html +214 -99
frontend/index.html CHANGED
@@ -1,3 +1,4 @@
 
1
  <!DOCTYPE html>
2
  <html lang="en">
3
  <head>
@@ -5,14 +6,10 @@
5
  <meta name="viewport" content="width=device-width,initial-scale=1" />
6
  <title>PDF Token Splitter</title>
7
  <style>
8
- html, body {
9
- padding-top: 72px;
10
- height: 100%;
11
- margin: 0;
12
- padding: 0;
13
- }
14
  body {
15
- padding-top: 72px;
16
  font-family: Arial, sans-serif;
17
  background: linear-gradient(to bottom right, #4facfe, #00f2fe);
18
  display: flex;
@@ -33,118 +30,236 @@
33
  justify-content: center;
34
  gap: 12px;
35
  align-items: center;
36
- position: fixed;
37
  top: 0;
38
- z-index: 2000;
39
- }
40
- header .left {
41
- font-weight: 700;
42
- }
43
- header .right {
44
- display: flex;
45
- gap: 8px;
46
- align-items: center;
47
- }
48
- header .right a {
49
- color: white;
50
- text-decoration: none;
51
- font-weight: 600;
52
- display: flex;
53
- gap: 6px;
54
- align-items: center;
55
- }
56
- .coffee {
57
- width: 18px;
58
- height: 18px;
59
- display: inline-block;
60
- }
61
- main {
62
- flex: 1;
63
- width: 100%;
64
- display: flex;
65
- flex-direction: column;
66
- align-items: center;
67
- margin-top: 32px;
68
  }
69
- .count {
70
- font-size: 1.25em;
71
- color: #222;
72
- margin: 8px 0;
 
 
 
 
 
 
 
 
 
 
 
73
  }
 
 
 
 
 
 
 
 
 
 
74
  </style>
75
  </head>
76
  <body>
77
  <header>
78
- <div class="left">
79
- Made with love by Rohit Sadanand
80
- </div>
81
  <div class="right">
82
- <span>Usage count: <span id="usageCountHeader" class="count">...</span></span>
83
  <a href="https://buymeacoffee.com/anchoredlattice" target="_blank" rel="noopener noreferrer">
84
- <span class="coffee">
85
- <!-- BuyMeACoffee SVG (replace with your actual SVG icon if needed) -->
86
- <svg xmlns="http://www.w3.org/2000/svg" height="18" viewBox="0 0 24 24"><path fill="yellow" d="M19 15H5l-1.405-4.222A1 1 0 0 1 4.535 9H19.465a1 1 0 0 1 .94 1.778L19 15zM7 5a5 5 0 1 1 10 0H7zm14 8.465V10a2 2 0 0 0-2-2H5a2 2 0 0 0-2 2v3.465l1.432 4.317A4 4 0 0 0 8.236 21h7.528a4 4 0 0 0 3.804-3.218L21 13.465zm-2.764 6.17A2 2 0 0 1 15.764 19H8.236a2 2 0 0 1-1.936-1.365L5.13 15H18.87l-1.634 4.635z"/></svg>
87
- </span>
 
88
  Buy me a coffee
89
  </a>
90
  </div>
91
  </header>
 
92
  <main>
93
  <h1>PDF Token Splitter</h1>
94
- <div class="count">
95
- Usage count: <span id="usageCount">...</span>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  </div>
97
- <!-- Your application content goes here -->
98
- <form id="uploadForm" enctype="multipart/form-data">
99
- <input type="file" name="pdf_file" accept=".pdf" required />
100
- <button type="submit">Upload & Split</button>
101
- </form>
102
- <div id="result"></div>
103
  </main>
104
- <script>
105
- const APIURL = ""; // Change if necessary for deployment, otherwise keep ""
 
 
 
106
  function setCountInUI(n) {
107
- document.getElementById('usageCount').innerText = n;
108
- document.getElementById('usageCountHeader').innerText = n;
109
- localStorage.setItem('tokenSplitterCount', n);
 
110
  }
111
- function fetchAndSetCount() {
112
- fetch(APIURL + "/api/count")
113
- .then(r => r.ok ? r.json() : Promise.reject())
114
- .then(res => {
115
- setCountInUI(res.count);
116
- })
117
- .catch(() => {
118
- // If backend unavailable, fallback to localStorage
119
- const localCount = localStorage.getItem('tokenSplitterCount');
120
- setCountInUI(localCount ? Number(localCount) : 0);
121
- });
122
  }
123
- fetchAndSetCount();
124
-
125
- document.getElementById('uploadForm').addEventListener('submit', function(e) {
126
- e.preventDefault();
127
- const formData = new FormData(e.target);
128
- fetch(APIURL + "/api/split", {
129
- method: "POST",
130
- body: formData
131
- })
132
- .then(r => r.json())
133
- .then(res => {
134
- document.getElementById('result').innerText = res.message || "Completed!";
135
- // Increment usage count for successful split
136
- fetch(APIURL + "/api/increment", { method: "POST" })
137
- .then(r => r.json())
138
- .then(res => setCountInUI(res.count))
139
- .catch(() => {
140
- let fallback = Number(localStorage.getItem('tokenSplitterCount') || '0') + 1;
141
- setCountInUI(fallback);
142
- });
143
- })
144
- .catch(err => {
145
- document.getElementById('result').innerText = "Failed: " + err;
146
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  });
148
- </script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  </body>
150
  </html>
 
1
+ <!-- frontend/index.html -->
2
  <!DOCTYPE html>
3
  <html lang="en">
4
  <head>
 
6
  <meta name="viewport" content="width=device-width,initial-scale=1" />
7
  <title>PDF Token Splitter</title>
8
  <style>
9
+ html,body {
10
+ padding-top: 72px; height: 100%; margin: 0; padding: 0; }
 
 
 
 
11
  body {
12
+ padding-top: 72px;
13
  font-family: Arial, sans-serif;
14
  background: linear-gradient(to bottom right, #4facfe, #00f2fe);
15
  display: flex;
 
30
  justify-content: center;
31
  gap: 12px;
32
  align-items: center;
33
+ position: fixed; top: 56px; width: 100%; z-index: 2000;
34
  top: 0;
35
+ z-index: 1000;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  }
37
+ header .left { font-weight: 700; }
38
+ header .right { display: flex; gap: 8px; align-items: center; }
39
+ header .right a { color: white; text-decoration: none; font-weight: 600; display:flex; gap:6px; align-items:center; }
40
+ .coffee { width:18px; height:18px; display:inline-block; }
41
+ main { flex: 1; width: 100%; display: flex; flex-direction: column; align-items: center; padding: 28px 12px 60px; box-sizing: border-box; }
42
+ h1 { color: white; margin: 10px 0 6px; font-size: 2.4rem; text-align:center; }
43
+ p.lead { color: white; margin: 0 0 18px; text-align:center; font-size: 1.05rem; }
44
+ .upload-box {
45
+ background: #f7f7f7;
46
+ padding: 2rem;
47
+ border-radius: 12px;
48
+ box-shadow: 0 6px 30px rgba(0,0,0,0.22);
49
+ width: 380px;
50
+ max-width: calc(100% - 40px);
51
+ text-align: center;
52
  }
53
+ input[type="file"], input[type="number"], input[type="range"] { margin: 0.5rem 0; width: 100%; }
54
+ .slider-container { margin: 1rem 0; text-align: left; }
55
+ .counter { margin-top: 1rem; font-weight: 700; color: #222; }
56
+ button { background: #4facfe; color:white; border:none; padding: 0.7rem 1.4rem; border-radius: 8px; cursor:pointer; font-size:1rem; }
57
+ button:hover { background:#3b8fe0; }
58
+ .progress-bar { height:10px; background:#ddd; border-radius:6px; overflow:hidden; margin: 12px 0; display:none; }
59
+ .progress-bar-inner { height:100%; width:0%; background:#4facfe; transition:width .3s; }
60
+ .past-jobs { margin-top: 18px; background: rgba(255,255,255,0.95); padding: 10px; border-radius:8px; width: 100%; max-width:600px; }
61
+ .job { display:flex; justify-content:space-between; padding:8px; border-radius:6px; margin:6px 0; background:#fafafa; }
62
+ .small-muted { font-size:0.85rem; color:#666; margin-top:6px; }
63
  </style>
64
  </head>
65
  <body>
66
  <header>
67
+ <div class="left">Made with ❤️ by Rohit Sadanand</div>
 
 
68
  <div class="right">
69
+ <div id="headerCount" style="font-weight:600;">Used by <span id="usageCountHeader">0</span> people</div>
70
  <a href="https://buymeacoffee.com/anchoredlattice" target="_blank" rel="noopener noreferrer">
71
+ <!-- simple inline SVG coffee icon -->
72
+ <svg class="coffee" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
73
+ <path d="M3 7h14v6a5 5 0 0 1-5 5H8a5 5 0 0 1-5-5V7z" stroke="currentColor" stroke-width="1.4" stroke-linecap="round" stroke-linejoin="round"/>
74
+ <path d="M19 8a3 3 0 0 0 0 6h1" stroke="currentColor" stroke-width="1.4" stroke-linecap="round" stroke-linejoin="round"/>
75
+ </svg>
76
  Buy me a coffee
77
  </a>
78
  </div>
79
  </header>
80
+
81
  <main>
82
  <h1>PDF Token Splitter</h1>
83
+ <p class="lead">Upload large PDFs and split them into custom token-sized chunks</p>
84
+
85
+ <div class="upload-box" role="region" aria-label="upload box">
86
+ <input type="file" id="pdfFile" accept="application/pdf" multiple style="display:none;" />
87
+ <label for="pdfFile" id="fileLabel" style="background:#4facfe;color:white;padding:0.45rem 1rem;border-radius:6px;cursor:pointer;display:inline-block;">Choose PDF(s)</label>
88
+ <div id="fileNames" style="margin-top:0.6rem;font-size:0.95rem;color:#333;"></div>
89
+
90
+ <div class="slider-container">
91
+ <label>Token Size:</label>
92
+ <input type="range" id="tokenSize" min="500" max="500000" step="100" value="1000" />
93
+ <input type="number" id="customTokenSize" min="500" value="1000" />
94
+ </div>
95
+
96
+ <div style="margin-top:8px;">
97
+ <button id="uploadBtn">Upload & Process</button>
98
+ </div>
99
+
100
+ <div class="progress-bar"><div class="progress-bar-inner" id="progressInner"></div></div>
101
+
102
+ <div class="counter">Used by <span id="usageCount">0</span> people</div>
103
+ <div class="small-muted">Your files remain available for 15 minutes for download</div>
104
+ </div>
105
+
106
+ <div class="past-jobs" id="pastJobs" style="display:none;">
107
+ <h3>Past Jobs</h3>
108
+ <div id="jobsList"></div>
109
  </div>
 
 
 
 
 
 
110
  </main>
111
+
112
+
113
+ <script>
114
+ const API_URL = ""; // relative path
115
+
116
  function setCountInUI(n) {
117
+ const safe = Number.isFinite(n) ? n : 0;
118
+ const els = document.querySelectorAll('#usageCount, #usageCountHeader');
119
+ els.forEach(el => { if (el) el.innerText = safe; });
120
+ try { localStorage.setItem('localUsageCount', String(safe)); } catch(e){}
121
  }
122
+
123
+ function readLocalFallback() {
124
+ try {
125
+ const v = parseInt(localStorage.getItem('localUsageCount') || '0', 10);
126
+ return Number.isFinite(v) ? v : 0;
127
+ } catch (e) { return 0; }
 
 
 
 
 
128
  }
129
+
130
+ async function fetchCount() {
131
+ try {
132
+ const res = await fetch(API_URL + '/api/count', { cache: 'no-store' });
133
+ if (!res.ok) throw new Error('Bad response ' + res.status);
134
+ const data = await res.json();
135
+ if (!data || typeof data.count === 'undefined') throw new Error('Missing count');
136
+ const num = Number(data.count);
137
+ if (!Number.isFinite(num)) throw new Error('Invalid count');
138
+ setCountInUI(num);
139
+ } catch (err) {
140
+ console.warn('fetchCount failed', err);
141
+ setCountInUI(readLocalFallback());
142
+ }
143
+ }
144
+
145
+ async function incrementCount() {
146
+ try {
147
+ const res = await fetch(API_URL + '/api/increment', { method: 'POST' });
148
+ if (!res.ok) throw new Error('Bad increment ' + res.status);
149
+ const data = await res.json();
150
+ const num = Number(data.count);
151
+ if (!Number.isFinite(num)) throw new Error('Invalid count');
152
+ setCountInUI(num);
153
+ } catch (err) {
154
+ console.warn('incrementCount failed', err);
155
+ const local = readLocalFallback() + 1;
156
+ setCountInUI(local);
157
+ }
158
+ }
159
+
160
+ // --- existing UI logic below (upload, jobs, etc.) ---
161
+
162
+ const pdfFile = document.getElementById('pdfFile');
163
+ let pastJobs = JSON.parse(localStorage.getItem('pastJobs') || '[]');
164
+
165
+ document.getElementById('tokenSize').addEventListener('input', (e) => {
166
+ document.getElementById('customTokenSize').value = e.target.value;
167
  });
168
+ document.getElementById('customTokenSize').addEventListener('input', (e) => {
169
+ document.getElementById('tokenSize').value = e.target.value;
170
+ });
171
+ pdfFile.addEventListener('change', function () {
172
+ const names = Array.from(this.files).map(f => f.name).join(', ');
173
+ document.getElementById('fileNames').innerText = names || '';
174
+ });
175
+
176
+ function updateJobsUI() {
177
+ const jobsList = document.getElementById('jobsList');
178
+ jobsList.innerHTML = '';
179
+ const now = Date.now();
180
+ pastJobs.forEach(job => {
181
+ const expired = (now - job.timestamp) > 15 * 60 * 1000;
182
+ const div = document.createElement('div');
183
+ div.className = 'job' + (expired ? ' expired' : '');
184
+ const span = document.createElement('span');
185
+ span.textContent = job.filename;
186
+ const button = document.createElement('button');
187
+ button.textContent = 'Download';
188
+ if (expired) button.disabled = true;
189
+ else button.onclick = () => downloadFile(job.job_id, job.filename);
190
+ div.appendChild(span);
191
+ div.appendChild(button);
192
+ jobsList.appendChild(div);
193
+ });
194
+ document.getElementById('pastJobs').style.display = pastJobs.length ? 'block' : 'none';
195
+ localStorage.setItem('pastJobs', JSON.stringify(pastJobs));
196
+ }
197
+
198
+ async function downloadFile(jobId, filename) {
199
+ const url = API_URL + '/download/' + jobId;
200
+ const baseName = filename.replace(/\.[^/.]+$/, '');
201
+ const downloadName = baseName + '_chunks.zip';
202
+ try {
203
+ const res = await fetch(url);
204
+ if (!res.ok) throw new Error('File not found or expired');
205
+ const blob = await res.blob();
206
+ const link = document.createElement('a');
207
+ link.href = window.URL.createObjectURL(blob);
208
+ link.download = downloadName;
209
+ link.click();
210
+ } catch (err) {
211
+ alert(err.message);
212
+ }
213
+ }
214
+
215
+ async function uploadFile() {
216
+ const fileInput = pdfFile;
217
+ const tokenSize = parseInt(document.getElementById('customTokenSize').value);
218
+ if (!fileInput.files.length) {
219
+ alert('Please select a PDF file');
220
+ return;
221
+ }
222
+
223
+ const progressBar = document.querySelector('.progress-bar');
224
+ const progressInner = document.getElementById('progressInner');
225
+ progressBar.style.display = 'block';
226
+ progressInner.style.width = '0%';
227
+ let progress = 0;
228
+ const interval = setInterval(() => {
229
+ progress = Math.min(progress + 8, 90);
230
+ progressInner.style.width = progress + '%';
231
+ }, 300);
232
+
233
+ const formData = new FormData();
234
+ for (const f of fileInput.files) formData.append('files', f);
235
+ formData.append('token_size', tokenSize);
236
+
237
+ try {
238
+ const res = await fetch(API_URL + '/api/upload', { method: 'POST', body: formData });
239
+ const data = await res.json();
240
+ if (data.job_id) {
241
+ pastJobs.unshift({ filename: data.filename, job_id: data.job_id, timestamp: Date.now() });
242
+ updateJobsUI();
243
+ // increment the counter now that upload succeeded
244
+ incrementCount();
245
+ } else {
246
+ alert('Error processing file: ' + (data.error || 'unknown'));
247
+ }
248
+ } catch (err) {
249
+ alert('Upload failed: ' + err.message);
250
+ } finally {
251
+ clearInterval(interval);
252
+ progressInner.style.width = '100%';
253
+ setTimeout(() => { progressBar.style.display = 'none'; progressInner.style.width = '0%'; }, 400);
254
+ }
255
+ }
256
+
257
+ document.getElementById('uploadBtn').addEventListener('click', uploadFile);
258
+
259
+ // init
260
+ fetchCount();
261
+ updateJobsUI();
262
+ </script>
263
+
264
  </body>
265
  </html>