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)