File size: 1,604 Bytes
3393a89
 
8986465
6197cb4
 
8986465
6197cb4
8986465
 
 
 
3393a89
6197cb4
 
3393a89
 
6197cb4
3393a89
 
1c9094b
 
 
 
 
 
6197cb4
3393a89
 
 
1c9094b
 
 
 
0f7917e
3393a89
 
 
 
 
 
 
 
 
 
 
 
fdcdc66
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* classify uploaded images using a Huggingface model */
async function classifyImages(acceptedFiles) {
  let formData = new FormData();

  for (const file of acceptedFiles){
    formData.append('files', file);
  }
 let classifyResponse = await fetch('classify', {
    method: 'POST',
   body: formData
 });
  return classifyResponse;
}

const inputDoc = document.getElementById('doc-input');
const outputDocLink = document.getElementById('doc-output-link');

inputDoc.addEventListener("change", async event =>{
  /* hide Download button when in classification process */
  // outputDocLink.style.visibility = "hidden";

  // dlOutputBtn.disabled = true;
  outputDocLink.ariaDisabled = "true";
  outputDocLink.classList.add("disabled");

  const files = event.target.files;
  
  /* make Download button's visible, and add the link to download the resulting .csv file */
  await classifyImages(files).then(async (result) => {
    // outputDocLink.style.visibility = "visible";
    // dlOutputBtn.disabled = false;
    outputDocLink.ariaDisabled = "false";
    outputDocLink.classList.remove("disabled");

    const disposition = result.headers.get('Content-Disposition');
    const filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
    const matches = filenameRegex.exec(disposition);
    // const filename = matches != null && matches[1] ? matches[1].replace(/['"]/g, '') : 'data.csv';
    const filename = 'data.csv';

    const blob = await result.blob();
    const url = window.URL.createObjectURL(blob);

    // set download link to the download button
    outputDocLink.href = url;
  });
});