Geoeasy commited on
Commit
c8e229a
·
verified ·
1 Parent(s): e120349

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +42 -48
app.R CHANGED
@@ -1,58 +1,52 @@
1
  library(shiny)
2
- library(bslib)
3
- library(dplyr)
4
- library(ggplot2)
5
 
6
- df <- readr::read_csv("penguins.csv")
7
- # Find subset of columns that are suitable for scatter plot
8
- df_num <- df |> select(where(is.numeric), -Year)
9
-
10
- ui <- page_sidebar(
11
- theme = bs_theme(bootswatch = "minty"),
12
- title = "Penguins explorer",
13
- sidebar = sidebar(
14
- varSelectInput("xvar", "X variable", df_num, selected = "Bill Length (mm)"),
15
- varSelectInput("yvar", "Y variable", df_num, selected = "Bill Depth (mm)"),
16
- checkboxGroupInput("species", "Filter by species",
17
- choices = unique(df$Species), selected = unique(df$Species)
18
  ),
19
- hr(), # Add a horizontal rule
20
- checkboxInput("by_species", "Show species", TRUE),
21
- checkboxInput("show_margins", "Show marginal plots", TRUE),
22
- checkboxInput("smooth", "Add smoother"),
23
- ),
24
- plotOutput("scatter")
25
  )
26
 
27
  server <- function(input, output, session) {
28
- subsetted <- reactive({
29
- req(input$species)
30
- df |> filter(Species %in% input$species)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  })
32
-
33
- output$scatter <- renderPlot(
34
- {
35
- p <- ggplot(subsetted(), aes(!!input$xvar, !!input$yvar)) +
36
- theme_light() +
37
- list(
38
- theme(legend.position = "bottom"),
39
- if (input$by_species) aes(color = Species),
40
- geom_point(),
41
- if (input$smooth) geom_smooth()
42
- )
43
-
44
- if (input$show_margins) {
45
- margin_type <- if (input$by_species) "density" else "histogram"
46
- p <- p |> ggExtra::ggMarginal(
47
- type = margin_type, margins = "both",
48
- size = 8, groupColour = input$by_species, groupFill = input$by_species
49
- )
50
- }
51
-
52
- p
53
- },
54
- res = 100
55
- )
56
  }
57
 
58
  shinyApp(ui, server)
 
 
1
  library(shiny)
2
+ library(httr2)
 
 
3
 
4
+ ui <- fluidPage(
5
+ titlePanel("Chat com NVIDIA NIM via httr2"),
6
+ sidebarLayout(
7
+ sidebarPanel(
8
+ textAreaInput("pergunta", "Digite sua pergunta:",
9
+ "Descreva o package leaflet no R.", rows = 4),
10
+ actionButton("enviar", "Enviar")
 
 
 
 
 
11
  ),
12
+ mainPanel(
13
+ h4("Resposta:"),
14
+ verbatimTextOutput("resposta")
15
+ )
16
+ )
 
17
  )
18
 
19
  server <- function(input, output, session) {
20
+ resposta <- reactiveVal("")
21
+
22
+ observeEvent(input$enviar, {
23
+ req <- request("https://integrate.api.nvidia.com/v1/chat/completions") |>
24
+ req_headers(
25
+ Authorization = paste("Bearer", Sys.getenv("OPENAI_API_KEY")),
26
+ `Content-Type` = "application/json"
27
+ ) |>
28
+ req_body_json(list(
29
+ model = "meta/llama-3.1-70b-instruct",
30
+ messages = list(
31
+ list(role = "system", content = "You are a helpful assistant."),
32
+ list(role = "user", content = input$pergunta)
33
+ ),
34
+ max_tokens = 400
35
+ ))
36
+
37
+ tryCatch({
38
+ resp <- req_perform(req)
39
+ out <- resp_body_json(resp)
40
+ resposta(out$choices[[1]]$message$content)
41
+ }, error = function(e) {
42
+ resposta(paste("Erro:", e$message))
43
+ })
44
+ })
45
+
46
+ output$resposta <- renderText({
47
+ resposta()
48
  })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  }
50
 
51
  shinyApp(ui, server)
52
+