Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,21 +5,14 @@ from PIL import Image
|
|
| 5 |
import os
|
| 6 |
import time
|
| 7 |
import joblib
|
| 8 |
-
import pickle
|
| 9 |
-
import torch
|
| 10 |
-
import torch.nn as nn
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
# ===============================
|
| 15 |
-
|
| 16 |
-
label_df = pd.read_csv("labels.csv") # columns: filename,label
|
| 17 |
test_dir = "test_images"
|
| 18 |
|
| 19 |
def image_to_features(image: Image.Image) -> np.ndarray:
|
| 20 |
image = image.resize((64, 64))
|
| 21 |
-
|
| 22 |
-
return array.flatten()
|
| 23 |
|
| 24 |
X_test = []
|
| 25 |
y_test = []
|
|
@@ -27,101 +20,40 @@ y_test = []
|
|
| 27 |
for _, row in label_df.iterrows():
|
| 28 |
img_path = os.path.join(test_dir, row["filename"])
|
| 29 |
label = row["label"]
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
|
| 35 |
X_test = np.array(X_test)
|
| 36 |
y_test = np.array(y_test)
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
# Step 2: Define Leaderboard File
|
| 40 |
-
# ===============================
|
| 41 |
-
|
| 42 |
LEADERBOARD_PATH = "leaderboard.csv"
|
| 43 |
if not os.path.exists(LEADERBOARD_PATH):
|
| 44 |
pd.DataFrame(columns=["Name", "Accuracy", "Avg Time (ms)"]).to_csv(LEADERBOARD_PATH, index=False)
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
# Step 3: Define Model Wrappers
|
| 48 |
-
# ===============================
|
| 49 |
-
|
| 50 |
-
class SimpleTorchModel(nn.Module):
|
| 51 |
-
def __init__(self):
|
| 52 |
-
super().__init__()
|
| 53 |
-
self.fc = nn.Sequential(
|
| 54 |
-
nn.Linear(64 * 64, 128),
|
| 55 |
-
nn.ReLU(),
|
| 56 |
-
nn.Linear(128, 2)
|
| 57 |
-
)
|
| 58 |
-
|
| 59 |
-
def forward(self, x):
|
| 60 |
-
return self.fc(x)
|
| 61 |
-
|
| 62 |
-
class SKLearnWrapper:
|
| 63 |
-
def __init__(self, model):
|
| 64 |
-
self.model = model
|
| 65 |
-
|
| 66 |
-
def predict(self, X: np.ndarray) -> np.ndarray:
|
| 67 |
-
return self.model.predict(X)
|
| 68 |
-
|
| 69 |
-
class TorchPredictWrapper:
|
| 70 |
-
def __init__(self, model_path: str):
|
| 71 |
-
self.model = SimpleTorchModel()
|
| 72 |
-
self.model.load_state_dict(torch.load(model_path, map_location="cpu"))
|
| 73 |
-
self.model.eval()
|
| 74 |
-
|
| 75 |
-
def predict(self, X: np.ndarray) -> np.ndarray:
|
| 76 |
-
with torch.no_grad():
|
| 77 |
-
tensor = torch.tensor(X, dtype=torch.float32)
|
| 78 |
-
logits = self.model(tensor)
|
| 79 |
-
return torch.argmax(logits, dim=1).numpy()
|
| 80 |
-
|
| 81 |
-
# ===============================
|
| 82 |
-
# Step 4: Load Model From File
|
| 83 |
-
# ===============================
|
| 84 |
-
|
| 85 |
-
def load_model(file_path: str):
|
| 86 |
-
try:
|
| 87 |
-
model = joblib.load(file_path)
|
| 88 |
-
return SKLearnWrapper(model) # now wrapping just clf
|
| 89 |
-
except Exception as e1:
|
| 90 |
-
try:
|
| 91 |
-
with open(file_path, "rb") as f:
|
| 92 |
-
model = pickle.load(f)
|
| 93 |
-
return SKLearnWrapper(model)
|
| 94 |
-
except Exception as e2:
|
| 95 |
-
try:
|
| 96 |
-
return TorchPredictWrapper(file_path)
|
| 97 |
-
except Exception as e3:
|
| 98 |
-
raise RuntimeError(f"Unrecognized model format or failed to load:\n"
|
| 99 |
-
f"joblib error: {e1}\n"
|
| 100 |
-
f"pickle error: {e2}\n"
|
| 101 |
-
f"torch error: {e3}")
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
# ===============================
|
| 105 |
-
# Step 5: Evaluation Logic
|
| 106 |
-
# ===============================
|
| 107 |
-
|
| 108 |
def evaluate_model(file, name):
|
| 109 |
try:
|
| 110 |
-
model =
|
| 111 |
|
| 112 |
start = time.time()
|
| 113 |
y_pred = model.predict(X_test)
|
| 114 |
-
elapsed = (time.time() - start) * 1000
|
| 115 |
|
| 116 |
accuracy = 100 * (y_pred == y_test).mean()
|
| 117 |
avg_time = elapsed / len(X_test)
|
| 118 |
|
| 119 |
leaderboard = pd.read_csv(LEADERBOARD_PATH)
|
| 120 |
-
|
| 121 |
"Name": name,
|
| 122 |
"Accuracy": round(accuracy, 2),
|
| 123 |
"Avg Time (ms)": round(avg_time, 2)
|
| 124 |
-
}])
|
|
|
|
| 125 |
leaderboard.to_csv(LEADERBOARD_PATH, index=False)
|
| 126 |
|
| 127 |
return leaderboard
|
|
@@ -129,19 +61,14 @@ def evaluate_model(file, name):
|
|
| 129 |
except Exception as e:
|
| 130 |
return f"❌ Error during evaluation: {e}"
|
| 131 |
|
| 132 |
-
#
|
| 133 |
-
|
| 134 |
-
# ===============================
|
| 135 |
-
|
| 136 |
-
interface = gr.Interface(
|
| 137 |
fn=evaluate_model,
|
| 138 |
inputs=[
|
| 139 |
-
gr.File(label="Upload your `.joblib
|
| 140 |
-
gr.Text(label="Your
|
| 141 |
],
|
| 142 |
outputs=gr.Dataframe(label="Leaderboard"),
|
| 143 |
-
title="
|
| 144 |
-
description="Upload a `.joblib
|
| 145 |
-
)
|
| 146 |
-
|
| 147 |
-
interface.launch()
|
|
|
|
| 5 |
import os
|
| 6 |
import time
|
| 7 |
import joblib
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Load test data
|
| 10 |
+
label_df = pd.read_csv("labels.csv")
|
|
|
|
|
|
|
|
|
|
| 11 |
test_dir = "test_images"
|
| 12 |
|
| 13 |
def image_to_features(image: Image.Image) -> np.ndarray:
|
| 14 |
image = image.resize((64, 64))
|
| 15 |
+
return np.array(image.convert("L")).flatten()
|
|
|
|
| 16 |
|
| 17 |
X_test = []
|
| 18 |
y_test = []
|
|
|
|
| 20 |
for _, row in label_df.iterrows():
|
| 21 |
img_path = os.path.join(test_dir, row["filename"])
|
| 22 |
label = row["label"]
|
| 23 |
+
try:
|
| 24 |
+
img = Image.open(img_path)
|
| 25 |
+
X_test.append(image_to_features(img))
|
| 26 |
+
y_test.append(label)
|
| 27 |
+
except Exception as e:
|
| 28 |
+
print(f"Failed to load {img_path}: {e}")
|
| 29 |
|
| 30 |
X_test = np.array(X_test)
|
| 31 |
y_test = np.array(y_test)
|
| 32 |
|
| 33 |
+
# Initialize leaderboard
|
|
|
|
|
|
|
|
|
|
| 34 |
LEADERBOARD_PATH = "leaderboard.csv"
|
| 35 |
if not os.path.exists(LEADERBOARD_PATH):
|
| 36 |
pd.DataFrame(columns=["Name", "Accuracy", "Avg Time (ms)"]).to_csv(LEADERBOARD_PATH, index=False)
|
| 37 |
|
| 38 |
+
# Evaluation function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
def evaluate_model(file, name):
|
| 40 |
try:
|
| 41 |
+
model = joblib.load(file.name)
|
| 42 |
|
| 43 |
start = time.time()
|
| 44 |
y_pred = model.predict(X_test)
|
| 45 |
+
elapsed = (time.time() - start) * 1000
|
| 46 |
|
| 47 |
accuracy = 100 * (y_pred == y_test).mean()
|
| 48 |
avg_time = elapsed / len(X_test)
|
| 49 |
|
| 50 |
leaderboard = pd.read_csv(LEADERBOARD_PATH)
|
| 51 |
+
new_entry = pd.DataFrame([{
|
| 52 |
"Name": name,
|
| 53 |
"Accuracy": round(accuracy, 2),
|
| 54 |
"Avg Time (ms)": round(avg_time, 2)
|
| 55 |
+
}])
|
| 56 |
+
leaderboard = pd.concat([leaderboard, new_entry], ignore_index=True)
|
| 57 |
leaderboard.to_csv(LEADERBOARD_PATH, index=False)
|
| 58 |
|
| 59 |
return leaderboard
|
|
|
|
| 61 |
except Exception as e:
|
| 62 |
return f"❌ Error during evaluation: {e}"
|
| 63 |
|
| 64 |
+
# Gradio interface
|
| 65 |
+
gr.Interface(
|
|
|
|
|
|
|
|
|
|
| 66 |
fn=evaluate_model,
|
| 67 |
inputs=[
|
| 68 |
+
gr.File(label="Upload your `.joblib` model"),
|
| 69 |
+
gr.Text(label="Your name or team")
|
| 70 |
],
|
| 71 |
outputs=gr.Dataframe(label="Leaderboard"),
|
| 72 |
+
title="🧠 Joblib Classifier Leaderboard",
|
| 73 |
+
description="Upload a `.joblib` file for 64×64 grayscale image classification. Accuracy and speed will be measured."
|
| 74 |
+
).launch()
|
|
|
|
|
|