Spaces:
Running
Running
Download script.js from faisalbilal/mymlmodel: direct link, hf CLI and curl.
- Browser
- Download file 1.42 kB
-
https://huggingface.co/spaces/faisalbilal/mymlmodel/resolve/main/script.js
- Command line
-
hf download hf://spaces/faisalbilal/mymlmodel/script.js
-
curl -L -o script.js https://huggingface.co/spaces/faisalbilal/mymlmodel/resolve/main/script.js
1.42 kB
| let session = null; | |
| async function loadModel() { | |
| try { | |
| session = await ort.InferenceSession.create("iris_model.onnx"); | |
| document.getElementById("result").innerText = | |
| "Model loaded successfully!"; | |
| } catch (error) { | |
| document.getElementById("result").innerText = | |
| "Error loading model: " + error; | |
| } | |
| } | |
| async function predict() { | |
| if (!session) { | |
| alert("Model is still loading. Please wait."); | |
| return; | |
| } | |
| const sepalLength = parseFloat( | |
| document.getElementById("sepalLength").value | |
| ); | |
| const sepalWidth = parseFloat( | |
| document.getElementById("sepalWidth").value | |
| ); | |
| const petalLength = parseFloat( | |
| document.getElementById("petalLength").value | |
| ); | |
| const petalWidth = parseFloat( | |
| document.getElementById("petalWidth").value | |
| ); | |
| const input = new Float32Array([ | |
| sepalLength, | |
| sepalWidth, | |
| petalLength, | |
| petalWidth | |
| ]); | |
| const tensor = new ort.Tensor( | |
| "float32", | |
| input, | |
| [1, 4] | |
| ); | |
| const results = await session.run({ | |
| float_input: tensor | |
| }); | |
| const prediction = results.label.data[0]; | |
| const flowers = [ | |
| "Setosa", | |
| "Versicolor", | |
| "Virginica" | |
| ]; | |
| document.getElementById("result").innerText = | |
| "Prediction: " + flowers[prediction]; | |
| } | |
| loadModel(); | |