Spaces:
Runtime error
Runtime error
Julio Cesar Contreras Huerta
commited on
Commit
·
1a0754f
1
Parent(s):
e6039dd
XCVXZCV
Browse files- app.py +80 -203
- evaluate.py +34 -0
app.py
CHANGED
|
@@ -1,204 +1,81 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
from huggingface_hub import
|
| 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 |
-
ColumnFilter(
|
| 84 |
-
AutoEvalColumn.still_on_hub.name, type="boolean", label="Deleted/incomplete", default=True
|
| 85 |
-
),
|
| 86 |
-
],
|
| 87 |
-
bool_checkboxgroup_label="Hide models",
|
| 88 |
-
interactive=False,
|
| 89 |
-
)
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
demo = gr.Blocks(css=custom_css)
|
| 93 |
-
with demo:
|
| 94 |
-
gr.HTML(TITLE)
|
| 95 |
-
gr.Markdown(INTRODUCTION_TEXT, elem_classes="markdown-text")
|
| 96 |
-
|
| 97 |
-
with gr.Tabs(elem_classes="tab-buttons") as tabs:
|
| 98 |
-
with gr.TabItem("🏅 LLM Benchmark", elem_id="llm-benchmark-tab-table", id=0):
|
| 99 |
-
leaderboard = init_leaderboard(LEADERBOARD_DF)
|
| 100 |
-
|
| 101 |
-
with gr.TabItem("📝 About", elem_id="llm-benchmark-tab-table", id=2):
|
| 102 |
-
gr.Markdown(LLM_BENCHMARKS_TEXT, elem_classes="markdown-text")
|
| 103 |
-
|
| 104 |
-
with gr.TabItem("🚀 Submit here! ", elem_id="llm-benchmark-tab-table", id=3):
|
| 105 |
-
with gr.Column():
|
| 106 |
-
with gr.Row():
|
| 107 |
-
gr.Markdown(EVALUATION_QUEUE_TEXT, elem_classes="markdown-text")
|
| 108 |
-
|
| 109 |
-
with gr.Column():
|
| 110 |
-
with gr.Accordion(
|
| 111 |
-
f"✅ Finished Evaluations ({len(finished_eval_queue_df)})",
|
| 112 |
-
open=False,
|
| 113 |
-
):
|
| 114 |
-
with gr.Row():
|
| 115 |
-
finished_eval_table = gr.components.Dataframe(
|
| 116 |
-
value=finished_eval_queue_df,
|
| 117 |
-
headers=EVAL_COLS,
|
| 118 |
-
datatype=EVAL_TYPES,
|
| 119 |
-
row_count=5,
|
| 120 |
-
)
|
| 121 |
-
with gr.Accordion(
|
| 122 |
-
f"🔄 Running Evaluation Queue ({len(running_eval_queue_df)})",
|
| 123 |
-
open=False,
|
| 124 |
-
):
|
| 125 |
-
with gr.Row():
|
| 126 |
-
running_eval_table = gr.components.Dataframe(
|
| 127 |
-
value=running_eval_queue_df,
|
| 128 |
-
headers=EVAL_COLS,
|
| 129 |
-
datatype=EVAL_TYPES,
|
| 130 |
-
row_count=5,
|
| 131 |
-
)
|
| 132 |
-
|
| 133 |
-
with gr.Accordion(
|
| 134 |
-
f"⏳ Pending Evaluation Queue ({len(pending_eval_queue_df)})",
|
| 135 |
-
open=False,
|
| 136 |
-
):
|
| 137 |
-
with gr.Row():
|
| 138 |
-
pending_eval_table = gr.components.Dataframe(
|
| 139 |
-
value=pending_eval_queue_df,
|
| 140 |
-
headers=EVAL_COLS,
|
| 141 |
-
datatype=EVAL_TYPES,
|
| 142 |
-
row_count=5,
|
| 143 |
-
)
|
| 144 |
-
with gr.Row():
|
| 145 |
-
gr.Markdown("# ✉️✨ Submit your model here!", elem_classes="markdown-text")
|
| 146 |
-
|
| 147 |
-
with gr.Row():
|
| 148 |
-
with gr.Column():
|
| 149 |
-
model_name_textbox = gr.Textbox(label="Model name")
|
| 150 |
-
revision_name_textbox = gr.Textbox(label="Revision commit", placeholder="main")
|
| 151 |
-
model_type = gr.Dropdown(
|
| 152 |
-
choices=[t.to_str(" : ") for t in ModelType if t != ModelType.Unknown],
|
| 153 |
-
label="Model type",
|
| 154 |
-
multiselect=False,
|
| 155 |
-
value=None,
|
| 156 |
-
interactive=True,
|
| 157 |
-
)
|
| 158 |
-
|
| 159 |
-
with gr.Column():
|
| 160 |
-
precision = gr.Dropdown(
|
| 161 |
-
choices=[i.value.name for i in Precision if i != Precision.Unknown],
|
| 162 |
-
label="Precision",
|
| 163 |
-
multiselect=False,
|
| 164 |
-
value="float16",
|
| 165 |
-
interactive=True,
|
| 166 |
-
)
|
| 167 |
-
weight_type = gr.Dropdown(
|
| 168 |
-
choices=[i.value.name for i in WeightType],
|
| 169 |
-
label="Weights type",
|
| 170 |
-
multiselect=False,
|
| 171 |
-
value="Original",
|
| 172 |
-
interactive=True,
|
| 173 |
-
)
|
| 174 |
-
base_model_name_textbox = gr.Textbox(label="Base model (for delta or adapter weights)")
|
| 175 |
-
|
| 176 |
-
submit_button = gr.Button("Submit Eval")
|
| 177 |
-
submission_result = gr.Markdown()
|
| 178 |
-
submit_button.click(
|
| 179 |
-
add_new_eval,
|
| 180 |
-
[
|
| 181 |
-
model_name_textbox,
|
| 182 |
-
base_model_name_textbox,
|
| 183 |
-
revision_name_textbox,
|
| 184 |
-
precision,
|
| 185 |
-
weight_type,
|
| 186 |
-
model_type,
|
| 187 |
-
],
|
| 188 |
-
submission_result,
|
| 189 |
-
)
|
| 190 |
-
|
| 191 |
-
with gr.Row():
|
| 192 |
-
with gr.Accordion("📙 Citation", open=False):
|
| 193 |
-
citation_button = gr.Textbox(
|
| 194 |
-
value=CITATION_BUTTON_TEXT,
|
| 195 |
-
label=CITATION_BUTTON_LABEL,
|
| 196 |
-
lines=20,
|
| 197 |
-
elem_id="citation-button",
|
| 198 |
-
show_copy_button=True,
|
| 199 |
-
)
|
| 200 |
-
|
| 201 |
-
scheduler = BackgroundScheduler()
|
| 202 |
-
scheduler.add_job(restart_space, "interval", seconds=1800)
|
| 203 |
-
scheduler.start()
|
| 204 |
-
demo.queue(default_concurrency_limit=40).launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
import json
|
| 5 |
+
# from huggingface_hub import HfApi, HfFolder
|
| 6 |
+
# from evaluate import evaluate_prediction # importas tu función
|
| 7 |
+
|
| 8 |
+
REFERENCE_FILE_URL = "https://huggingface.co/datasets/juliocontrerash/my-challenge-data/resolve/main/reference.nc"
|
| 9 |
+
LOCAL_REF_PATH = "reference.nc"
|
| 10 |
+
|
| 11 |
+
def download_reference():
|
| 12 |
+
if not os.path.exists(LOCAL_REF_PATH):
|
| 13 |
+
r = requests.get(REFERENCE_FILE_URL)
|
| 14 |
+
with open(LOCAL_REF_PATH, 'wb') as f:
|
| 15 |
+
f.write(r.content)
|
| 16 |
+
|
| 17 |
+
download_reference() # bajamos la referencia al iniciar el Space
|
| 18 |
+
|
| 19 |
+
def evaluate_and_save(pred_file, participant_name):
|
| 20 |
+
"""
|
| 21 |
+
1. Guarda el archivo subido como local
|
| 22 |
+
2. Llama a evaluate_prediction
|
| 23 |
+
3. Registra los resultados en el dataset (opcional)
|
| 24 |
+
4. Retorna alguna visualización / texto
|
| 25 |
+
"""
|
| 26 |
+
if not pred_file:
|
| 27 |
+
return "No file uploaded", None
|
| 28 |
+
|
| 29 |
+
# Guardar local
|
| 30 |
+
pred_path = pred_file.name
|
| 31 |
+
|
| 32 |
+
# Evaluar
|
| 33 |
+
results = evaluate_prediction(pred_path, LOCAL_REF_PATH)
|
| 34 |
+
|
| 35 |
+
# Subir resultados a dataset en HF Hub (opcional)
|
| 36 |
+
# 1. Descarga submissions.jsonl
|
| 37 |
+
# 2. Añade una nueva línea con participant_name, results, time, etc.
|
| 38 |
+
# 3. `git push` o usar huggingface_hub para subir la versión actualizada
|
| 39 |
+
|
| 40 |
+
# Aqui creamos una grafica (opcional)
|
| 41 |
+
# Por ejemplo un plot con MRE_spectrum:
|
| 42 |
+
import matplotlib
|
| 43 |
+
matplotlib.use('Agg')
|
| 44 |
+
import matplotlib.pyplot as plt
|
| 45 |
+
import io
|
| 46 |
+
import base64
|
| 47 |
+
import numpy as np
|
| 48 |
+
|
| 49 |
+
mre_spectrum = results["mre_spectrum"]
|
| 50 |
+
plt.figure(figsize=(6,4))
|
| 51 |
+
plt.plot(np.arange(len(mre_spectrum)), mre_spectrum, label='MRE Spectrum')
|
| 52 |
+
plt.xlabel('Wavelength index')
|
| 53 |
+
plt.ylabel('Error')
|
| 54 |
+
plt.title('Spectral Error')
|
| 55 |
+
plt.legend()
|
| 56 |
+
|
| 57 |
+
buf = io.BytesIO()
|
| 58 |
+
plt.savefig(buf, format='png')
|
| 59 |
+
plt.close()
|
| 60 |
+
buf.seek(0)
|
| 61 |
+
img_str = base64.b64encode(buf.read())
|
| 62 |
+
img_str = "data:image/png;base64," + img_str.decode('utf-8')
|
| 63 |
+
|
| 64 |
+
message = f"Participant: {participant_name}\nMRE mean: {results['mre_mean']:.4f}\nRMSE: {results['rmse']:.4f}"
|
| 65 |
+
return message, img_str
|
| 66 |
+
|
| 67 |
+
with gr.Blocks() as demo:
|
| 68 |
+
gr.Markdown("# My Challenge\nSube tu archivo de predicciones para evaluar tu modelo.")
|
| 69 |
+
participant_name = gr.Textbox(label="Nombre del participante")
|
| 70 |
+
pred_file = gr.File(label="Subir archivo (csv, netcdf, etc.)")
|
| 71 |
+
|
| 72 |
+
output_message = gr.Textbox(label="Resultados")
|
| 73 |
+
output_image = gr.HTML(label="Gráfica")
|
| 74 |
+
|
| 75 |
+
submit_btn = gr.Button("Evaluar")
|
| 76 |
+
|
| 77 |
+
submit_btn.click(fn=evaluate_and_save,
|
| 78 |
+
inputs=[pred_file, participant_name],
|
| 79 |
+
outputs=[output_message, output_image])
|
| 80 |
+
|
| 81 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
evaluate.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import xarray as xr # si usas netCDF
|
| 3 |
+
# o from netCDF4 import Dataset
|
| 4 |
+
# o import csv etc. según tu formato
|
| 5 |
+
|
| 6 |
+
def evaluate_prediction(pred_file_path, reference_file_path):
|
| 7 |
+
"""
|
| 8 |
+
pred_file_path: str - Ruta al archivo subido por el participante
|
| 9 |
+
reference_file_path: str - Ruta a tu ground-truth, local o en la web
|
| 10 |
+
returns: dict - un diccionario con las métricas calculadas
|
| 11 |
+
"""
|
| 12 |
+
# Ejemplo usando netCDF
|
| 13 |
+
pred_data = xr.open_dataset(pred_file_path)
|
| 14 |
+
ref_data = xr.open_dataset(reference_file_path)
|
| 15 |
+
|
| 16 |
+
# Asume que ambos tienen la misma dimensión "wavelength" o algo similar
|
| 17 |
+
pred_values = pred_data["spectrum"].values # shape (n_wavelengths,)
|
| 18 |
+
ref_values = ref_data["spectrum"].values # shape (n_wavelengths,)
|
| 19 |
+
|
| 20 |
+
# Calcular MRE por banda
|
| 21 |
+
mre = np.abs((pred_values - ref_values) / ref_values)
|
| 22 |
+
|
| 23 |
+
# MRE medio
|
| 24 |
+
mre_mean = mre.mean()
|
| 25 |
+
|
| 26 |
+
# Otras métricas
|
| 27 |
+
rmse = np.sqrt(((pred_values - ref_values)**2).mean())
|
| 28 |
+
|
| 29 |
+
# Retornar resultados en un dict
|
| 30 |
+
return {
|
| 31 |
+
"mre_mean": float(mre_mean),
|
| 32 |
+
"rmse": float(rmse),
|
| 33 |
+
"mre_spectrum": mre.tolist(), # El espectro de MRE completo
|
| 34 |
+
}
|