Team Ai
Apppublic

ConradLax/document_classifier

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
script.js47 linesDownload Raw Back to static
1/* classify uploaded images using a Huggingface model */2async function classifyImages(acceptedFiles) {3  let formData = new FormData();4 5  for (const file of acceptedFiles){6    formData.append('files', file);7  }8 let classifyResponse = await fetch('classify', {9    method: 'POST',10   body: formData11 });12  return classifyResponse;13}14 15const inputDoc = document.getElementById('doc-input');16const outputDocLink = document.getElementById('doc-output-link');17 18inputDoc.addEventListener("change", async event =>{19  /* hide Download button when in classification process */20  // outputDocLink.style.visibility = "hidden";21 22  // dlOutputBtn.disabled = true;23  outputDocLink.ariaDisabled = "true";24  outputDocLink.classList.add("disabled");25 26  const files = event.target.files;27  28  /* make Download button's visible, and add the link to download the resulting .csv file */29  await classifyImages(files).then(async (result) => {30    // outputDocLink.style.visibility = "visible";31    // dlOutputBtn.disabled = false;32    outputDocLink.ariaDisabled = "false";33    outputDocLink.classList.remove("disabled");34 35    const disposition = result.headers.get('Content-Disposition');36    const filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;37    const matches = filenameRegex.exec(disposition);38    // const filename = matches != null && matches[1] ? matches[1].replace(/['"]/g, '') : 'data.csv';39    const filename = 'data.csv';40 41    const blob = await result.blob();42    const url = window.URL.createObjectURL(blob);43 44    // set download link to the download button45    outputDocLink.href = url;46  });47});