Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -25,7 +25,6 @@ def image_to_features(image: Image.Image) -> np.ndarray:
|
|
| 25 |
def load_test_data():
|
| 26 |
if not os.path.exists(LABEL_FILE) or not os.path.exists(TEST_DIR):
|
| 27 |
raise FileNotFoundError("Missing labels.csv or test_images/ folder.")
|
| 28 |
-
|
| 29 |
df = pd.read_csv(LABEL_FILE)
|
| 30 |
X_test, y_test = [], []
|
| 31 |
for _, row in df.iterrows():
|
|
@@ -38,11 +37,16 @@ def load_test_data():
|
|
| 38 |
print(f"❌ Error loading {img_path}: {e}")
|
| 39 |
return np.array(X_test), np.array(y_test)
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
# =========================
|
| 45 |
-
# Evaluation
|
| 46 |
# =========================
|
| 47 |
def evaluate_model(file, name):
|
| 48 |
try:
|
|
@@ -55,12 +59,12 @@ def evaluate_model(file, name):
|
|
| 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.",
|
| 59 |
|
| 60 |
accuracy = 100.0 * (y_pred == y_test).mean()
|
| 61 |
avg_time = elapsed / len(X_test)
|
| 62 |
|
| 63 |
-
leaderboard =
|
| 64 |
new_entry = pd.DataFrame([{
|
| 65 |
"Name": name,
|
| 66 |
"Accuracy": round(accuracy, 2),
|
|
@@ -72,34 +76,34 @@ def evaluate_model(file, name):
|
|
| 72 |
return "", leaderboard
|
| 73 |
|
| 74 |
except Exception as e:
|
| 75 |
-
return f"❌ Error:\n{traceback.format_exc()}",
|
| 76 |
|
| 77 |
# =========================
|
| 78 |
-
# UI
|
| 79 |
# =========================
|
| 80 |
with gr.Blocks(title="Olive Fly Classifier Leaderboard") as demo:
|
| 81 |
-
gr.Markdown("# Olive Fly Classifier Leaderboard"
|
| 82 |
gr.Markdown(
|
| 83 |
-
"Upload your `.cloudpkl` model trained on
|
| 84 |
"We'll evaluate it and update the leaderboard."
|
| 85 |
)
|
| 86 |
|
| 87 |
-
with gr.Row(
|
| 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=
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
|
| 102 |
-
#
|
| 103 |
submit_btn.click(
|
| 104 |
fn=evaluate_model,
|
| 105 |
inputs=[file_input, name_input],
|
|
|
|
| 25 |
def load_test_data():
|
| 26 |
if not os.path.exists(LABEL_FILE) or not os.path.exists(TEST_DIR):
|
| 27 |
raise FileNotFoundError("Missing labels.csv or test_images/ folder.")
|
|
|
|
| 28 |
df = pd.read_csv(LABEL_FILE)
|
| 29 |
X_test, y_test = [], []
|
| 30 |
for _, row in df.iterrows():
|
|
|
|
| 37 |
print(f"❌ Error loading {img_path}: {e}")
|
| 38 |
return np.array(X_test), np.array(y_test)
|
| 39 |
|
| 40 |
+
def load_leaderboard():
|
| 41 |
+
if os.path.exists(LEADERBOARD_PATH):
|
| 42 |
+
return pd.read_csv(LEADERBOARD_PATH)
|
| 43 |
+
else:
|
| 44 |
+
df = pd.DataFrame(columns=["Name", "Accuracy", "Avg Time (ms)"])
|
| 45 |
+
df.to_csv(LEADERBOARD_PATH, index=False)
|
| 46 |
+
return df
|
| 47 |
|
| 48 |
# =========================
|
| 49 |
+
# Evaluation Logic
|
| 50 |
# =========================
|
| 51 |
def evaluate_model(file, name):
|
| 52 |
try:
|
|
|
|
| 59 |
elapsed = (time.time() - start) * 1000
|
| 60 |
|
| 61 |
if len(y_pred) != len(y_test):
|
| 62 |
+
return "❌ Model output length does not match test set.", load_leaderboard()
|
| 63 |
|
| 64 |
accuracy = 100.0 * (y_pred == y_test).mean()
|
| 65 |
avg_time = elapsed / len(X_test)
|
| 66 |
|
| 67 |
+
leaderboard = load_leaderboard()
|
| 68 |
new_entry = pd.DataFrame([{
|
| 69 |
"Name": name,
|
| 70 |
"Accuracy": round(accuracy, 2),
|
|
|
|
| 76 |
return "", leaderboard
|
| 77 |
|
| 78 |
except Exception as e:
|
| 79 |
+
return f"❌ Error:\n{traceback.format_exc()}", load_leaderboard()
|
| 80 |
|
| 81 |
# =========================
|
| 82 |
+
# UI
|
| 83 |
# =========================
|
| 84 |
with gr.Blocks(title="Olive Fly Classifier Leaderboard") as demo:
|
| 85 |
+
gr.Markdown("## Olive Fly Classifier Leaderboard")
|
| 86 |
gr.Markdown(
|
| 87 |
+
"Upload your `.cloudpkl` model trained on Olive Fly images. "
|
| 88 |
"We'll evaluate it and update the leaderboard."
|
| 89 |
)
|
| 90 |
|
| 91 |
+
with gr.Row():
|
| 92 |
with gr.Column(scale=1):
|
| 93 |
file_input = gr.File(label="Upload your model")
|
| 94 |
name_input = gr.Text(label="Your name or team")
|
| 95 |
submit_btn = gr.Button("Submit model")
|
| 96 |
|
| 97 |
+
error_box = gr.Textbox(label="Output log", visible=True)
|
| 98 |
|
| 99 |
+
leaderboard_table = gr.Dataframe(
|
| 100 |
+
value=load_leaderboard(),
|
| 101 |
+
label="Leaderboard",
|
| 102 |
+
interactive=False,
|
| 103 |
+
wrap=True
|
| 104 |
+
)
|
| 105 |
|
| 106 |
+
# Always update leaderboard after submission
|
| 107 |
submit_btn.click(
|
| 108 |
fn=evaluate_model,
|
| 109 |
inputs=[file_input, name_input],
|