Spaces:
Runtime error
Runtime error
File size: 6,495 Bytes
0c6d96d c201fa7 fd2910f 0c6d96d 18aba38 0c6d96d c201fa7 0c6d96d c201fa7 0c6d96d e146c10 0c6d96d e146c10 0c6d96d e146c10 0c6d96d c201fa7 e146c10 0c6d96d c201fa7 0c6d96d fd2910f 0c6d96d fd2910f c201fa7 0c6d96d c201fa7 520ebb3 fd2910f e146c10 e442972 0c6d96d e442972 0c6d96d e146c10 0c6d96d e146c10 18aba38 0c6d96d 10aa74f 0c6d96d 10aa74f 0c6d96d c201fa7 0c6d96d c201fa7 |
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
import os
import shutil
from datetime import datetime
from typing import List
import gradio as gr
import pandas as pd
from huggingface_hub import snapshot_download, upload_file
from gradio_leaderboard import Leaderboard
# ํ๋ก์ ํธ ๊ตฌ์กฐ์ ๋ง์ถฐ import (src.*)
from src.envs import RESULTS_REPO, EVAL_RESULTS_PATH
from src.grader import grade # grade(submission_df, team_id) -> (score_df, report_dir)
# ---------------------------
# ํ ๋น๋ฐ๋ฒํธ ๋งคํ (์์ฒญ ๋ฐ์)
# ---------------------------
TEAM_PWD_MAP = {
"02Aug#29112#h": "JN_HACK13",
"02Aug!86113!g": "JN_HACK14",
"02Aug#33114#h": "JN_HACK15",
}
# ์ ์ฅ ํ์ผ๋ช
(๋ฆฌ๋๋ณด๋ ์ง๊ณ)
HISTORY_NAME = "graded_results.csv"
HISTORY_PATH = os.path.join(EVAL_RESULTS_PATH, HISTORY_NAME)
# ---------------------------
# ์ด๊ธฐํ: Space ์ฌ์์ ๋๋น
# ---------------------------
def init_cache() -> pd.DataFrame:
"""
- RESULTS_REPO ์ค๋
์ท์ EVAL_RESULTS_PATH๋ก ๋ค์ด๋ก๋(๋๊ธฐํ)
- graded_results.csv ๋ก๋ (์์ผ๋ฉด ๋น DF ์์ฑ)
"""
os.makedirs(EVAL_RESULTS_PATH, exist_ok=True)
try:
snapshot_download(
repo_id=RESULTS_REPO,
repo_type="dataset",
local_dir=EVAL_RESULTS_PATH,
tqdm_class=None,
etag_timeout=30,
token=os.environ.get("HF_TOKEN"),
)
except Exception as e:
print(f"[WARN] snapshot_download failed: {e}")
if os.path.exists(HISTORY_PATH):
try:
df = pd.read_csv(HISTORY_PATH)
except Exception as e:
print(f"[WARN] failed to read {HISTORY_NAME}: {e}")
df = pd.DataFrame()
else:
df = pd.DataFrame()
# ์ต์ ์คํค๋ง ๋ณด์
for col in ["TEAM", "TIMESTAMP"]:
if col not in df.columns:
df[col] = []
return df
# ์ ์ญ ํ์คํ ๋ฆฌ ์ํ (์ฑ ๋ถํ
์ ๋ก๋)
history_state = init_cache()
def save_and_upload_history(history_df: pd.DataFrame) -> None:
"""
- ๋ก์ปฌ graded_results.csv ์ ์ฅ
- RESULTS_REPO์ ๋ฎ์ด์ฐ๊ธฐ ์
๋ก๋
"""
history_df.to_csv(HISTORY_PATH, index=False)
upload_file(
path_or_fileobj=HISTORY_PATH,
path_in_repo=HISTORY_NAME,
repo_id=RESULTS_REPO,
repo_type="dataset",
token=os.environ.get("HF_TOKEN"),
)
# ---------------------------
# ์ฑ์ ํธ๋ค๋ฌ
# ---------------------------
def grade_csv(passwd: str, file):
global history_state
if file is None:
raise gr.Error("ํ์ผ ์
๋ก๋๊ฐ ๋๋ฝ๋์์ต๋๋ค.")
team_id = TEAM_PWD_MAP.get((passwd or "").strip(), "UNKNOWN")
# if not team_id:
# raise gr.Error("๋น๋ฐ๋ฒํธ๊ฐ ์ฌ๋ฐ๋ฅด์ง ์์ต๋๋ค.")
# ์ ์ถ CSV ๋ก๋
submission_df = pd.read_csv(file.name)
# ์ฑ์ ๋ฐ ๋ฆฌํฌํธ ์์ฑ
score_df, report_dir = grade(submission_df, team_id=team_id)
# ๋ฉํ ์์ฑ ๋ถ์ฌ
ts = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%SZ")
score_df.insert(0, "TEAM", team_id)
score_df.insert(1, "TIMESTAMP", ts)
# ๊ธฐ์กด ํ์คํ ๋ฆฌ ๋ก๋(์์ผ๋ฉด ๋น DF)
if os.path.exists(HISTORY_PATH):
try:
saved_df = pd.read_csv(HISTORY_PATH)
except Exception:
saved_df = pd.DataFrame()
else:
saved_df = pd.DataFrame()
# ์ต์ ์คํค๋ง ํต์ผ
for col in set(["TEAM", "TIMESTAMP"]) - set(saved_df.columns):
saved_df[col] = []
# ํ์คํ ๋ฆฌ append
merged_score = pd.concat([saved_df, score_df], ignore_index=True)
history_state = merged_score.copy() # ์ ์ญ ์ํ ๊ฐฑ์
# ๋ก์ปฌ ์ ์ฅ + Hub ์
๋ก๋
save_and_upload_history(history_state)
# ๋ฆฌํฌํธ ZIP ์์ฑ
report_zip = f"report_{team_id}.zip"
shutil.make_archive(f"report_{team_id}", "zip", report_dir)
# ๊ฐค๋ฌ๋ฆฌ ์ด๋ฏธ์ง ํ์ผ ๋ชฉ๋ก
image_files: List[str] = [
os.path.join(report_dir, f)
for f in os.listdir(report_dir)
if f.lower().endswith(".png")
]
# UI ๋ฐํ: ์ ์ํ, ZIP, ๊ฐค๋ฌ๋ฆฌ, ๋ฆฌ๋๋ณด๋ ๊ฐฑ์ ๋ณธ
return score_df, report_zip, image_files, history_state
def refresh_leaderboard():
"""์ฌ์ฉ์ ์์ฒญ ์ Hub ์ฌ๋๊ธฐํ ํ ๋ฆฌ๋๋ณด๋ ๊ฐฑ์ ."""
global history_state
history_state = init_cache()
return history_state
# ---------------------------
# UI
# ---------------------------
with gr.Blocks() as demo:
gr.Markdown("## Hackathon with <Caramella Inc.,>")
with gr.Tabs():
with gr.Tab("ํ๊ฐ ์์ฒญ"):
with gr.Row():
with gr.Column():
csv_input = gr.File(label="CSV ์
๋ก๋", file_types=[".csv"])
password = gr.Textbox(label="ํ ๋น๋ฐ๋ฒํธ", type="password", placeholder="ํ๋ณ๋ก ๊ณต์ง๋ ๋น๋ฐ๋ฒํธ")
submit_button = gr.Button("ํ๊ฐ ์์ฒญ", variant="primary")
df_output = gr.Dataframe(label="ํ๊ฐ ์งํ ๊ฒฐ๊ณผ")
with gr.Column():
report_output = gr.File(label="๋ฆฌํฌํธ ZIP ๋ค์ด๋ก๋", height="100px")
image_gallery = gr.Gallery(
label="Plant๋ณ ๋น๊ต ๊ทธ๋ํ",
show_label=True,
height="auto"
)
with gr.Tab("๋ฆฌ๋๋ณด๋"):
leaderboard_table = Leaderboard(
value=history_state.rename(columns={"TOTAL": "TOTAL โฌ๏ธ"}),
search_columns=["TEAM", "RMSE_AC", "RMSE_AC_SCALED", "NMAE_RANGE", "NMAE_MEAN", "TOTAL โฌ๏ธ"],
hide_columns=[], # ์จ๊ธธ ๊ฒ ์์ผ๋ฉด ์ฌ๊ธฐ์ ์ถ๊ฐ
filter_columns=["TEAM"],
label="Leaderboard",
)
with gr.Row():
refresh_btn = gr.Button("๋ฆฌ๋๋ณด๋ ์๋ก๊ณ ์นจ (Hub ๋๊ธฐํ)")
# ์๋ก๊ณ ์นจ: Hub โ ๋ก์ปฌ ์ฌ๋๊ธฐํ ํ ํ
์ด๋ธ ๊ฐฑ์
refresh_btn.click(
fn=refresh_leaderboard,
inputs=None,
outputs=leaderboard_table
)
# ์
๋ก๋ โ ์ฑ์ ์คํ
submit_button.click(
fn=grade_csv,
inputs=[password, csv_input],
outputs=[df_output, report_output, image_gallery, leaderboard_table], # ๋ง์ง๋ง์ ๋ฆฌ๋๋ณด๋ ํญ์ ํ
์ด๋ธ์ ์ฐ๊ฒฐ
queue=True
)
if __name__ == "__main__":
demo.launch()
# demo.launch(debug=True, show_error=True, enable_monitoring=True)
|