Skip to content

Commit

Permalink
Merge pull request #277 from davidycliao/0.1.7
Browse files Browse the repository at this point in the history
0.1.7
  • Loading branch information
davidycliao authored Jan 6, 2025
2 parents ce72144 + 22ccd58 commit 395fc51
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 144 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 && \
Expand Down
225 changes: 105 additions & 120 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -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__)\"")
Expand All @@ -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 = "")))
# }
# }


14 changes: 6 additions & 8 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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).

</div>
Expand Down
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

</div>
Expand Down
Loading

0 comments on commit 395fc51

Please sign in to comment.