-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
100 lines (76 loc) · 2.94 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
import base64, secrets, io, os
from PIL import Image, ImageOps
from urllib.request import urlopen
from flask import Flask, request, jsonify, render_template
import numpy as np
import psycopg2
import psycopg2.extras
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.preprocessing import image
from tensorflow.keras.models import load_model
#model = load_model("./model/model.h5")
app = Flask(__name__)
#Enter here your database informations
DB_HOST = "ec2-18-235-86-66.compute-1.amazonaws.com"
DB_NAME = "da1c06fi82ev6c"
DB_USER = "tjhqznxnlxlreh"
DB_PASS = "f21ee19a9e50d41255f16288c978201fbbffd3fc9e22b644fb03b904f5216056"
conn = psycopg2.connect(dbname=DB_NAME, user=DB_USER, password=DB_PASS, host=DB_HOST)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/fetchrecords",methods=["POST","GET"])
def fetchrecords():
cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
if request.method == 'POST':
try:
search_word = request.form['query']
print(search_word)
if search_word == '':
query = "SELECT title, authors, published_year from books_eng"
cur.execute(query)
titles = cur.fetchall()
else:
cur.execute('SELECT title, authors, published_year from books_eng WHERE LOWER(title) LIKE LOWER(%(name)s) LIMIT 25', {'name': '%{}%'.format(search_word)})
numrows = int(cur.rowcount)
titles = cur.fetchall()
print(numrows)
except:
pass
return jsonify({'htmlresponse': render_template('response.html', titles=titles, numrows=numrows)})
'''@app.route("/api", methods=["POST"])
def main():
miJSON = request.json
try:
imagen64 = miJSON["imagen"]
imgdata = urlopen(imagen64)
imgdata = imgdata.read()
imgdata = Image.open(io.BytesIO(imgdata))
password_length = 13
extension = "png"
nomre_unico = f'{secrets.token_urlsafe(password_length)}.{extension}'
imgdata = imgdata.resize((28, 28))
imgdata.save(nomre_unico)
im = image.load_img(nomre_unico, color_mode='grayscale', target_size=(28, 28))
os.remove(nomre_unico)
etiqueta = predecir_im(im, invertir=False)
respuesta = {"etiqueta": etiqueta}
return jsonify(respuesta)
except:
return {"error": "Tuvimos un problema"}'''
if __name__ == "__main__":
app.run()
'''def predecir_im(im, invertir=True):
image = img_to_array(im)
image.shape
# Scale the image pixels by 255 (or use a scaler from sklearn here)
image /= 255
# Flatten into a 1x28*28 array
img = image.flatten().reshape(-1, 28 * 28)
img.shape
if invertir:
img = 1 - img
# plt.imshow(img.reshape(28, 28), cmap=plt.cm.Greys)
resultado = model.predict(img)
resultado = np.argmax(resultado, axis=-1)
return int(resultado[0])'''