Create app.py
Browse files- kitchen_model/app.py +73 -0
kitchen_model/app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torchvision.transforms as transforms
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# -----------------------
|
| 7 |
+
# Device
|
| 8 |
+
# -----------------------
|
| 9 |
+
|
| 10 |
+
device = torch.device("cpu")
|
| 11 |
+
|
| 12 |
+
# -----------------------
|
| 13 |
+
# Load Model
|
| 14 |
+
# -----------------------
|
| 15 |
+
|
| 16 |
+
MODEL_PATH = "kitchen_model"
|
| 17 |
+
|
| 18 |
+
model = torch.load(
|
| 19 |
+
MODEL_PATH,
|
| 20 |
+
map_location=device
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
model.eval()
|
| 24 |
+
|
| 25 |
+
# -----------------------
|
| 26 |
+
# Image Transform
|
| 27 |
+
# -----------------------
|
| 28 |
+
|
| 29 |
+
transform = transforms.Compose([
|
| 30 |
+
transforms.Resize((224, 224)),
|
| 31 |
+
transforms.ToTensor(),
|
| 32 |
+
])
|
| 33 |
+
|
| 34 |
+
# -----------------------
|
| 35 |
+
# Prediction Function
|
| 36 |
+
# -----------------------
|
| 37 |
+
|
| 38 |
+
def predict(image):
|
| 39 |
+
|
| 40 |
+
image = transform(image).unsqueeze(0)
|
| 41 |
+
|
| 42 |
+
with torch.no_grad():
|
| 43 |
+
|
| 44 |
+
outputs = model(image)
|
| 45 |
+
|
| 46 |
+
# Binary classification
|
| 47 |
+
probability = torch.sigmoid(outputs).item()
|
| 48 |
+
|
| 49 |
+
if probability > 0.5:
|
| 50 |
+
label = "Clean Kitchen"
|
| 51 |
+
else:
|
| 52 |
+
label = "Unclean Kitchen"
|
| 53 |
+
|
| 54 |
+
confidence = round(probability * 100, 2)
|
| 55 |
+
|
| 56 |
+
return {
|
| 57 |
+
"Prediction": label,
|
| 58 |
+
"Confidence (%)": confidence
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
# -----------------------
|
| 62 |
+
# Gradio UI
|
| 63 |
+
# -----------------------
|
| 64 |
+
|
| 65 |
+
interface = gr.Interface(
|
| 66 |
+
fn=predict,
|
| 67 |
+
inputs=gr.Image(type="pil"),
|
| 68 |
+
outputs="json",
|
| 69 |
+
title="Kitchen Hygiene Detection",
|
| 70 |
+
description="Upload a kitchen image to check cleanliness."
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
interface.launch()
|