How can we deploy a Machine Learning Model using Flask? - python

I am trying, for the first time ever, to deploy a ML model, using Flask. I'm following the instructions from the link below.
https://towardsdatascience.com/deploy-a-machine-learning-model-using-flask-da580f84e60c
I created three separate and distinct .py files named 'model.py', 'server.py', and 'request.py'. I open my Anaconda Prompt end entered this: 'C:\Users\ryans>C:\Users\ryans\model.py'
Now, I get this.
I definitely have Numpy installed! Something must be wrong with my setup, or maybe the way I am starting the process is wrong, but I'm not sure what the issue is. Has anyone encountered this problem before.

I would suggest the following steps since you mentioned its your first time and when deploying a project for experimenting, it's good practice to put it in a virtual environment, which we can do with the virtualenv tool.
Assuming you already have pip installed, install virtualenv:
C:\> pip install virtualenv
Create a fresh working directory, switch to it and clone the repository from Github mentioned by the author of the article:
C:\your-working-directory\> git clone https://github.com/vyashemang/flask-salary-predictor.git
Start virtualenv in the repository's top directory
C:\your-working-directory\flask-salary-predictor\> virtualenv env
Activate the created batch file, \env\Scripts\activate.bat:
C:\Users\'Username'\venv\Scripts\activate.bat
Now in the virtual environment, install all requirements:
(env) C:\your-working-directory\flask-salary-predictor\> pip install -r requirements.txt
You can now run the model.py like you have above. I'd suggest that you run the server.py using gunicorn or on Windows use waitress according to this post so that instead of running request.py every time you want to send a request, you can use Insomnia or Postman to send API requests.

Related

Can users download Python code and run it with no setup?

I was curious about the process of sending someone a simple Python script, with some imported modules and how that work from the receiving end.
I guess my overall question is the following. Does the receiver of the script have to do anything at all or can they just run that file and get the intended result? Do they have to install Python, the modules used in the script, etc?
Thanks for any answers, I am sure there are plenty of “well it depends..” examples, which are fine. I am still learning so any answer is great.
If you are sending it to someone who has everything needed to develop with Python, what you need to do is work on a virtual environment:
Virtual environments (venv), in a nutshell, are python's way to handle the versioning of packages and ensuring that if someone else tries to run your script they can replicate your dependencies. To start, run
python -m venv your_venv_name
#if on linux:
source your_venv_name/bin/activate
#if on windows:
./your_venv_name/Scripts/activate
Then you will have a fresh version of the python version you were using with no dependencies installed, so you can then start installing with pip.
After you install everything run
pip freeze > requirements.txt
Now you can share your project, and the other devs just have to create their own venv and run
pip install -r requirements.txt
If on the other hand you are sending the script to someone who doesn't have python installed on their machine you will have to generate and executable file: https://realpython.com/pyinstaller-python/

PackagesNotFoundError: The following packages are not available from current channels: - manage

This is my first question here. I'm new to Python, Django and Anaconda. I am trying to follow this tutorial but I keep running into hiccups. I found a similar answer to my question and I'm willing to admit that I could be misunderstanding something due to my lack of experience. I am using PyCharm and I'm installing the necessary packages (as needed for the tutorial) via the gui and I'm using the terminal inside PyCharm.
At this part of the tutorial where you're supposed to run the server of the project, I keep getting this error:
PackagesNotFoundError: The following packages are not available from current channels: - manage
Whenever I go to install from conda forge or pip install in the terminal, I am met with the same errors.
Like I said, I found a similar post on here, but it is not the solution I need. I'm also not exactly sure what I'm doing wrong. I've installed and updated python and django, and everything requested in the tutorial.
PackagesNotFoundError: The following packages are not available from current channels:
Thank you in advance for anyone who helps me.
Hey next time when you post a question add code in which you running and the whole error output in order to get the best help possible.
I am assuming running the Django application is the problem based on the information you've provided.
Try this open your terminal/command prompt and execute the following commands:
Unix
creating directory for the project
mkdir <some_directory_name>
cd <some_directory_name>
Creating python virtual env and activating it for your project.
python3 -m venv <some_env_name_of_choice>
source <some_env_name_of_choice>/bin/activate
Installing Django and creating a project.
pip3 install django
django-admin startproject <project_name_of_choice>
run Django app
cd <project_name_of_choice>
python manage.py runserver
Go to your browser and type this to verify that the app is running
http://127.0.0.1:8000
Windows
change into <some_directory_of_choice>
cd <some_directory_of_choice>
create virtual environment
python3 -m venv ./venv
activate the virtual env
source venv/bin/activate
upgrade or install pip if need be
python -m pip install --upgrade pip
run these 2 commands
python manage.py makemigrations
python manage.py migrate
run a server
python manage.py runserver
Go to your browser and type http://127.0.0.1:8000/
You might want to use python3 if running the command python gives an error. Don't forget to remove these <>
Check the official Django's site for examples on writing your first program with Django app. familiarise yourself with the basics of Django syntax etc.

