-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
159 lines (117 loc) · 4 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
from flask import Flask, render_template, request, redirect, url_for, jsonify
import flask_login
import os, json
from oauthlib.oauth2 import WebApplicationClient
import requests
import cv2
import numpy
from utilities import *
from dotenv import load_dotenv
from CustomFileStorage import *
from flask_cors import CORS, cross_origin
import xlrd, pandas as pd, numpy as np
load_dotenv()
app = Flask(__name__)
app.secret_key = "7y8gb87t76g878t6243rnd2wor8dj98"
CORS(app, supports_credentials=True)
# login_manager = flask_login.LoginManager()
# login_manager.init_app(app)
# ## Mock Database
# users = {
# "Shyam":{
# "id" : "Shyam",
# "email" : "[email protected]"
# }
# }
# class User(flask_login.UserMixin):
# pass
# ## ------------------------------ Login Handler ----------------------------
# @login_manager.user_loader
# def user_loader(name):
# if name not in users:
# return
# user = User()
# user.id = name
# return user
# @login_manager.request_loader
# def request_loader(request):
# name = request.form.get("name")
# if name not in users:
# return
# user = User()
# user.id = name
# user.is_authenticated = request.form["email"] == users[name]["email"]
# return user
# @app.route("/glogin")
# def google_login():
# google_provider_cfg = get_google_provider_cfg()
# authorization_endpoint = google_provider_cfg["authorization_endpoint"]
# request_uri = client.prepare_request_uri(
# authorization_endpoint,
# redirect_uri = request.base_url[:-7] + "/api/glogin/callback",
# scope=["openid", "email", "profile"],
# )
# return redirect(request_uri)
# @app.route("/api/glogin/callback")
# def callback():
# return handle_google_login_callback(request, User, users)
## ------------------------------------- Logout Handler -----------------------------------
# @app.route('/logout', methods=["GET","POST"])
# @flask_login.login_required
# def logout():
# flask_login.logout_user()
# return 'Logged out'
## ------------------------------------- Home/Dashboard -------------------------------------
# @app.route("/home")
# @app.route("/")
# @flask_login.login_required
# def protected():
# return render_template("index.html",user_id=flask_login.current_user.id)
## ------------------------------------- API - Characterizer -------------------------------------
@app.route("/api/characterizer101", methods=["POST"])
def characterizerV101():
req = request.get_json()
user_id = req["user_id"]
file_uri = req["file_uri"]
original_file_name = req["file_name"]
file_response = requests.get(file_uri)
npimg = np.fromstring(file_response.content, np.uint8)
request_file_data = cv2.imdecode(npimg, cv2.IMREAD_COLOR)
chard_image_data = characterize101(request_file_data)
file_object = CustomFileStorage(user_id=user_id)
file_object.store_file(
file_name="chard-101-" + original_file_name,
file_data=chard_image_data,
file_type="text",
)
characterized_image_uri = file_object.saved_file_uri
return {
"characterized-image-data": chard_image_data,
"characterized-image-uri": characterized_image_uri,
}
@app.route("/show_image", methods=["GET", "POST"])
def displayImage():
if request.method == "GET":
return render_template("display_image.html")
else:
return handle_show_image(request)
@app.route("/test", methods=["POST"])
def testApi():
req = request.get_json()
return req
## ------------------------------------- Unauthorized Handler -------------------------------------
# @login_manager.unauthorized_handler
# def unauthorized_handler():
# return '''
# Please <a href="/glogin">login</a> first!
# '''
# @app.route("/checker",methods=["POST"])
# def checker():
# req = request.get_json()
# print("this is key-val","a -",req["a"])
# print("Just checking",req)
# return {
# "x":"y"
# }
if __name__ == "__main__":
app.run(port=8080, debug=True)