Update app.py
Browse files
app.py
CHANGED
|
@@ -95,82 +95,81 @@ transform = transforms.Compose([
|
|
| 95 |
def index():
|
| 96 |
return render_template("index.html")
|
| 97 |
|
| 98 |
-
@app.route("/predict", methods=["POST"])
|
| 99 |
@app.route("/predict", methods=["POST"])
|
| 100 |
def predict():
|
| 101 |
-
if "file" not in request.files:
|
| 102 |
-
return jsonify({"error": "No file uploaded"}), 400
|
| 103 |
-
|
| 104 |
-
```
|
| 105 |
-
file = request.files["file"]
|
| 106 |
-
|
| 107 |
-
if file.filename == "":
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
try:
|
| 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 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
except Exception as e:
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
```
|
| 174 |
|
| 175 |
|
| 176 |
@app.route("/model-info")
|
|
|
|
| 95 |
def index():
|
| 96 |
return render_template("index.html")
|
| 97 |
|
|
|
|
| 98 |
@app.route("/predict", methods=["POST"])
|
| 99 |
def predict():
|
| 100 |
+
if "file" not in request.files:
|
| 101 |
+
return jsonify({"error": "No file uploaded"}), 400
|
| 102 |
+
|
| 103 |
+
```
|
| 104 |
+
file = request.files["file"]
|
| 105 |
+
|
| 106 |
+
if file.filename == "":
|
| 107 |
+
return jsonify({"error": "Empty filename"}), 400
|
| 108 |
+
|
| 109 |
+
try:
|
| 110 |
+
data = file.read()
|
| 111 |
+
|
| 112 |
+
print("=" * 60)
|
| 113 |
+
print("Filename:", file.filename)
|
| 114 |
+
print("Content-Type:", file.content_type)
|
| 115 |
+
print("Bytes length:", len(data))
|
| 116 |
+
|
| 117 |
+
img = Image.open(io.BytesIO(data))
|
| 118 |
+
print("Format:", img.format)
|
| 119 |
+
print("Size:", img.size)
|
| 120 |
+
|
| 121 |
+
img = img.convert("RGB")
|
| 122 |
+
print("Step 1: RGB conversion done")
|
| 123 |
+
|
| 124 |
+
tensor = transform(img).unsqueeze(0).to(device)
|
| 125 |
+
print("Step 2: Transform done")
|
| 126 |
+
print("Tensor shape:", tensor.shape)
|
| 127 |
+
print("Tensor dtype:", tensor.dtype)
|
| 128 |
+
|
| 129 |
+
t0 = time.time()
|
| 130 |
+
|
| 131 |
+
with torch.no_grad():
|
| 132 |
+
print("Step 3: Starting inference")
|
| 133 |
+
logits = model(tensor)
|
| 134 |
+
print("Step 4: Inference complete")
|
| 135 |
+
|
| 136 |
+
probs = F.softmax(logits, dim=1).squeeze(0)
|
| 137 |
+
print("Step 5: Softmax complete")
|
| 138 |
+
|
| 139 |
+
ms = round((time.time() - t0) * 1000, 2)
|
| 140 |
+
|
| 141 |
+
results = sorted(
|
| 142 |
+
[
|
| 143 |
+
{
|
| 144 |
+
"class": CLASSES[i],
|
| 145 |
+
"probability": float(probs[i])
|
| 146 |
+
}
|
| 147 |
+
for i in range(10)
|
| 148 |
+
],
|
| 149 |
+
key=lambda x: x["probability"],
|
| 150 |
+
reverse=True
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
print("Step 6: Results generated")
|
| 154 |
+
print("Top class:", results[0]["class"])
|
| 155 |
+
print("Top probability:", results[0]["probability"])
|
| 156 |
+
|
| 157 |
+
return jsonify({
|
| 158 |
+
"top_class": results[0]["class"],
|
| 159 |
+
"top_probability": results[0]["probability"],
|
| 160 |
+
"all_predictions": results,
|
| 161 |
+
"inference_ms": ms
|
| 162 |
+
})
|
| 163 |
+
|
| 164 |
+
except Exception as e:
|
| 165 |
+
import traceback
|
| 166 |
+
print("ERROR OCCURRED:")
|
| 167 |
+
traceback.print_exc()
|
| 168 |
+
|
| 169 |
+
return jsonify({
|
| 170 |
+
"error": str(e)
|
| 171 |
+
}), 500
|
| 172 |
+
```
|
| 173 |
|
| 174 |
|
| 175 |
@app.route("/model-info")
|