VeenaLim commited on
Commit
56b634e
·
verified ·
1 Parent(s): 5993f2e

Upload plumber_hf.R

Browse files
Files changed (1) hide show
  1. plumber_hf.R +117 -0
plumber_hf.R ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ #* Users send their own parameter CSV data from their local machine.
11
+
12
+ #* Welcome page - API info
13
+ #* @get /
14
+ function() {
15
+ list(
16
+ name = "DARTH Health Economic Model API",
17
+ version = "1.0",
18
+ status = "running",
19
+ endpoints = list(
20
+ "GET /" = "This welcome page",
21
+ "GET /health" = "Health check",
22
+ "POST /runDARTHmodel" = "Run the DARTH model with your CSV data"
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 psa_inputs_json JSON string containing the full parameter distributions data from client's local CSV
36
+ #* @param param_updates JSON string with optional parameter overrides (applied after loading psa_inputs)
37
+ #* @post /runDARTHmodel
38
+ function(psa_inputs_json = NULL, param_updates = NULL) {
39
+
40
+ # Source the model functions from local file or GitHub
41
+ model_functions_path <- "/app/model/darth_funcs.R"
42
+ if (file.exists(model_functions_path)) {
43
+ source(model_functions_path)
44
+ } else {
45
+ # Fallback to GitHub if local file not found
46
+ source(paste0("https://raw.githubusercontent.com/",
47
+ "BresMed/plumberHE/main/R/darth_funcs.R"))
48
+ }
49
+
50
+ # Parse the PSA inputs from client's local CSV (sent as JSON)
51
+ if (is.null(psa_inputs_json) || psa_inputs_json == "") {
52
+ stop("Error: psa_inputs_json is required. Please send your local CSV data.")
53
+ }
54
+
55
+ # Convert JSON string to data frame
56
+ tryCatch({
57
+ psa_inputs <- jsonlite::fromJSON(psa_inputs_json)
58
+ psa_inputs <- as.data.frame(psa_inputs)
59
+ }, error = function(e) {
60
+ stop(paste("Error parsing JSON:", e$message))
61
+ })
62
+
63
+ # Validate required columns exist
64
+ required_cols <- c("parameter", "distribution", "v1", "v2")
65
+ if (!all(required_cols %in% names(psa_inputs))) {
66
+ stop(paste("Error: CSV must contain columns:", paste(required_cols, collapse = ", ")))
67
+ }
68
+
69
+ # Ensure v1 and v2 are numeric (handle NULL/NA values)
70
+ psa_inputs$v1 <- as.numeric(psa_inputs$v1)
71
+ psa_inputs$v2 <- as.numeric(psa_inputs$v2)
72
+
73
+ # Replace NA with 0 for v2 (needed for 'fixed' distributions)
74
+ psa_inputs$v2[is.na(psa_inputs$v2)] <- 0
75
+
76
+ # Parse param_updates if provided (for additional overrides)
77
+ if (!is.null(param_updates) && param_updates != "") {
78
+ # Handle both JSON string and data frame input
79
+ if (is.character(param_updates)) {
80
+ param_updates <- jsonlite::fromJSON(param_updates)
81
+ }
82
+
83
+ # Convert to data frame if needed
84
+ if (!is.data.frame(param_updates)) {
85
+ param_updates <- as.data.frame(param_updates)
86
+ }
87
+
88
+ # For each row of the data-frame containing the variables to be changed...
89
+ for(n in 1:nrow(param_updates)) {
90
+ # Update parameters from API input
91
+ psa_inputs <- overwrite_parameter_value(
92
+ existing_df = psa_inputs,
93
+ parameter = param_updates[n, "parameter"],
94
+ distribution = param_updates[n, "distribution"],
95
+ v1 = param_updates[n, "v1"],
96
+ v2 = param_updates[n, "v2"]
97
+ )
98
+ }
99
+ }
100
+
101
+ # Run the model using the single run-model function
102
+ results <- run_model(psa_inputs)
103
+
104
+ # Check that the model results being returned are the correct dimensions
105
+ assertthat::assert_that(
106
+ all(dim(x = results) == c(1000, 6)),
107
+ class(results) == "data.frame",
108
+ msg = "Dimensions or type of data are incorrect, please check the model code."
109
+ )
110
+
111
+ # Check that no sensitive data is included in the output
112
+ assertthat::assert_that(
113
+ all(psa_inputs[, 1] %in% as.character(unlist(x = results, recursive = TRUE)) == FALSE)
114
+ )
115
+
116
+ return(results)
117
+ }