CoolFace
Apppublic

BluechipTechnologiesAsia/Warehouse-Management

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py218 linesDownload Raw Back to root
1from  flask import Flask ,render_template,url_for,redirect,request,flash2import numpy as np3import pyrebase4import joblib5import sys6 7import datetime8import json9import requests10 11config ={12    "apiKey": "AIzaSyAy_-B05G2AYFrOi0FUQAMHrbew7zThnc8",13    "authDomain": "food-chain1.firebaseapp.com",14    "databaseURL": "https://food-chain1-default-rtdb.firebaseio.com",15    "projectId": "food-chain1",16    "storageBucket": "food-chain1.appspot.com",17    "messagingSenderId": "465892674183",18    "appId": "1:465892674183:web:9340d2c92f2b8d50f08622",19    "measurementId": "G-HSSJE3JW49"20}21 22firebase= pyrebase.initialize_app(config)23 24db=firebase.database()25##db.child("message to r from wh").push({"msg":"have u recived??"})26##for x in ok:27##    print(ok[x]["msg"])28 29# Alternate node which hosts the blockchain server.30# Multiple nodes possible31CONNECTED_NODE_ADDRESS = "http://127.0.0.1:8000"32 33posts = []34 35app = Flask(__name__)36 37@app.route('/')38def home():39    return render_template('landing.html')40 41@app.route("/login",methods=['POST'])42def login():43    data=request.form44    if(data['username'].upper() == 'SUPPLIER' and data['password'] == '12345'):45        return redirect(url_for('Supplier'))46    elif(data['username'].upper() == 'RESTAURANT' and data['password'] == '12345'):47        return redirect(url_for('Restaurant'))48    elif(data['username'].upper() == 'WAREHOUSE' and data['password'] == '12345'):49        return redirect(url_for('Warehouse'))50    else:51        return redirect(url_for('home'))52 53@app.route("/Supplier")54def Supplier():55    ok1=db.child("message to wh from s").get()56    ok1=ok1.val()57    ok2=db.child("message to s from wh").get()58    ok2=ok2.val()59    return render_template('Supplier.html',info1=ok1, info2=ok2)60 61 62@app.route("/Warehouse")63def Warehouse():64    ok1=db.child("message to s from wh").get()65    ok1=ok1.val()66    ok2=db.child("message to r from wh").get()67    ok2=ok2.val()68    ok3=db.child("message to wh from r").get()69    ok3=ok3.val()70    ok4=db.child("message to wh from s").get()71    ok4=ok4.val()72    return render_template('Warehouse.html', info1=ok1, info2=ok2, info3=ok3, info4=ok4)73 74 75@app.route("/Restaurant")76def Restaurant():77    ok1=db.child("message to wh from r").get()78    ok1=ok1.val()79    ok2=db.child("message to r from wh").get()80    ok2=ok2.val()81    return render_template('Restaurant.html',info1=ok1, info2=ok2)82 83 84##message to warehouse from supplier85@app.route("/msgtowhfs",methods=['POST'])86def msgtowhfs():87    data=request.form88    db.child("message to wh from s").push({"msg":data["mtwfs"]})89    return redirect(url_for('Supplier'))90 91 92##message to supplier from warehouse93@app.route("/msgtosfwh",methods=['POST'])94def msgtosfwh():95    data=request.form96    db.child("message to s from wh").push({"msg":data["mtsfw"]})97    return redirect(url_for('Warehouse'))98 99 100##message to restaurant from warehouse101@app.route("/msgtorfwh",methods=['POST'])102def msgtorfwh():103    data=request.form104    db.child("message to r from wh").push({"msg":data["mtrfw"]})105    return redirect(url_for('Warehouse'))106 107 108##message to warehouse fron restaurant109@app.route("/msgtowhfr",methods=['POST'])110def msgtowhfr():111    data=request.form112    db.child("message to wh from r").push({"msg":data["mtwfr"]})113    return redirect(url_for('Restaurant'))114 115 116 117 118##Warehouse model form and display page119 120@app.route("/pp")121def wpp():122    return render_template('pp.html')123 124 125@app.route("/predict",methods=['POST'])126def predict():127 128 129 130    direct_input=[x for x in request.form.values()]131 132    model1 = joblib.load(open("saved models/"+direct_input[0]+'_'+direct_input[1]+'.sav', 'rb'))133    model2 = joblib.load(open("saved models/"+direct_input[0]+'_'+direct_input[1]+'2.sav', 'rb'))134 135 136 137    flt_features = [float(x) for x in request.form.values()]138 139    flt_features[3]/=100140    flt_features[2]/=100141 142    flt_features.append(flt_features[3]-flt_features[2])143    final_features1 = [np.array(flt_features[2:])]144    print(flt_features)145    print(final_features1)146 147    prediction1 = model1.predict(final_features1)148    output1 = round(prediction1[0])149    print(output1)150 151    flt_features.append(output1)152    final_features2 = [np.array(flt_features[2:])]153    prediction2 = model2.predict(final_features2)154    output2 = round(prediction2[0])155    print(output2)156    return render_template('pp.html',prediction_text=output2)157 158 159#blockchain routes+ API160 161def fetch_posts():162    """163    Function to fetch the chain from a blockchain node, parse the164    data and store it locally.165    """166    get_chain_address = "{}/chain".format(CONNECTED_NODE_ADDRESS)167    response = requests.get(get_chain_address)168    if response.status_code == 200:169        content = []170        chain = json.loads(response.content)171        for block in chain["chain"]:172            for tx in block["transactions"]:173                tx["index"] = block["index"]174                tx["hash"] = block["previous_hash"]175                content.append(tx)176 177        global posts178        posts = sorted(content, key=lambda k: k['timestamp'],179                       reverse=True)180 181@app.route('/find_my_food')182def index():183    fetch_posts()184    return render_template('index.html',185                           title='Find My Food ',186                           posts=posts,187                           node_address=CONNECTED_NODE_ADDRESS,188                           readable_time=timestamp_to_string)189 190 191@app.route('/submit', methods=['POST'])192def submit_textarea():193    """194    Endpoint to create a new transaction via our application.195    """196    post_content = request.form["content"]197    author = request.form["author"]198 199    post_object = {200        'author': author,201        'content': post_content,202    }203 204    # Submit a transaction205    new_tx_address = "{}/new_transaction".format(CONNECTED_NODE_ADDRESS)206 207    requests.post(new_tx_address,208                  json=post_object,209                  headers={'Content-type': 'application/json'})210 211    return redirect('/find_my_food')212 213 214def timestamp_to_string(epoch_time):215    return datetime.datetime.fromtimestamp(epoch_time).strftime('%H:%M')216 217if __name__=="__main__":218    app.run(port=5000)