Spaces:
Sleeping
Sleeping
International Rice Research Institute commited on
Update app.R
Browse files
app.R
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
# ============================================================
|
| 2 |
-
# QTL-Mapper -
|
| 3 |
# ============================================================
|
| 4 |
|
| 5 |
# Package installation for Hugging Face
|
|
@@ -23,7 +23,7 @@ for (pkg in required_packages) {
|
|
| 23 |
# DEBUGGING - Check if token is loaded
|
| 24 |
# ============================================================
|
| 25 |
|
| 26 |
-
# Print token status to logs
|
| 27 |
token <- Sys.getenv("HF_API_TOKEN")
|
| 28 |
token_exists <- token != ""
|
| 29 |
token_length <- if(token_exists) nchar(token) else 0
|
|
@@ -42,13 +42,23 @@ print("===================================")
|
|
| 42 |
|
| 43 |
# Configuration
|
| 44 |
HF_API_TOKEN <- token
|
| 45 |
-
use_hf_api <- token_exists
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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() {
|
|
@@ -71,7 +81,12 @@ test_api_connection <- function() {
|
|
| 71 |
} else if (status == 503) {
|
| 72 |
return(list(success = FALSE, error = "Model loading", loading = TRUE))
|
| 73 |
} else {
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
}
|
| 76 |
}, error = function(e) {
|
| 77 |
return(list(success = FALSE, error = e$message))
|
|
@@ -85,10 +100,45 @@ print(paste("API Test result:", if(api_test$success) "SUCCESS" else paste("FAILE
|
|
| 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
|
|
@@ -97,12 +147,29 @@ query_hf_api <- function(prompt, max_length = 500, temperature = 0.7) {
|
|
| 97 |
return(list(success = FALSE, error = "HF_API_TOKEN not set"))
|
| 98 |
}
|
| 99 |
|
| 100 |
-
# Format prompt for better responses
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
tryCatch({
|
| 108 |
response <- POST(
|
|
@@ -1068,7 +1135,7 @@ ui <- fluidPage(
|
|
| 1068 |
h4(style = "color:#58a6ff; font-family: 'IBM Plex Mono'; margin: 12px 0;", "AI Assistant Status"),
|
| 1069 |
p(style = "color: #8b949e; line-height: 1.7;",
|
| 1070 |
if (use_hf_api) {
|
| 1071 |
-
"β
AI Assistant is connected to Hugging Face API
|
| 1072 |
} else {
|
| 1073 |
"β οΈ AI Assistant is in rule-based mode. Set HF_API_TOKEN in Space secrets to enable transformer-based responses."
|
| 1074 |
}
|
|
@@ -1095,10 +1162,10 @@ ui <- fluidPage(
|
|
| 1095 |
tags$div(
|
| 1096 |
tags$h4("QTL Analysis AI Assistant",
|
| 1097 |
tags$span(class = "model-badge",
|
| 1098 |
-
if(use_hf_api) "
|
| 1099 |
),
|
| 1100 |
tags$p(id = "model_status",
|
| 1101 |
-
if(use_hf_api) "π€ AI mode
|
| 1102 |
class = if(use_hf_api) "ai-mode" else "rule-mode"
|
| 1103 |
)
|
| 1104 |
)
|
|
@@ -1170,18 +1237,13 @@ server <- function(input, output, session) {
|
|
| 1170 |
chat_messages = list(),
|
| 1171 |
is_typing = FALSE,
|
| 1172 |
lod_threshold = 2,
|
| 1173 |
-
use_ai = use_hf_api
|
| 1174 |
)
|
| 1175 |
|
| 1176 |
# Update status based on API availability
|
| 1177 |
observe({
|
| 1178 |
if (values$use_ai) {
|
| 1179 |
-
shinyjs::html("model_status", "π€ AI mode
|
| 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")
|
| 1187 |
}
|
|
@@ -1235,11 +1297,10 @@ server <- function(input, output, session) {
|
|
| 1235 |
observe({
|
| 1236 |
if (length(values$chat_messages) == 0) {
|
| 1237 |
mode_msg <- if (values$use_ai) {
|
| 1238 |
-
paste("I'm using the
|
| 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,
|
| 1243 |
}
|
| 1244 |
add_message("assistant", paste(
|
| 1245 |
"π Hello! I'm your QTL analysis assistant.",
|
|
|
|
| 1 |
# ============================================================
|
| 2 |
+
# QTL-Mapper - Fixed API Connection
|
| 3 |
# ============================================================
|
| 4 |
|
| 5 |
# Package installation for Hugging Face
|
|
|
|
| 23 |
# DEBUGGING - Check if token is loaded
|
| 24 |
# ============================================================
|
| 25 |
|
| 26 |
+
# Print token status to logs
|
| 27 |
token <- Sys.getenv("HF_API_TOKEN")
|
| 28 |
token_exists <- token != ""
|
| 29 |
token_length <- if(token_exists) nchar(token) else 0
|
|
|
|
| 42 |
|
| 43 |
# Configuration
|
| 44 |
HF_API_TOKEN <- token
|
|
|
|
| 45 |
|
| 46 |
+
# Try different models that are more reliable on the free inference API
|
| 47 |
+
# Option 1: Mistral 7B Instruct (very reliable)
|
| 48 |
+
# hf_model <- "mistralai/Mistral-7B-Instruct-v0.2"
|
| 49 |
+
|
| 50 |
+
# Option 2: Microsoft Phi-2 (small but powerful)
|
| 51 |
+
# hf_model <- "microsoft/phi-2"
|
| 52 |
+
|
| 53 |
+
# Option 3: Google Gemma (good for instruction)
|
| 54 |
+
# hf_model <- "google/gemma-2b-it"
|
| 55 |
+
|
| 56 |
+
# Option 4: HuggingFaceH4/zephyr-7b-beta (very reliable)
|
| 57 |
+
hf_model <- "HuggingFaceH4/zephyr-7b-beta"
|
| 58 |
+
|
| 59 |
+
#HF_API_URL <- paste0("https://api-inference.huggingface.co/models/", hf_model)
|
| 60 |
+
HF_API_URL <- paste0("https://router.huggingface.co/hf-inference/models/", hf_model)
|
| 61 |
|
|
|
|
| 62 |
|
| 63 |
# Function to test API connection
|
| 64 |
test_api_connection <- function() {
|
|
|
|
| 81 |
} else if (status == 503) {
|
| 82 |
return(list(success = FALSE, error = "Model loading", loading = TRUE))
|
| 83 |
} else {
|
| 84 |
+
error_content <- tryCatch({
|
| 85 |
+
content(response, "text", encoding = "UTF-8")
|
| 86 |
+
}, error = function(e) {
|
| 87 |
+
paste("Status:", status)
|
| 88 |
+
})
|
| 89 |
+
return(list(success = FALSE, error = paste("Status:", status, "-", substr(error_content, 1, 100))))
|
| 90 |
}
|
| 91 |
}, error = function(e) {
|
| 92 |
return(list(success = FALSE, error = e$message))
|
|
|
|
| 100 |
# Update use_hf_api based on test
|
| 101 |
if (token_exists && api_test$success) {
|
| 102 |
use_hf_api <- TRUE
|
| 103 |
+
print(paste("β
API is working with model:", hf_model, "- AI mode enabled"))
|
| 104 |
} else {
|
| 105 |
use_hf_api <- FALSE
|
| 106 |
+
print(paste("β οΈ API not available with model:", hf_model, "- using rule-based mode"))
|
| 107 |
+
print("Will try alternative models if this fails...")
|
| 108 |
+
|
| 109 |
+
# Try alternative models if first one fails
|
| 110 |
+
alt_models <- c(
|
| 111 |
+
"mistralai/Mistral-7B-Instruct-v0.2",
|
| 112 |
+
"microsoft/phi-2",
|
| 113 |
+
"google/gemma-2b-it",
|
| 114 |
+
"HuggingFaceH4/zephyr-7b-beta"
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
for (alt_model in alt_models) {
|
| 118 |
+
if (alt_model == hf_model) next
|
| 119 |
+
|
| 120 |
+
print(paste("Trying alternative model:", alt_model))
|
| 121 |
+
test_url <- paste0("https://api-inference.huggingface.co/models/", alt_model)
|
| 122 |
+
|
| 123 |
+
tryCatch({
|
| 124 |
+
response <- POST(
|
| 125 |
+
url = test_url,
|
| 126 |
+
add_headers(Authorization = paste("Bearer", HF_API_TOKEN)),
|
| 127 |
+
body = toJSON(list(inputs = "Hello"), auto_unbox = TRUE),
|
| 128 |
+
encode = "json",
|
| 129 |
+
timeout(5)
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
status <- status_code(response)
|
| 133 |
+
if (status == 200) {
|
| 134 |
+
hf_model <<- alt_model
|
| 135 |
+
HF_API_URL <<- test_url
|
| 136 |
+
use_hf_api <<- TRUE
|
| 137 |
+
print(paste("β
Found working model:", alt_model, "- AI mode enabled"))
|
| 138 |
+
break
|
| 139 |
+
}
|
| 140 |
+
}, error = function(e) {})
|
| 141 |
+
}
|
| 142 |
}
|
| 143 |
|
| 144 |
# Function to query Hugging Face API
|
|
|
|
| 147 |
return(list(success = FALSE, error = "HF_API_TOKEN not set"))
|
| 148 |
}
|
| 149 |
|
| 150 |
+
# Format prompt for better responses based on model type
|
| 151 |
+
if (grepl("mistral|zephyr", hf_model)) {
|
| 152 |
+
# Chat template for Mistral/Zephyr
|
| 153 |
+
formatted_prompt <- paste0(
|
| 154 |
+
"<|system|>\nYou are an expert bioinformatics assistant specializing in QTL analysis.\n</s>\n",
|
| 155 |
+
"<|user|>\n", prompt, "\n</s>\n",
|
| 156 |
+
"<|assistant|>\n"
|
| 157 |
+
)
|
| 158 |
+
} else if (grepl("phi", hf_model)) {
|
| 159 |
+
# Simple prompt for Phi-2
|
| 160 |
+
formatted_prompt <- paste0(
|
| 161 |
+
"Instruct: You are a QTL analysis expert. Based on this data, answer the question.\n",
|
| 162 |
+
"Data: ", prompt, "\n",
|
| 163 |
+
"Output:"
|
| 164 |
+
)
|
| 165 |
+
} else {
|
| 166 |
+
# Generic prompt
|
| 167 |
+
formatted_prompt <- paste0(
|
| 168 |
+
"You are an expert bioinformatics assistant specializing in QTL analysis.\n\n",
|
| 169 |
+
"Based on this data:\n", prompt, "\n\n",
|
| 170 |
+
"Answer the question accurately and concisely:"
|
| 171 |
+
)
|
| 172 |
+
}
|
| 173 |
|
| 174 |
tryCatch({
|
| 175 |
response <- POST(
|
|
|
|
| 1135 |
h4(style = "color:#58a6ff; font-family: 'IBM Plex Mono'; margin: 12px 0;", "AI Assistant Status"),
|
| 1136 |
p(style = "color: #8b949e; line-height: 1.7;",
|
| 1137 |
if (use_hf_api) {
|
| 1138 |
+
paste("β
AI Assistant is connected to Hugging Face API using", hf_model)
|
| 1139 |
} else {
|
| 1140 |
"β οΈ AI Assistant is in rule-based mode. Set HF_API_TOKEN in Space secrets to enable transformer-based responses."
|
| 1141 |
}
|
|
|
|
| 1162 |
tags$div(
|
| 1163 |
tags$h4("QTL Analysis AI Assistant",
|
| 1164 |
tags$span(class = "model-badge",
|
| 1165 |
+
if(use_hf_api) gsub(".*/", "", hf_model) else "rules")
|
| 1166 |
),
|
| 1167 |
tags$p(id = "model_status",
|
| 1168 |
+
if(use_hf_api) paste("π€ AI mode -", gsub(".*/", "", hf_model)) else "π Rule-based mode",
|
| 1169 |
class = if(use_hf_api) "ai-mode" else "rule-mode"
|
| 1170 |
)
|
| 1171 |
)
|
|
|
|
| 1237 |
chat_messages = list(),
|
| 1238 |
is_typing = FALSE,
|
| 1239 |
lod_threshold = 2,
|
| 1240 |
+
use_ai = use_hf_api
|
| 1241 |
)
|
| 1242 |
|
| 1243 |
# Update status based on API availability
|
| 1244 |
observe({
|
| 1245 |
if (values$use_ai) {
|
| 1246 |
+
shinyjs::html("model_status", paste("π€ AI mode -", gsub(".*/", "", hf_model)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1247 |
} else {
|
| 1248 |
shinyjs::html("model_status", "π Rule-based mode")
|
| 1249 |
}
|
|
|
|
| 1297 |
observe({
|
| 1298 |
if (length(values$chat_messages) == 0) {
|
| 1299 |
mode_msg <- if (values$use_ai) {
|
| 1300 |
+
paste("I'm using the", gsub(".*/", "", hf_model), "model via Hugging Face API!")
|
|
|
|
| 1301 |
} else {
|
| 1302 |
paste("I'm using enhanced rule-based responses.",
|
| 1303 |
+
"To enable AI mode, check your HF_API_TOKEN in Space secrets.")
|
| 1304 |
}
|
| 1305 |
add_message("assistant", paste(
|
| 1306 |
"π Hello! I'm your QTL analysis assistant.",
|