import pathlib from pathlib import Path import tempfile from typing import BinaryIO, Literal import json import pandas as pd import gradio as gr from datasets import load_dataset from gradio_leaderboard import ColumnFilter, Leaderboard, SelectColumns from evaluation import evaluate_problem from datetime import datetime import os from submit import submit_boundary from about import PROBLEM_TYPES, TOKEN, CACHE_PATH, API, submissions_repo, results_repo from utils import read_boundary, write_results, get_user from visualize import make_visual def evaluate_boundary(filename): print(filename) local_path = read_boundary(filename) with Path(local_path).open("r") as f: raw = f.read() data_dict = json.loads(raw) result = evaluate_problem(data_dict['problem_type'], local_path) write_results(data_dict, result) return def make_clickable(name): link =f'https://huggingface.co/{name}' return f'{name}' def get_leaderboard(): ds = load_dataset(results_repo, split='train') df = pd.DataFrame(ds) df.rename(columns={'submission_time': 'submission time', 'problem_type': 'problem type'}, inplace=True) # df['user'] = df['user'].apply(lambda x: make_clickable(x)).astype(str) score_field = "score" if "score" in df.columns else "objective" # fallback df = df.sort_values(by=score_field, ascending=True) return df def show_output_box(message): return gr.update(value=message, visible=True) def gradio_interface() -> gr.Blocks: with gr.Blocks() as demo: gr.Markdown("## Welcome to the ConStellaration Boundary Leaderboard!") with gr.Tabs(elem_classes="tab-buttons"): with gr.TabItem("Leaderboard", elem_id="boundary-benchmark-tab-table"): gr.Markdown("# Boundary Design Leaderboard") Leaderboard( value=get_leaderboard(), datatype=['str', 'date', 'str', 'str', 'bool', 'markdown', 'number', 'bool', 'number', 'number', 'str'], select_columns=["submission time", "feasibility", "score", "problem type", "user"], search_columns=["submission time", "score", "user"], hide_columns=["result_filename", "submission_filename", "objective", "minimize_objective", "boundary_json", "evaluated"], filter_columns=["problem type"], every=60, render=True ) with gr.TabItem("About", elem_id="boundary-benchmark-tab-table"): gr.Markdown( """ ## About This Challenge **Welcome to the ConStellaration Leaderboard**, a community-driven effort to accelerate fusion energy research using machine learning. In collaboration with [Proxima Fusion](https://www.proximafusion.com/), we're inviting the ML and physics communities to optimize plasma configurations for stellarators—a class of fusion reactors that offer steady-state operation and strong stability advantages over tokamaks. This leaderboard tracks submissions to a series of open benchmark tasks focused on: - **Geometrically optimized stellarators** - **Simple-to-build quasi-isodynamic (QI) stellarators** - **Multi-objective, MHD-stable QI stellarators** Participants are encouraged to build surrogate models, optimize plasma boundaries, and explore differentiable design pipelines that could replace or accelerate slow traditional solvers like VMEC++. ### Why It Matters Fusion promises clean, abundant, zero-carbon energy. But designing stellarators is computationally intense and geometrically complex. With open datasets, reference baselines, and your contributions, we can reimagine this process as fast, iterative, and ML-native. ### How to Participate - Clone the [ConStellaration dataset](https://huggingface.co/datasets/proxima-fusion/constellaration) - Build or train your model on the provided QI equilibria - Submit your predicted boundaries and results here to benchmark against others - Join the discussion and help expand the frontier of fusion optimization Let's bring fusion down to Earth—together. """ ) # dropdown = gr.Dropdown(choices=filenames, label="Choose a file") # plot_output = gr.Plot() with gr.TabItem("Submit", elem_id="boundary-benchmark-tab-table"): gr.Markdown( """ # Plasma Boundary Evaluation Submission Upload your plasma boundary JSON and select the problem type to get your score. """ ) user_state = gr.State(value=None) filename = gr.State(value=None) eval_state = gr.State(value=None) gr.LoginButton() demo.load(get_user, inputs=None, outputs=user_state) with gr.Row(): problem_type = gr.Dropdown(PROBLEM_TYPES, label="Problem Type") boundary_file = gr.File(label="Boundary JSON File (.json)") boundary_file submit_btn = gr.Button("Evaluate") message = gr.Textbox(label="Status", lines=1, visible=False) submit_btn.click( submit_boundary, inputs=[problem_type, boundary_file, user_state], outputs=[message, filename], ).then( fn=show_output_box, inputs=[message], outputs=[message], ).then( fn=evaluate_boundary, inputs=[filename], outputs=[eval_state] ) return demo if __name__ == "__main__": gradio_interface().launch()