File size: 1,546 Bytes
0f56e30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
function onFileSelected(event) {
  const selectedFile = event.target.files[0];
  const reader = new FileReader();

  const imgtag = document.getElementById("myImage");
  imgtag.title = selectedFile.name;

  reader.onload = function(event) {
    // set the myImage div to show the uploaded image file
    imgtag.src = event.target.result;
  };

  reader.readAsDataURL(selectedFile);

  const predictionDiv = document.getElementById('prediction')
  const errorDiv = document.getElementById('error')

  reader.addEventListener("loadend", function() {             
      fetch('https://hf.space/embed/jph00/testing/+/api/predict/', {
          method: "POST",
          // reader.result is the base64 string of the uploaded image
          body: JSON.stringify({"data": [reader.result]}),
          headers: { "Content-Type": "application/json" } })
          .then(function(response) {
            if (response.status != 200) {
                // early return if the api errors out and show error message
                errorDiv.innerHTML = '<u>Sorry the API is not working currently. Please try again later</u>'
                predictionDiv.innerHTML = '';
                return;
            }
            return response.json(); })
            .then(function(json_response) {
                const label = json_response?.data[0]?.label
                // show the prediction
                predictionDiv.innerHTML = `πŸŽ‰ <u>Prediction: ${label}</u> πŸŽ‰`
                errorDiv.innerHTML = '';
                return;
            })
  });
}