Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,152 +1,169 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
from
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from typing import Any, Dict, List
|
| 3 |
+
|
| 4 |
+
from src.registry import get_model_display_names, get_model
|
| 5 |
+
|
| 6 |
+
APP_TITLE = "Machine Learning CS 6140 Project: Pet Recognizer"
|
| 7 |
+
TOP_K_DEFAULT = 5
|
| 8 |
+
|
| 9 |
+
DARK_CSS = """
|
| 10 |
+
body {
|
| 11 |
+
background-color: #0f172a !important;
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
.gradio-container {
|
| 15 |
+
background-color: #0f172a !important;
|
| 16 |
+
color: #e5e7eb !important;
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
h1, h2, h3, h4, p, li, label {
|
| 20 |
+
color: #e5e7eb !important;
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
a {
|
| 24 |
+
color: #60a5fa !important;
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
.gr-box {
|
| 28 |
+
background-color: #020617 !important;
|
| 29 |
+
border-radius: 10px;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
.gr-button {
|
| 33 |
+
background-color: #1e293b !important;
|
| 34 |
+
color: #e5e7eb !important;
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
.gr-button:hover {
|
| 38 |
+
background-color: #334155 !important;
|
| 39 |
+
}
|
| 40 |
+
"""
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
# -----------------------------
|
| 44 |
+
# Helpers
|
| 45 |
+
# -----------------------------
|
| 46 |
+
def format_topk_for_table(top_k: List[Dict[str, Any]]) -> List[List[Any]]:
|
| 47 |
+
rows = []
|
| 48 |
+
for rank, entry in enumerate(top_k, start=1):
|
| 49 |
+
class_name = entry.get("class_name", f"id={entry.get('class_id', '?')}")
|
| 50 |
+
prob = entry.get("probability", 0.0)
|
| 51 |
+
rows.append([rank, class_name, round(float(prob) * 100.0, 2)])
|
| 52 |
+
return rows
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def run_inference(model_id: str, image) -> Dict[str, Any]:
|
| 56 |
+
if image is None:
|
| 57 |
+
return {
|
| 58 |
+
"main_text": "Please upload an image first.",
|
| 59 |
+
"topk_table": [],
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
model = get_model(model_id)
|
| 63 |
+
result = model.predict(image, top_k=TOP_K_DEFAULT)
|
| 64 |
+
|
| 65 |
+
class_name = result.get("class_name", "Unknown")
|
| 66 |
+
class_id = result.get("class_id", "N/A")
|
| 67 |
+
top_k = result.get("top_k", [])
|
| 68 |
+
|
| 69 |
+
main_text = (
|
| 70 |
+
f"**Predicted Class:** {class_name} \n"
|
| 71 |
+
f"**Class ID:** {class_id}"
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
return {
|
| 75 |
+
"main_text": main_text,
|
| 76 |
+
"topk_table": format_topk_for_table(top_k),
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
# -----------------------------
|
| 81 |
+
# UI
|
| 82 |
+
# -----------------------------
|
| 83 |
+
def build_demo() -> gr.Blocks:
|
| 84 |
+
model_display_names = get_model_display_names()
|
| 85 |
+
name_to_id = {v: k for k, v in model_display_names.items()}
|
| 86 |
+
default_display_name = next(iter(name_to_id.keys()))
|
| 87 |
+
|
| 88 |
+
with gr.Blocks(css=DARK_CSS) as demo:
|
| 89 |
+
|
| 90 |
+
# Title
|
| 91 |
+
gr.Markdown(
|
| 92 |
+
f"""
|
| 93 |
+
# {APP_TITLE}
|
| 94 |
+
|
| 95 |
+
This project demonstrates **pet breed recognition** using the
|
| 96 |
+
**Oxford-IIIT Pet Dataset**, comparing **classical machine learning models**
|
| 97 |
+
(Logistic Regression, SVM) with **deep feature-based models**
|
| 98 |
+
(Pretrained ResNet18).
|
| 99 |
+
|
| 100 |
+
**Dataset & Supported Breeds**
|
| 101 |
+
The models are trained on **37 cat and dog breeds** from the Oxford-IIIT Pet Dataset.
|
| 102 |
+
https://www.robots.ox.ac.uk/~vgg/data/pets/
|
| 103 |
+
"""
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
# Instructions
|
| 107 |
+
gr.Markdown(
|
| 108 |
+
"""
|
| 109 |
+
## Instructions
|
| 110 |
+
|
| 111 |
+
1. **Upload** a clear, close-up image of a **cat or dog** belonging to one of the supported breeds
|
| 112 |
+
2. **Select a model** to run the recognition:
|
| 113 |
+
- **LR / SVM** → Expected to perform poorly on raw pixel inputs
|
| 114 |
+
- **ResNet-based models** → Use pretrained deep visual features and produce much better results
|
| 115 |
+
3. Click **Run Identification** to view the **Top-5 predictions**
|
| 116 |
+
"""
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
with gr.Row():
|
| 120 |
+
# Left column
|
| 121 |
+
with gr.Column(scale=1):
|
| 122 |
+
gr.Markdown("### Select Model & Upload Image")
|
| 123 |
+
|
| 124 |
+
model_dropdown = gr.Dropdown(
|
| 125 |
+
choices=list(name_to_id.keys()),
|
| 126 |
+
value=default_display_name,
|
| 127 |
+
label="Select Model",
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
image_input = gr.Image(
|
| 131 |
+
type="pil",
|
| 132 |
+
label="Upload your pet image (JPEG / PNG)",
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
run_button = gr.Button("Run Identification")
|
| 136 |
+
|
| 137 |
+
# Right column
|
| 138 |
+
with gr.Column(scale=1):
|
| 139 |
+
gr.Markdown("### Model Prediction")
|
| 140 |
+
|
| 141 |
+
main_output = gr.Markdown(
|
| 142 |
+
value="Prediction will appear here.",
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
topk_output = gr.Dataframe(
|
| 146 |
+
headers=["Rank", "Class Name", "Probability (%)"],
|
| 147 |
+
datatype=["number", "str", "number"],
|
| 148 |
+
column_count=3,
|
| 149 |
+
label=f"Top-{TOP_K_DEFAULT} Predictions",
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
# Button wiring
|
| 153 |
+
def _gradio_infer(selected_display_name, img):
|
| 154 |
+
model_id = name_to_id[selected_display_name]
|
| 155 |
+
result = run_inference(model_id, img)
|
| 156 |
+
return result["main_text"], result["topk_table"]
|
| 157 |
+
|
| 158 |
+
run_button.click(
|
| 159 |
+
fn=_gradio_infer,
|
| 160 |
+
inputs=[model_dropdown, image_input],
|
| 161 |
+
outputs=[main_output, topk_output],
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
return demo
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
if __name__ == "__main__":
|
| 168 |
+
demo = build_demo()
|
| 169 |
+
demo.launch()
|