File size: 2,497 Bytes
281711d |
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 |
import pathlib
import tempfile
from typing import BinaryIO, Literal
import json
from datasets import Dataset
import gradio as gr
from evaluation import evaluate_problem
from datetime import datetime
PROBLEM_TYPES = ["geometrical", "simple_to_build", "mhd_stable"]
def evaluate_boundary(
problem_type: Literal["geometrical", "simple_to_build", "mhd_stable"],
boundary_file: BinaryIO,
) -> str:
file_path = boundary_file.name
if not file_path:
return "Error: Uploaded file object does not have a valid file path."
path_obj = pathlib.Path(file_path)
timestamp = datetime.utcnow().isoformat()
with (
path_obj.open("rb") as f_in,
tempfile.NamedTemporaryFile(delete=False, suffix=".json") as tmp,
):
file_content = f_in.read()
tmp.write(file_content)
tmp_path = pathlib.Path(tmp.name)
try:
result = evaluate_problem(problem_type, str(tmp_path))
output = str(result)
except Exception as e:
output = f"Error during evaluation:\n{e}"
finally:
tmp_path.unlink()
save_to_dataset(problem_type, file_content, timestamp, output)
return output
def save_to_dataset(problem_type, file_content, timestamp, result):
# Example: convert to one-row dataframe
record = {
"submission_time": timestamp,
"problem_type": problem_type,
"boundary_json": file_content.decode("utf-8"), # Or store file path or URL
"result": result,
}
# If this is the first entry, create dataset
dataset_file = "local_dataset.jsonl"
with open(dataset_file, "a") as f:
f.write(json.dumps(record) + "\n")
def gradio_interface() -> gr.Blocks:
with gr.Blocks() as demo:
gr.Markdown(
"""
# Plasma Boundary Evaluation App
Upload your plasma boundary JSON and select the problem type to get your score.
"""
)
with gr.Row():
problem_type = gr.Dropdown(
PROBLEM_TYPES, label="Problem Type", value="geometrical"
)
boundary_file = gr.File(label="Boundary JSON File (.json)")
boundary_file
output = gr.Textbox(label="Evaluation Result", lines=10)
submit_btn = gr.Button("Evaluate")
submit_btn.click(
evaluate_boundary,
inputs=[problem_type, boundary_file],
outputs=output,
)
return demo
if __name__ == "__main__":
gradio_interface().launch()
|