first commit
Browse files
app.py
CHANGED
|
@@ -2,19 +2,22 @@ import pathlib
|
|
| 2 |
import tempfile
|
| 3 |
from typing import BinaryIO, Literal
|
| 4 |
import json
|
| 5 |
-
from datasets import Dataset
|
| 6 |
|
| 7 |
import gradio as gr
|
|
|
|
|
|
|
| 8 |
from evaluation import evaluate_problem
|
| 9 |
from datetime import datetime
|
| 10 |
|
| 11 |
|
| 12 |
PROBLEM_TYPES = ["geometrical", "simple_to_build", "mhd_stable"]
|
|
|
|
| 13 |
|
| 14 |
-
def
|
| 15 |
problem_type: Literal["geometrical", "simple_to_build", "mhd_stable"],
|
| 16 |
boundary_file: BinaryIO,
|
| 17 |
) -> str:
|
|
|
|
| 18 |
file_path = boundary_file.name
|
| 19 |
if not file_path:
|
| 20 |
return "Error: Uploaded file object does not have a valid file path."
|
|
@@ -29,6 +32,24 @@ def evaluate_boundary(
|
|
| 29 |
file_content = f_in.read()
|
| 30 |
tmp.write(file_content)
|
| 31 |
tmp_path = pathlib.Path(tmp.name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
try:
|
| 33 |
result = evaluate_problem(problem_type, str(tmp_path))
|
| 34 |
output = str(result)
|
|
@@ -37,24 +58,8 @@ def evaluate_boundary(
|
|
| 37 |
finally:
|
| 38 |
tmp_path.unlink()
|
| 39 |
|
| 40 |
-
save_to_dataset(problem_type, file_content, timestamp, output)
|
| 41 |
return output
|
| 42 |
|
| 43 |
-
|
| 44 |
-
def save_to_dataset(problem_type, file_content, timestamp, result):
|
| 45 |
-
# Example: convert to one-row dataframe
|
| 46 |
-
record = {
|
| 47 |
-
"submission_time": timestamp,
|
| 48 |
-
"problem_type": problem_type,
|
| 49 |
-
"boundary_json": file_content.decode("utf-8"), # Or store file path or URL
|
| 50 |
-
"result": result,
|
| 51 |
-
}
|
| 52 |
-
|
| 53 |
-
# If this is the first entry, create dataset
|
| 54 |
-
dataset_file = "local_dataset.jsonl"
|
| 55 |
-
with open(dataset_file, "a") as f:
|
| 56 |
-
f.write(json.dumps(record) + "\n")
|
| 57 |
-
|
| 58 |
def gradio_interface() -> gr.Blocks:
|
| 59 |
with gr.Blocks() as demo:
|
| 60 |
gr.Markdown(
|
|
@@ -73,7 +78,7 @@ def gradio_interface() -> gr.Blocks:
|
|
| 73 |
output = gr.Textbox(label="Evaluation Result", lines=10)
|
| 74 |
submit_btn = gr.Button("Evaluate")
|
| 75 |
submit_btn.click(
|
| 76 |
-
|
| 77 |
inputs=[problem_type, boundary_file],
|
| 78 |
outputs=output,
|
| 79 |
)
|
|
|
|
| 2 |
import tempfile
|
| 3 |
from typing import BinaryIO, Literal
|
| 4 |
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 |
|
| 12 |
|
| 13 |
PROBLEM_TYPES = ["geometrical", "simple_to_build", "mhd_stable"]
|
| 14 |
+
repo_id = "cgeorgiaw/constellaration-submissions"
|
| 15 |
|
| 16 |
+
def submit_boundary(
|
| 17 |
problem_type: Literal["geometrical", "simple_to_build", "mhd_stable"],
|
| 18 |
boundary_file: BinaryIO,
|
| 19 |
) -> str:
|
| 20 |
+
|
| 21 |
file_path = boundary_file.name
|
| 22 |
if not file_path:
|
| 23 |
return "Error: Uploaded file object does not have a valid file path."
|
|
|
|
| 32 |
file_content = f_in.read()
|
| 33 |
tmp.write(file_content)
|
| 34 |
tmp_path = pathlib.Path(tmp.name)
|
| 35 |
+
|
| 36 |
+
# write to dataset
|
| 37 |
+
filename = f"data/{timestamp.replace(':', '-')}_{problem_type}.json"
|
| 38 |
+
record = {
|
| 39 |
+
"submission_time": timestamp,
|
| 40 |
+
"problem_type": problem_type,
|
| 41 |
+
"boundary_json": file_content.decode("utf-8"), # Or store file path or URL
|
| 42 |
+
}
|
| 43 |
+
json.dump(record, tmp, indent=2)
|
| 44 |
+
tmp.flush()
|
| 45 |
+
upload_file(
|
| 46 |
+
path_or_fileobj=tmp.name, # this is just a string path
|
| 47 |
+
path_in_repo=filename,
|
| 48 |
+
repo_id=repo_id,
|
| 49 |
+
repo_type="dataset",
|
| 50 |
+
commit_message=f"Add submission for {problem_type} at {timestamp}"
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
try:
|
| 54 |
result = evaluate_problem(problem_type, str(tmp_path))
|
| 55 |
output = str(result)
|
|
|
|
| 58 |
finally:
|
| 59 |
tmp_path.unlink()
|
| 60 |
|
|
|
|
| 61 |
return output
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
def gradio_interface() -> gr.Blocks:
|
| 64 |
with gr.Blocks() as demo:
|
| 65 |
gr.Markdown(
|
|
|
|
| 78 |
output = gr.Textbox(label="Evaluation Result", lines=10)
|
| 79 |
submit_btn = gr.Button("Evaluate")
|
| 80 |
submit_btn.click(
|
| 81 |
+
submit_boundary,
|
| 82 |
inputs=[problem_type, boundary_file],
|
| 83 |
outputs=output,
|
| 84 |
)
|