Skip to content

Commit fbc0682

Browse files
committed
Set up the default Gemini API
1 parent 3636b88 commit fbc0682

4 files changed

Lines changed: 36 additions & 77 deletions

File tree

inst/TextAnalysisR.app/global.R

Lines changed: 27 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@ server_gemini_key <- Sys.getenv("GEMINI_API_KEY", "")
66
has_server_gemini <- nzchar(server_gemini_key)
77
server_gemini_model <- Sys.getenv("GEMINI_DEFAULT_MODEL", "gemini-2.5-flash")
88

9+
.gemini_provider_label <- if (has_server_gemini) "Gemini" else "Gemini (API Key Required)"
10+
.llm_provider_choices <- c("OpenAI (API Key Required)" = "openai", "gemini")
11+
names(.llm_provider_choices)[2] <- .gemini_provider_label
12+
.llm_provider_default <- if (has_server_gemini) "gemini" else "openai"
13+
.embed_provider_choices <- c("Sentence Transformers (Python)" = "sentence-transformers", "OpenAI (API Key Required)" = "openai", "gemini")
14+
names(.embed_provider_choices)[3] <- .gemini_provider_label
15+
.embed_provider_default <- if (has_server_gemini) "gemini" else "sentence-transformers"
16+
917
shiny::enableBookmarking("disable")
1018

