Spaces:
Runtime error
Runtime error
| 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) | |