DEV Community

Cover image for Fixed Machine Learning Tutorials Code
Mahboob Hussain
Mahboob Hussain

Posted on

Fixed Machine Learning Tutorials Code

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 Create a complete Machine Learning web application using React and Flask 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:

Terminal 1

$ cd ui
$ yarn install
$ sudo npm install -g serve
$ npm run build
$ serve -s build -l 3000
Enter fullscreen mode Exit fullscreen mode

Terminal 2

$ cd service
$ FLASK_APP=app.py flask run
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 Content-Based Recommendation System, 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 Build a React + Flask App that Suggests Novel Novels 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

Terminal 1

The following steps are required only once after you have checked out the code.

$ npx create-react-app gwr
Enter fullscreen mode Exit fullscreen mode

Copy all the files from the ReactApp folder and paste them in gwr folder

Terminal 2

$ export FLASK_APP=api
$ flask run
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)