Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,12 +5,19 @@ from PIL import Image
|
|
| 5 |
import os
|
| 6 |
import time
|
| 7 |
import cloudpickle
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
IMAGE_SIZE = (64, 64)
|
| 10 |
LEADERBOARD_PATH = "leaderboard.csv"
|
| 11 |
LABEL_FILE = "labels.csv"
|
| 12 |
TEST_DIR = "test_images"
|
| 13 |
|
|
|
|
|
|
|
|
|
|
| 14 |
def image_to_features(image: Image.Image) -> np.ndarray:
|
| 15 |
image = image.resize(IMAGE_SIZE)
|
| 16 |
return np.array(image.convert("L")).flatten()
|
|
@@ -34,6 +41,9 @@ def load_test_data():
|
|
| 34 |
if not os.path.exists(LEADERBOARD_PATH):
|
| 35 |
pd.DataFrame(columns=["Name", "Accuracy", "Avg Time (ms)"]).to_csv(LEADERBOARD_PATH, index=False)
|
| 36 |
|
|
|
|
|
|
|
|
|
|
| 37 |
def evaluate_model(file, name):
|
| 38 |
try:
|
| 39 |
with open(file.name, "rb") as f:
|
|
@@ -45,32 +55,55 @@ def evaluate_model(file, name):
|
|
| 45 |
elapsed = (time.time() - start) * 1000
|
| 46 |
|
| 47 |
if len(y_pred) != len(y_test):
|
| 48 |
-
return "❌ Model output length does not match test set."
|
| 49 |
|
| 50 |
accuracy = 100.0 * (y_pred == y_test).mean()
|
| 51 |
avg_time = elapsed / len(X_test)
|
| 52 |
|
| 53 |
leaderboard = pd.read_csv(LEADERBOARD_PATH)
|
| 54 |
-
|
| 55 |
"Name": name,
|
| 56 |
"Accuracy": round(accuracy, 2),
|
| 57 |
"Avg Time (ms)": round(avg_time, 2)
|
| 58 |
-
}])
|
|
|
|
| 59 |
leaderboard.to_csv(LEADERBOARD_PATH, index=False)
|
| 60 |
|
| 61 |
-
return leaderboard
|
| 62 |
|
| 63 |
except Exception as e:
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import os
|
| 6 |
import time
|
| 7 |
import cloudpickle
|
| 8 |
+
import traceback
|
| 9 |
|
| 10 |
+
# =========================
|
| 11 |
+
# Configuration
|
| 12 |
+
# =========================
|
| 13 |
IMAGE_SIZE = (64, 64)
|
| 14 |
LEADERBOARD_PATH = "leaderboard.csv"
|
| 15 |
LABEL_FILE = "labels.csv"
|
| 16 |
TEST_DIR = "test_images"
|
| 17 |
|
| 18 |
+
# =========================
|
| 19 |
+
# Helpers
|
| 20 |
+
# =========================
|
| 21 |
def image_to_features(image: Image.Image) -> np.ndarray:
|
| 22 |
image = image.resize(IMAGE_SIZE)
|
| 23 |
return np.array(image.convert("L")).flatten()
|
|
|
|
| 41 |
if not os.path.exists(LEADERBOARD_PATH):
|
| 42 |
pd.DataFrame(columns=["Name", "Accuracy", "Avg Time (ms)"]).to_csv(LEADERBOARD_PATH, index=False)
|
| 43 |
|
| 44 |
+
# =========================
|
| 45 |
+
# Evaluation
|
| 46 |
+
# =========================
|
| 47 |
def evaluate_model(file, name):
|
| 48 |
try:
|
| 49 |
with open(file.name, "rb") as f:
|
|
|
|
| 55 |
elapsed = (time.time() - start) * 1000
|
| 56 |
|
| 57 |
if len(y_pred) != len(y_test):
|
| 58 |
+
return "❌ Model output length does not match test set.", None
|
| 59 |
|
| 60 |
accuracy = 100.0 * (y_pred == y_test).mean()
|
| 61 |
avg_time = elapsed / len(X_test)
|
| 62 |
|
| 63 |
leaderboard = pd.read_csv(LEADERBOARD_PATH)
|
| 64 |
+
new_entry = pd.DataFrame([{
|
| 65 |
"Name": name,
|
| 66 |
"Accuracy": round(accuracy, 2),
|
| 67 |
"Avg Time (ms)": round(avg_time, 2)
|
| 68 |
+
}])
|
| 69 |
+
leaderboard = pd.concat([leaderboard, new_entry], ignore_index=True)
|
| 70 |
leaderboard.to_csv(LEADERBOARD_PATH, index=False)
|
| 71 |
|
| 72 |
+
return "", leaderboard
|
| 73 |
|
| 74 |
except Exception as e:
|
| 75 |
+
return f"❌ Error:\n{traceback.format_exc()}", None
|
| 76 |
+
|
| 77 |
+
# =========================
|
| 78 |
+
# UI with Gradio Blocks
|
| 79 |
+
# =========================
|
| 80 |
+
with gr.Blocks(title="Olive Fly Classifier Leaderboard") as demo:
|
| 81 |
+
gr.Markdown("## 🧠 Olive Fly Classifier Leaderboard", elem_id="title")
|
| 82 |
+
gr.Markdown(
|
| 83 |
+
"Upload your `classifier-joblib.joblib` model trained on 64×64 grayscale images. "
|
| 84 |
+
"We'll evaluate it and update the leaderboard."
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
with gr.Row(variant="default"):
|
| 88 |
+
with gr.Column(scale=1):
|
| 89 |
+
file_input = gr.File(label="Upload your model")
|
| 90 |
+
name_input = gr.Text(label="Your name or team")
|
| 91 |
+
submit_btn = gr.Button("Submit model")
|
| 92 |
+
|
| 93 |
+
error_box = gr.Textbox(label="Output log", visible=False)
|
| 94 |
+
|
| 95 |
+
with gr.Row():
|
| 96 |
+
leaderboard_table = gr.Dataframe(
|
| 97 |
+
label="Leaderboard",
|
| 98 |
+
headers=["Name", "Accuracy", "Avg Time (ms)"],
|
| 99 |
+
interactive=False
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
# Define button logic
|
| 103 |
+
submit_btn.click(
|
| 104 |
+
fn=evaluate_model,
|
| 105 |
+
inputs=[file_input, name_input],
|
| 106 |
+
outputs=[error_box, leaderboard_table]
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
demo.launch()
|