VeenaLim commited on
Commit
e2ccc75
·
verified ·
1 Parent(s): 180f41c

Added root endpoint (/) with API info

Browse files
Files changed (1) hide show
  1. plumber_hf.R +97 -80
plumber_hf.R CHANGED
@@ -1,80 +1,97 @@
1
- library(dampack)
2
- library(readr)
3
- library(assertthat)
4
-
5
- #* @apiTitle DARTH Health Economic Model API
6
- #*
7
- #* @apiDescription This API runs a health economic Markov model (Sick-Sicker)
8
- #* for probabilistic sensitivity analysis. Hosted on Hugging Face Spaces.
9
-
10
- #* Health check endpoint
11
- #* @get /health
12
- function() {
13
- list(status = "healthy", timestamp = Sys.time())
14
- }
15
-
16
- #* Run the DARTH model
17
- #* @serializer csv
18
- #* @param param_updates gives the replacement values of the editable parameters (JSON string)
19
- #* @post /runDARTHmodel
20
- function(param_updates = NULL) {
21
-
22
- # Source the model functions from local file or GitHub
23
- model_functions_path <- "/app/model/darth_funcs.R"
24
- if (file.exists(model_functions_path)) {
25
- source(model_functions_path)
26
- } else {
27
- # Fallback to GitHub if local file not found
28
- source(paste0("https://raw.githubusercontent.com/",
29
- "BresMed/plumberHE/main/R/darth_funcs.R"))
30
- }
31
-
32
- # Read in the CSV containing parameter inputs
33
- psa_inputs_path <- "/app/parameter_distributions.csv"
34
- if (!file.exists(psa_inputs_path)) {
35
- psa_inputs_path <- "parameter_distributions.csv"
36
- }
37
- psa_inputs <- as.data.frame(readr::read_csv(psa_inputs_path))
38
-
39
- # Parse param_updates if provided
40
- if (!is.null(param_updates) && param_updates != "") {
41
- # Handle both JSON string and data frame input
42
- if (is.character(param_updates)) {
43
- param_updates <- jsonlite::fromJSON(param_updates)
44
- }
45
-
46
- # Convert to data frame if needed
47
- if (!is.data.frame(param_updates)) {
48
- param_updates <- as.data.frame(param_updates)
49
- }
50
-
51
- # For each row of the data-frame containing the variables to be changed...
52
- for(n in 1:nrow(param_updates)) {
53
- # Update parameters from API input
54
- psa_inputs <- overwrite_parameter_value(
55
- existing_df = psa_inputs,
56
- parameter = param_updates[n, "parameter"],
57
- distribution = param_updates[n, "distribution"],
58
- v1 = param_updates[n, "v1"],
59
- v2 = param_updates[n, "v2"]
60
- )
61
- }
62
- }
63
-
64
- # Run the model using the single run-model function
65
- results <- run_model(psa_inputs)
66
-
67
- # Check that the model results being returned are the correct dimensions
68
- assertthat::assert_that(
69
- all(dim(x = results) == c(1000, 6)),
70
- class(results) == "data.frame",
71
- msg = "Dimensions or type of data are incorrect, please check the model code."
72
- )
73
-
74
- # Check that no sensitive data is included in the output
75
- assertthat::assert_that(
76
- all(psa_inputs[, 1] %in% as.character(unlist(x = results, recursive = TRUE)) == FALSE)
77
- )
78
-
79
- return(results)
80
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ library(dampack)
2
+ library(readr)
3
+ library(assertthat)
4
+ library(jsonlite)
5
+
6
+ #* @apiTitle DARTH Health Economic Model API
7
+ #*
8
+ #* @apiDescription This API runs a health economic Markov model (Sick-Sicker)
9
+ #* for probabilistic sensitivity analysis. Hosted on Hugging Face Spaces.
10
+
11
+ #* Welcome page - API info
12
+ #* @get /
13
+ function() {
14
+ list(
15
+ name = "DARTH Health Economic Model API",
16
+ version = "1.0",
17
+ status = "running",
18
+ endpoints = list(
19
+ "GET /" = "This welcome page",
20
+ "GET /health" = "Health check",
21
+ "POST /runDARTHmodel" = "Run the DARTH model with custom parameters"
22
+ )
23
+ )
24
+ }
25
+
26
+
27
+ #* Health check endpoint
28
+ #* @get /health
29
+ function() {
30
+ list(status = "healthy", timestamp = Sys.time())
31
+ }
32
+
33
+ #* Run the DARTH model
34
+ #* @serializer csv
35
+ #* @param param_updates gives the replacement values of the editable parameters (JSON string)
36
+ #* @post /runDARTHmodel
37
+ function(param_updates = NULL) {
38
+
39
+ # Source the model functions from local file or GitHub
40
+ model_functions_path <- "/app/model/darth_funcs.R"
41
+ if (file.exists(model_functions_path)) {
42
+ source(model_functions_path)
43
+ } else {
44
+ # Fallback to GitHub if local file not found
45
+ source(paste0("https://raw.githubusercontent.com/",
46
+ "BresMed/plumberHE/main/R/darth_funcs.R"))
47
+ }
48
+
49
+ # Read in the CSV containing parameter inputs
50
+ psa_inputs_path <- "/app/parameter_distributions.csv"
51
+ if (!file.exists(psa_inputs_path)) {
52
+ psa_inputs_path <- "parameter_distributions.csv"
53
+ }
54
+ psa_inputs <- as.data.frame(readr::read_csv(psa_inputs_path))
55
+
56
+ # Parse param_updates if provided
57
+ if (!is.null(param_updates) && param_updates != "") {
58
+ # Handle both JSON string and data frame input
59
+ if (is.character(param_updates)) {
60
+ param_updates <- jsonlite::fromJSON(param_updates)
61
+ }
62
+
63
+ # Convert to data frame if needed
64
+ if (!is.data.frame(param_updates)) {
65
+ param_updates <- as.data.frame(param_updates)
66
+ }
67
+
68
+ # For each row of the data-frame containing the variables to be changed...
69
+ for(n in 1:nrow(param_updates)) {
70
+ # Update parameters from API input
71
+ psa_inputs <- overwrite_parameter_value(
72
+ existing_df = psa_inputs,
73
+ parameter = param_updates[n, "parameter"],
74
+ distribution = param_updates[n, "distribution"],
75
+ v1 = param_updates[n, "v1"],
76
+ v2 = param_updates[n, "v2"]
77
+ )
78
+ }
79
+ }
80
+
81
+ # Run the model using the single run-model function
82
+ results <- run_model(psa_inputs)
83
+
84
+ # Check that the model results being returned are the correct dimensions
85
+ assertthat::assert_that(
86
+ all(dim(x = results) == c(1000, 6)),
87
+ class(results) == "data.frame",
88
+ msg = "Dimensions or type of data are incorrect, please check the model code."
89
+ )
90
+
91
+ # Check that no sensitive data is included in the output
92
+ assertthat::assert_that(
93
+ all(psa_inputs[, 1] %in% as.character(unlist(x = results, recursive = TRUE)) == FALSE)
94
+ )
95
+
96
+ return(results)
97
+ }