1119
.password_input <- function(inputId, label, value = "", placeholder = NULL) {
1220
if (isTRUE(has_server_gemini) && grepl("gemini", inputId, ignore.case = TRUE)) {
13-
return(tags$p(
14-
style = "color: #475569; font-size: 13px; margin-bottom: 8px;",
15-
"Usage is free here, supported by the Google Cloud Research program."
16-
))
21+
placeholder <- "Optional — leave blank to use the provided key"
1722
}
1823
pw <- shiny::passwordInput(inputId, label, value = value, placeholder = placeholder)
1924
pw$children <- lapply(pw$children, function(ch) {
@@ -228,11 +233,8 @@ topic_modeling_ui_content <- function() {
228233
radioButtons(
229234
"stm_label_provider",
230235
"AI Provider:",
231-
choices = c(
232-
"OpenAI (API Key Required)" = "openai",
233-
"Gemini (API Key Required)" = "gemini"
234-
),
235-
selected = "openai",
236+
choices = .llm_provider_choices,
237+
selected = .llm_provider_default,
236238
inline = FALSE
237239
),
238240
conditionalPanel(
@@ -402,11 +404,8 @@ Focus on incorporating the most significant keywords while following the guideli
402404
radioButtons(
403405
"k_rec_provider",
404406
"AI Provider:",
405-
choices = c(
406-
"OpenAI (API Key Required)" = "openai",
407-
"Gemini (API Key Required)" = "gemini"
408-
),
409-
selected = "openai",
407+
choices = .llm_provider_choices,
408+
selected = .llm_provider_default,
410409
inline = FALSE
411410
),
412411

@@ -563,11 +562,8 @@ Focus on incorporating the most significant keywords while following the guideli
563562
radioButtons(
564563
"content_provider",
565564
"AI Provider:",
566-
choices = c(
567-
"OpenAI (API Key Required)" = "openai",
568-
"Gemini (API Key Required)" = "gemini"
569-
),
570-
selected = "openai",
565+
choices = .llm_provider_choices,
566+
selected = .llm_provider_default,
571567
inline = FALSE
572568
),
573569
conditionalPanel(
@@ -650,12 +646,8 @@ Focus on incorporating the most significant keywords while following the guideli
650646
radioButtons(
651647
"topic_embedding_provider",
652648
"AI Provider:",
653-
choices = c(
654-
"Sentence Transformers (Python)" = "sentence-transformers",
655-
"OpenAI (API Key Required)" = "openai",
656-
"Gemini (API Key Required)" = "gemini"
657-
),
658-
selected = "sentence-transformers",
649+
choices = .embed_provider_choices,
650+
selected = .embed_provider_default,
659651
inline = FALSE
660652
),
661653

@@ -2616,12 +2608,8 @@ semantic_analysis_ui_content <- function() {
26162608
radioButtons(
26172609
"embedding_provider",
26182610
"AI Provider:",
2619-
choices = c(
2620-
"Sentence Transformers (Python)" = "sentence-transformers",
2621-
"OpenAI (API Key Required)" = "openai",
2622-
"Gemini (API Key Required)" = "gemini"
2623-
),
2624-
selected = "sentence-transformers",
2611+
choices = .embed_provider_choices,
2612+
selected = .embed_provider_default,
26252613
inline = FALSE
26262614
),
26272615
conditionalPanel(
@@ -2787,12 +2775,8 @@ semantic_analysis_ui_content <- function() {
27872775
radioButtons(
27882776
"search_embedding_provider",
27892777
"AI Provider:",
2790-
choices = c(
2791-
"Sentence Transformers (Python)" = "sentence-transformers",
2792-
"OpenAI (API Key Required)" = "openai",
2793-
"Gemini (API Key Required)" = "gemini"
2794-
),
2795-
selected = "sentence-transformers"
2778+
choices = .embed_provider_choices,
2779+
selected = .embed_provider_default
27962780
),
27972781
conditionalPanel(
27982782
condition = "input.search_embedding_provider == 'sentence-transformers'",
@@ -2857,11 +2841,8 @@ semantic_analysis_ui_content <- function() {
28572841
radioButtons(
28582842
"rag_provider",
28592843
"AI Provider:",
2860-
choices = c(
2861-
"OpenAI (API Key Required)" = "openai",
2862-
"Gemini (API Key Required)" = "gemini"
2863-
),
2864-
selected = "openai"
2844+
choices = .llm_provider_choices,
2845+
selected = .llm_provider_default
28652846
),
28662847
conditionalPanel(
28672848
condition = "input.rag_provider == 'openai'",
@@ -3081,11 +3062,8 @@ semantic_analysis_ui_content <- function() {
30813062
radioButtons(
30823063
"cluster_label_provider",
30833064
"AI Provider:",
3084-
choices = c(
3085-
"OpenAI (API Key Required)" = "openai",
3086-
"Gemini (API Key Required)" = "gemini"
3087-
),
3088-
selected = "openai",
3065+
choices = .llm_provider_choices,
3066+
selected = .llm_provider_default,
30893067
inline = FALSE
30903068
),
30913069

@@ -3191,11 +3169,8 @@ semantic_analysis_ui_content <- function() {
31913169
radioButtons(
31923170
"llm_sentiment_provider",
31933171
"AI Provider:",
3194-
choices = c(
3195-
"OpenAI (API Key Required)" = "openai",
3196-
"Gemini (API Key Required)" = "gemini"
3197-
),
3198-
selected = "openai",
3172+
choices = .llm_provider_choices,
3173+
selected = .llm_provider_default,
31993174
inline = FALSE
32003175
),
32013176
uiOutput("llm_sentiment_model_ui"),

inst/TextAnalysisR.app/markdown/ai_integration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## AI Integration
22

3-
Gemini usage is free here, supported by the Google Cloud Research program. OpenAI requires your own API key.
3+
Gemini here uses Google Cloud Research credits — no key needed. OpenAI requires your own API key.
44

55
TextAnalysisR provides AI/NLP capabilities via cloud-based providers.
66

inst/TextAnalysisR.app/server.R

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -228,27 +228,6 @@ server <- shinyServer(function(input, output, session) {
228228
TextAnalysisR:::get_feature_status()
229229
})
230230

231-
observe({
232-
if (!is_remote) return()
233-
gemini_label <- if (has_server_gemini) "Gemini (free, Google Cloud Research)" else "Gemini (API Key Required)"
234-
default_llm <- if (has_server_gemini) "gemini" else "openai"
235-
default_embed <- if (has_server_gemini) "gemini" else "sentence-transformers"
236-
llm_providers <- c("OpenAI (API Key Required)" = "openai", "Gemini" = "gemini")
237-
names(llm_providers)[2] <- gemini_label
238-
embed_providers <- c("Sentence Transformers (Python)" = "sentence-transformers",
239-
"OpenAI (API Key Required)" = "openai", "Gemini" = "gemini")
240-
names(embed_providers)[3] <- gemini_label
241-
242-
updateRadioButtons(session, "embedding_provider", choices = embed_providers, selected = default_embed)
243-
updateRadioButtons(session, "search_embedding_provider", choices = embed_providers, selected = default_embed)
244-
updateRadioButtons(session, "topic_embedding_provider", choices = embed_providers, selected = default_embed)
245-
updateRadioButtons(session, "rag_provider", choices = llm_providers, selected = default_llm)
246-
updateRadioButtons(session, "cluster_label_provider", choices = llm_providers, selected = default_llm)
247-
updateRadioButtons(session, "llm_sentiment_provider", choices = llm_providers, selected = default_llm)
248-
updateRadioButtons(session, "stm_label_provider", choices = llm_providers, selected = default_llm)
249-
updateRadioButtons(session, "k_rec_provider", choices = llm_providers, selected = default_llm)
250-
updateRadioButtons(session, "content_provider", choices = llm_providers, selected = default_llm)
251-
})
252231

253232
# spaCy initialization status
254233
spacy_initialized <- reactiveVal(FALSE)
@@ -379,8 +358,8 @@ server <- shinyServer(function(input, output, session) {
379358
condition = "input.enable_multimodal == true",
380359
radioButtons("vision_provider",
381360
"Vision provider:",
382-
choices = c("OpenAI (API Key Required)" = "openai", "Gemini (API Key Required)" = "gemini"),
383-
selected = "openai",
361+
choices = .llm_provider_choices,
362+
selected = .llm_provider_default,
384363
inline = FALSE
385364
),
386365
conditionalPanel(

inst/TextAnalysisR.app/ui.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,12 @@ ui <- fluidPage(
355355
tags$p(style = "margin: 0 0 6px 0; font-size: 16px; color: #5C6E88;",
356356
tags$strong("OpenAI"), " \u2014 Cloud API. Enter key above or set ", tags$code("OPENAI_API_KEY"), " in .Renviron."),
357357
tags$p(style = "margin: 0; font-size: 16px; color: #5C6E88;",
358-
tags$strong("Gemini"), " \u2014 Cloud API. Enter key above or set ", tags$code("GEMINI_API_KEY"), " in .Renviron.")
358+
tags$strong("Gemini"),
359+
if (has_server_gemini) {
360+
" \u2014 Cloud API. Google Cloud Research credits \u2014 no key needed here."
361+
} else {
362+
list(" \u2014 Cloud API. Enter key above or set ", tags$code("GEMINI_API_KEY"), " in .Renviron.")
363+
})
359364
)
360365
)
361366
)

0 commit comments

Comments
 (0)