Spaces:
Sleeping
Sleeping
File size: 2,694 Bytes
9b9d1ba 7115182 eed8510 9b9d1ba 7115182 9b9d1ba 4883610 0918407 ebe9307 eed8510 9b9d1ba eed8510 9b9d1ba eed8510 9b9d1ba eed8510 ebe9307 7115182 9b9d1ba eed8510 9b9d1ba 491b4a4 eed8510 491b4a4 9b9d1ba eed8510 9b9d1ba eed8510 9b9d1ba 491b4a4 7115182 eed8510 7115182 491b4a4 eed8510 491b4a4 eed8510 491b4a4 eed8510 491b4a4 7115182 eed8510 7115182 0918407 7115182 0918407 eed8510 491b4a4 eed8510 7115182 eed8510 1874aa4 491b4a4 0918407 491b4a4 1874aa4 7115182 eed8510 491b4a4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | import joblib
import numpy as np
import pandas as pd
import gradio as gr
# ---------------- Load CSV ----------------
CSV_PATH = "best_models_summary.csv"
df = pd.read_csv(CSV_PATH)
# ---------------- Prediction Function ----------------
def predict_features(uploaded_file, dataset_type, metric_priority):
try:
features = np.load(uploaded_file) # user uploads .npy feature
except Exception as e:
return f"β Error loading feature file: {e}"
# Ensure correct shape
if len(features.shape) == 1:
features = features.reshape(1, -1)
# ---------------- Select Best Model ----------------
subset = df[df['dataset'] == dataset_type]
if metric_priority != "best_overall":
subset = subset.sort_values(by=metric_priority, ascending=False)
else:
subset = subset.sort_values(by='f1_score', ascending=False)
model_row = subset.iloc[0]
model_path = model_row['model_path']
model_name = model_row['model_name']
# ---------------- Load Model ----------------
try:
model = joblib.load(model_path)
except Exception as e:
return f"β Error loading model: {e}"
# ---------------- Prediction ----------------
try:
pred = model.predict(features)[0]
except Exception as e:
return f"β Prediction error: {e}"
# ---------------- Output ----------------
result_text = f"""
### π₯ Prediction Result
**Model Used:** {model_name}
**Dataset:** {dataset_type}
**Predicted Class:** {pred}
**Metrics:**
Accuracy: {model_row['accuracy']:.4f}
Precision: {model_row['precision']:.4f}
Recall: {model_row['recall']:.4f}
F1 Score: {model_row['f1_score']:.4f}
"""
return result_text
# ---------------- Gradio UI ----------------
with gr.Blocks(css="""
h1 {color: #ff6f61; text-align: center;}
.gr-button {background-color: #ff6f61; color:white; font-weight:bold;}
""") as demo:
gr.Markdown("<h1>π₯ Wildfire Feature Classification π₯</h1>")
gr.Markdown("Upload a feature vector (.npy file) extracted using Xception.")
with gr.Row():
uploaded_file = gr.File(label="Upload Feature (.npy)", file_types=['.npy'])
dataset_type = gr.Dropdown(choices=['satellite','uav'], label="Dataset Type")
metric_priority = gr.Dropdown(
choices=['accuracy','precision','recall','f1_score','best_overall'],
label="Metric Priority"
)
output_text = gr.Markdown()
predict_btn = gr.Button("Predict π₯")
predict_btn.click(
fn=predict_features,
inputs=[uploaded_file, dataset_type, metric_priority],
outputs=output_text
)
# Launch
demo.launch() |