-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
47 lines (33 loc) · 1.16 KB
/
app.py
File metadata and controls
47 lines (33 loc) · 1.16 KB
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
#!/usr/bin/python
import os
import requests
import numpy as np
from flask import Flask, request, jsonify
from flask_cors import CORS
import pickle
app = Flask(__name__)
CORS(app)
MODEL_FILEPATH = "model.pkl"
if not os.path.exists(MODEL_FILEPATH):
print("Downloading model file...")
url = "https://github.com/GreenGridIn/Backend/releases/download/model/model.pkl"
r = requests.get(url)
with open(MODEL_FILEPATH, "wb") as f:
f.write(r.content)
print("Model file downloaded successfully")
with open(MODEL_FILEPATH, "rb") as f:
model = pickle.load(f)
@app.route("/", methods=["GET"])
def default():
return jsonify({"message": "Welcome to the API"})
@app.route("/predict", methods=["POST"])
def predict():
req_data = ["air_temperature", "pressure", "wind_speed"]
if not request.json:
return jsonify({"error": "Request data not found"})
if not all(k in request.json for k in req_data):
return jsonify({"error": "Missing required fields"})
data = [request.json[k] for k in req_data]
data = np.array(data).reshape(1, -1)
prediction = model.predict(data)[0]
return jsonify({"power": prediction})