Spaces:
Runtime error
Runtime error
maintain a session specific idinstead of global
Browse files- app.R +41 -15
- promise-session.R +3 -0
app.R
CHANGED
@@ -36,6 +36,7 @@ ui <- page_fillable(
|
|
36 |
server <- function(input, output, session) {
|
37 |
prompt <- reactiveVal(value = system_prompt)
|
38 |
n_tokens <- reactiveVal(value = 0)
|
|
|
39 |
|
40 |
observeEvent(input$send, {
|
41 |
if (is.null(input$prompt) || input$prompt == "") {
|
@@ -43,7 +44,7 @@ server <- function(input, output, session) {
|
|
43 |
}
|
44 |
shinyjs::disable("send")
|
45 |
updateActionButton(inputId = "send", label = "Waiting for model...")
|
46 |
-
insert_message(as.character(glue::glue("π€: {input$prompt}")))
|
47 |
|
48 |
# we modify the prompt to trigger the 'next_token' reactive
|
49 |
prompt(paste0(prompt(), "<|USER|>", input$prompt, "<|ASSISTANT|>"))
|
@@ -55,7 +56,7 @@ server <- function(input, output, session) {
|
|
55 |
promises::then(
|
56 |
onFulfilled = function(x) {x},
|
57 |
onRejected = function(x) {
|
58 |
-
insert_message(paste0("π Error generating token.", as.character(x)))
|
59 |
updateActionButton(inputId = "send", label = "Failing generation. Contact admin.")
|
60 |
NULL
|
61 |
}
|
@@ -68,9 +69,9 @@ server <- function(input, output, session) {
|
|
68 |
n_tokens(n_tokens() + 1)
|
69 |
tok %>% promises::then(function(tok) {
|
70 |
if (n_tokens() == 1) {
|
71 |
-
insert_message(paste0("π€: ", tok), append = FALSE)
|
72 |
} else {
|
73 |
-
insert_message(tok, append = TRUE)
|
74 |
}
|
75 |
|
76 |
if (tok != "" && n_tokens() < max_n_tokens) {
|
@@ -92,50 +93,75 @@ server <- function(input, output, session) {
|
|
92 |
|
93 |
# Observer used at app startup time to allow using the 'Send' button once the
|
94 |
# model has been loaded.
|
95 |
-
|
|
|
|
|
|
|
|
|
96 |
if (!is.null(sess$is_loaded) && sess$is_loaded) return()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
if (is.null(sess$is_loaded)) {
|
98 |
cat("Started loading model ....", "\n")
|
99 |
-
model_loaded
|
100 |
sess$is_loaded <- FALSE # not yet loaded, but loading
|
101 |
}
|
102 |
|
|
|
|
|
|
|
|
|
103 |
cat("Loading model:",sess$sess$poll_process(), "\n")
|
104 |
-
|
105 |
-
model_loaded <- model_loaded %>%
|
106 |
promises::then(onFulfilled = function(x) {
|
|
|
107 |
shinyjs::enable("send")
|
108 |
updateActionButton(inputId = "send", label = "Send")
|
109 |
sess$is_loaded <- TRUE
|
|
|
110 |
}, onRejected = function(x) {
|
111 |
shinyjs::disable("send")
|
112 |
insert_message(paste0("π Error loading the model:\n", as.character(x)))
|
113 |
sess$is_loaded <- NULL # means failure!
|
114 |
sess$sess <- NULL
|
|
|
|
|
|
|
|
|
|
|
115 |
})
|
116 |
-
|
117 |
-
NULL # we return NULL so we don't stuck waiting for the above.
|
118 |
})
|
119 |
}
|
120 |
|
121 |
-
|
122 |
-
insert_message <- function(msg, append = FALSE) {
|
123 |
if (!append) {
|
124 |
-
id <- message_id
|
|
|
|
|
125 |
insertUI(
|
126 |
"#messages",
|
127 |
"beforeEnd",
|
128 |
immediate = TRUE,
|
129 |
-
ui = card(
|
|
|
|
|
130 |
)
|
131 |
} else {
|
132 |
-
id <- message_id
|
133 |
shinyjs::runjs(glue::glue(
|
134 |
"document.getElementById('msg-{id}').textContent += '{msg}'"
|
135 |
))
|
136 |
}
|
137 |
# scroll to bottom
|
138 |
shinyjs::runjs("var elem = document.getElementById('messages'); elem.scrollTop = elem.scrollHeight;")
|
|
|
139 |
}
|
140 |
|
141 |
|
|
|
36 |
server <- function(input, output, session) {
|
37 |
prompt <- reactiveVal(value = system_prompt)
|
38 |
n_tokens <- reactiveVal(value = 0)
|
39 |
+
msg_id <- reactiveVal(value = 0)
|
40 |
|
41 |
observeEvent(input$send, {
|
42 |
if (is.null(input$prompt) || input$prompt == "") {
|
|
|
44 |
}
|
45 |
shinyjs::disable("send")
|
46 |
updateActionButton(inputId = "send", label = "Waiting for model...")
|
47 |
+
insert_message(msg_id, as.character(glue::glue("π€: {input$prompt}")))
|
48 |
|
49 |
# we modify the prompt to trigger the 'next_token' reactive
|
50 |
prompt(paste0(prompt(), "<|USER|>", input$prompt, "<|ASSISTANT|>"))
|
|
|
56 |
promises::then(
|
57 |
onFulfilled = function(x) {x},
|
58 |
onRejected = function(x) {
|
59 |
+
insert_message(msg_id, paste0("π Error generating token.", as.character(x)))
|
60 |
updateActionButton(inputId = "send", label = "Failing generation. Contact admin.")
|
61 |
NULL
|
62 |
}
|
|
|
69 |
n_tokens(n_tokens() + 1)
|
70 |
tok %>% promises::then(function(tok) {
|
71 |
if (n_tokens() == 1) {
|
72 |
+
insert_message(msg_id, paste0("π€: ", tok), append = FALSE)
|
73 |
} else {
|
74 |
+
insert_message(msg_id, tok, append = TRUE)
|
75 |
}
|
76 |
|
77 |
if (tok != "" && n_tokens() < max_n_tokens) {
|
|
|
93 |
|
94 |
# Observer used at app startup time to allow using the 'Send' button once the
|
95 |
# model has been loaded.
|
96 |
+
model_loaded <- reactiveVal()
|
97 |
+
event_reload <- reactiveVal(val = 0)
|
98 |
+
observeEvent(event_reload(), ignoreNULL=FALSE, {
|
99 |
+
|
100 |
+
# the model is already loaded, nothing to do
|
101 |
if (!is.null(sess$is_loaded) && sess$is_loaded) return()
|
102 |
+
|
103 |
+
# the model isn't loaded, this we disable the send button and
|
104 |
+
# show that we are loading the model
|
105 |
+
shinyjs::disable("send")
|
106 |
+
updateActionButton(inputId = "send", label = "Loading the model...")
|
107 |
+
|
108 |
+
# the model isn't loaded and no task is trying to load it, so we start a new
|
109 |
+
# task to load it
|
110 |
if (is.null(sess$is_loaded)) {
|
111 |
cat("Started loading model ....", "\n")
|
112 |
+
model_loaded(sess$load_model(repo))
|
113 |
sess$is_loaded <- FALSE # not yet loaded, but loading
|
114 |
}
|
115 |
|
116 |
+
# this runs for the cases where sess$is_loaded was NULL or if
|
117 |
+
# sess$is_loaded = FALSE ie (another connection already tried)
|
118 |
+
# to load the model.
|
119 |
+
|
120 |
cat("Loading model:",sess$sess$poll_process(), "\n")
|
121 |
+
m <- model_loaded() %>%
|
|
|
122 |
promises::then(onFulfilled = function(x) {
|
123 |
+
cat("Model has been loaded!", "\n")
|
124 |
shinyjs::enable("send")
|
125 |
updateActionButton(inputId = "send", label = "Send")
|
126 |
sess$is_loaded <- TRUE
|
127 |
+
TRUE
|
128 |
}, onRejected = function(x) {
|
129 |
shinyjs::disable("send")
|
130 |
insert_message(paste0("π Error loading the model:\n", as.character(x)))
|
131 |
sess$is_loaded <- NULL # means failure!
|
132 |
sess$sess <- NULL
|
133 |
+
if (event_reload() < 10) {
|
134 |
+
Sys.sleep(5)
|
135 |
+
event_reload(event_reload() + 1)
|
136 |
+
}
|
137 |
+
FALSE
|
138 |
})
|
139 |
+
model_loaded(m)
|
|
|
140 |
})
|
141 |
}
|
142 |
|
143 |
+
insert_message <- function(message_id, msg, append = FALSE) {
|
|
|
144 |
if (!append) {
|
145 |
+
id <- message_id() + 1
|
146 |
+
message_id(id)
|
147 |
+
|
148 |
insertUI(
|
149 |
"#messages",
|
150 |
"beforeEnd",
|
151 |
immediate = TRUE,
|
152 |
+
ui = card(style="margin-bottom:5px;", card_body(
|
153 |
+
p(id = paste0("msg-",id), msg)
|
154 |
+
))
|
155 |
)
|
156 |
} else {
|
157 |
+
id <- message_id()
|
158 |
shinyjs::runjs(glue::glue(
|
159 |
"document.getElementById('msg-{id}').textContent += '{msg}'"
|
160 |
))
|
161 |
}
|
162 |
# scroll to bottom
|
163 |
shinyjs::runjs("var elem = document.getElementById('messages'); elem.scrollTop = elem.scrollHeight;")
|
164 |
+
id
|
165 |
}
|
166 |
|
167 |
|
promise-session.R
CHANGED
@@ -36,6 +36,7 @@ promise_session <- R6::R6Class(
|
|
36 |
self$sess$call(task$func, args = task$args)
|
37 |
},
|
38 |
resolve_task = function() {
|
|
|
39 |
out <- self$sess$read()
|
40 |
if (!is.null(out$error)) {
|
41 |
self$tasks[[1]]$reject(out$error)
|
@@ -44,6 +45,8 @@ promise_session <- R6::R6Class(
|
|
44 |
}
|
45 |
|
46 |
self$tasks <- self$tasks[-1]
|
|
|
|
|
47 |
self$is_running <- FALSE
|
48 |
|
49 |
self$run_task()
|
|
|
36 |
self$sess$call(task$func, args = task$args)
|
37 |
},
|
38 |
resolve_task = function() {
|
39 |
+
cat("Resolving task! ")
|
40 |
out <- self$sess$read()
|
41 |
if (!is.null(out$error)) {
|
42 |
self$tasks[[1]]$reject(out$error)
|
|
|
45 |
}
|
46 |
|
47 |
self$tasks <- self$tasks[-1]
|
48 |
+
cat("now we have ", length(self$tasks), "on queue\n")
|
49 |
+
|
50 |
self$is_running <- FALSE
|
51 |
|
52 |
self$run_task()
|