Skip to content
This repository was archived by the owner on Apr 16, 2023. It is now read-only.

Commit 2c7f764

Browse files
author
Patrick Kuehn
committed
Move routes to folder
1 parent 4d0a3dd commit 2c7f764

File tree

6 files changed

+70
-56
lines changed

6 files changed

+70
-56
lines changed

autofocus/predict/app/app.py

Lines changed: 5 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,14 @@
1-
import time
1+
from flask import Flask
22

3-
from flask import Flask, jsonify, request
3+
from .routes.predict import predict_route
4+
from .routes.predict_zip import predict_zip_route
45

5-
from .models.File import File
6-
from .models.ZipArchive import ZipArchive
7-
from .validation.predict import validate_predict_request
8-
from .validation.predict_zip import validate_predict_zip_request
9-
from .validation.validation import abort_with_errors
10-
from .prediction.prediction import predict, predict_multiple
11-
12-
13-
# We are going to upload the files to the server as part of the request, so set tmp folder here.
14-
UPLOAD_FOLDER = "/tmp/"
156

167
app = Flask(__name__)
178
app.config.from_object(__name__)
18-
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
19-
20-
21-
@app.route("/predict", methods=["POST"])
22-
def classify_single():
23-
"""Classify a single image"""
24-
# Validate request
25-
validate_predict_request(request)
26-
27-
# Get File object
28-
file = File(request.files["file"], app.config["UPLOAD_FOLDER"])
29-
30-
# Return ziped probabilities
31-
return jsonify(predict(file))
32-
33-
34-
@app.route("/predict_zip", methods=["POST"])
35-
def classify_zip():
36-
"""Classify all images from a zip file"""
37-
# Validate request
38-
validate_predict_zip_request(request)
39-
40-
file = ZipArchive(request.files["file"], app.config["UPLOAD_FOLDER"])
41-
if not file.hasImages():
42-
error = {
43-
"file": "No image files detected in the zip file."
44-
}
45-
abort_with_errors(error)
46-
47-
# Extract files
48-
files = file.extractAll(app.config["UPLOAD_FOLDER"], file.listAllImages())
49-
50-
# Make prediction
51-
return jsonify(predict_multiple(files))
52-
539

54-
@app.route("/hello")
55-
def hello():
56-
"""Just a test endpoint to make sure server is running"""
57-
return "Hey there!\n"
10+
app.register_blueprint(predict_route)
11+
app.register_blueprint(predict_zip_route)
5812

5913

6014
if __name__ == "__main__":

autofocus/predict/app/models/File.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from werkzeug import secure_filename
44

55

6+
UPLOAD_FOLDER = "/tmp/"
7+
68
class File:
79
"""
810
Store a file and remove it upon destruction
@@ -12,7 +14,7 @@ class File:
1214
name: Secured filename (Can be empty)
1315
"""
1416

15-
def __init__(self, file=None, upload_path=None):
17+
def __init__(self, file=None, upload_path=UPLOAD_FOLDER):
1618
"""
1719
Constructor of File
1820
@@ -22,6 +24,7 @@ def __init__(self, file=None, upload_path=None):
2224
file: Uploaded file object from flask
2325
upload_path: The path to upload the file
2426
"""
27+
self.upload_folder = upload_path
2528
if file:
2629
self.setFromUploadedFile(file, upload_path)
2730

autofocus/predict/app/models/ZipArchive.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
from zipfile import ZipFile
33

4-
from .File import File
4+
from .File import File, UPLOAD_FOLDER
55
from ..validation.validation import allowed_file, ALLOWED_IMAGE_FILES
66

77

@@ -16,7 +16,7 @@ class ZipArchive:
1616
zip: Opened zip file
1717
"""
1818

19-
def __init__(self, file, upload_folder=None):
19+
def __init__(self, file):
2020
"""
2121
Constructor of ZipFile
2222
@@ -26,7 +26,7 @@ def __init__(self, file, upload_folder=None):
2626
file: Uploaded file from flask
2727
upload_folder: The folder to save the zip file
2828
"""
29-
self.file = File(file, upload_folder)
29+
self.file = File(file)
3030
self.zip = ZipFile(self.file.getPath())
3131

3232
def listFiles(self):
@@ -64,7 +64,7 @@ def hasImages(self, extensions=ALLOWED_IMAGE_FILES):
6464
"""
6565
return len(self.listAllImages(extensions)) > 0
6666

67-
def extractAll(self, path=None, members=None):
67+
def extractAll(self, path=UPLOAD_FOLDER, members=None):
6868
"""
6969
Extract all the given files
7070

autofocus/predict/app/routes/__init__.py

Whitespace-only changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import time
2+
3+
from flask import Blueprint, jsonify, request
4+
5+
from ..models.File import File
6+
from ..models.ZipArchive import ZipArchive
7+
from ..validation.predict import validate_predict_request
8+
from ..validation.predict_zip import validate_predict_zip_request
9+
from ..validation.validation import abort_with_errors
10+
from ..prediction.prediction import predict, predict_multiple
11+
12+
13+
predict_route = Blueprint("predict", __name__)
14+
15+
@predict_route.route("/predict", methods=["POST"])
16+
def classify_single():
17+
"""Classify a single image"""
18+
# Validate request
19+
validate_predict_request(request)
20+
21+
# Get File object
22+
file = File(request.files["file"])
23+
24+
# Return ziped probabilities
25+
return jsonify(predict(file))
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import time
2+
3+
from flask import Blueprint, jsonify, request
4+
5+
from ..models.File import File
6+
from ..models.ZipArchive import ZipArchive
7+
from ..validation.predict import validate_predict_request
8+
from ..validation.predict_zip import validate_predict_zip_request
9+
from ..validation.validation import abort_with_errors
10+
from ..prediction.prediction import predict, predict_multiple
11+
12+
13+
predict_zip_route = Blueprint("predict_zip", __name__)
14+
15+
@predict_zip_route.route("/predict_zip", methods=["POST"])
16+
def classify_zip():
17+
"""Classify all images from a zip file"""
18+
# Validate request
19+
validate_predict_zip_request(request)
20+
21+
file = ZipArchive(request.files["file"])
22+
if not file.hasImages():
23+
error = {
24+
"file": "No image files detected in the zip file."
25+
}
26+
abort_with_errors(error)
27+
28+
# Extract files
29+
files = file.extractAll(members=file.listAllImages())
30+
31+
# Make prediction
32+
return jsonify(predict_multiple(files))

0 commit comments

Comments
 (0)