Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,39 +6,43 @@ import os
|
|
| 6 |
import time
|
| 7 |
import joblib
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
def image_to_features(image: Image.Image) -> np.ndarray:
|
| 14 |
-
image = image.resize(
|
| 15 |
return np.array(image.convert("L")).flatten()
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
for _, row in label_df.iterrows():
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
| 31 |
-
y_test = np.array(y_test)
|
| 32 |
|
| 33 |
-
#
|
| 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)
|
|
@@ -61,7 +65,6 @@ def evaluate_model(file, name):
|
|
| 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=[
|
|
|
|
| 6 |
import time
|
| 7 |
import joblib
|
| 8 |
|
| 9 |
+
LEADERBOARD_PATH = "leaderboard.csv"
|
| 10 |
+
IMAGE_SIZE = (64, 64)
|
| 11 |
+
TEST_DIR = "test_images"
|
| 12 |
+
LABEL_FILE = "labels.csv"
|
| 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 test data or label file.")
|
| 21 |
+
|
| 22 |
+
label_df = pd.read_csv(LABEL_FILE)
|
| 23 |
+
X_test = []
|
| 24 |
+
y_test = []
|
| 25 |
|
| 26 |
+
for _, row in label_df.iterrows():
|
| 27 |
+
img_path = os.path.join(TEST_DIR, row["filename"])
|
| 28 |
+
label = row["label"]
|
| 29 |
+
try:
|
| 30 |
+
img = Image.open(img_path)
|
| 31 |
+
X_test.append(image_to_features(img))
|
| 32 |
+
y_test.append(label)
|
| 33 |
+
except Exception as e:
|
| 34 |
+
print(f"Failed to load {img_path}: {e}")
|
| 35 |
|
| 36 |
+
return np.array(X_test), np.array(y_test)
|
|
|
|
| 37 |
|
| 38 |
+
# Create leaderboard file if missing
|
|
|
|
| 39 |
if not os.path.exists(LEADERBOARD_PATH):
|
| 40 |
pd.DataFrame(columns=["Name", "Accuracy", "Avg Time (ms)"]).to_csv(LEADERBOARD_PATH, index=False)
|
| 41 |
|
|
|
|
| 42 |
def evaluate_model(file, name):
|
| 43 |
try:
|
| 44 |
model = joblib.load(file.name)
|
| 45 |
+
X_test, y_test = load_test_data()
|
| 46 |
|
| 47 |
start = time.time()
|
| 48 |
y_pred = model.predict(X_test)
|
|
|
|
| 65 |
except Exception as e:
|
| 66 |
return f"❌ Error during evaluation: {e}"
|
| 67 |
|
|
|
|
| 68 |
gr.Interface(
|
| 69 |
fn=evaluate_model,
|
| 70 |
inputs=[
|