-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
62 lines (53 loc) · 2.14 KB
/
main.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
#
from flask import Flask, request, send_from_directory
import json
import regressiontree
import requests
from flask_cors import CORS
import os
FLASK_APP = os.environ.get("FLASK_APP", "main.py")
FLASK_ENV = os.environ.get("FLASK_ENV", "False")
USE_SAMPLE_DATA = os.environ.get("USE_SAMPLE_DATA", "False")
app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}})
#How much time will it take to the client to get his service?
@app.route('/api/estimatedtime', methods=['POST'])
def get_estimated_time():
delay = regressiontree.regressiontree(None, request.json, loadTrained = True)
return delay
@app.route('/api/estimatedtime/noTrain', methods=['POST'])
def get_estimated_time_noTrain():
if USE_SAMPLE_DATA == "True":
with open('sampleData.json', 'r') as file:
historical = json.load(file)
else:
response = requests.get('http://localhost:4321/api/serviceConsumptions/serviceConsumptions')
historical = response.json()
delay = regressiontree.regressiontree(historical, request.json, loadTrained = False)
return delay
#Train my model
@app.route('/api/estimatedtime/train', methods=['POST'])
def train_model():
# Is the app in development mode?
if FLASK_ENV == "development":
if USE_SAMPLE_DATA == "True":
with open('sampleData.json', 'r') as file:
historical = json.load(file)
else:
response = requests.get('http://localhost:4321/api/serviceConsumptions/serviceConsumptions')
historical = response.json()
delay = regressiontree.regressiontree(historical, request.json, loadTrained=False, test=True)
return delay
else:
return "Not in development mode."
#say hi
@app.route('/')
def home():
return 'Bienvenido a mi aplicación Flask!'
@app.route('/favicon.ico')
def favicon():
return send_from_directory('public/icon', 'photo_1.png', mimetype='image/png')
# CORS(app)
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000)) # Usa el puerto definido por Render o 5000 por defecto
app.run(host='0.0.0.0', port=port, debug=False) # Asegúrate de desactivar el modo debug en producción