Spaces:
Runtime error
Runtime error
some more organization
Browse files
app.R
CHANGED
@@ -44,14 +44,14 @@ server <- function(input, output, session) {
|
|
44 |
# Observer used at app startup time to allow using the 'Send' button once the
|
45 |
# model has been loaded.
|
46 |
observeEvent(loading$reload, ignoreInit = FALSE, ignoreNULL = FALSE, priority = 0, {
|
47 |
-
|
48 |
# the model is already loaded, we just make sure that we propagate this
|
49 |
# by setting generating to FALSE
|
50 |
if (!is.null(sess$is_loaded) && sess$is_loaded) {
|
51 |
context$generating <- FALSE
|
52 |
return()
|
53 |
}
|
54 |
-
|
55 |
# the model isn't loaded and no task is trying to load it, so we start a new
|
56 |
# task to load it
|
57 |
if (is.null(sess$is_loaded)) {
|
@@ -62,7 +62,7 @@ server <- function(input, output, session) {
|
|
62 |
# the model is loading, but this is handled by another session. We should
|
63 |
# come back to this observer later to enable the send button once model
|
64 |
# is loaded.
|
65 |
-
invalidateLater(
|
66 |
return()
|
67 |
}
|
68 |
|
@@ -71,6 +71,7 @@ server <- function(input, output, session) {
|
|
71 |
m <- loading$model %>%
|
72 |
promises::then(
|
73 |
onFulfilled = function(x) {
|
|
|
74 |
cat("Model has been loaded!", "\n")
|
75 |
context$generating <- FALSE
|
76 |
sess$is_loaded <- TRUE
|
@@ -146,22 +147,37 @@ server <- function(input, output, session) {
|
|
146 |
# this controls the state of the send button.
|
147 |
# if generating is TRUE we want it to be disabled, otherwise it's enabled
|
148 |
# if generating is `NULL`, then the model is not yet loaded
|
149 |
-
|
150 |
-
|
151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
insertUI(
|
153 |
-
"#sendButton",
|
154 |
-
ui =
|
155 |
-
"send",
|
156 |
-
icon = btn$icon,
|
157 |
-
width = "100%",
|
158 |
-
label = btn$label,
|
159 |
-
class = btn$class
|
160 |
-
),
|
161 |
immediate = TRUE
|
162 |
)
|
163 |
-
|
164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
list(class = "btn-secondary disabled", label = "Generating error ...")
|
166 |
} else if (context$generating) {
|
167 |
list(class = "btn-secondary disabled", label = "Generating response ...",
|
@@ -178,16 +194,8 @@ server <- function(input, output, session) {
|
|
178 |
|
179 |
observeEvent(context$messages, priority = 10, {
|
180 |
# this observer generates and updates the messages list
|
181 |
-
msg_cards <- context$messages %>%
|
182 |
-
lapply(function(msg) {
|
183 |
-
emoji <- if (msg$role == "user") "π€" else "π€"
|
184 |
-
card(style="margin-bottom:5px;", card_body(
|
185 |
-
p(paste0(emoji, ":", msg$content))
|
186 |
-
))
|
187 |
-
})
|
188 |
-
|
189 |
output$messages <- renderUI({
|
190 |
-
|
191 |
})
|
192 |
})
|
193 |
|
@@ -245,4 +253,25 @@ server <- function(input, output, session) {
|
|
245 |
})
|
246 |
}
|
247 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
248 |
shinyApp(ui, server)
|
|
|
44 |
# Observer used at app startup time to allow using the 'Send' button once the
|
45 |
# model has been loaded.
|
46 |
observeEvent(loading$reload, ignoreInit = FALSE, ignoreNULL = FALSE, priority = 0, {
|
47 |
+
|
48 |
# the model is already loaded, we just make sure that we propagate this
|
49 |
# by setting generating to FALSE
|
50 |
if (!is.null(sess$is_loaded) && sess$is_loaded) {
|
51 |
context$generating <- FALSE
|
52 |
return()
|
53 |
}
|
54 |
+
|
55 |
# the model isn't loaded and no task is trying to load it, so we start a new
|
56 |
# task to load it
|
57 |
if (is.null(sess$is_loaded)) {
|
|
|
62 |
# the model is loading, but this is handled by another session. We should
|
63 |
# come back to this observer later to enable the send button once model
|
64 |
# is loaded.
|
65 |
+
invalidateLater(100, session)
|
66 |
return()
|
67 |
}
|
68 |
|
|
|
71 |
m <- loading$model %>%
|
72 |
promises::then(
|
73 |
onFulfilled = function(x) {
|
74 |
+
Sys.sleep(5)
|
75 |
cat("Model has been loaded!", "\n")
|
76 |
context$generating <- FALSE
|
77 |
sess$is_loaded <- TRUE
|
|
|
147 |
# this controls the state of the send button.
|
148 |
# if generating is TRUE we want it to be disabled, otherwise it's enabled
|
149 |
# if generating is `NULL`, then the model is not yet loaded
|
150 |
+
|
151 |
+
# in the first startup we have to insert the send button directly, otherwise
|
152 |
+
# it would exist after all observers ran, including the one that loads the
|
153 |
+
# model
|
154 |
+
if (context$generating == "loading") {
|
155 |
+
btn <- list(
|
156 |
+
class = "btn-secondary disabled",
|
157 |
+
label = "Loading model ...",
|
158 |
+
icon = icon("spinner", class="fa-spin")
|
159 |
+
)
|
160 |
insertUI(
|
161 |
+
"#sendButton",
|
162 |
+
ui = ui_send(btn),
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
immediate = TRUE
|
164 |
)
|
165 |
+
|
166 |
+
msgs <- list(list(
|
167 |
+
role = "info",
|
168 |
+
content = "Model is loading. It might take some time."
|
169 |
+
))
|
170 |
+
insertUI(
|
171 |
+
"#messages",
|
172 |
+
ui = ui_messages(msgs),
|
173 |
+
immediate = TRUE
|
174 |
+
)
|
175 |
+
|
176 |
+
return()
|
177 |
+
}
|
178 |
+
|
179 |
+
# now we can use the loop for everyone
|
180 |
+
btn <- if (context$generating == "error") {
|
181 |
list(class = "btn-secondary disabled", label = "Generating error ...")
|
182 |
} else if (context$generating) {
|
183 |
list(class = "btn-secondary disabled", label = "Generating response ...",
|
|
|
194 |
|
195 |
observeEvent(context$messages, priority = 10, {
|
196 |
# this observer generates and updates the messages list
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
197 |
output$messages <- renderUI({
|
198 |
+
ui_messages(context$messages)
|
199 |
})
|
200 |
})
|
201 |
|
|
|
253 |
})
|
254 |
}
|
255 |
|
256 |
+
ui_messages <- function(messages) {
|
257 |
+
emojis <- c(user = "π€", assistant = "π€", info = "π£", error = "π")
|
258 |
+
msg_cards <- messages %>%
|
259 |
+
lapply(function(msg) {
|
260 |
+
card(style="margin-bottom:5px;", card_body(
|
261 |
+
p(paste0(emojis[msg$role], ": ", msg$content))
|
262 |
+
))
|
263 |
+
})
|
264 |
+
rlang::exec(card_body, !!!msg_cards, gap = 5, fillable = FALSE)
|
265 |
+
}
|
266 |
+
|
267 |
+
ui_send <- function(btn) {
|
268 |
+
actionButton(
|
269 |
+
"send",
|
270 |
+
icon = btn$icon,
|
271 |
+
width = "100%",
|
272 |
+
label = btn$label,
|
273 |
+
class = btn$class
|
274 |
+
)
|
275 |
+
}
|
276 |
+
|
277 |
shinyApp(ui, server)
|