zainali95 commited on
Commit
53a43fe
·
1 Parent(s): 28d13bb

plain html js implementation of the Gradio api implementation

Browse files
Files changed (1) hide show
  1. index.html +46 -0
index.html ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Gradio API Image Upload</title>
7
+ </head>
8
+ <body>
9
+ <h2>Gradio API Image Upload and Prediction</h2>
10
+ <label for="fileInput">Upload an Image: </label>
11
+ <input type="file" id="fileInput" accept="image/*">
12
+ <button id="submitButton">Predict</button>
13
+
14
+ <h3>Prediction Result:</h3>
15
+ <pre id="result"></pre>
16
+
17
+ <script type="module">
18
+ import { Client } from "https://cdn.jsdelivr.net/npm/@gradio/client/dist/index.min.js";
19
+
20
+ document.getElementById('submitButton').addEventListener('click', async () => {
21
+ const fileInput = document.getElementById('fileInput');
22
+ const file = fileInput.files[0];
23
+
24
+ if (!file) {
25
+ document.getElementById('result').textContent = 'Please select an image file.';
26
+ return;
27
+ }
28
+
29
+ try {
30
+ // Create a Blob from the uploaded file
31
+ const exampleImage = await file.arrayBuffer();
32
+ const client = await Client.connect("zainali95/test001");
33
+
34
+ // Make the prediction request
35
+ const result = await client.predict("/predict", { img: new Blob([exampleImage], { type: file.type }) });
36
+ console.log(result);
37
+
38
+ // Display the prediction result
39
+ document.getElementById('result').textContent = JSON.stringify(result.data, null, 2);
40
+ } catch (error) {
41
+ document.getElementById('result').textContent = 'Error: ' + error.message;
42
+ }
43
+ });
44
+ </script>
45
+ </body>
46
+ </html>