Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,62 +4,45 @@ import numpy as np
|
|
| 4 |
from PIL import Image
|
| 5 |
import os
|
| 6 |
import time
|
| 7 |
-
import
|
| 8 |
|
| 9 |
-
# Config
|
| 10 |
IMAGE_SIZE = (64, 64)
|
| 11 |
LEADERBOARD_PATH = "leaderboard.csv"
|
| 12 |
LABEL_FILE = "labels.csv"
|
| 13 |
TEST_DIR = "test_images"
|
| 14 |
|
| 15 |
-
# ====================================
|
| 16 |
-
# Image preprocessing
|
| 17 |
-
# ====================================
|
| 18 |
def image_to_features(image: Image.Image) -> np.ndarray:
|
| 19 |
image = image.resize(IMAGE_SIZE)
|
| 20 |
return np.array(image.convert("L")).flatten()
|
| 21 |
|
| 22 |
-
# ====================================
|
| 23 |
-
# Load test images + labels
|
| 24 |
-
# ====================================
|
| 25 |
def load_test_data():
|
| 26 |
if not os.path.exists(LABEL_FILE) or not os.path.exists(TEST_DIR):
|
| 27 |
-
raise FileNotFoundError("Missing
|
| 28 |
-
|
| 29 |
-
df = pd.read_csv(LABEL_FILE)
|
| 30 |
-
X_test = []
|
| 31 |
-
y_test = []
|
| 32 |
|
|
|
|
|
|
|
| 33 |
for _, row in df.iterrows():
|
| 34 |
img_path = os.path.join(TEST_DIR, row["filename"])
|
| 35 |
-
label = row["label"]
|
| 36 |
try:
|
| 37 |
img = Image.open(img_path)
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
y_test.append(label)
|
| 41 |
except Exception as e:
|
| 42 |
-
print(f"Error
|
| 43 |
-
|
| 44 |
return np.array(X_test), np.array(y_test)
|
| 45 |
|
| 46 |
-
# ====================================
|
| 47 |
-
# Initialize leaderboard
|
| 48 |
-
# ====================================
|
| 49 |
if not os.path.exists(LEADERBOARD_PATH):
|
| 50 |
pd.DataFrame(columns=["Name", "Accuracy", "Avg Time (ms)"]).to_csv(LEADERBOARD_PATH, index=False)
|
| 51 |
|
| 52 |
-
# ====================================
|
| 53 |
-
# Evaluate uploaded model
|
| 54 |
-
# ====================================
|
| 55 |
def evaluate_model(file, name):
|
| 56 |
try:
|
| 57 |
-
|
| 58 |
-
|
| 59 |
|
|
|
|
| 60 |
start = time.time()
|
| 61 |
y_pred = model.predict(X_test)
|
| 62 |
-
elapsed = (time.time() - start) * 1000
|
| 63 |
|
| 64 |
if len(y_pred) != len(y_test):
|
| 65 |
return "❌ Model output length does not match test set."
|
|
@@ -68,29 +51,26 @@ def evaluate_model(file, name):
|
|
| 68 |
avg_time = elapsed / len(X_test)
|
| 69 |
|
| 70 |
leaderboard = pd.read_csv(LEADERBOARD_PATH)
|
| 71 |
-
|
| 72 |
"Name": name,
|
| 73 |
"Accuracy": round(accuracy, 2),
|
| 74 |
"Avg Time (ms)": round(avg_time, 2)
|
| 75 |
-
}])
|
| 76 |
-
leaderboard = pd.concat([leaderboard, new_entry], ignore_index=True)
|
| 77 |
leaderboard.to_csv(LEADERBOARD_PATH, index=False)
|
| 78 |
|
| 79 |
return leaderboard
|
| 80 |
|
| 81 |
except Exception as e:
|
| 82 |
-
|
|
|
|
| 83 |
|
| 84 |
-
# ====================================
|
| 85 |
-
# Launch Gradio app
|
| 86 |
-
# ====================================
|
| 87 |
gr.Interface(
|
| 88 |
fn=evaluate_model,
|
| 89 |
inputs=[
|
| 90 |
-
gr.File(label="Upload your `.joblib`
|
| 91 |
gr.Text(label="Your name or team")
|
| 92 |
],
|
| 93 |
outputs=gr.Dataframe(label="Leaderboard"),
|
| 94 |
-
title="🧠
|
| 95 |
-
description="Upload
|
| 96 |
).launch()
|
|
|
|
| 4 |
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()
|
| 17 |
|
|
|
|
|
|
|
|
|
|
| 18 |
def load_test_data():
|
| 19 |
if not os.path.exists(LABEL_FILE) or not os.path.exists(TEST_DIR):
|
| 20 |
+
raise FileNotFoundError("Missing labels.csv or test_images/ folder.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
df = pd.read_csv(LABEL_FILE)
|
| 23 |
+
X_test, y_test = [], []
|
| 24 |
for _, row in df.iterrows():
|
| 25 |
img_path = os.path.join(TEST_DIR, row["filename"])
|
|
|
|
| 26 |
try:
|
| 27 |
img = Image.open(img_path)
|
| 28 |
+
X_test.append(image_to_features(img))
|
| 29 |
+
y_test.append(row["label"])
|
|
|
|
| 30 |
except Exception as e:
|
| 31 |
+
print(f"❌ Error loading {img_path}: {e}")
|
|
|
|
| 32 |
return np.array(X_test), np.array(y_test)
|
| 33 |
|
|
|
|
|
|
|
|
|
|
| 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:
|
| 40 |
+
model = cloudpickle.load(f)
|
| 41 |
|
| 42 |
+
X_test, y_test = load_test_data()
|
| 43 |
start = time.time()
|
| 44 |
y_pred = model.predict(X_test)
|
| 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."
|
|
|
|
| 51 |
avg_time = elapsed / len(X_test)
|
| 52 |
|
| 53 |
leaderboard = pd.read_csv(LEADERBOARD_PATH)
|
| 54 |
+
leaderboard = pd.concat([leaderboard, pd.DataFrame([{
|
| 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 |
+
import traceback
|
| 65 |
+
return f"❌ Error:\n{traceback.format_exc()}"
|
| 66 |
|
|
|
|
|
|
|
|
|
|
| 67 |
gr.Interface(
|
| 68 |
fn=evaluate_model,
|
| 69 |
inputs=[
|
| 70 |
+
gr.File(label="Upload your `classifier-joblib.joblib` file"),
|
| 71 |
gr.Text(label="Your name or team")
|
| 72 |
],
|
| 73 |
outputs=gr.Dataframe(label="Leaderboard"),
|
| 74 |
+
title="🧠 Olive Fly Classifier Leaderboard",
|
| 75 |
+
description="Upload your `classifier-joblib.joblib` model trained on 64×64 grayscale images. We'll evaluate it and update the leaderboard."
|
| 76 |
).launch()
|