-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
218 lines (164 loc) · 5.96 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
from flask import Flask ,render_template,url_for,redirect,request,flash
import numpy as np
import pyrebase
import joblib
import sys
import datetime
import json
import requests
config ={
"apiKey": "AIzaSyAy_-B05G2AYFrOi0FUQAMHrbew7zThnc8",
"authDomain": "food-chain1.firebaseapp.com",
"databaseURL": "https://food-chain1-default-rtdb.firebaseio.com",
"projectId": "food-chain1",
"storageBucket": "food-chain1.appspot.com",
"messagingSenderId": "465892674183",
"appId": "1:465892674183:web:9340d2c92f2b8d50f08622",
"measurementId": "G-HSSJE3JW49"
}
firebase= pyrebase.initialize_app(config)
db=firebase.database()
##db.child("message to r from wh").push({"msg":"have u recived??"})
##for x in ok:
## print(ok[x]["msg"])
# Alternate node which hosts the blockchain server.
# Multiple nodes possible
CONNECTED_NODE_ADDRESS = "http://127.0.0.1:8000"
posts = []
app = Flask(__name__)
@app.route('/')
def home():
return render_template('landing.html')
@app.route("/login",methods=['POST'])
def login():
data=request.form
if(data['username'].upper() == 'SUPPLIER' and data['password'] == '12345'):
return redirect(url_for('Supplier'))
elif(data['username'].upper() == 'RESTAURANT' and data['password'] == '12345'):
return redirect(url_for('Restaurant'))
elif(data['username'].upper() == 'WAREHOUSE' and data['password'] == '12345'):
return redirect(url_for('Warehouse'))
else:
return redirect(url_for('home'))
@app.route("/Supplier")
def Supplier():
ok1=db.child("message to wh from s").get()
ok1=ok1.val()
ok2=db.child("message to s from wh").get()
ok2=ok2.val()
return render_template('Supplier.html',info1=ok1, info2=ok2)
@app.route("/Warehouse")
def Warehouse():
ok1=db.child("message to s from wh").get()
ok1=ok1.val()
ok2=db.child("message to r from wh").get()
ok2=ok2.val()
ok3=db.child("message to wh from r").get()
ok3=ok3.val()
ok4=db.child("message to wh from s").get()
ok4=ok4.val()
return render_template('Warehouse.html', info1=ok1, info2=ok2, info3=ok3, info4=ok4)
@app.route("/Restaurant")
def Restaurant():
ok1=db.child("message to wh from r").get()
ok1=ok1.val()
ok2=db.child("message to r from wh").get()
ok2=ok2.val()
return render_template('Restaurant.html',info1=ok1, info2=ok2)
##message to warehouse from supplier
@app.route("/msgtowhfs",methods=['POST'])
def msgtowhfs():
data=request.form
db.child("message to wh from s").push({"msg":data["mtwfs"]})
return redirect(url_for('Supplier'))
##message to supplier from warehouse
@app.route("/msgtosfwh",methods=['POST'])
def msgtosfwh():
data=request.form
db.child("message to s from wh").push({"msg":data["mtsfw"]})
return redirect(url_for('Warehouse'))
##message to restaurant from warehouse
@app.route("/msgtorfwh",methods=['POST'])
def msgtorfwh():
data=request.form
db.child("message to r from wh").push({"msg":data["mtrfw"]})
return redirect(url_for('Warehouse'))
##message to warehouse fron restaurant
@app.route("/msgtowhfr",methods=['POST'])
def msgtowhfr():
data=request.form
db.child("message to wh from r").push({"msg":data["mtwfr"]})
return redirect(url_for('Restaurant'))
##Warehouse model form and display page
@app.route("/pp")
def wpp():
return render_template('pp.html')
@app.route("/predict",methods=['POST'])
def predict():
direct_input=[x for x in request.form.values()]
model1 = joblib.load(open("saved models/"+direct_input[0]+'_'+direct_input[1]+'.sav', 'rb'))
model2 = joblib.load(open("saved models/"+direct_input[0]+'_'+direct_input[1]+'2.sav', 'rb'))
flt_features = [float(x) for x in request.form.values()]
flt_features[3]/=100
flt_features[2]/=100
flt_features.append(flt_features[3]-flt_features[2])
final_features1 = [np.array(flt_features[2:])]
print(flt_features)
print(final_features1)
prediction1 = model1.predict(final_features1)
output1 = round(prediction1[0])
print(output1)
flt_features.append(output1)
final_features2 = [np.array(flt_features[2:])]
prediction2 = model2.predict(final_features2)
output2 = round(prediction2[0])
print(output2)
return render_template('pp.html',prediction_text=output2)
#blockchain routes+ API
def fetch_posts():
"""
Function to fetch the chain from a blockchain node, parse the
data and store it locally.
"""
get_chain_address = "{}/chain".format(CONNECTED_NODE_ADDRESS)
response = requests.get(get_chain_address)
if response.status_code == 200:
content = []
chain = json.loads(response.content)
for block in chain["chain"]:
for tx in block["transactions"]:
tx["index"] = block["index"]
tx["hash"] = block["previous_hash"]
content.append(tx)
global posts
posts = sorted(content, key=lambda k: k['timestamp'],
reverse=True)
@app.route('/find_my_food')
def index():
fetch_posts()
return render_template('index.html',
title='Find My Food ',
posts=posts,
node_address=CONNECTED_NODE_ADDRESS,
readable_time=timestamp_to_string)
@app.route('/submit', methods=['POST'])
def submit_textarea():
"""
Endpoint to create a new transaction via our application.
"""
post_content = request.form["content"]
author = request.form["author"]
post_object = {
'author': author,
'content': post_content,
}
# Submit a transaction
new_tx_address = "{}/new_transaction".format(CONNECTED_NODE_ADDRESS)
requests.post(new_tx_address,
json=post_object,
headers={'Content-type': 'application/json'})
return redirect('/find_my_food')
def timestamp_to_string(epoch_time):
return datetime.datetime.fromtimestamp(epoch_time).strftime('%H:%M')
if __name__=="__main__":
app.run(port=5000)