VeenaLim commited on
Commit
7439186
·
verified ·
1 Parent(s): 668ed09

Upload plumber_hf.R

Browse files
Files changed (1) hide show
  1. plumber_hf.R +96 -0
plumber_hf.R ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ #* Health check endpoint
27
+ #* @get /health
28
+ function() {
29
+ list(status = "healthy", timestamp = Sys.time())
30
+ }
31
+
32
+ #* Run the DARTH model
33
+ #* @serializer csv
34
+ #* @param param_updates gives the replacement values of the editable parameters (JSON string)
35
+ #* @post /runDARTHmodel
36
+ function(param_updates = NULL) {
37
+
38
+ # Source the model functions from local file or GitHub
39
+ model_functions_path <- "/app/model/darth_funcs.R"
40
+ if (file.exists(model_functions_path)) {
41
+ source(model_functions_path)
42
+ } else {
43
+ # Fallback to GitHub if local file not found
44
+ source(paste0("https://raw.githubusercontent.com/",
45
+ "BresMed/plumberHE/main/R/darth_funcs.R"))
46
+ }
47
+
48
+ # Read in the CSV containing parameter inputs
49
+ psa_inputs_path <- "/app/parameter_distributions.csv"
50
+ if (!file.exists(psa_inputs_path)) {
51
+ psa_inputs_path <- "parameter_distributions.csv"
52
+ }
53
+ psa_inputs <- as.data.frame(readr::read_csv(psa_inputs_path))
54
+
55
+ # Parse param_updates if provided
56
+ if (!is.null(param_updates) && param_updates != "") {
57
+ # Handle both JSON string and data frame input
58
+ if (is.character(param_updates)) {
59
+ param_updates <- jsonlite::fromJSON(param_updates)
60
+ }
61
+
62
+ # Convert to data frame if needed
63
+ if (!is.data.frame(param_updates)) {
64
+ param_updates <- as.data.frame(param_updates)
65
+ }
66
+
67
+ # For each row of the data-frame containing the variables to be changed...
68
+ for(n in 1:nrow(param_updates)) {
69
+ # Update parameters from API input
70
+ psa_inputs <- overwrite_parameter_value(
71
+ existing_df = psa_inputs,
72
+ parameter = param_updates[n, "parameter"],
73
+ distribution = param_updates[n, "distribution"],
74
+ v1 = param_updates[n, "v1"],
75
+ v2 = param_updates[n, "v2"]
76
+ )
77
+ }
78
+ }
79
+
80
+ # Run the model using the single run-model function
81
+ results <- run_model(psa_inputs)
82
+
83
+ # Check that the model results being returned are the correct dimensions
84
+ assertthat::assert_that(
85
+ all(dim(x = results) == c(1000, 6)),
86
+ class(results) == "data.frame",
87
+ msg = "Dimensions or type of data are incorrect, please check the model code."
88
+ )
89
+
90
+ # Check that no sensitive data is included in the output
91
+ assertthat::assert_that(
92
+ all(psa_inputs[, 1] %in% as.character(unlist(x = results, recursive = TRUE)) == FALSE)
93
+ )
94
+
95
+ return(results)
96
+ }