Spaces:
Running
Running
better chat box
Browse files
app.R
CHANGED
@@ -8,25 +8,46 @@ library(duckdbfs)
|
|
8 |
library(fontawesome)
|
9 |
library(bsicons)
|
10 |
library(gt)
|
|
|
|
|
11 |
duckdbfs::load_spatial()
|
12 |
|
|
|
|
|
13 |
pmtiles <- "https://data.source.coop/cboettig/us-boundaries/mappinginequality.pmtiles"
|
14 |
|
15 |
# Define the UI
|
16 |
ui <- page_sidebar(
|
17 |
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
value = "Which county has the highest average social vulnerability?",
|
23 |
-
width = "100%",
|
24 |
-
height = 100
|
25 |
-
),
|
26 |
|
27 |
-
|
28 |
|
29 |
|
|
|
|
|
|
|
30 |
accordion(
|
31 |
open = FALSE,
|
32 |
accordion_panel("generated SQL Code",
|
@@ -35,28 +56,21 @@ ui <- page_sidebar(
|
|
35 |
accordion_panel("Explanation",
|
36 |
textOutput("explanation"),
|
37 |
)
|
38 |
-
),
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
input_switch("rsr", "Biodiversity Range Size Rarity"),
|
43 |
-
width = 350,
|
44 |
),
|
45 |
-
titlePanel("Demo App"),
|
46 |
|
47 |
-
|
48 |
-
card(maplibreOutput("map")),
|
49 |
-
card(includeMarkdown("## Plot"),
|
50 |
-
plotOutput("chart1"),
|
51 |
-
plotOutput("chart2"),
|
52 |
-
),
|
53 |
|
54 |
-
|
|
|
|
|
|
|
|
|
55 |
),
|
56 |
-
|
57 |
-
gt_output("table"),
|
58 |
-
|
59 |
-
theme = bs_theme(version = "5")
|
60 |
)
|
61 |
|
62 |
svi <- "https://data.source.coop/cboettig/social-vulnerability/svi2020_us_tract.parquet" |>
|
@@ -64,7 +78,6 @@ svi <- "https://data.source.coop/cboettig/social-vulnerability/svi2020_us_tract.
|
|
64 |
|
65 |
con <- duckdbfs::cached_connection()
|
66 |
schema <- DBI::dbGetQuery(con, "PRAGMA table_info(svi)")
|
67 |
-
#schema <- svi |> head() |> collect() |> str()
|
68 |
|
69 |
system_prompt = glue::glue('
|
70 |
You are a helpful agent who always replies strictly in JSON-formatted text.
|
@@ -103,13 +116,13 @@ server <- function(input, output, session) {
|
|
103 |
chat_append("chat", stream)
|
104 |
response <- jsonlite::fromJSON(stream)
|
105 |
|
106 |
-
output$sql_code <- renderText({stringr::str_wrap(response$query, width=
|
107 |
-
output$explanation <- renderText(
|
108 |
|
109 |
df <- DBI::dbGetQuery(con, response$query)
|
110 |
|
111 |
df <- df |> select(-any_of("Shape"))
|
112 |
-
output$table <- render_gt(df, height=300)
|
113 |
|
114 |
})
|
115 |
|
|
|
8 |
library(fontawesome)
|
9 |
library(bsicons)
|
10 |
library(gt)
|
11 |
+
library(htmltools)
|
12 |
+
|
13 |
duckdbfs::load_spatial()
|
14 |
|
15 |
+
css <- HTML("<link rel='stylesheet' type='text/css' href='styles.css'>")
|
16 |
+
|
17 |
pmtiles <- "https://data.source.coop/cboettig/us-boundaries/mappinginequality.pmtiles"
|
18 |
|
19 |
# Define the UI
|
20 |
ui <- page_sidebar(
|
21 |
|
22 |
+
titlePanel("Demo App"),
|
23 |
+
card(
|
24 |
+
layout_columns(
|
25 |
+
textInput("chat",
|
26 |
+
label = NULL,
|
27 |
+
"Which county has the highest average social vulnerability?",
|
28 |
+
width = "100%"),
|
29 |
+
div(
|
30 |
+
actionButton("user_msg", "", icon = icon("paper-plane"), class = "btn-primary btn-sm align-bottom"),
|
31 |
+
class = "align-text-bottom"),
|
32 |
+
col_widths = c(11, 1)),
|
33 |
+
fill = FALSE
|
34 |
+
),
|
35 |
+
layout_columns(
|
36 |
+
card(maplibreOutput("map")),
|
37 |
+
card(includeMarkdown("## Plot"),
|
38 |
+
plotOutput("chart1"),
|
39 |
+
plotOutput("chart2"),
|
40 |
+
),
|
41 |
|
42 |
+
col_widths = c(8, 4)
|
43 |
+
),
|
|
|
|
|
|
|
|
|
44 |
|
45 |
+
gt_output("table"),
|
46 |
|
47 |
|
48 |
+
card(fill = FALSE,
|
49 |
+
layout_columns(
|
50 |
+
br(),
|
51 |
accordion(
|
52 |
open = FALSE,
|
53 |
accordion_panel("generated SQL Code",
|
|
|
56 |
accordion_panel("Explanation",
|
57 |
textOutput("explanation"),
|
58 |
)
|
59 |
+
),
|
60 |
+
br(),
|
61 |
+
col_widths = c(2, 8, 2)
|
62 |
+
)
|
|
|
|
|
63 |
),
|
|
|
64 |
|
65 |
+
sidebar = sidebar(
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
+
input_switch("redlines", "Redlined Areas", value = FALSE),
|
68 |
+
input_switch("svi", "Social Vulnerability", value = FALSE),
|
69 |
+
input_switch("richness", "Biodiversity Richness", value = FALSE),
|
70 |
+
input_switch("rsr", "Biodiversity Range Size Rarity", value = FALSE),
|
71 |
+
# width = 350,
|
72 |
),
|
73 |
+
theme = bs_theme(version = "5")
|
|
|
|
|
|
|
74 |
)
|
75 |
|
76 |
svi <- "https://data.source.coop/cboettig/social-vulnerability/svi2020_us_tract.parquet" |>
|
|
|
78 |
|
79 |
con <- duckdbfs::cached_connection()
|
80 |
schema <- DBI::dbGetQuery(con, "PRAGMA table_info(svi)")
|
|
|
81 |
|
82 |
system_prompt = glue::glue('
|
83 |
You are a helpful agent who always replies strictly in JSON-formatted text.
|
|
|
116 |
chat_append("chat", stream)
|
117 |
response <- jsonlite::fromJSON(stream)
|
118 |
|
119 |
+
output$sql_code <- renderText({stringr::str_wrap(response$query, width = 60)})
|
120 |
+
output$explanation <- renderText(response$explanation)
|
121 |
|
122 |
df <- DBI::dbGetQuery(con, response$query)
|
123 |
|
124 |
df <- df |> select(-any_of("Shape"))
|
125 |
+
output$table <- render_gt(df, height = 300)
|
126 |
|
127 |
})
|
128 |
|