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

Commit

Permalink
Black code formating
Browse files Browse the repository at this point in the history
- Rename File to temporary file
  • Loading branch information
Patrick Kuehn committed Nov 5, 2019
1 parent 2c7f764 commit 8b6ecc1
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

UPLOAD_FOLDER = "/tmp/"

class File:

class TemporaryFile:
"""
Store a file and remove it upon destruction
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
from zipfile import ZipFile

from .File import File, UPLOAD_FOLDER
from .TemporaryFile import TemporaryFile, UPLOAD_FOLDER
from ..validation.validation import allowed_file, ALLOWED_IMAGE_FILES


Expand All @@ -26,7 +26,7 @@ def __init__(self, file):
file: Uploaded file from flask
upload_folder: The folder to save the zip file
"""
self.file = File(file)
self.file = TemporaryFile(file)
self.zip = ZipFile(self.file.getPath())

def listFiles(self):
Expand Down Expand Up @@ -81,7 +81,7 @@ def extractAll(self, path=UPLOAD_FOLDER, members=None):
self.zip.extractall(path, members)
extractedFiles = {}
for member in members:
file = File()
file = TemporaryFile()
file.setPath(os.path.join(path, member))
extractedFiles[member] = file
return extractedFiles
2 changes: 2 additions & 0 deletions autofocus/predict/app/prediction/prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def predict_multiple(files):
predictions[key] = predict(files[key])
return predictions


def predict(file):
"""
Predict probabilities of single file
Expand All @@ -36,6 +37,7 @@ def predict(file):
pred_classes, preds, probs = model.predict(image)
return getProbabilities([prob.item() for prob in probs])


def getProbabilities(probabilities):
"""
Return formated Probabilities
Expand Down
12 changes: 4 additions & 8 deletions autofocus/predict/app/routes/predict.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
import time

from flask import Blueprint, jsonify, request

from ..models.File import File
from ..models.ZipArchive import ZipArchive
from ..filesystem.TemporaryFile import TemporaryFile
from ..prediction.prediction import predict
from ..validation.predict import validate_predict_request
from ..validation.predict_zip import validate_predict_zip_request
from ..validation.validation import abort_with_errors
from ..prediction.prediction import predict, predict_multiple


predict_route = Blueprint("predict", __name__)


@predict_route.route("/predict", methods=["POST"])
def classify_single():
"""Classify a single image"""
# Validate request
validate_predict_request(request)

# Get File object
file = File(request.files["file"])
file = TemporaryFile(request.files["file"])

# Return ziped probabilities
return jsonify(predict(file))
13 changes: 4 additions & 9 deletions autofocus/predict/app/routes/predict_zip.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import time

from flask import Blueprint, jsonify, request

from ..models.File import File
from ..models.ZipArchive import ZipArchive
from ..validation.predict import validate_predict_request
from ..filesystem.ZipArchive import ZipArchive
from ..prediction.prediction import predict_multiple
from ..validation.predict_zip import validate_predict_zip_request
from ..validation.validation import abort_with_errors
from ..prediction.prediction import predict, predict_multiple


predict_zip_route = Blueprint("predict_zip", __name__)


@predict_zip_route.route("/predict_zip", methods=["POST"])
def classify_zip():
"""Classify all images from a zip file"""
Expand All @@ -20,9 +17,7 @@ def classify_zip():

file = ZipArchive(request.files["file"])
if not file.hasImages():
error = {
"file": "No image files detected in the zip file."
}
error = {"file": "No image files detected in the zip file."}
abort_with_errors(error)

# Extract files
Expand Down
8 changes: 3 additions & 5 deletions autofocus/predict/app/validation/predict.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .validation import abort_with_errors, ALLOWED_IMAGE_FILES, allowed_file
from .validation import abort_with_errors, allowed_file, ALLOWED_IMAGE_FILES


def validate_predict_request(request):
Expand All @@ -13,11 +13,9 @@ def validate_predict_request(request):
if not file:
error["file"] = "No file given."
elif not allowed_file(file.filename, ALLOWED_IMAGE_FILES):
error[
"file"
] = "File type not allowed. File must be of type {allowed}".format(
error["file"] = "File type not allowed. File must be of type {allowed}".format(
allowed=ALLOWED_IMAGE_FILES
)

if (error):
if error:
abort_with_errors(error)
7 changes: 3 additions & 4 deletions autofocus/predict/app/validation/predict_zip.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .validation import abort_with_errors, ALLOWED_ZIP_FILES, allowed_file
from .validation import abort_with_errors, allowed_file, ALLOWED_ZIP_FILES


def validate_predict_zip_request(request):
"""
Expand All @@ -15,9 +16,7 @@ def validate_predict_zip_request(request):
if not file:
error["file"] = "No file given."
elif not allowed_file(file.filename, ALLOWED_ZIP_FILES):
error[
"file"
] = "File type not allowed. File must be of type {allowed}".format(
error["file"] = "File type not allowed. File must be of type {allowed}".format(
allowed=ALLOWED_ZIP_FILES
)

Expand Down
1 change: 1 addition & 0 deletions autofocus/predict/app/validation/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def abort_with_errors(error):
)
)


def allowed_file(filename, allowed_extensions):
"""
Check for whether a filename is in the ALLOWED_EXTENSIONS
Expand Down

0 comments on commit 8b6ecc1

Please sign in to comment.