Weather Prediction —2. Deploying a Machine Learning model locally using Flask

VARSHITHA GUDIMALLA
4 min readMay 2, 2021
image by Dave Hoefler on unsplash

Make a separate directory for the project and save the model in this directory. I named the directory as “flask”.

  1. Install the virtual environment
pip install virtualenvvirtualenv weather-prediction-venv

In the flask directory, you will a new file

2. Activating the virtual environment

weather-prediction-venv\Scripts\activate.bat

when we activate the virtual environment we will see the environment name on the left side.

4. Installing Flask

pip install flask

5. Create app.py file in the root folder(flask directory) and write the following code

#importing 
from flask import Flask
#create an instance of Flask
app = Flask(__name__)@app.route('/')
def home():
return "Hello World"if __name__ == '__main__':
app.run(debug=True)

6. Run the application

python app.py

server will run on http://127.0.0.1:5000/

Create a templates folder inside the root directory.

7. Create a home.html page in the templates folder and write the following code

<!doctype html>
<html>
<head>
<title> Weather Predictions </title>
</head>
<body>
<h1 style="text-align: center; font-family: Times New Roman; background-color:LightGray">Weather Predictions</h1>
<div style="padding:20px;">
<form action = "/predict/" method="post">
<div style="padding:10px; padding-left:325px">
<label for="Summary">Summary : </label>
<input type="text" id="Summary" name="Summary">
</div>
<div style="padding:10px;padding-left:325px">
<label for="Humidity">Humidity : </label>
<input type="text" id="Humidity" name="Humidity">
</div>
<div style="padding:10px;padding-left:325px">
<label for="WindSpeed">Wind Speed (km/h) : </label>
<input type="text" id="WindSpeed" name="WindSpeed">
</div>
<div style="padding:10px;padding-left:325px">
<label for="WindBearing">Wind Bearing (degrees) : </label>
<input type="text" id="WindBearing" name="WindBearing">
</div>
<div style="padding:10px;padding-left:325px">
<label for="Visibility">Visibility (km) : </label>
<input type="text" id="Visibility" name="Visibility">
</div>
<div style="padding:10px;padding-left:325px">
<label for="Pressure">Pressure (millibars) : </label>
<input type="text" id="Pressure" name="Pressure">
</div>
<div style="padding:10px; text-align: center">
<button type="submit" class="btn btn-primary">Predict</button>
</div>

</form>
</div>
</body>
</html>

8. Create predict.html file in the templates folder and write the following code

<!doctype html>
<html>
<head>
<title> Prediction </title>
</head>
<body>
<div align = "center" >
<h1>Temperature is <span style="color:red;">{{ prediction[0].round(3) }}</span></h1>
</div>
</body>
</html>

9. Replace app.py file with the following code

#import Flask 
from flask import Flask, render_template, request
import numpy as np
import pandas as pd
import joblib
#create an instance of Flask
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home.html')
@app.route('/predict/', methods = ['Get', 'POST'])
def predict():
if request.method == "POST":
#get form data
Summary = request.form.get('Summary')
Humidity = request.form.get('Humidity')
WindSpeed = request.form.get('WindSpeed')
WindBearing = request.form.get('WindBearing')
Visibility = request.form.get('Visibility')
Pressure = request.form.get('Pressure')
try:
prediction = preprocessDataAndPredict(Summary, Humidity, WindSpeed, WindBearing, Visibility, Pressure)
#pass prediction to template
return render_template('predict.html', prediction = prediction)
except ValueError:
return "Please Enter valid values"
pass
pass

def preprocessDataAndPredict(Summary, Humidity, WindSpeed, WindBearing, Visibility, Pressure):
#put all inputs in array
test_data = [[Summary, Humidity, WindSpeed, WindBearing, Visibility, Pressure]]
print(test_data)
#convert value data into numpy array
test_data = np.array(test_data)
#creating a dataframe
test_data = pd.DataFrame(test_data)
print(test_data)
#open file
file = open("model.pkl","rb")
#load trained model
trained_model = joblib.load(file)
#predict
prediction = trained_model.predict(test_data)
return prediction
pass

if __name__ == '__main__':
app.run(debug=True)

10. Installing the required packages

pip install numpy pandas sklearn

11. Run the application

python app.py

server running on http://127.0.01:5000/

Prediction:

--

--

VARSHITHA GUDIMALLA

Computer Science Engineering Graduate || Machine Learning enthusiast.