Last active
September 25, 2022 14:42
-
-
Save PeerChristensen/cc8706abe5f24543dc7afc7d2585635f to your computer and use it in GitHub Desktop.
Code for a simple Shiny that exposes a transformer model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(shiny) | |
library(reticulate) | |
Sys.setenv(RETICULATE_PYTHON="/your-path/miniconda3/envs/my_env/bin/python") | |
reticulate::use_condaenv("my_env") | |
reticulate::py_run_file("python_functions.py") | |
model <- py$get_model() | |
ui <- fluidPage( | |
titlePanel("My very basic emotion classification app"), | |
sidebarLayout( | |
sidebarPanel( | |
textAreaInput("text", "Input", placeholder = "Type some text here.."), | |
actionButton("submit", "Submit") | |
), | |
mainPanel( | |
plotOutput("plot") | |
) | |
) | |
) | |
server <- function(input, output) { | |
observeEvent(input$submit, { | |
predictions <- py$get_predictions(input$text,model) | |
df <- map_df(predictions[[1]], unlist) | |
output$plot <- renderPlot({ | |
df %>% | |
mutate(score = as.numeric(score)) %>% | |
ggplot(aes(reorder(label, score), score)) + | |
geom_col(fill="steelblue") + | |
coord_flip() + | |
theme_minimal(base_size = 24) + | |
theme(axis.title = element_blank()) | |
}) | |
}) | |
} | |
# Run the application | |
shinyApp(ui = ui, server = server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment