Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web UI - stage1, readonly part #606

Open
wants to merge 47 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
040d446
added fastapi as web ui backend
VukW Jul 11, 2024
97e6bd7
Added cube + benchmark basic listing
VukW Jul 12, 2024
0382684
Adds navigation
VukW Jul 15, 2024
55fe60e
Aded mlcube detailed page
VukW Jul 19, 2024
fb1bca3
Improved mlcubes detailed layout
VukW Jul 19, 2024
64cf53e
Improved mlcube layout
VukW Jul 19, 2024
36611e1
yaml displaying
VukW Jul 19, 2024
56fa5c4
yaml: spinner
VukW Jul 19, 2024
8563887
yaml panel improvement
VukW Jul 19, 2024
07ce4ab
yaml panel layout improvement
VukW Jul 19, 2024
b260401
layout fixes
VukW Jul 19, 2024
b7980a8
Added benchmark detailed page
VukW Jul 19, 2024
ca356cc
added links to mlcube
VukW Jul 19, 2024
6efd724
benchmark page: added owner
VukW Jul 19, 2024
319b1bf
Colors refactoring
VukW Jul 19, 2024
58008f3
Dataset detailed page
VukW Jul 23, 2024
375d89e
Forgot to add js file
VukW Jul 23, 2024
c6d8a56
Unified data format for all data fields automatically
VukW Jul 23, 2024
74f7743
(mlcube-detailed) Display image tarball and additional files always
VukW Jul 24, 2024
b312882
Fixed scrolling and reinvented basic page layout
VukW Jul 24, 2024
0e282cb
Fix navbar is hiding
VukW Jul 24, 2024
6b28ebb
Make templates & static files independent of user's workdir
VukW Jul 29, 2024
881b281
Added error handling
VukW Jul 29, 2024
e28107b
Display invalid entities correctly
VukW Jul 30, 2024
5b718eb
Added invalid entities highlighting + badges
VukW Jul 30, 2024
0f95027
Added benchmark associations
VukW Aug 5, 2024
444786e
Improved association panel style
VukW Aug 5, 2024
e273577
Added association card
VukW Aug 6, 2024
eea1e77
Sorted associations by status / timestamp
VukW Aug 6, 2024
7b68911
Sorted mlcubes and datasets: mine first
VukW Aug 6, 2024
8251c42
Added associations to dataset page
VukW Aug 7, 2024
b669358
Added associations to mlcube page
VukW Aug 7, 2024
039f496
Refactored details page - extracted common styles to the base template
VukW Aug 10, 2024
c225a5e
Refactored association sorting to common util
VukW Aug 10, 2024
ad0451f
Display my benchmarks first
VukW Aug 10, 2024
12ffef2
Hid empty links
VukW Aug 12, 2024
cedad96
Mlcube-as-a-link unified view
VukW Aug 12, 2024
3ac8a74
resources.path cannot return a dir with subdirs for py3.9
VukW Aug 13, 2024
6170b53
Fixed resources path for templates also
VukW Aug 14, 2024
53b557b
linter fix
VukW Aug 14, 2024
2b73c4f
static local resources instead of remote ones
VukW Aug 26, 2024
75d6776
layout fix: align mlcubes vertically
VukW Aug 27, 2024
c47a751
bugfix: add some dependencies for isolated run
VukW Aug 27, 2024
d837837
Merge branch 'main' into web-ui
VukW Aug 27, 2024
c58efd8
Fixes after merging main
VukW Aug 28, 2024
8e73e54
MedperfSchema requires a name field
VukW Sep 17, 2024
a78ef8d
Linter fix
VukW Sep 17, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cli/medperf/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import medperf.commands.association.association as association
import medperf.commands.compatibility_test.compatibility_test as compatibility_test
import medperf.commands.storage as storage
import medperf.web_ui.app as web_ui
from medperf.utils import check_for_updates
from medperf.logging.utils import log_machine_details

Expand All @@ -30,6 +31,7 @@
app.add_typer(compatibility_test.app, name="test", help="Manage compatibility tests")
app.add_typer(auth.app, name="auth", help="Authentication")
app.add_typer(storage.app, name="storage", help="Storage management")
app.add_typer(web_ui.app, name="web-ui", help="local web UI to manage medperf entities")


@app.command("run")
Expand Down
2 changes: 2 additions & 0 deletions cli/medperf/commands/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def run(
"""Lists all local datasets

Args:
entity_class: entity class to instantiate (Dataset, Model, etc.)
fields (list[str]): list of fields to display
unregistered (bool, optional): Display only local unregistered results. Defaults to False.
mine_only (bool, optional): Display all registered current-user results. Defaults to False.
kwargs (dict): Additional parameters for filtering entity lists.
Expand Down
16 changes: 16 additions & 0 deletions cli/medperf/entities/association.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from datetime import datetime
from typing import Optional

from medperf.entities.schemas import ApprovableSchema, MedperfSchema


class Association(MedperfSchema, ApprovableSchema):
id: int
metadata: dict
dataset: Optional[int]
model_mlcube: Optional[int]
benchmark: int
initiated_by: int
created_at: Optional[datetime]
modified_at: Optional[datetime]
name: str = "Association" # The server data doesn't have name, while MedperfSchema requires it
32 changes: 31 additions & 1 deletion cli/medperf/entities/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pydantic import HttpUrl, Field

import medperf.config as config
from medperf.entities.association import Association
from medperf.entities.interface import Entity
from medperf.entities.schemas import ApprovableSchema, DeployableSchema
from medperf.account_management import get_medperf_user_data
Expand Down Expand Up @@ -83,7 +84,6 @@ def get_models_uids(cls, benchmark_uid: int) -> List[int]:

Args:
benchmark_uid (int): UID of the benchmark.
comms (Comms): Instance of the communications interface.

Returns:
List[int]: List of mlcube uids
Expand All @@ -96,6 +96,36 @@ def get_models_uids(cls, benchmark_uid: int) -> List[int]:
]
return models_uids

@classmethod
def get_models_associations(cls, benchmark_uid: int) -> List[Association]:
"""Retrieves the list of model associations to the benchmark

Args:
benchmark_uid (int): UID of the benchmark.

Returns:
List[Association]: List of associations
"""
associations = config.comms.get_cubes_associations()
associations = [Association(**assoc) for assoc in associations]
associations = [a for a in associations if a.benchmark == benchmark_uid]
return associations

@classmethod
def get_datasets_associations(cls, benchmark_uid: int) -> List[Association]:
"""Retrieves the list of models associated to the benchmark

Args:
benchmark_uid (int): UID of the benchmark.

Returns:
List[Association]: List of associations
"""
associations = config.comms.get_datasets_associations()
associations = [Association(**assoc) for assoc in associations]
associations = [a for a in associations if a.benchmark == benchmark_uid]
return associations

def display_dict(self):
return {
"UID": self.identifier,
Expand Down
42 changes: 30 additions & 12 deletions cli/medperf/entities/cube.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import os
import yaml
import logging
from typing import Dict, Optional, Union
from typing import Dict, Optional, Union, List
from pydantic import Field
from pathlib import Path

from medperf.entities.association import Association
from medperf.utils import (
combine_proc_sp_text,
log_storage,
Expand Down Expand Up @@ -96,19 +97,20 @@ def remote_prefilter(filters: dict):
return comms_fn

@classmethod
def get(cls, cube_uid: Union[str, int], local_only: bool = False) -> "Cube":
def get(cls, cube_uid: Union[str, int], local_only: bool = False, valid_only: bool = True) -> "Cube":
"""Retrieves and creates a Cube instance from the comms. If cube already exists
inside the user's computer then retrieves it from there.

Args:
valid_only: if to raise an error in case of invalidated Cube
cube_uid (str): UID of the cube.

Returns:
Cube : a Cube instance with the retrieved data.
"""

cube = super().get(cube_uid, local_only)
if not cube.is_valid:
if not cube.is_valid and valid_only:
raise InvalidEntityError("The requested MLCube is marked as INVALID.")
cube.download_config_files()
return cube
Expand Down Expand Up @@ -152,7 +154,7 @@ def download_image(self):
# For singularity, we need the hash first before trying to convert
self._set_image_hash_from_registry()

image_folder = os.path.join(config.cubes_folder, config.image_path)
image_folder: str = os.path.join(config.cubes_folder, config.image_path)
if os.path.exists(image_folder):
for file in os.listdir(image_folder):
if file == self._converted_singularity_image_name:
Expand Down Expand Up @@ -198,7 +200,7 @@ def _get_image_from_registry(self):
cmd += f" -Psingularity.image={self._converted_singularity_image_name}"
logging.info(f"Running MLCube command: {cmd}")
with spawn_and_kill(
cmd, timeout=config.mlcube_configure_timeout
cmd, timeout=config.mlcube_configure_timeout
) as proc_wrapper:
proc = proc_wrapper.proc
combine_proc_sp_text(proc)
Expand Down Expand Up @@ -228,13 +230,13 @@ def download_run_files(self):
raise InvalidEntityError(f"MLCube {self.name} image file: {e}")

def run(
self,
task: str,
output_logs: str = None,
string_params: Dict[str, str] = {},
timeout: int = None,
read_protected_input: bool = True,
**kwargs,
self,
task: str,
output_logs: str = None,
string_params: Dict[str, str] = {},
timeout: int = None,
read_protected_input: bool = True,
**kwargs,
):
"""Executes a given task on the cube instance

Expand Down Expand Up @@ -358,6 +360,22 @@ def get_config(self, identifier):

return cube

@classmethod
def get_benchmarks_associations(cls, mlcube_uid: int) -> List[Association]:
"""Retrieves the list of benchmarks model is associated with

Args:
mlcube_uid (int): UID of the cube.
comms (Comms): Instance of the communications interface.

Returns:
List[Association]: List of associations
"""
associations = config.comms.get_cubes_associations()
associations = [Association(**assoc) for assoc in associations]
associations = [a for a in associations if a.model_mlcube == mlcube_uid]
return associations

def display_dict(self):
return {
"UID": self.identifier,
Expand Down
18 changes: 16 additions & 2 deletions cli/medperf/entities/dataset.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import os
import yaml
from pydantic import Field, validator
from typing import Optional, Union
from typing import Optional, Union, List

from medperf.entities.association import Association
from medperf.utils import remove_path
from medperf.entities.interface import Entity
from medperf.entities.schemas import DeployableSchema
Expand Down Expand Up @@ -112,14 +113,27 @@ def remote_prefilter(filters: dict) -> callable:
comms_fn = config.comms.get_user_datasets

if "mlcube" in filters and filters["mlcube"] is not None:

def func():
return config.comms.get_mlcube_datasets(filters["mlcube"])

comms_fn = func

return comms_fn

@classmethod
def get_benchmarks_associations(cls, dataset_uid: int) -> List[Association]:
"""Retrieves the list of benchmarks dataset is associated with

Args:
dataset_uid (int): UID of the dataset.
Returns:
List[Association]: List of associations
"""
associations = config.comms.get_datasets_associations()
associations = [Association(**assoc) for assoc in associations]
associations = [a for a in associations if a.dataset == dataset_uid]
return associations

def display_dict(self):
return {
"UID": self.identifier,
Expand Down
5 changes: 3 additions & 2 deletions cli/medperf/entities/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

@classmethod
def all(
cls: Type[EntityType], unregistered: bool = False, filters: dict = {}
cls: Type[EntityType], unregistered: bool = False, filters: dict = {}
) -> List[EntityType]:
"""Gets a list of all instances of the respective entity.
Whether the list is local or remote depends on the implementation.
Expand Down Expand Up @@ -112,7 +112,7 @@

@classmethod
def get(
cls: Type[EntityType], uid: Union[str, int], local_only: bool = False
cls: Type[EntityType], uid: Union[str, int], local_only: bool = False, valid_only: bool = True
) -> EntityType:
"""Gets an instance of the respective entity.
Wether this requires only local read or remote calls depends
Expand All @@ -121,6 +121,7 @@
Args:
uid (str): Unique Identifier to retrieve the entity
local_only (bool): If True, the entity will be retrieved locally
valid_only: if to raise en error in case of invalidated entity

Returns:
Entity: Entity Instance associated to the UID
Expand Down Expand Up @@ -176,11 +177,11 @@
storage_path = cls.get_storage_path()
metadata_filename = cls.get_metadata_filename()
entity_file = os.path.join(storage_path, str(uid), metadata_filename)
if not os.path.exists(entity_file):

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
raise InvalidArgumentError(
f"No {cls.get_type()} with the given uid could be found"
)
with open(entity_file, "r") as f:

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
data = yaml.safe_load(f)

return data
Expand Down
5 changes: 3 additions & 2 deletions cli/medperf/entities/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,16 @@ def all(cls, unregistered: bool = False, filters: dict = {}) -> List["TestReport
return super().all(unregistered=True, filters={})

@classmethod
def get(cls, uid: str, local_only: bool = False) -> "TestReport":
def get(cls, uid: str, local_only: bool = False, valid_only: bool = True) -> "TestReport":
"""Gets an instance of the TestReport. ignores local_only inherited flag as TestReport is always a local entity.
Args:
uid (str): Report Unique Identifier
local_only (bool): ignored. Left for aligning with parent Entity class
valid_only: if to raise an error in case of invalidated entity
Returns:
TestReport: Report Instance associated to the UID
"""
return super().get(uid, local_only=True)
return super().get(uid, local_only=True, valid_only=valid_only)

def display_dict(self):
if self.data_path:
Expand Down
Empty file.
50 changes: 50 additions & 0 deletions cli/medperf/web_ui/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from importlib import resources
from pathlib import Path

import typer
from fastapi import FastAPI
from fastapi.responses import RedirectResponse
from fastapi.staticfiles import StaticFiles

from medperf import config
from medperf.decorators import clean_except
from medperf.web_ui.common import custom_exception_handler
from medperf.web_ui.datasets.routes import router as datasets_router
from medperf.web_ui.benchmarks.routes import router as benchmarks_router
from medperf.web_ui.mlcubes.routes import router as mlcubes_router
from medperf.web_ui.yaml_fetch.routes import router as yaml_fetch_router

web_app = FastAPI()

web_app.include_router(datasets_router, prefix="/datasets")
web_app.include_router(benchmarks_router, prefix="/benchmarks")
web_app.include_router(mlcubes_router, prefix="/mlcubes")
web_app.include_router(yaml_fetch_router)

static_folder_path = Path(resources.files("medperf.web_ui")) / "static" # noqa
web_app.mount(
"/static",
StaticFiles(
directory=static_folder_path,
)
)

web_app.add_exception_handler(Exception, custom_exception_handler)


@web_app.get("/", include_in_schema=False)
def read_root():
return RedirectResponse(url="/benchmarks/ui")


app = typer.Typer()


@app.command("run")
@clean_except
def run(
port: int = typer.Option(8100, "--port", help="port to use"),
):
"""Runs a local web UI"""
import uvicorn
uvicorn.run(web_app, host="127.0.0.1", port=port, log_level=config.loglevel)
Empty file.
65 changes: 65 additions & 0 deletions cli/medperf/web_ui/benchmarks/routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import logging

from fastapi import APIRouter
from fastapi.responses import HTMLResponse
from fastapi import Request

from medperf.entities.benchmark import Benchmark
from medperf.entities.dataset import Dataset
from medperf.entities.cube import Cube
from medperf.account_management import get_medperf_user_data
from medperf.web_ui.common import templates, sort_associations_display

router = APIRouter()
logger = logging.getLogger(__name__)


@router.get("/ui", response_class=HTMLResponse)
def benchmarks_ui(request: Request, mine_only: bool = False):
filters = {}
my_user_id = get_medperf_user_data()["id"]
if mine_only:
filters["owner"] = my_user_id

benchmarks = Benchmark.all(
filters=filters,
)

benchmarks = sorted(benchmarks, key=lambda x: x.created_at, reverse=True)
# sort by (mine recent) (mine oldish), (other recent), (other oldish)
mine_benchmarks = [d for d in benchmarks if d.owner == my_user_id]
other_benchmarks = [d for d in benchmarks if d.owner != my_user_id]
benchmarks = mine_benchmarks + other_benchmarks
return templates.TemplateResponse("benchmarks.html", {"request": request, "benchmarks": benchmarks})


@router.get("/ui/{benchmark_id}", response_class=HTMLResponse)
def benchmark_detail_ui(request: Request, benchmark_id: int):
benchmark = Benchmark.get(benchmark_id)
data_preparation_mlcube = Cube.get(cube_uid=benchmark.data_preparation_mlcube)
reference_model_mlcube = Cube.get(cube_uid=benchmark.reference_model_mlcube)
metrics_mlcube = Cube.get(cube_uid=benchmark.data_evaluator_mlcube)
datasets_associations = Benchmark.get_datasets_associations(benchmark_uid=benchmark_id)
models_associations = Benchmark.get_models_associations(benchmark_uid=benchmark_id)

datasets_associations = sort_associations_display(datasets_associations)
models_associations = sort_associations_display(models_associations)

datasets = {assoc.dataset: Dataset.get(assoc.dataset) for assoc in datasets_associations if assoc.dataset}
models = {assoc.model_mlcube: Cube.get(assoc.model_mlcube) for assoc in models_associations if assoc.model_mlcube}

return templates.TemplateResponse(
"benchmark_detail.html",
{
"request": request,
"entity": benchmark,
"entity_name": benchmark.name,
"data_preparation_mlcube": data_preparation_mlcube,
"reference_model_mlcube": reference_model_mlcube,
"metrics_mlcube": metrics_mlcube,
"datasets_associations": datasets_associations,
"models_associations": models_associations,
"datasets": datasets,
"models": models
}
)
Loading
Loading