Spaces:
Running

PDF_Tokenizer / frontend /index.html
iThors's picture
Update frontend/index.html
dbf4268 verified
Raw History Blame Contribute Delete
8.24 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>PDF Token Splitter</title>
<style>
html,body { height: 100%; margin: 0; padding: 0; }
body {
font-family: Arial, sans-serif;
background: linear-gradient(to bottom right, #4facfe, #00f2fe);
display: flex;
flex-direction: column;
align-items: center;
color: #333;
min-height: 100vh;
}
header { display:none; } /* avoid HF topbar conflict */
main { flex: 1; width: 100%; display: flex; flex-direction: column; align-items: center; padding: 28px 12px 60px; box-sizing: border-box; }
.title-wrap { text-align:center; margin-top: 18px; }
h1 { color: white; margin: 10px 0 6px; font-size: 2.4rem; }
.byline { color: white; margin: 6px 0 12px; font-size: 1.05rem; }
.byline a { color: #002f6c; font-weight:600; text-decoration: underline; margin-left:8px; }
.upload-box {
background: #f7f7f7;
padding: 2rem;
border-radius: 12px;
box-shadow: 0 6px 30px rgba(0,0,0,0.22);
width: 380px;
max-width: calc(100% - 40px);
text-align: center;
}
input[type="file"], input[type="number"], input[type="range"] { margin: 0.5rem 0; width: 100%; }
.slider-container { margin: 1rem 0; text-align: left; }
.counter { margin-top: 1rem; font-weight: 700; color: #222; }
button { background: #4facfe; color:white; border:none; padding: 0.7rem 1.4rem; border-radius: 8px; cursor:pointer; font-size:1rem; }
button:hover { background:#3b8fe0; }
.progress-bar { height:10px; background:#ddd; border-radius:6px; overflow:hidden; margin: 12px 0; display:none; }
.progress-bar-inner { height:100%; width:0%; background:#4facfe; transition:width .3s; }
.past-jobs { margin-top: 18px; background: rgba(255,255,255,0.95); padding: 10px; border-radius:8px; width: 100%; max-width:600px; }
.job { display:flex; justify-content:space-between; padding:8px; border-radius:6px; margin:6px 0; background:#fafafa; }
.small-muted { font-size:0.85rem; color:#666; margin-top:6px; }
</style>
</head>
<body>
<main>
<div class="title-wrap">
<h1>PDF Token Splitter</h1>
<div class="byline">Made with ❤️ by Rohit Sadanand | <a href="https://buymeacoffee.com/anchoredlattice" target="_blank" rel="noopener noreferrer">Buy me a coffee</a> · <span id="usageLine">Used by <strong id="usageCount">0</strong> people</span></div>
</div>
<h2 class="lead" style="color:white;margin:14px 0 22px;">Upload large PDFs and split them into custom token-sized chunks</h2>
<div class="upload-box" role="region" aria-label="upload box">
<input type="file" id="pdfFile" accept="application/pdf" multiple style="display:none;" />
<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>
<div id="fileNames" style="margin-top:0.6rem;font-size:0.95rem;color:#333;"></div>
<div class="slider-container">
<label>Token Size:</label>
<input type="range" id="tokenSize" min="500" max="500000" step="100" value="1000" />
<input type="number" id="customTokenSize" min="500" value="1000" />
</div>
<div style="margin-top:8px;">
<button id="uploadBtn">Upload & Process</button>
</div>
<div class="progress-bar"><div class="progress-bar-inner" id="progressInner"></div></div>
<div class="counter">Used by <span id="usageCountAlt" style="font-weight:800;">0</span> people</div>
<div class="small-muted">Your files remain available for 15 minutes for download</div>
</div>
<div class="past-jobs" id="pastJobs" style="display:none;">
<h3>Past Jobs</h3>
<div id="jobsList"></div>
</div>
</main>
<script>
const API_URL = "";
async function fetchCount() {
try {
const res = await fetch(API_URL + '/api/count', {cache:'no-store'});
if (!res.ok) throw new Error('bad');
const data = await res.json();
const n = Number(data.count);
if (!Number.isFinite(n)) throw new Error('badval');
document.getElementById('usageCount').innerText = n;
const alt = document.getElementById('usageCountAlt');
if (alt) alt.innerText = n;
} catch (e) {
console.warn('fetchCount failed', e);
}
}
async function incrementCount() {
try {
const res = await fetch(API_URL + '/api/increment', {method:'POST'});
if (!res.ok) throw new Error('bad');
const data = await res.json();
const n = Number(data.count);
if (Number.isFinite(n)) {
document.getElementById('usageCount').innerText = n;
const alt = document.getElementById('usageCountAlt');
if (alt) alt.innerText = n;
}
} catch (e) {
console.warn('incrementCount failed', e);
}
}
const pdfFile = document.getElementById('pdfFile');
let pastJobs = JSON.parse(localStorage.getItem('pastJobs') || '[]');
document.getElementById('tokenSize').addEventListener('input', (e)=>{ document.getElementById('customTokenSize').value = e.target.value; });
document.getElementById('customTokenSize').addEventListener('input', (e)=>{ document.getElementById('tokenSize').value = e.target.value; });
pdfFile.addEventListener('change', function(){ document.getElementById('fileNames').innerText = Array.from(this.files).map(f=>f.name).join(', ') || ''; });
function updateJobsUI(){ const jobsList = document.getElementById('jobsList'); jobsList.innerHTML=''; const now=Date.now(); pastJobs.forEach(job=>{ const expired=(now-job.timestamp)>15*60*1000; const div=document.createElement('div'); div.className='job'+(expired?' expired':''); const span=document.createElement('span'); span.textContent=job.filename; const button=document.createElement('button'); button.textContent='Download'; if(expired) button.disabled=true; else button.onclick=()=>downloadFile(job.job_id, job.filename); div.appendChild(span); div.appendChild(button); jobsList.appendChild(div); }); document.getElementById('pastJobs').style.display=pastJobs.length?'block':'none'; localStorage.setItem('pastJobs', JSON.stringify(pastJobs)); }
async function downloadFile(jobId, filename){ const url=API_URL+'/download/'+jobId; const baseName=filename.replace(/\.[^/.]+$/,''); const downloadName=baseName+'_chunks.zip'; try{ const res=await fetch(url); if(!res.ok) throw new Error('File missing'); const blob=await res.blob(); const link=document.createElement('a'); link.href=URL.createObjectURL(blob); link.download=downloadName; link.click(); }catch(e){ alert(e.message); } }
async function uploadFile(){
const fileInput=pdfFile;
const tokenSize=parseInt(document.getElementById('customTokenSize').value);
if(!fileInput.files.length){ alert('Please select a PDF file'); return; }
const progressBar=document.querySelector('.progress-bar');
const progressInner=document.getElementById('progressInner');
progressBar.style.display='block'; progressInner.style.width='0%';
let progress=0; const interval=setInterval(()=>{ progress=Math.min(progress+8,90); progressInner.style.width=progress+'%'; },300);
const formData=new FormData();
for(const f of fileInput.files) formData.append('files', f);
formData.append('token_size', tokenSize);
try{
const res=await fetch(API_URL+'/api/upload',{method:'POST', body: formData});
const data=await res.json();
if(data.job_id){ pastJobs.unshift({filename: data.filename, job_id: data.job_id, timestamp: Date.now()}); updateJobsUI(); incrementCount(); }
else alert('Error: '+(data.error||'unknown'));
}catch(e){ alert('Upload failed: '+e.message); }
finally{ clearInterval(interval); progressInner.style.width='100%'; setTimeout(()=>{ progressBar.style.display='none'; progressInner.style.width='0%'; },400); }
}
document.getElementById('uploadBtn').addEventListener('click', uploadFile);
fetchCount(); updateJobsUI();
</script>
</body>
</html>