diff --git a/Dockerfile b/Dockerfile index 25250c40..fef69f45 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,6 +12,7 @@ RUN apt-get update && apt-get install -y \ curl # Create rstudio user +# default accout 'rstudio'; password: rstudio123 ENV USER=rstudio ENV PASSWORD=rstudio123 RUN useradd -m $USER && \ diff --git a/R/zzz.R b/R/zzz.R index 180768ec..725ff14c 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -19,42 +19,105 @@ #' @importFrom reticulate py_module_available #' @importFrom reticulate py_install #' @keywords internal +# .onAttach <- function(...) { +# # Determine Python command +# python_cmd <- if (Sys.info()["sysname"] == "Windows") "python" else "python3" +# python_path <- Sys.which(python_cmd) +# +# # Check Python path +# if (python_path == "") { +# packageStartupMessage(paste("Cannot locate the", python_cmd, "executable. Ensure it's installed and in your system's PATH. flaiR functionality requiring Python will not be available.")) +# return(invisible(NULL)) # Exit .onAttach without stopping package loading +# } +# +# # Check Python versio Try to get Python version +# tryCatch({ +# python_version <- system(paste(python_path, "--version"), intern = TRUE) +# if (!grepl("Python 3", python_version)) { +# packageStartupMessage("Python 3 is required, but a different version was found. Please install Python 3. flaiR functionality requiring Python will not be available.") +# return(invisible(NULL)) # Exit .onAttach without stopping package loading +# } +# }, error = function(e) { +# packageStartupMessage(paste("Failed to get Python version with path:", python_path, "Error:", e$message, ". flaiR functionality requiring Python will not be available.")) +# return(invisible(NULL)) # Exit .onAttach without stopping package loading +# }) +# +# # Check if PyTorch is installed +# check_torch_version <- function() { +# # torch_version_command <- paste(python_path, "-c 'import torch; print(torch.__version__)'") +# torch_version_command <- paste(python_path, "-c \"import torch; print(torch.__version__)\"") +# result <- system(torch_version_command, intern = TRUE) +# if (length(result) == 0 || result[1] == "ERROR" || is.na(result[1])) { +# return(list(paste("PyTorch", paste0("\033[31m", "\u2717", "\033[39m"), sep = " "), FALSE)) +# } +# # Return flair version +# return(list(paste("PyTorch", paste0("\033[32m", "\u2713", "\033[39m") ,result[1], sep = " "), TRUE, result[1])) +# } +# +# # Check if flair is installed + # check_flair_version <- function() { + # # flair_version_command <- paste(python_path, "-c 'import flair; print(flair.__version__)'") + # flair_version_command <- paste(python_path, "-c \"import flair; print(flair.__version__)\"") + # result <- system(flair_version_command, intern = TRUE) + # if (length(result) == 0 || result[1] == "ERROR" || is.na(result[1])) { + # return(list(paste("flair", paste0("\033[31m", "\u2717", "\033[39m"), sep = " "), FALSE)) + # } + # # Return flair version + # return(list(paste("flair", paste0("\033[32m", "\u2713", "\033[39m"),result[1], sep = " "), TRUE, result[1])) + # } + +# flair_version <- check_flair_version() +# torch_version <- check_torch_version() +# +# if (isFALSE(flair_version[[2]])) { +# packageStartupMessage(sprintf(" Flair %-50s", paste0("is installing from Python"))) +# +# commands <- c( +# paste(python_path, "-m pip install --upgrade pip"), +# paste(python_path, "-m pip install torch"), +# paste(python_path, "-m pip install flair"), +# paste(python_path, "-m pip install scipy==1.12.0") +# ) +# command_statuses <- vapply(commands, system, FUN.VALUE = integer(1)) +# +# flair_check_again <- check_flair_version() +# if (isFALSE(flair_check_again[[2]])) { +# packageStartupMessage("Failed to install Flair. {flaiR} requires Flair NLP. Please ensure Flair NLP is installed in Python manually.") +# } +# } else { +# packageStartupMessage(sprintf("\033[1m\033[34mflaiR\033[39m\033[22m: \033[1m\033[33mAn R Wrapper for Accessing Flair NLP\033[39m\033[22m %-5s", paste("\033[1m\033[33m", flair_version[[3]], "\033[39m\033[22m", sep = ""))) +# } +# } + + + .onAttach <- function(...) { - # Determine Python command - python_cmd <- if (Sys.info()["sysname"] == "Windows") "python" else "python3" - python_path <- Sys.which(python_cmd) - - # Check Python path - if (python_path == "") { - packageStartupMessage(paste("Cannot locate the", python_cmd, "executable. Ensure it's installed and in your system's PATH. flaiR functionality requiring Python will not be available.")) - return(invisible(NULL)) # Exit .onAttach without stopping package loading - } + # 总是先清除环境变量,避免冲突 + Sys.unsetenv("RETICULATE_PYTHON") - # Check Python versio Try to get Python version + # 使用 reticulate 的 py_discover_config 来检测 Python tryCatch({ - python_version <- system(paste(python_path, "--version"), intern = TRUE) - if (!grepl("Python 3", python_version)) { - packageStartupMessage("Python 3 is required, but a different version was found. Please install Python 3. flaiR functionality requiring Python will not be available.") - return(invisible(NULL)) # Exit .onAttach without stopping package loading - } + python_config <- reticulate::py_discover_config() + python_path <- python_config$python }, error = function(e) { - packageStartupMessage(paste("Failed to get Python version with path:", python_path, "Error:", e$message, ". flaiR functionality requiring Python will not be available.")) - return(invisible(NULL)) # Exit .onAttach without stopping package loading + # 如果 py_discover_config 失败,尝试找系统 Python + python_cmd <- if (Sys.info()["sysname"] == "Windows") "python" else "python3" + python_path <- Sys.which(python_cmd) }) - # Check if PyTorch is installed - check_torch_version <- function() { - # torch_version_command <- paste(python_path, "-c 'import torch; print(torch.__version__)'") - torch_version_command <- paste(python_path, "-c \"import torch; print(torch.__version__)\"") - result <- system(torch_version_command, intern = TRUE) - if (length(result) == 0 || result[1] == "ERROR" || is.na(result[1])) { - return(list(paste("PyTorch", paste0("\033[31m", "\u2717", "\033[39m"), sep = " "), FALSE)) - } - # Return flair version - return(list(paste("PyTorch", paste0("\033[32m", "\u2713", "\033[39m") ,result[1], sep = " "), TRUE, result[1])) + # 检查 Python 路径 + if (!file.exists(python_path)) { + packageStartupMessage("Cannot locate Python. Please ensure Python 3 is installed.") + return(invisible(NULL)) } - # Check if flair is installed + # 设置 Python 路径并检查版本(全部静默执行) + suppressWarnings({ + Sys.setenv(RETICULATE_PYTHON = python_path) + reticulate::use_python(python_path, required = TRUE) + }) + + check_flair_version <- function() { # flair_version_command <- paste(python_path, "-c 'import flair; print(flair.__version__)'") flair_version_command <- paste(python_path, "-c \"import flair; print(flair.__version__)\"") @@ -66,113 +129,35 @@ return(list(paste("flair", paste0("\033[32m", "\u2713", "\033[39m"),result[1], sep = " "), TRUE, result[1])) } - flair_version <- check_flair_version() - torch_version <- check_torch_version() + flair_version <- suppressMessages(check_flair_version()) if (isFALSE(flair_version[[2]])) { packageStartupMessage(sprintf(" Flair %-50s", paste0("is installing from Python"))) - commands <- c( paste(python_path, "-m pip install --upgrade pip"), paste(python_path, "-m pip install torch"), paste(python_path, "-m pip install flair"), paste(python_path, "-m pip install scipy==1.12.0") ) - command_statuses <- vapply(commands, system, FUN.VALUE = integer(1)) - flair_check_again <- check_flair_version() + tryCatch({ + for (cmd in commands) { + system(cmd, intern = TRUE) + } + }, error = function(e) { + packageStartupMessage(paste("Failed to install packages:", e$message)) + }) + + flair_check_again <- suppressMessages(check_flair_version()) if (isFALSE(flair_check_again[[2]])) { packageStartupMessage("Failed to install Flair. {flaiR} requires Flair NLP. Please ensure Flair NLP is installed in Python manually.") } } else { - packageStartupMessage(sprintf("\033[1m\033[34mflaiR\033[39m\033[22m: \033[1m\033[33mAn R Wrapper for Accessing Flair NLP\033[39m\033[22m %-5s", paste("\033[1m\033[33m", flair_version[[3]], "\033[39m\033[22m", sep = ""))) + packageStartupMessage(sprintf("\033[1m\033[34mflaiR\033[39m\033[22m: \033[1m\033[33mAn R Wrapper for Accessing Flair NLP\033[39m\033[22m %-5s", + paste("\033[1m\033[33m", flair_version[[3]], "\033[39m\033[22m", sep = ""))) } } -# .onAttach <- function(...) { -# # Docker 環境檢查及 Python 設置 -# Sys.unsetenv("RETICULATE_PYTHON") -# in_docker <- file.exists("/.dockerenv") -# -# if (in_docker) { -# # Docker 環境使用固定路徑 -# python_path <- "/opt/venv/bin/python3" -# Sys.setenv(RETICULATE_PYTHON = python_path) -# } else { -# # 非 Docker 環境 -# python_cmd <- if (Sys.info()["sysname"] == "Windows") "python" else "python3" -# python_path <- Sys.which(python_cmd) -# } -# -# # 檢查 Python 路徑 -# if (python_path == "") { -# packageStartupMessage("Cannot locate Python executable. Ensure Python is installed and in PATH.") -# return(invisible(NULL)) -# } -# -# # 檢查 Python 版本 -# tryCatch({ -# python_version <- system(paste(python_path, "--version"), intern = TRUE) -# if (!grepl("Python 3", python_version)) { -# packageStartupMessage("Python 3 is required.") -# return(invisible(NULL)) -# } -# }, error = function(e) { -# packageStartupMessage(paste("Failed to check Python version:", e$message)) -# return(invisible(NULL)) -# }) -# -# # Version check functions -# check_torch_version <- function() { -# torch_version_command <- paste(python_path, "-c \"import torch; print(torch.__version__)\"") -# result <- system(torch_version_command, intern = TRUE) -# if (length(result) == 0 || result[1] == "ERROR" || is.na(result[1])) { -# return(list(paste("PyTorch", paste0("\033[31m", "\u2717", "\033[39m"), sep = " "), FALSE)) -# } -# return(list(paste("PyTorch", paste0("\033[32m", "\u2713", "\033[39m"), result[1], sep = " "), TRUE, result[1])) -# } -# -# check_flair_version <- function() { -# flair_version_command <- paste(python_path, "-c \"import flair; print(flair.__version__)\"") -# result <- system(flair_version_command, intern = TRUE) -# if (length(result) == 0 || result[1] == "ERROR" || is.na(result[1])) { -# return(list(paste("flair", paste0("\033[31m", "\u2717", "\033[39m"), sep = " "), FALSE)) -# } -# return(list(paste("flair", paste0("\033[32m", "\u2713", "\033[39m"), result[1], sep = " "), TRUE, result[1])) -# } -# -# flair_version <- check_flair_version() -# torch_version <- check_torch_version() -# -# if (isFALSE(flair_version[[2]])) { -# packageStartupMessage(sprintf(" Flair %-50s", paste0("is installing from Python"))) -# -# # 安裝命令 -# if (in_docker) { -# commands <- c( -# paste(python_path, "-m pip install --no-cache-dir numpy==1.26.4"), -# paste(python_path, "-m pip install --no-cache-dir torch"), -# paste(python_path, "-m pip install --no-cache-dir flair"), -# paste(python_path, "-m pip install --no-cache-dir scipy==1.12.0") -# ) -# } else { -# commands <- c( -# paste(python_path, "-m pip install --upgrade pip"), -# paste(python_path, "-m pip install torch"), -# paste(python_path, "-m pip install flair"), -# paste(python_path, "-m pip install scipy==1.12.0") -# ) -# } -# -# command_statuses <- vapply(commands, system, FUN.VALUE = integer(1)) -# -# flair_check_again <- check_flair_version() -# if (isFALSE(flair_check_again[[2]])) { -# packageStartupMessage("Failed to install Flair. Please install manually.") -# } -# } else { -# packageStartupMessage(sprintf("\033[1m\033[34mflaiR\033[39m\033[22m: \033[1m\033[33mAn R Wrapper for Accessing Flair NLP\033[39m\033[22m %-5s", -# paste("\033[1m\033[33m", flair_version[[3]], "\033[39m\033[22m", sep = ""))) -# } -# } + + diff --git a/README.Rmd b/README.Rmd index cc19893a..14938d8f 100644 --- a/README.Rmd +++ b/README.Rmd @@ -106,26 +106,24 @@ __Intel/AMD Processors:__ ```r # Pull image docker pull ghcr.io/davidycliao/flair-rstudio:latest -``` -```r # Run container -docker run -p 8787:8787 ghcr.io/davidycliao/flair-rstudio:latest +docker run -d -p 8787:8787 --user root --name flair-rstudio ghcr.io/davidycliao/flair-rstudio:latest ``` __Apple Silicon (M1/M2 Mac):__ ```r -# Pull and run with platform specification +# Pull image docker pull --platform linux/amd64 ghcr.io/davidycliao/flair-rstudio:latest -``` -```r -docker run --platform linux/amd64 -p 8787:8787 ghcr.io/davidycliao/flair-rstudio:latest +# Run container +docker run -d -p 8787:8787 --platform linux/amd64 --user root --name flair-rstudio ghcr.io/davidycliao/flair-rstudio:latest ``` -After running these commands in terminal (or powershell), open your browser and navigate to [`http://localhost:8787`]() to access RStudio. + +After running these commands in terminal (or powershell), open your browser and navigate to [`http://localhost:8787`](http://localhost:8787) to access RStudio. For detailed installation instructions, please visit [Quick Start Guide](https://davidycliao.github.io/flaiR/articles/quickstart.html#flair-installation). diff --git a/README.md b/README.md index 09c1a8b0..b2fd8b2a 100644 --- a/README.md +++ b/README.md @@ -92,27 +92,25 @@ library(flaiR) ``` r # Pull image docker pull ghcr.io/davidycliao/flair-rstudio:latest -``` -``` r # Run container -docker run -p 8787:8787 ghcr.io/davidycliao/flair-rstudio:latest +docker run -d -p 8787:8787 --user root --name flair-rstudio ghcr.io/davidycliao/flair-rstudio:latest ``` **Apple Silicon (M1/M2 Mac):** ``` r -# Pull and run with platform specification +# Pull image docker pull --platform linux/amd64 ghcr.io/davidycliao/flair-rstudio:latest -``` -``` r -docker run --platform linux/amd64 -p 8787:8787 ghcr.io/davidycliao/flair-rstudio:latest +# Run container +docker run -d -p 8787:8787 --platform linux/amd64 --user root --name flair-rstudio ghcr.io/davidycliao/flair-rstudio:latest ``` After running these commands in terminal (or powershell), open your -browser and navigate to [`http://localhost:8787`]() to access RStudio. -For detailed installation instructions, please visit [Quick Start +browser and navigate to [`http://localhost:8787`](http://localhost:8787) +to access RStudio. For detailed installation instructions, please visit +[Quick Start Guide](https://davidycliao.github.io/flaiR/articles/quickstart.html#flair-installation). diff --git a/vignettes/quickstart.Rmd b/vignettes/quickstart.Rmd index 1eade630..969331d9 100644 --- a/vignettes/quickstart.Rmd +++ b/vignettes/quickstart.Rmd @@ -99,7 +99,13 @@ __Intel/AMD Processors:__ ```bash # Pull and run docker pull ghcr.io/davidycliao/flair-rstudio:latest -docker run -p 8787:8787 ghcr.io/davidycliao/flair-rstudio:latest +docker run -d \ + -p 8787:8787 \ + --user root \ + -e USER=rstudio \ + -e PASSWORD=rstudio123 \ + --name flair-rstudio \ + ghcr.io/davidycliao/flair-rstudio:latest ``` - Apple Silicon (M1/M2 Mac): @@ -107,12 +113,19 @@ docker run -p 8787:8787 ghcr.io/davidycliao/flair-rstudio:latest ```bash # Pull and run with platform specification docker pull --platform linux/amd64 ghcr.io/davidycliao/flair-rstudio:latest -docker run --platform linux/amd64 -p 8787:8787 ghcr.io/davidycliao/flair-rstudio:latest +docker run -d \ + -p 8787:8787 \ + --platform linux/amd64 \ + --user root \ + -e USER=rstudio \ + -e PASSWORD=rstudio123 \ + --name flair-rstudio \ + ghcr.io/davidycliao/flair-rstudio:latest ``` __Access RStudio Server:__ -- Open browser: `http://localhost:8787` +- Open browser: [`http://localhost:8787`](http://localhost:8787) - Username: `rstudio` - Password: `rstudio123` @@ -220,6 +233,50 @@ If you encounter any problems or have questions: +__Troubleshooting for Docker__ + +1. If you encounter any issues: + +``` +# Try using a different port (e.g., 8788) +docker run -d \ + -p 8788:8787 \ + --platform linux/amd64 \ + --user root \ + --name flair-rstudio \ + ghcr.io/davidycliao/flair-rstudio:latest +``` +Then access via [`http://localhost:8788`](http://localhost:8788) + +2. Container already exists: + +``` +# Remove existing container +docker stop flair-rstudio +docker rm flair-rstudio +``` + +Then retry the run command. + +3. Check container status: + +``` +# View running containers +docker ps +# View all containers including stopped ones +docker ps -a +# View container logs +docker logs flair-rstudio +``` + + + + + + + + + ----- diff --git a/vignettes/tutorial.Rmd b/vignettes/tutorial.Rmd index c81e6df1..8466f2c5 100644 --- a/vignettes/tutorial.Rmd +++ b/vignettes/tutorial.Rmd @@ -2315,9 +2315,9 @@ print(confidence)