0

So currently I'm working on a web application that uses a model I built previously. I have the file stored locally in my github repository and I'm trying to retrieve that model and load it into a variable as so

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.0.0/dist/tf.min.js"></script>

<script>
const model = await tf.loadLayersModel('https://foo.bar/tfjs_artifacts/model.json');
</script>

The first script tag should load in the tensorflow.js library (https://www.tensorflow.org/js/tutorials/setup).

The error that appears in the terminal is

Uncaught SyntaxError: await is only valid in async function

Am I missing something with the tensorflow.js implementation? Or am I missing something with the await keyword in the model assignment?

edkeveked
  • 17,989
  • 10
  • 55
  • 93
  • Does this answer your question? [await is only valid in async function](https://stackoverflow.com/questions/49432579/await-is-only-valid-in-async-function) – edkeveked Nov 12 '20 at 14:47

2 Answers2

0

Your issue is that you are using the using the await keyword outside of an async function. Async functions are special functions that allow the word await, which makes it so the program waits for the function to finish running.

A solution to this would be

async function tensorFlow(){
    const model = await tf.loadLayersModel('https://foo.bar/tfjs_artifacts/model.json');
}

tensorFlow()

or you can use the .then() function.

tf.loadLayersModel('https://foo.bar/tfjs_artifacts/model.json').then(model => {
     
};
Max Marcus
  • 106
  • 1
  • 1
  • 4
  • So now my error is the following ``` platform_browser.js:28 Fetch API cannot load file:///C:/Users/firstName%20lastName/Your-Story/AI-Model/saved_model.pb. URL scheme must be "http" or "https" for CORS request. ``` How do I resolve this? –  Nov 12 '20 at 16:16
  • are you running on a localhost server or from a local file? – Max Marcus Nov 12 '20 at 18:36
  • If you are not running on localhost, it may solve your problem. [Here](https://stackoverflow.com/questions/32332485/run-a-file-from-http-localhost) is a link to another stack overflow page that has instructions. – Max Marcus Nov 12 '20 at 19:23
  • This is a local file from a repository –  Nov 12 '20 at 22:53
0

I use this

async function run(){
....
}

( async() =>{
  await run()
})()
lex mulya
  • 31
  • 3