Commit
·
1b8fb45
1
Parent(s):
299f980
Model Deployment
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ultralytics import YOLO
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from huggingface_hub import snapshot_download
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# 1) Change this to your model repo (you already have it)
|
| 8 |
+
REPO_ID = "Interior-Style-Classification"
|
| 9 |
+
WEIGHTS_FILENAME = "best.pt" # file in your model repo
|
| 10 |
+
|
| 11 |
+
def load_model(repo_id: str) -> YOLO:
|
| 12 |
+
"""
|
| 13 |
+
Downloads the model repo from Hugging Face Hub into the Space cache
|
| 14 |
+
and loads the YOLO model from the weights file.
|
| 15 |
+
"""
|
| 16 |
+
download_dir = snapshot_download(repo_id=repo_id)
|
| 17 |
+
weights_path = os.path.join(download_dir, WEIGHTS_FILENAME)
|
| 18 |
+
|
| 19 |
+
if not os.path.exists(weights_path):
|
| 20 |
+
raise FileNotFoundError(
|
| 21 |
+
f"Cannot find {WEIGHTS_FILENAME} inside downloaded repo: {download_dir}\n"
|
| 22 |
+
f"Files found: {os.listdir(download_dir)}"
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
return YOLO(weights_path, task="classify")
|
| 26 |
+
|
| 27 |
+
classification_model = load_model(REPO_ID)
|
| 28 |
+
|
| 29 |
+
def predict(pilimg: Image.Image) -> str:
|
| 30 |
+
results = classification_model.predict(pilimg)
|
| 31 |
+
|
| 32 |
+
probs = results[0].probs
|
| 33 |
+
class_id = probs.top1
|
| 34 |
+
confidence = probs.top1conf.item()
|
| 35 |
+
class_name = classification_model.names[class_id]
|
| 36 |
+
|
| 37 |
+
return f"{class_name} ({confidence:.2f})"
|
| 38 |
+
|
| 39 |
+
demo = gr.Interface(
|
| 40 |
+
fn=predict,
|
| 41 |
+
inputs=gr.Image(type="pil", label="Upload an interior image"),
|
| 42 |
+
outputs=gr.Textbox(label="Predicted interior style"),
|
| 43 |
+
title="Interior Style Classification (YOLOv8)",
|
| 44 |
+
description="Upload an image and the model will classify the interior design style."
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
demo.launch()
|