2

I am trying to add offline support to an AI-based app I am building on React Native, The app is similar to Not Hotdog of Silicon Valley. I recently found out Tenserflow.js and tried to implement it with React native and even outputted my python model to the one that is supported by Tenserflow.js but when I try to import Tenserflow.js inside one of the React Native components using import * as tf from '@tensorflow/tfjs'; this syntax but React Native throws the error, the error screenshot is attached with the question.

Screenshot of the error message from React Native

Thank You !!!

rabingaire
  • 179
  • 5
  • 20

2 Answers2

11

The short answer is no as stated in the description of the project.

A WebGL accelerated, browser based JavaScript library

The error you are getting shows that it's trying to use window which is part of the DOM API in browsers. React Native has no such API as it's not a browser. You can see that they use window in multiple places in their Core API.

The long answer is a maybe but is out of scope for a StackOverflow answer as it would be akin to asking for a tutorial. To elaborate, I can see two approaches to possibly getting TensorFlow.js to work:

  1. Polyfill everything that is missing:
    • This might be possible but it probably won't work as you'd expect because it leverages WebGL. I'm not familiar with TensorFlow.js, but I would believe that WebGL is used because it would allow access to GPU acceleration making the workload feasible in the browser. You would need to do something similar in order for it to work well on a mobile device.
  2. Leverage a WebView. However, you may again run into WebGL/performance issues as indicated in older questions such as this.

Since you didn't post any code whatsoever, there is no other guidance that I can give you on this question. One suggestion would be to look at maybe a native module such as react-native-tensorflow and the discussions people had around it.

Michael Cheng
  • 9,644
  • 6
  • 46
  • 48
2

@tensorflow/tfjs-react-native has been available on npm (in alpha) since late August 2019.

Brent Barbata
  • 3,631
  • 3
  • 24
  • 23