Skip to content

Commit

Permalink
Merge pull request #26 from denBruneBarone/api
Browse files Browse the repository at this point in the history
FEATURE: Added an api for the model code. To be used with webapp.
  • Loading branch information
casparemiljensen authored Mar 26, 2024
2 parents 76d773f + cb538d8 commit 67ed59f
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions api/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from flask import Flask, jsonify, request
import json

app = Flask(__name__)


@app.route('/', methods=["GET"])
def index():
return jsonify('hello world!')


@app.route('/predict', methods=["POST"])
def predict():
print("Received POST request...")
print("Predicting power consumption for given route")

try:
post_data = request.get_data().decode('utf-8')
post_json = json.loads(post_data)

print(post_json)

power_consump_wh = 23.23
response_data = {
"wh": power_consump_wh
}
return jsonify(response_data), 200

except Exception as e:
return jsonify(error=f"Error occured: {str(e)}"), 422


@app.errorhandler(404)
def page_not_found(error):
message = "Invalid endpoint"
return jsonify(error=message), 404


if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)

0 comments on commit 67ed59f

Please sign in to comment.