-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlee.py
120 lines (93 loc) · 3.84 KB
/
lee.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#import flask
from flask import request
import os
import json
import ast
import pulp
from flask_cors import CORS
from pulp import LpVariable,LpProblem,LpStatus,LpMaximize,LpMinimize, value, lpSum
import ast
import requests
from flask import Flask, render_template, request, redirect, url_for,jsonify
app = Flask(__name__)
app.secret_key='N@twestkey1_2907!'
cors = CORS(app)
authenticated = False
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
@app.after_request
def add_header(r):
"""
Add headers to both force latest IE rendering engine or Chrome Frame,
and also to cache the rendered page for 10 minutes.
"""
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
r.headers['Cache-Control'] = 'public, max-age=0'
return r
@app.route('/')
def home():
#return render_template('index.html', svc='http://127.0.0.1:1000/')
return render_template('index.html')
@app.route('/optimize', methods=['GET', 'POST'])
def optimize():
# define the limits of a reasonable adjustment
no_adjustment = 0
hardship = .15
monthly_income = float(request.args['income'])
spending_categories = ast.literal_eval(request.args['categories'])
spending = ast.literal_eval(request.args['spending'])
spending_minimum = ast.literal_eval(request.args['min'])
# create adjustment factor variables for each spending category
adjustment_factor_variable = {}
for category in spending_categories:
variable = LpVariable(category, no_adjustment, hardship)
adjustment_factor_variable.update({category: variable})
# define the linear optimization problem
monthly_adjustments = LpProblem("total_adjustments", LpMinimize)
# the objective is to minimize the total adjustments needed
factors = []
for category in spending_categories:
factors.append(adjustment_factor_variable[category])
monthly_adjustments += lpSum(factors)
# we don't have the option to sell our possessions (negative adjustment factor)
for category in spending_categories:
monthly_adjustments += adjustment_factor_variable[category] >= 0
# the adjustments cannot take us below a minimum standard of living
for category in spending_categories:
monthly_adjustments += spending[category] * (1 - adjustment_factor_variable[category]) >= spending_minimum[
category]
# we must live within our means
linear_factors = []
for category in spending_categories:
linear_factors.append(spending[category] * (1 - adjustment_factor_variable[category]))
monthly_adjustments += lpSum(linear_factors) <= monthly_income
# find budget adjustments that work
status = monthly_adjustments.solve()
successfully_optimized = status > 0
# Return values of the variables
new_budget = {}
if successfully_optimized:
for category in spending_categories:
adjustment = value(adjustment_factor_variable[category])
new_budget[category] = spending[category] * (1 - adjustment)
else:
new_budget = {}
return jsonify(new_budget)
@app.route('/cma9_connect', methods=['GET', 'POST'])
def cma9_connect():
# Authorize a CMA-9 connecton
params = {
'app': 'demo-team-5cf68683-1d9a-45aa-a24f-d61a26cd60d6',
}
resp = request.get('https://api.sandbox.ulsterbank.ie/.well-known/openid-configuration', params=params)
if resp.status_code != 200:
# Something went wrong with the connection
return 'GET /tasks/ {}'.format(resp.status_code)
# Pull transaction data and update the AI
for customer_transaction in resp.json():
optimize('{} {}'.format(customer_transaction['id'], customer_transaction['summary']))
if __name__ == '__main__':
port = int(os.getenv('PORT'))
host = str(os.getenv('HOST'))
app.run(port=port, host=host)