-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
125 lines (90 loc) · 3.69 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
import os
from TextAnalysis import chatbot
from TextAnalysis.file.BaseFile import BaseFile
from flask import Flask
from flask import request
from flask import jsonify
from flask_cors import CORS, cross_origin
from werkzeug.utils import secure_filename
# create and configure the app
app = Flask(__name__)
app.config["UPLOAD_FOLDER"] = "pdf_files"
CORS(app)
@app.route('/', methods=['GET'])
def run():
message = request.args.get('message')
readySubmit = request.args.get('readySubmit')
topicFound = request.args.get('topicFound')
fileSubmit = request.args.get('fileSubmit')
classifiedMsg = request.args.get('classifiedMsg')
topicSelected = request.args.get('topicSelected')
topicFinal = request.args.get('topicFinal')
file = request.args.get('file')
webResources = request.args.get('resource')
analysedFile = request.args.get('fileAnalysed')
providedResources = request.args.get('resourcesProvided')
conversationFinished = request.args.get('conversationFinished')
clarify = request.args.get('isClarify')
answered = request.args.get('isAnswered')
topicsPossible = request.args.get('possibleTopics')
return chatbot.run(message, readySubmit, topicFound, fileSubmit, classifiedMsg, topicSelected, topicFinal, file,
webResources, analysedFile, providedResources, conversationFinished, clarify, answered, topicsPossible)
@app.route("/upload/pdf", methods=["GET", "POST"])
def uploadPDF() -> str:
"""
Saves the uploaded PDF file uploaded via POST.
If no file was sent, the file is not a PDF, an error occurred, or the request method is not
POST, an empty string will be returned.
:return: The file path, or an empty string.
"""
response = None
if request.method == "POST":
try:
file = request.files["pdf"]
# If the file is a PDF
if file.mimetype == "application/pdf":
path = toAvailableUploadFilePath(file.filename)
file.save(path)
response = jsonify({'file': path})
except Exception as e:
print(e)
if response == None:
response = jsonify({})
response.headers.add('Access-Control-Allow-Origin', "*")
return response
def toUploadFilePath(filename: str) -> str:
"""
Prepends the upload file path to the filename.
:return: The updated path.
"""
return os.path.join(app.config['UPLOAD_FOLDER'],
secure_filename(filename))
def toAvailableUploadFilePath(filename: str) -> str:
"""
Updates the given filename to one that does not exist in the upload folder
by appending an incrementing number to the filename until a file at that
location does not exist.
If a file with the given filename does not initial exist in the upload
folder, a number won't be appended, returning the path with the original
filename.
E.g., if a file exists with the name example.txt, it will have an
incrementing number appended to it, such as:
example_1.txt
example_2.txt
example_3.txt
until a filename with that name and number does not exist.
:param filename: The name of the file being saved
:return: The available file path with the given filename
"""
basename = os.path.splitext(filename)[0]
path = toUploadFilePath(filename)
newPath = path
count = 0
# Append count to the original filename until a file with the updated name
# does not exist
while os.path.isfile(newPath):
count += 1
newPath = toUploadFilePath("{}_{}.pdf".format(basename, count))
return newPath
if __name__ == '__main__':
app.run()