Fixed Machine Learning Tutorials Code
Tags: machinelearning, python, tutorial
Published: 2021-02-13 04:26:17

For the machine learning application I am developing for writing a technical article, I checked out a few tutorials and their code. Unfortunately, as one sees often, the code doesn't run as-is. Many tutorial writers don't bother to update their code base and one has to spend time fixing the errors. I am sharing three such examples here along with the corrected fork repository on github.

Karan Bhanot in his [article](https://towardsdatascience.com/create-a-complete-machine-learning-web-application-using-react-and-flask-859340bddb33) <i>Create a complete Machine Learning web application using React and Flask</i> describes a template of serving machine learning functionality over a Flask web application with a React front end.

The use case described is iris flowers prediction. To run it, clone the repository and open two terminals. In both of them, cd into example/iris-data-classifier/ML-React-App-Template and run the commands as follows:

<u>Terminal 1</u>
```
$ cd ui
$ yarn install
$ sudo npm install -g serve
$ npm run build
$ serve -s build -l 3000
```
<u>Terminal 2</u>
```
$ cd service
$ FLASK_APP=app.py flask run
```
The run command throws an error:

> from werkzeug import cached_property
> Import_Error : cannot import name ‘cached_property’

To fix this error, three changes are required.

1) requirements.txt
```Change flask-restplus==0.12.1 to flask-restx==0.2.0```
2) Then run the command
```
$ pip3 install -r requirements.txt
```
3) app.py

In the line: `from flask_restplus import Api, Resource, fields`

change

`flask_restplus` to `flask_restx`

After executing the command ‘npm run build’ in Terminal 1 and starting the servers in both the terminals, you can access the application at http://localhost:3000

I have forked the repository and made the changes, you can check out and run the application without errors. It’s available at: https://github.com/mh-github/ML-React-App-Template

---
The next example is Dhilip Subramanian's [tutorial](https://medium.com/towards-artificial-intelligence/content-based-recommendation-system-using-word-embeddings-c1c15de1ef95) <i>Content-Based Recommendation System</i>, which he explains is “a content-based recommendation system recommends books to a user by taking similarity of books based on the description. It also considers the user’s previous book history in order to recommend a similar book. Cosine similarity is used in our recommender system to recommend the books.”

The code is given in a Python notebook. Its errors and fixes are given below:

Cell [2] :
> df = pd.read_csv("test.csv")

I replaced `test.csv` with `data.csv`

`df = pd.read_csv("data.csv")`

Cell [9] :
> google_model.build_vocab(line_of_sentance)

I replaced `line_of_sentance` with `corpus` 

`google_model.build_vocab(corpus)`

Cell [9] : 
> %%time
> SyntaxError: invalid syntax

I commented it.

Cell [13] : 
> cosine_similarities = cosine_similarity(array_embeddings, array_embeddings)

I replaced `array_embeddings` with `word_embeddings`.

```cosine_similarities = cosine_similarity(word_embeddings, word_embeddings)```

I forked the repository and made the changes. It is available at: https://github.com/mh-github/Content-Based-Recommendation---Good-Reads-data

---
The last example is Jay Franck's [article](https://towardsdatascience.com/build-a-react-flask-app-that-suggests-novel-novels-with-a-python-graph-9491e714bbdf) <i>Build a React + Flask App that Suggests Novel Novels</i> with a Python Graph in which he describes a recommendation application based on GoodReads data using a Python graph. The summary is:

* Build a Graph database of Users and the Books they read
* Develop a Flask App that serves up rare, interesting Books to Users based on their submitted favorites
* Implement a React App that integrates with Flask + our Graph to showcase to users their next favorite book

How to run

<u>Terminal 1</u>

The following steps are required only once after you have checked out the code.
```
$ npx create-react-app gwr
```
Copy all the files from the ReactApp folder and paste them in gwr folder

<u>Terminal 2</u>
```
$ export FLASK_APP=api
$ flask run
```
Let the flask app complete building the graph and be ready to serve web requests. That is, wait until you see the following line at the prompt:
`* Running on htpp://127.0.0.1:5000/ (Press CTRL+C to quit)`

Then, go to Terminal 1 and run the following commands:
```
$ cd gwr
$ npm start
```

The errors that I got are :

> 1) ./src/index.js
> Module not found: Can’t resolve ‘semantic-ui-css/semantic.min.css’

Solution:
```
$ yarn add semantic-ui-css
```
> 2) Error: [BABEL] /mnt/e/Code/Python/Flask
> Applications/GoodReadsGraph/gwr/src/index.js: Cannot find module ‘@babel/helper-builder-react-jsx’

Solution:
```
$ yarn add @babel/helper-builder-react-jsx
```
> 3) return jsonify({“image_url”: ouput_URL}), 200
NameError: name ‘ouput_URL’ is not defined

The solution is to replace `ouput_URL` with `output_URL` in the file `api/app.py`. Make the same change in lines 46 and 47.

For some reason, the book recommendation does not appear unless you refresh the page. Enter ‘Neuromancer’ in the input text field and click Add. In the text field, you will see ‘We found your favorite book!’. But nothing else is displayed on the screen. Then, refresh the webpage (F5 or Ctrl-R) and the recommended book will appear below the Add button.

The repository also has a Python notebook, Graph_e2e.ipynb with the full application code and it also has errors. The path to the ratings.csv and books.csv had to be corrected by prepending with api/ as follows:

```uir = pd.read_csv("api/data/goodbooks-10k-master/ratings.csv")```
```books = pd.read_csv("api/data/goodbooks-10k-master/books.csv")```

I have forked the repository and made the changes, you can check out and run the application without errors. It’s available at: https://github.com/mh-github/GoodReadsGraph

###### Originally posted on [LinkedIn](https://www.linkedin.com/pulse/fixed-machine-learning-tutorials-code-mahboob-hussain/)