forked from waleed318/mlops-a2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
98 lines (76 loc) · 2.71 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from sklearn.linear_model import LogisticRegression
import pickle
from flask import Flask, request, jsonify,render_template
import requests
import json
import random
# # Load the dataset
# data = pd.read_csv("swarming_dataset.csv")
# # Split the data into features and labels
# X = data.drop("Swarming", axis=1)
# y = data["Swarming"]
# # Train a logistic regression model
# model = LogisticRegression()
# model.fit(X, y)
# # Save the model to a pickle file
# with open("model.pkl", "wb") as f:
# pickle.dump(model, f)
# Create a Flask application
app = Flask(__name__)
temps = []
humids = []
sounds = []
preds = []
# Define a route for the live dashboard mod
@app.route("/")
def dashboard():
# Load live data and predict swarming using the trained model
# Display different metrics of the model on the dashboard
host = "159.203.147.149:8080"
#host = "localhost:8080"
# set the API endpoint
endpoint = "/api/data"
# send a GET request to the endpoint
response = requests.get(f"http://{host}{endpoint}", stream=True)
count=0
# iterate over the response stream
for line in response.iter_lines():
if line:
try:
# process the received data
d=line.decode('utf-8').split('data:')[1]
data=json.loads(d)
instance=[data['Temperature'],data['Humidity'],data['Sound']]
temp = data['Temperature']
humid = data['Humidity']
sound = data['Sound']
# add live data to the respective lists
temps.append(temp)
humids.append(humid)
sounds.append(sound)
# Load the trained model from the pickle file
with open("model.pkl", "rb") as f:
model = pickle.load(f)
prediction = model.predict([instance])[0]
preds.append(prediction)
count+=1
except:
continue
if count==10:
break
data=zip(temps, humids,sounds,preds)
return render_template('dashboard.html', data=zip(temps,humids,sounds,preds))
# Define a route for the service mode
@app.route("/service", methods=["POST"])
def service():
# Get an instance of live data from the user
instance = request.json['data']
# Load the trained model from the pickle file
with open("model.pkl", "rb") as f:
model = pickle.load(f)
# Predict swarming for the instance of live data using the trained model
prediction = model.predict([instance])
# Return the prediction to the user as JSON
return jsonify({"prediction": prediction.tolist()})
if __name__ == "__main__":
app.run('0.0.0.0',debug=True)