Spaces:
Sleeping
Sleeping
International Rice Research Institute commited on
Update app.R
Browse files
app.R
CHANGED
|
@@ -19,25 +19,81 @@ for (pkg in required_packages) {
|
|
| 19 |
}
|
| 20 |
}
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# ============================================================
|
| 23 |
# TRANSFORMER-BASED LLM SYSTEM - FIXED VERSION
|
| 24 |
# ============================================================
|
| 25 |
|
| 26 |
-
# Configuration
|
| 27 |
-
HF_API_TOKEN <-
|
| 28 |
-
use_hf_api <-
|
| 29 |
|
| 30 |
# Using FLAN-T5 which is more reliable for instruction following
|
| 31 |
hf_model <- "google/flan-t5-large" # Good for Q&A tasks
|
| 32 |
-
# Alternative
|
| 33 |
-
# hf_model <- "microsoft/phi-2"
|
| 34 |
-
# hf_model <- "mistralai/Mistral-7B-Instruct-v0.2"
|
| 35 |
|
| 36 |
HF_API_URL <- paste0("https://api-inference.huggingface.co/models/", hf_model)
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
# Function to query Hugging Face API
|
| 39 |
query_hf_api <- function(prompt, max_length = 500, temperature = 0.7) {
|
| 40 |
-
if (
|
| 41 |
return(list(success = FALSE, error = "HF_API_TOKEN not set"))
|
| 42 |
}
|
| 43 |
|
|
@@ -659,7 +715,14 @@ ui <- fluidPage(
|
|
| 659 |
font-size: 16px; color: #0d1117; font-weight: bold;
|
| 660 |
}
|
| 661 |
#chat-header h4 { font-family: var(--font-mono); font-size: 13px; font-weight: 600; color: var(--text-primary); margin: 0; }
|
| 662 |
-
#chat-header p {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 663 |
#chat-close-btn { background: none; border: none; color: var(--text-muted); cursor: pointer; font-size: 18px; padding: 4px; transition: color 0.15s; }
|
| 664 |
#chat-close-btn:hover { color: var(--text-primary); }
|
| 665 |
|
|
@@ -1032,10 +1095,11 @@ ui <- fluidPage(
|
|
| 1032 |
tags$div(
|
| 1033 |
tags$h4("QTL Analysis AI Assistant",
|
| 1034 |
tags$span(class = "model-badge",
|
| 1035 |
-
if(use_hf_api) "
|
| 1036 |
),
|
| 1037 |
tags$p(id = "model_status",
|
| 1038 |
-
if(use_hf_api) "π€
|
|
|
|
| 1039 |
)
|
| 1040 |
)
|
| 1041 |
),
|
|
@@ -1106,18 +1170,17 @@ server <- function(input, output, session) {
|
|
| 1106 |
chat_messages = list(),
|
| 1107 |
is_typing = FALSE,
|
| 1108 |
lod_threshold = 2,
|
| 1109 |
-
use_ai = use_hf_api
|
| 1110 |
)
|
| 1111 |
|
| 1112 |
-
# Update status
|
| 1113 |
observe({
|
| 1114 |
if (values$use_ai) {
|
| 1115 |
shinyjs::html("model_status", "π€ AI mode active")
|
| 1116 |
-
# Test connection
|
| 1117 |
-
|
| 1118 |
-
if (!test_result$success) {
|
| 1119 |
values$use_ai <- FALSE
|
| 1120 |
-
shinyjs::html("model_status", "π Rule-based mode
|
| 1121 |
}
|
| 1122 |
} else {
|
| 1123 |
shinyjs::html("model_status", "π Rule-based mode")
|
|
@@ -1172,12 +1235,15 @@ server <- function(input, output, session) {
|
|
| 1172 |
observe({
|
| 1173 |
if (length(values$chat_messages) == 0) {
|
| 1174 |
mode_msg <- if (values$use_ai) {
|
| 1175 |
-
"I'm using
|
|
|
|
| 1176 |
} else {
|
| 1177 |
-
"I'm using rule-based responses."
|
|
|
|
| 1178 |
}
|
| 1179 |
add_message("assistant", paste(
|
| 1180 |
-
"π Hello! I'm your QTL analysis assistant.",
|
|
|
|
| 1181 |
"\n\n**I can help you with:**",
|
| 1182 |
"β’ **What is PVE?** - Understand variance explained",
|
| 1183 |
"β’ **Show top QTLs** - List your most significant markers",
|
|
|
|
| 19 |
}
|
| 20 |
}
|
| 21 |
|
| 22 |
+
# ============================================================
|
| 23 |
+
# DEBUGGING - Check if token is loaded
|
| 24 |
+
# ============================================================
|
| 25 |
+
|
| 26 |
+
# Print token status to logs (visible in Hugging Face logs)
|
| 27 |
+
token <- Sys.getenv("HF_API_TOKEN")
|
| 28 |
+
token_exists <- token != ""
|
| 29 |
+
token_length <- if(token_exists) nchar(token) else 0
|
| 30 |
+
|
| 31 |
+
print("=== HUGGING FACE API TOKEN DEBUG ===")
|
| 32 |
+
print(paste("Token exists:", token_exists))
|
| 33 |
+
print(paste("Token length:", token_length))
|
| 34 |
+
if(token_exists) {
|
| 35 |
+
print(paste("Token starts with:", substr(token, 1, 10)))
|
| 36 |
+
}
|
| 37 |
+
print("===================================")
|
| 38 |
+
|
| 39 |
# ============================================================
|
| 40 |
# TRANSFORMER-BASED LLM SYSTEM - FIXED VERSION
|
| 41 |
# ============================================================
|
| 42 |
|
| 43 |
+
# Configuration
|
| 44 |
+
HF_API_TOKEN <- token
|
| 45 |
+
use_hf_api <- token_exists
|
| 46 |
|
| 47 |
# Using FLAN-T5 which is more reliable for instruction following
|
| 48 |
hf_model <- "google/flan-t5-large" # Good for Q&A tasks
|
| 49 |
+
# Alternative: "microsoft/phi-2" (needs trust_remote_code=True)
|
|
|
|
|
|
|
| 50 |
|
| 51 |
HF_API_URL <- paste0("https://api-inference.huggingface.co/models/", hf_model)
|
| 52 |
|
| 53 |
+
# Function to test API connection
|
| 54 |
+
test_api_connection <- function() {
|
| 55 |
+
if (!token_exists) {
|
| 56 |
+
return(list(success = FALSE, error = "No token found"))
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
tryCatch({
|
| 60 |
+
response <- POST(
|
| 61 |
+
url = HF_API_URL,
|
| 62 |
+
add_headers(Authorization = paste("Bearer", HF_API_TOKEN)),
|
| 63 |
+
body = toJSON(list(inputs = "Hello"), auto_unbox = TRUE),
|
| 64 |
+
encode = "json",
|
| 65 |
+
timeout(10)
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
status <- status_code(response)
|
| 69 |
+
if (status == 200) {
|
| 70 |
+
return(list(success = TRUE))
|
| 71 |
+
} else if (status == 503) {
|
| 72 |
+
return(list(success = FALSE, error = "Model loading", loading = TRUE))
|
| 73 |
+
} else {
|
| 74 |
+
return(list(success = FALSE, error = paste("Status:", status)))
|
| 75 |
+
}
|
| 76 |
+
}, error = function(e) {
|
| 77 |
+
return(list(success = FALSE, error = e$message))
|
| 78 |
+
})
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
# Run test on startup
|
| 82 |
+
api_test <- test_api_connection()
|
| 83 |
+
print(paste("API Test result:", if(api_test$success) "SUCCESS" else paste("FAILED -", api_test$error)))
|
| 84 |
+
|
| 85 |
+
# Update use_hf_api based on test
|
| 86 |
+
if (token_exists && api_test$success) {
|
| 87 |
+
use_hf_api <- TRUE
|
| 88 |
+
print("β
API is working, AI mode enabled")
|
| 89 |
+
} else {
|
| 90 |
+
use_hf_api <- FALSE
|
| 91 |
+
print("β οΈ API not available, using rule-based mode")
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
# Function to query Hugging Face API
|
| 95 |
query_hf_api <- function(prompt, max_length = 500, temperature = 0.7) {
|
| 96 |
+
if (!token_exists) {
|
| 97 |
return(list(success = FALSE, error = "HF_API_TOKEN not set"))
|
| 98 |
}
|
| 99 |
|
|
|
|
| 715 |
font-size: 16px; color: #0d1117; font-weight: bold;
|
| 716 |
}
|
| 717 |
#chat-header h4 { font-family: var(--font-mono); font-size: 13px; font-weight: 600; color: var(--text-primary); margin: 0; }
|
| 718 |
+
#chat-header p {
|
| 719 |
+
font-size: 11px;
|
| 720 |
+
color: var(--text-muted);
|
| 721 |
+
margin: 0;
|
| 722 |
+
font-weight: bold;
|
| 723 |
+
}
|
| 724 |
+
#chat-header p.ai-mode { color: var(--success); }
|
| 725 |
+
#chat-header p.rule-mode { color: var(--warning); }
|
| 726 |
#chat-close-btn { background: none; border: none; color: var(--text-muted); cursor: pointer; font-size: 18px; padding: 4px; transition: color 0.15s; }
|
| 727 |
#chat-close-btn:hover { color: var(--text-primary); }
|
| 728 |
|
|
|
|
| 1095 |
tags$div(
|
| 1096 |
tags$h4("QTL Analysis AI Assistant",
|
| 1097 |
tags$span(class = "model-badge",
|
| 1098 |
+
if(use_hf_api) "FLAN-T5" else "rules")
|
| 1099 |
),
|
| 1100 |
tags$p(id = "model_status",
|
| 1101 |
+
if(use_hf_api) "π€ AI mode active" else "π Rule-based mode",
|
| 1102 |
+
class = if(use_hf_api) "ai-mode" else "rule-mode"
|
| 1103 |
)
|
| 1104 |
)
|
| 1105 |
),
|
|
|
|
| 1170 |
chat_messages = list(),
|
| 1171 |
is_typing = FALSE,
|
| 1172 |
lod_threshold = 2,
|
| 1173 |
+
use_ai = use_hf_api # Use the global setting from startup
|
| 1174 |
)
|
| 1175 |
|
| 1176 |
+
# Update status based on API availability
|
| 1177 |
observe({
|
| 1178 |
if (values$use_ai) {
|
| 1179 |
shinyjs::html("model_status", "π€ AI mode active")
|
| 1180 |
+
# Test connection on startup (already tested globally)
|
| 1181 |
+
if (!use_hf_api) {
|
|
|
|
| 1182 |
values$use_ai <- FALSE
|
| 1183 |
+
shinyjs::html("model_status", "π Rule-based mode")
|
| 1184 |
}
|
| 1185 |
} else {
|
| 1186 |
shinyjs::html("model_status", "π Rule-based mode")
|
|
|
|
| 1235 |
observe({
|
| 1236 |
if (length(values$chat_messages) == 0) {
|
| 1237 |
mode_msg <- if (values$use_ai) {
|
| 1238 |
+
paste("I'm using the FLAN-T5 transformer model via Hugging Face API!",
|
| 1239 |
+
"My responses are based on your actual analysis results.")
|
| 1240 |
} else {
|
| 1241 |
+
paste("I'm using enhanced rule-based responses.",
|
| 1242 |
+
"To enable AI mode, set HF_API_TOKEN in Space secrets.")
|
| 1243 |
}
|
| 1244 |
add_message("assistant", paste(
|
| 1245 |
+
"π Hello! I'm your QTL analysis assistant.",
|
| 1246 |
+
mode_msg,
|
| 1247 |
"\n\n**I can help you with:**",
|
| 1248 |
"β’ **What is PVE?** - Understand variance explained",
|
| 1249 |
"β’ **Show top QTLs** - List your most significant markers",
|