Contributing to Scrapy - What is the correct way to run from source?

I would like to contribute to the project bu making some patches. However, I'm a little stuck with how to work on the github repo.
I selected a bug on the issue tracker, did some research, and came up with a possible fix. I made some modifications to my fork of the source. However, I have become stuck when trying to run scrapy from source.
I've read the documentation for the Install process, the Contributor guide and the FAQ. I cannot find any information on how to run the project from source code. (Only how to install from official distribution packages, which is not what I want.)
Do you have to create a virtual environment to run this in?
Can you provide simple steps on how you normally run the project from the github repo?
Thanks for your time,
D.
[Edit]
Thanks to folks on the IRC server I was able to install the repo version using this command:
pip install -e <path/to/scrapy/root/dir>
That answers my question. It was highly recommended to setup and activate a virtual environment to work from, which is described very well in the accepted answer below.
Thanks everyone!
To run the github repo you will need to clone the repo from remote to local using git clone, to get it started the step would be something like this in terminal or git bash
cd <base_folder> #folder where repo will be downloaded
git clone <repo_url> #this will clone remote repo to local
cd <repo_folder> #make repo folder current directory
After that you can work on the project, but to push/merge your local repo to remote you will need the permission from the owner
Also it is recomended to create separate virtual env for each project, it will be something like this
python3 -m venv <name_of_venv> #Creating venv
<name_of_venv>/bin/activate #activating venv in linux
<name_of_venv>/Scripts/activate #activating venv in windows

Python Click application with setuptools integration runs into a python OS error when I try to reproduce the project's venv on other machines

I'm trying to allow other computers to also view a simple CLI application that I've put in a virtual environment. After searching for a while, I pip froze and generated a requirements.txt. When I attempt to install dependencies on a remote computer via pip install --editable. The terminal outputs a Python OS error.
The project is available at https://github.com/JonW27/calc
Screenshots of the error are provided:
I have a strong feeling that I've made a beginner's mistake/ did something dumb. If the post needs clarification then please specify, I honestly don't know what's wrong with it– I made the venv relocatable and ran into no errors.
Based on the error message, it appears you are attempting to install the package to the python's system-wide location. Try re-creating your virtualenv.

How to export virtualenv?

I'm new to virtualenv but I'm writting django app and finally I will have to deploy it somehow.
So lets assume I have my app working on my local virtualenv where I installed all the required libraries. What I want to do now, is to run some kind of script, that will take my virtualenv, check what's installed inside and produce a script that will install all these libraries on fresh virtualenv on other machine. How this can be done? Please help.
You don't copy paste your virtualenv. You export the list of all the packages installed like -
pip freeze > requirements.txt
Then push the requirements.txt file to anywhere you want to deploy the code, and then just do what you did on dev machine -
$ virtualenv <env_name>
$ source <env_name>/bin/activate
(<env_name>)$ pip install -r path/to/requirements.txt
And there you have all your packages installed with the exact version.
You can also look into Fabric to automate this task, with a function like this -
def pip_install():
with cd(env.path):
with prefix('source venv/bin/activate'):
run('pip install -r requirements.txt')
You can install virtualenvwrapper and try cpvirtualenv, but the developers advise caution here:
Warning
Copying virtual environments is not well supported. Each virtualenv
has path information hard-coded into it, and there may be cases where
the copy code does not know it needs to update a particular file. Use
with caution.
If it is going to be on the same path you can tar it and extract it on another machine. If all the same dependencies, libraries etc are available on the target machine it will work.

Categories

Resources