VeenaLim commited on
Commit
e947df3
·
verified ·
1 Parent(s): bc108da

Upload plumber_hf.R

Browse files
Files changed (1) hide show
  1. plumber_hf.R +106 -0
plumber_hf.R ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ psa_inputs <- jsonlite::fromJSON(psa_inputs_json)
57
+ psa_inputs <- as.data.frame(psa_inputs)
58
+
59
+ # Validate required columns exist
60
+ required_cols <- c("parameter", "distribution", "v1", "v2")
61
+ if (!all(required_cols %in% names(psa_inputs))) {
62
+ stop(paste("Error: CSV must contain columns:", paste(required_cols, collapse = ", ")))
63
+ }
64
+
65
+ # Parse param_updates if provided (for additional overrides)
66
+ if (!is.null(param_updates) && param_updates != "") {
67
+ # Handle both JSON string and data frame input
68
+ if (is.character(param_updates)) {
69
+ param_updates <- jsonlite::fromJSON(param_updates)
70
+ }
71
+
72
+ # Convert to data frame if needed
73
+ if (!is.data.frame(param_updates)) {
74
+ param_updates <- as.data.frame(param_updates)
75
+ }
76
+
77
+ # For each row of the data-frame containing the variables to be changed...
78
+ for(n in 1:nrow(param_updates)) {
79
+ # Update parameters from API input
80
+ psa_inputs <- overwrite_parameter_value(
81
+ existing_df = psa_inputs,
82
+ parameter = param_updates[n, "parameter"],
83
+ distribution = param_updates[n, "distribution"],
84
+ v1 = param_updates[n, "v1"],
85
+ v2 = param_updates[n, "v2"]
86
+ )
87
+ }
88
+ }
89
+
90
+ # Run the model using the single run-model function
91
+ results <- run_model(psa_inputs)
92
+
93
+ # Check that the model results being returned are the correct dimensions
94
+ assertthat::assert_that(
95
+ all(dim(x = results) == c(1000, 6)),
96
+ class(results) == "data.frame",
97
+ msg = "Dimensions or type of data are incorrect, please check the model code."
98
+ )
99
+
100
+ # Check that no sensitive data is included in the output
101
+ assertthat::assert_that(
102
+ all(psa_inputs[, 1] %in% as.character(unlist(x = results, recursive = TRUE)) == FALSE)
103
+ )
104
+
105
+ return(results)
106
+ }