Repository navigation
Expand file tree
/
Copy pathIAWebAppFlaskServer.py
More file actions
146 lines (131 loc) · 5.34 KB
/
Copy pathIAWebAppFlaskServer.py
File metadata and controls
146 lines (131 loc) · 5.34 KB
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
#pip install pyopenssl
import os
from flask import Flask, flash, request, redirect, render_template, jsonify
from werkzeug.utils import secure_filename
from pythonProject.WebAppAI import HFinvoicereceipt, SpacyLeavesParser
from pythonProject.WebAppAI import HFQAItalian
#from pythonProject.WebAppAI.HFDocQuery import HFDocQuery
debug = True
UPLOAD_FOLDER = 'D:/temp/uploads'
ALLOWED_EXTENSIONS = {'pdf', 'png', 'jpg', 'jpeg', 'gif'}
ALLOWED_EXTENSIONS_CHATBOT = {'pdf'}
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1000 * 1000
app.config['SECRET_KEY'] = '12345'
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def allowed_file_chatbot(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS_CHATBOT
@app.route("/")
def def_home():
message = "This it root"
return render_template('index.html',
message=message)
# Per invio scontrino
@app.route('/sendReceipt', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
# Controlla id del file part sia wuello ammesso
if 'receipt-image' not in request.files :
flash('No file part')
return redirect(request.url)
file = request.files['receipt-image']
# Se l'utente non seleziona nessun file, il browser invia
# un file vuoto senza nome.
if file.filename == '':
flash('Nessun file selezionato')
return redirect(request.url)
# Controlla che il file abbia l'estensione ammessa
if file and allowed_file(file.filename):
# Controlla che il nome del file abbia caratteri NON validi
filename = secure_filename(file.filename)
if (debug) :
print(filename)
# Salva il file nella cartella configurata
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
# Invoca la componente IA Python che incapsula il funzionamento dell'OCR
jsonresponse = HFinvoicereceipt.readInfo(os.path.join(app.config['UPLOAD_FOLDER'], filename))
if (debug):
print(jsonresponse)
return jsonresponse
return jsonify(error='Metodo request non ammesso'), 403
# Invio file per Q&A
@app.route('/sendContext', methods=['GET', 'POST'])
def upload_file_QA():
if (request.method == 'POST' or request.method == 'GET') :
# Controlla id del file part sia wuello ammesso
if 'QA-file' not in request.files :
flash('No file part')
data = {
"filename": "",
"num_pages": 0,
"status": "KO",
"reason": "context_is_missing"
}
return jsonify(data)
file = request.files['QA-file']
# Se l'utente non seleziona nessun file, il browser invia
# un file vuoto senza nome.
if file.filename == '':
flash('Nessun file selezionato')
data = {
"filename": "",
"num_pages": 0,
"status": "KO",
"reason": "context_is_missing"
}
return jsonify(data)
# Controlla che il file abbia l'estensione ammessa
if file and allowed_file_chatbot(file.filename):
# Controlla che il nome del file abbia caratteri NON validi
filename = secure_filename(file.filename)
if (debug) :
print(filename)
# Salva il file nella cartella configurata
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
# Invoca la componente IA Python che incapsula il funzionamento dell'OCR
jsonresponse = HFQAItalian.prepare_context_for_question(os.path.join(app.config['UPLOAD_FOLDER'], filename))
if (debug):
print(jsonresponse)
return jsonresponse
else:
data = {
"filename": "",
"num_pages": 0,
"status": "KO",
"reason": "extension_not_allowed"
}
return jsonify(data)
return jsonify(error='Metodo request non ammesso'), 403
# Invio domanda per Q&A
@app.route('/answerQuestion', methods=['GET', 'POST'])
def answer_question():
if request.method == 'GET':
question = request.args.get('QA-question')
print(question)
jsonresponse = HFQAItalian.ask_for_question(question)
if (debug):
print(jsonresponse)
return jsonresponse
return jsonify(error='Metodo request non ammesso'), 403
# Invio comando
@app.route('/analyzeCommand', methods=['GET', 'POST'])
def analyze_command():
if request.method == 'GET':
command = request.args.get('command')
print(command)
jsonresponse = SpacyLeavesParser.readInfo(command)
if (debug):
print(jsonresponse)
return jsonresponse
return jsonify(error='Metodo request non ammesso'), 403
@app.route("/<template>")
def def_template(template):
message = "This template redirect"
return render_template(f'{template}.html',message=message)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000, debug=True, ssl_context='adhoc')