trying to write to dataset
Browse files- app.py +17 -5
- evaluation.py +2 -5
app.py
CHANGED
|
@@ -5,7 +5,7 @@ import json
|
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
from datasets import load_dataset, Dataset
|
| 8 |
-
from huggingface_hub import upload_file
|
| 9 |
from evaluation import evaluate_problem
|
| 10 |
from datetime import datetime
|
| 11 |
import os
|
|
@@ -14,7 +14,7 @@ from huggingface_hub import HfApi
|
|
| 14 |
PROBLEM_TYPES = ["geometrical", "simple_to_build", "mhd_stable"]
|
| 15 |
TOKEN = os.environ.get("HF_TOKEN")
|
| 16 |
API = HfApi(token=TOKEN)
|
| 17 |
-
|
| 18 |
|
| 19 |
def submit_boundary(
|
| 20 |
problem_type: Literal["geometrical", "simple_to_build", "mhd_stable"],
|
|
@@ -41,7 +41,8 @@ def submit_boundary(
|
|
| 41 |
record = {
|
| 42 |
"submission_time": timestamp,
|
| 43 |
"problem_type": problem_type,
|
| 44 |
-
"boundary_json": file_content.decode("utf-8"),
|
|
|
|
| 45 |
}
|
| 46 |
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as tmp:
|
| 47 |
json.dump(record, tmp, indent=2)
|
|
@@ -51,15 +52,18 @@ def submit_boundary(
|
|
| 51 |
API.upload_file(
|
| 52 |
path_or_fileobj=tmp_name,
|
| 53 |
path_in_repo=filename,
|
| 54 |
-
repo_id=
|
| 55 |
repo_type="dataset",
|
| 56 |
commit_message=f"Add submission for {problem_type} at {timestamp}"
|
| 57 |
)
|
| 58 |
pathlib.Path(tmp_name).unlink()
|
| 59 |
|
| 60 |
# then do eval
|
|
|
|
|
|
|
| 61 |
try:
|
| 62 |
-
result = evaluate_problem(problem_type, str(tmp_boundary_path))
|
|
|
|
| 63 |
output = str(result)
|
| 64 |
except Exception as e:
|
| 65 |
output = f"Error during evaluation:\n{e}"
|
|
@@ -68,6 +72,14 @@ def submit_boundary(
|
|
| 68 |
|
| 69 |
return output
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
def gradio_interface() -> gr.Blocks:
|
| 72 |
with gr.Blocks() as demo:
|
| 73 |
gr.Markdown(
|
|
|
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
from datasets import load_dataset, Dataset
|
| 8 |
+
from huggingface_hub import upload_file, hf_hub_download
|
| 9 |
from evaluation import evaluate_problem
|
| 10 |
from datetime import datetime
|
| 11 |
import os
|
|
|
|
| 14 |
PROBLEM_TYPES = ["geometrical", "simple_to_build", "mhd_stable"]
|
| 15 |
TOKEN = os.environ.get("HF_TOKEN")
|
| 16 |
API = HfApi(token=TOKEN)
|
| 17 |
+
submissions_repo = "cgeorgiaw/constellaration-submissions"
|
| 18 |
|
| 19 |
def submit_boundary(
|
| 20 |
problem_type: Literal["geometrical", "simple_to_build", "mhd_stable"],
|
|
|
|
| 41 |
record = {
|
| 42 |
"submission_time": timestamp,
|
| 43 |
"problem_type": problem_type,
|
| 44 |
+
"boundary_json": file_content, # .decode("utf-8"),
|
| 45 |
+
"evaluated": False,
|
| 46 |
}
|
| 47 |
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as tmp:
|
| 48 |
json.dump(record, tmp, indent=2)
|
|
|
|
| 52 |
API.upload_file(
|
| 53 |
path_or_fileobj=tmp_name,
|
| 54 |
path_in_repo=filename,
|
| 55 |
+
repo_id=submissions_repo,
|
| 56 |
repo_type="dataset",
|
| 57 |
commit_message=f"Add submission for {problem_type} at {timestamp}"
|
| 58 |
)
|
| 59 |
pathlib.Path(tmp_name).unlink()
|
| 60 |
|
| 61 |
# then do eval
|
| 62 |
+
local_path = read_boundary(filename)
|
| 63 |
+
|
| 64 |
try:
|
| 65 |
+
# result = evaluate_problem(problem_type, str(tmp_boundary_path))
|
| 66 |
+
result = evaluate_problem(problem_type, local_path)
|
| 67 |
output = str(result)
|
| 68 |
except Exception as e:
|
| 69 |
output = f"Error during evaluation:\n{e}"
|
|
|
|
| 72 |
|
| 73 |
return output
|
| 74 |
|
| 75 |
+
def read_boundary(filename):
|
| 76 |
+
local_path = hf_hub_download(
|
| 77 |
+
repo_id=submissions_repo,
|
| 78 |
+
repo_type="dataset",
|
| 79 |
+
filename=filename,
|
| 80 |
+
)
|
| 81 |
+
return local_path
|
| 82 |
+
|
| 83 |
def gradio_interface() -> gr.Blocks:
|
| 84 |
with gr.Blocks() as demo:
|
| 85 |
gr.Markdown(
|
evaluation.py
CHANGED
|
@@ -6,18 +6,15 @@ from constellaration.geometry import surface_rz_fourier
|
|
| 6 |
|
| 7 |
PROBLEM_TYPES = ["geometrical", "simple_to_build", "mhd_stable"]
|
| 8 |
|
| 9 |
-
|
| 10 |
def load_boundary(data: str) -> surface_rz_fourier.SurfaceRZFourier:
|
| 11 |
return surface_rz_fourier.SurfaceRZFourier.model_validate_json(data)
|
| 12 |
|
| 13 |
-
|
| 14 |
-
def load_boubdaries(data: str) -> list[surface_rz_fourier.SurfaceRZFourier]:
|
| 15 |
data_json = json.loads(data)
|
| 16 |
return [
|
| 17 |
surface_rz_fourier.SurfaceRZFourier.model_validate_json(b) for b in data_json
|
| 18 |
]
|
| 19 |
|
| 20 |
-
|
| 21 |
def evaluate_problem(
|
| 22 |
problem_type: str, input_file: str
|
| 23 |
) -> problems.EvaluationSingleObjective | problems.EvaluationMultiObjective:
|
|
@@ -32,7 +29,7 @@ def evaluate_problem(
|
|
| 32 |
boundary = load_boundary(data)
|
| 33 |
result = problems.SimpleToBuildQIStellarator().evaluate(boundary)
|
| 34 |
case "mhd_stable":
|
| 35 |
-
boundaries =
|
| 36 |
result = problems.MHDStableQIStellarator().evaluate(boundaries)
|
| 37 |
case _:
|
| 38 |
raise ValueError(f"Unknown problem type: {problem_type}")
|
|
|
|
| 6 |
|
| 7 |
PROBLEM_TYPES = ["geometrical", "simple_to_build", "mhd_stable"]
|
| 8 |
|
|
|
|
| 9 |
def load_boundary(data: str) -> surface_rz_fourier.SurfaceRZFourier:
|
| 10 |
return surface_rz_fourier.SurfaceRZFourier.model_validate_json(data)
|
| 11 |
|
| 12 |
+
def load_boundaries(data: str) -> list[surface_rz_fourier.SurfaceRZFourier]:
|
|
|
|
| 13 |
data_json = json.loads(data)
|
| 14 |
return [
|
| 15 |
surface_rz_fourier.SurfaceRZFourier.model_validate_json(b) for b in data_json
|
| 16 |
]
|
| 17 |
|
|
|
|
| 18 |
def evaluate_problem(
|
| 19 |
problem_type: str, input_file: str
|
| 20 |
) -> problems.EvaluationSingleObjective | problems.EvaluationMultiObjective:
|
|
|
|
| 29 |
boundary = load_boundary(data)
|
| 30 |
result = problems.SimpleToBuildQIStellarator().evaluate(boundary)
|
| 31 |
case "mhd_stable":
|
| 32 |
+
boundaries = load_boundaries(data)
|
| 33 |
result = problems.MHDStableQIStellarator().evaluate(boundaries)
|
| 34 |
case _:
|
| 35 |
raise ValueError(f"Unknown problem type: {problem_type}")
|