Spaces:
Sleeping
Sleeping
Commit ·
ee590d9
1
Parent(s): 0d267c5
app.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
from flask import Flask, jsonify, request
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
import csv
|
|
@@ -8,6 +10,7 @@ import torch
|
|
| 8 |
import torch.nn as nn
|
| 9 |
import numpy as np
|
| 10 |
from torchvision import models, transforms
|
|
|
|
| 11 |
|
| 12 |
app = Flask(__name__)
|
| 13 |
|
|
@@ -327,9 +330,19 @@ def get_model_0_result():
|
|
| 327 |
"message": str(e)
|
| 328 |
}), 500
|
| 329 |
|
| 330 |
-
@app.route("/model/1", methods=["POST"])
|
| 331 |
def model_1():
|
| 332 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
image_size = (224, 224)
|
| 334 |
class_labels = ("Fresh Condition", "Reject Condition")
|
| 335 |
max_image_pixels = 25_000_000
|
|
@@ -357,6 +370,19 @@ def model_1():
|
|
| 357 |
image = ImageOps.exif_transpose(image).convert("RGB")
|
| 358 |
image = image.resize(image_size, Image.Resampling.BILINEAR)
|
| 359 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 360 |
tensor = torch.frombuffer(
|
| 361 |
bytearray(image.tobytes()), dtype=torch.uint8
|
| 362 |
)
|
|
@@ -366,32 +392,39 @@ def model_1():
|
|
| 366 |
std = torch.tensor([0.229, 0.224, 0.225]).view(3, 1, 1)
|
| 367 |
tensor = ((tensor - mean) / std).unsqueeze(0)
|
| 368 |
|
| 369 |
-
if not model_path.is_file():
|
| 370 |
-
return jsonify({"error": f"Model file not found: {model_path.name}"}), 503
|
| 371 |
-
|
| 372 |
model = torch.jit.load(str(model_path), map_location="cpu")
|
| 373 |
model.eval()
|
| 374 |
with torch.inference_mode():
|
| 375 |
logits = model(tensor)
|
| 376 |
if logits.shape != (1, len(class_labels)):
|
| 377 |
-
return jsonify({
|
|
|
|
|
|
|
|
|
|
| 378 |
|
| 379 |
probabilities = torch.softmax(logits, dim=1)[0]
|
| 380 |
predicted_index = int(probabilities.argmax().item())
|
| 381 |
confidence = float(probabilities[predicted_index].item())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 382 |
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
return jsonify({"error": f"Prediction failed: {exc}"}), 500
|
| 387 |
|
| 388 |
return jsonify(
|
| 389 |
{
|
| 390 |
-
"
|
| 391 |
-
"
|
| 392 |
-
"
|
|
|
|
|
|
|
|
|
|
| 393 |
}
|
| 394 |
-
)
|
| 395 |
|
| 396 |
|
| 397 |
# ============================================================
|
|
|
|
| 1 |
import os
|
| 2 |
+
from io import BytesIO
|
| 3 |
+
from pathlib import Path
|
| 4 |
from flask import Flask, jsonify, request
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
import csv
|
|
|
|
| 10 |
import torch.nn as nn
|
| 11 |
import numpy as np
|
| 12 |
from torchvision import models, transforms
|
| 13 |
+
from PIL import Image, ImageOps
|
| 14 |
|
| 15 |
app = Flask(__name__)
|
| 16 |
|
|
|
|
| 330 |
"message": str(e)
|
| 331 |
}), 500
|
| 332 |
|
| 333 |
+
@app.route("/model/1", methods=["GET", "POST"])
|
| 334 |
def model_1():
|
| 335 |
+
if request.method == "GET":
|
| 336 |
+
return jsonify({
|
| 337 |
+
"status": "model is running",
|
| 338 |
+
"model": "Fruit and Vegetable Condition Classifier",
|
| 339 |
+
"classes": [
|
| 340 |
+
"Fresh Condition",
|
| 341 |
+
"Reject Condition"
|
| 342 |
+
]
|
| 343 |
+
}), 200
|
| 344 |
+
|
| 345 |
+
model_path = Path(__file__).parent / "models" / "fruit_veg.pt"
|
| 346 |
image_size = (224, 224)
|
| 347 |
class_labels = ("Fresh Condition", "Reject Condition")
|
| 348 |
max_image_pixels = 25_000_000
|
|
|
|
| 370 |
image = ImageOps.exif_transpose(image).convert("RGB")
|
| 371 |
image = image.resize(image_size, Image.Resampling.BILINEAR)
|
| 372 |
|
| 373 |
+
except (OSError, ValueError) as exc:
|
| 374 |
+
return jsonify({
|
| 375 |
+
"status": "error",
|
| 376 |
+
"message": f"Invalid image: {exc}"
|
| 377 |
+
}), 400
|
| 378 |
+
|
| 379 |
+
if not model_path.is_file():
|
| 380 |
+
return jsonify({
|
| 381 |
+
"status": "error",
|
| 382 |
+
"message": f"Model file not found: {model_path.name}"
|
| 383 |
+
}), 503
|
| 384 |
+
|
| 385 |
+
try:
|
| 386 |
tensor = torch.frombuffer(
|
| 387 |
bytearray(image.tobytes()), dtype=torch.uint8
|
| 388 |
)
|
|
|
|
| 392 |
std = torch.tensor([0.229, 0.224, 0.225]).view(3, 1, 1)
|
| 393 |
tensor = ((tensor - mean) / std).unsqueeze(0)
|
| 394 |
|
|
|
|
|
|
|
|
|
|
| 395 |
model = torch.jit.load(str(model_path), map_location="cpu")
|
| 396 |
model.eval()
|
| 397 |
with torch.inference_mode():
|
| 398 |
logits = model(tensor)
|
| 399 |
if logits.shape != (1, len(class_labels)):
|
| 400 |
+
return jsonify({
|
| 401 |
+
"status": "error",
|
| 402 |
+
"message": "Model returned an unexpected output."
|
| 403 |
+
}), 500
|
| 404 |
|
| 405 |
probabilities = torch.softmax(logits, dim=1)[0]
|
| 406 |
predicted_index = int(probabilities.argmax().item())
|
| 407 |
confidence = float(probabilities[predicted_index].item())
|
| 408 |
+
except (OSError, RuntimeError, ValueError) as exc:
|
| 409 |
+
return jsonify({
|
| 410 |
+
"status": "error",
|
| 411 |
+
"message": f"Prediction failed: {exc}"
|
| 412 |
+
}), 500
|
| 413 |
|
| 414 |
+
prediction = class_labels[predicted_index]
|
| 415 |
+
confidence = round(confidence, 4)
|
| 416 |
+
confidence_percent = round(confidence * 100, 2)
|
|
|
|
| 417 |
|
| 418 |
return jsonify(
|
| 419 |
{
|
| 420 |
+
"status": "success",
|
| 421 |
+
"prediction": prediction,
|
| 422 |
+
"confidence": confidence,
|
| 423 |
+
"confidence_percent": confidence_percent,
|
| 424 |
+
"class": prediction,
|
| 425 |
+
"confidence_score": confidence
|
| 426 |
}
|
| 427 |
+
), 200
|
| 428 |
|
| 429 |
|
| 430 |
# ============================================================
|