Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,9 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
import os, io, sys, base64, traceback
|
| 4 |
from pathlib import Path
|
| 5 |
from flask import Flask, request, render_template, jsonify
|
| 6 |
from PIL import Image
|
| 7 |
|
| 8 |
-
|
| 9 |
BASE_DIR = Path(__file__).resolve().parent
|
| 10 |
if str(BASE_DIR) not in sys.path:
|
| 11 |
sys.path.insert(0, str(BASE_DIR))
|
|
@@ -42,9 +40,9 @@ def predict():
|
|
| 42 |
file = request.files.get("image")
|
| 43 |
|
| 44 |
if not file or file.filename == "":
|
| 45 |
-
return render_template("index.html", error="
|
| 46 |
if not allowed_file(file.filename):
|
| 47 |
-
return render_template("index.html", error="
|
| 48 |
|
| 49 |
img_bytes = file.read()
|
| 50 |
pil_img = Image.open(io.BytesIO(img_bytes)).convert("RGB")
|
|
@@ -54,20 +52,20 @@ def predict():
|
|
| 54 |
try:
|
| 55 |
if model_choice == "pytorch":
|
| 56 |
if not Path(PYTORCH_MODEL_PATH).exists():
|
| 57 |
-
raise FileNotFoundError(f"
|
| 58 |
result = predict_pytorch(str(tmp_path), model_path=PYTORCH_MODEL_PATH)
|
| 59 |
else:
|
| 60 |
if not Path(TF_MODEL_PATH).exists():
|
| 61 |
-
raise FileNotFoundError(f"
|
| 62 |
result = predict_tensorflow(str(tmp_path), model_path=TF_MODEL_PATH)
|
| 63 |
|
| 64 |
except FileNotFoundError as e:
|
| 65 |
tmp_path.unlink(missing_ok=True)
|
| 66 |
-
return render_template("index.html", error=f"
|
| 67 |
except Exception as e:
|
| 68 |
tmp_path.unlink(missing_ok=True)
|
| 69 |
print("ERREUR INFERENCE :", traceback.format_exc())
|
| 70 |
-
return render_template("index.html", error=f"
|
| 71 |
finally:
|
| 72 |
tmp_path.unlink(missing_ok=True)
|
| 73 |
|
|
|
|
|
|
|
|
|
|
| 1 |
import os, io, sys, base64, traceback
|
| 2 |
from pathlib import Path
|
| 3 |
from flask import Flask, request, render_template, jsonify
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
|
| 7 |
BASE_DIR = Path(__file__).resolve().parent
|
| 8 |
if str(BASE_DIR) not in sys.path:
|
| 9 |
sys.path.insert(0, str(BASE_DIR))
|
|
|
|
| 40 |
file = request.files.get("image")
|
| 41 |
|
| 42 |
if not file or file.filename == "":
|
| 43 |
+
return render_template("index.html", error="Upload an image."), 400
|
| 44 |
if not allowed_file(file.filename):
|
| 45 |
+
return render_template("index.html", error="You have to use JPG, PNG, WEBP ou BMP."), 400
|
| 46 |
|
| 47 |
img_bytes = file.read()
|
| 48 |
pil_img = Image.open(io.BytesIO(img_bytes)).convert("RGB")
|
|
|
|
| 52 |
try:
|
| 53 |
if model_choice == "pytorch":
|
| 54 |
if not Path(PYTORCH_MODEL_PATH).exists():
|
| 55 |
+
raise FileNotFoundError(f"Model PyTorch not found : {PYTORCH_MODEL_PATH}")
|
| 56 |
result = predict_pytorch(str(tmp_path), model_path=PYTORCH_MODEL_PATH)
|
| 57 |
else:
|
| 58 |
if not Path(TF_MODEL_PATH).exists():
|
| 59 |
+
raise FileNotFoundError(f"Model TensorFlow not found : {TF_MODEL_PATH}")
|
| 60 |
result = predict_tensorflow(str(tmp_path), model_path=TF_MODEL_PATH)
|
| 61 |
|
| 62 |
except FileNotFoundError as e:
|
| 63 |
tmp_path.unlink(missing_ok=True)
|
| 64 |
+
return render_template("index.html", error=f"Model not found : {e}"), 500
|
| 65 |
except Exception as e:
|
| 66 |
tmp_path.unlink(missing_ok=True)
|
| 67 |
print("ERREUR INFERENCE :", traceback.format_exc())
|
| 68 |
+
return render_template("index.html", error=f"Error : {str(e)}"), 500
|
| 69 |
finally:
|
| 70 |
tmp_path.unlink(missing_ok=True)
|
| 71 |
|