|
19 | 19 | #' @importFrom reticulate py_module_available
|
20 | 20 | #' @importFrom reticulate py_install
|
21 | 21 | #' @keywords internal
|
22 |
| -# .onAttach <- function(...) { |
23 |
| -# # Determine Python command |
24 |
| -# python_cmd <- if (Sys.info()["sysname"] == "Windows") "python" else "python3" |
25 |
| -# python_path <- Sys.which(python_cmd) |
26 |
| -# |
27 |
| -# # Check Python path |
28 |
| -# if (python_path == "") { |
29 |
| -# 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.")) |
30 |
| -# return(invisible(NULL)) # Exit .onAttach without stopping package loading |
31 |
| -# } |
32 |
| -# |
33 |
| -# # Check Python versio Try to get Python version |
34 |
| -# tryCatch({ |
35 |
| -# python_version <- system(paste(python_path, "--version"), intern = TRUE) |
36 |
| -# if (!grepl("Python 3", python_version)) { |
37 |
| -# packageStartupMessage("Python 3 is required, but a different version was found. Please install Python 3. flaiR functionality requiring Python will not be available.") |
38 |
| -# return(invisible(NULL)) # Exit .onAttach without stopping package loading |
39 |
| -# } |
40 |
| -# }, error = function(e) { |
41 |
| -# packageStartupMessage(paste("Failed to get Python version with path:", python_path, "Error:", e$message, ". flaiR functionality requiring Python will not be available.")) |
42 |
| -# return(invisible(NULL)) # Exit .onAttach without stopping package loading |
43 |
| -# }) |
44 |
| -# |
45 |
| -# # Check if PyTorch is installed |
46 |
| -# check_torch_version <- function() { |
47 |
| -# # torch_version_command <- paste(python_path, "-c 'import torch; print(torch.__version__)'") |
48 |
| -# torch_version_command <- paste(python_path, "-c \"import torch; print(torch.__version__)\"") |
49 |
| -# result <- system(torch_version_command, intern = TRUE) |
50 |
| -# if (length(result) == 0 || result[1] == "ERROR" || is.na(result[1])) { |
51 |
| -# return(list(paste("PyTorch", paste0("\033[31m", "\u2717", "\033[39m"), sep = " "), FALSE)) |
52 |
| -# } |
53 |
| -# # Return flair version |
54 |
| -# return(list(paste("PyTorch", paste0("\033[32m", "\u2713", "\033[39m") ,result[1], sep = " "), TRUE, result[1])) |
55 |
| -# } |
56 |
| -# |
57 |
| -# # Check if flair is installed |
58 |
| -# check_flair_version <- function() { |
59 |
| -# # flair_version_command <- paste(python_path, "-c 'import flair; print(flair.__version__)'") |
60 |
| -# flair_version_command <- paste(python_path, "-c \"import flair; print(flair.__version__)\"") |
61 |
| -# result <- system(flair_version_command, intern = TRUE) |
62 |
| -# if (length(result) == 0 || result[1] == "ERROR" || is.na(result[1])) { |
63 |
| -# return(list(paste("flair", paste0("\033[31m", "\u2717", "\033[39m"), sep = " "), FALSE)) |
64 |
| -# } |
65 |
| -# # Return flair version |
66 |
| -# return(list(paste("flair", paste0("\033[32m", "\u2713", "\033[39m"),result[1], sep = " "), TRUE, result[1])) |
67 |
| -# } |
68 |
| -# |
69 |
| -# flair_version <- check_flair_version() |
70 |
| -# torch_version <- check_torch_version() |
71 |
| -# |
72 |
| -# if (isFALSE(flair_version[[2]])) { |
73 |
| -# packageStartupMessage(sprintf(" Flair %-50s", paste0("is installing from Python"))) |
74 |
| -# |
75 |
| -# commands <- c( |
76 |
| -# paste(python_path, "-m pip install --upgrade pip"), |
77 |
| -# paste(python_path, "-m pip install torch"), |
78 |
| -# paste(python_path, "-m pip install flair"), |
79 |
| -# paste(python_path, "-m pip install scipy==1.12.0") |
80 |
| -# ) |
81 |
| -# command_statuses <- vapply(commands, system, FUN.VALUE = integer(1)) |
82 |
| -# |
83 |
| -# flair_check_again <- check_flair_version() |
84 |
| -# if (isFALSE(flair_check_again[[2]])) { |
85 |
| -# packageStartupMessage("Failed to install Flair. {flaiR} requires Flair NLP. Please ensure Flair NLP is installed in Python manually.") |
86 |
| -# } |
87 |
| -# } else { |
88 |
| -# 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 = ""))) |
89 |
| -# } |
90 |
| -# } |
91 | 22 | .onAttach <- function(...) {
|
92 |
| - # Docker 環境檢查及 Python 設置 |
93 |
| - Sys.unsetenv("RETICULATE_PYTHON") |
94 |
| - in_docker <- file.exists("/.dockerenv") |
| 23 | + # Determine Python command |
| 24 | + python_cmd <- if (Sys.info()["sysname"] == "Windows") "python" else "python3" |
| 25 | + python_path <- Sys.which(python_cmd) |
95 | 26 |
|
96 |
| - if (in_docker) { |
97 |
| - # Docker 環境使用固定路徑 |
98 |
| - python_path <- "/opt/venv/bin/python3" |
99 |
| - Sys.setenv(RETICULATE_PYTHON = python_path) |
100 |
| - } else { |
101 |
| - # 非 Docker 環境 |
102 |
| - python_cmd <- if (Sys.info()["sysname"] == "Windows") "python" else "python3" |
103 |
| - python_path <- Sys.which(python_cmd) |
104 |
| - } |
105 |
| - |
106 |
| - # 檢查 Python 路徑 |
| 27 | + # Check Python path |
107 | 28 | if (python_path == "") {
|
108 |
| - packageStartupMessage("Cannot locate Python executable. Ensure Python is installed and in PATH.") |
109 |
| - return(invisible(NULL)) |
| 29 | + 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.")) |
| 30 | + return(invisible(NULL)) # Exit .onAttach without stopping package loading |
110 | 31 | }
|
111 | 32 |
|
112 |
| - # 檢查 Python 版本 |
| 33 | + # Check Python versio Try to get Python version |
113 | 34 | tryCatch({
|
114 | 35 | python_version <- system(paste(python_path, "--version"), intern = TRUE)
|
115 | 36 | if (!grepl("Python 3", python_version)) {
|
116 |
| - packageStartupMessage("Python 3 is required.") |
117 |
| - return(invisible(NULL)) |
| 37 | + packageStartupMessage("Python 3 is required, but a different version was found. Please install Python 3. flaiR functionality requiring Python will not be available.") |
| 38 | + return(invisible(NULL)) # Exit .onAttach without stopping package loading |
118 | 39 | }
|
119 | 40 | }, error = function(e) {
|
120 |
| - packageStartupMessage(paste("Failed to check Python version:", e$message)) |
121 |
| - return(invisible(NULL)) |
| 41 | + packageStartupMessage(paste("Failed to get Python version with path:", python_path, "Error:", e$message, ". flaiR functionality requiring Python will not be available.")) |
| 42 | + return(invisible(NULL)) # Exit .onAttach without stopping package loading |
122 | 43 | })
|
123 | 44 |
|
124 |
| - # Version check functions |
| 45 | + # Check if PyTorch is installed |
125 | 46 | check_torch_version <- function() {
|
| 47 | + # torch_version_command <- paste(python_path, "-c 'import torch; print(torch.__version__)'") |
126 | 48 | torch_version_command <- paste(python_path, "-c \"import torch; print(torch.__version__)\"")
|
127 | 49 | result <- system(torch_version_command, intern = TRUE)
|
128 | 50 | if (length(result) == 0 || result[1] == "ERROR" || is.na(result[1])) {
|
129 | 51 | return(list(paste("PyTorch", paste0("\033[31m", "\u2717", "\033[39m"), sep = " "), FALSE))
|
130 | 52 | }
|
131 |
| - return(list(paste("PyTorch", paste0("\033[32m", "\u2713", "\033[39m"), result[1], sep = " "), TRUE, result[1])) |
| 53 | + # Return flair version |
| 54 | + return(list(paste("PyTorch", paste0("\033[32m", "\u2713", "\033[39m") ,result[1], sep = " "), TRUE, result[1])) |
132 | 55 | }
|
133 | 56 |
|
| 57 | + # Check if flair is installed |
134 | 58 | check_flair_version <- function() {
|
| 59 | + # flair_version_command <- paste(python_path, "-c 'import flair; print(flair.__version__)'") |
135 | 60 | flair_version_command <- paste(python_path, "-c \"import flair; print(flair.__version__)\"")
|
136 | 61 | result <- system(flair_version_command, intern = TRUE)
|
137 | 62 | if (length(result) == 0 || result[1] == "ERROR" || is.na(result[1])) {
|
138 | 63 | return(list(paste("flair", paste0("\033[31m", "\u2717", "\033[39m"), sep = " "), FALSE))
|
139 | 64 | }
|
140 |
| - return(list(paste("flair", paste0("\033[32m", "\u2713", "\033[39m"), result[1], sep = " "), TRUE, result[1])) |
| 65 | + # Return flair version |
| 66 | + return(list(paste("flair", paste0("\033[32m", "\u2713", "\033[39m"),result[1], sep = " "), TRUE, result[1])) |
141 | 67 | }
|
142 | 68 |
|
143 | 69 | flair_version <- check_flair_version()
|
|
146 | 72 | if (isFALSE(flair_version[[2]])) {
|
147 | 73 | packageStartupMessage(sprintf(" Flair %-50s", paste0("is installing from Python")))
|
148 | 74 |
|
149 |
| - # 安裝命令 |
150 |
| - if (in_docker) { |
151 |
| - commands <- c( |
152 |
| - paste(python_path, "-m pip install --no-cache-dir numpy==1.26.4"), |
153 |
| - paste(python_path, "-m pip install --no-cache-dir torch"), |
154 |
| - paste(python_path, "-m pip install --no-cache-dir flair"), |
155 |
| - paste(python_path, "-m pip install --no-cache-dir scipy==1.12.0") |
156 |
| - ) |
157 |
| - } else { |
158 |
| - commands <- c( |
159 |
| - paste(python_path, "-m pip install --upgrade pip"), |
160 |
| - paste(python_path, "-m pip install torch"), |
161 |
| - paste(python_path, "-m pip install flair"), |
162 |
| - paste(python_path, "-m pip install scipy==1.12.0") |
163 |
| - ) |
164 |
| - } |
165 |
| - |
| 75 | + commands <- c( |
| 76 | + paste(python_path, "-m pip install --upgrade pip"), |
| 77 | + paste(python_path, "-m pip install torch"), |
| 78 | + paste(python_path, "-m pip install flair"), |
| 79 | + paste(python_path, "-m pip install scipy==1.12.0") |
| 80 | + ) |
166 | 81 | command_statuses <- vapply(commands, system, FUN.VALUE = integer(1))
|
167 | 82 |
|
168 | 83 | flair_check_again <- check_flair_version()
|
169 | 84 | if (isFALSE(flair_check_again[[2]])) {
|
170 |
| - packageStartupMessage("Failed to install Flair. Please install manually.") |
| 85 | + packageStartupMessage("Failed to install Flair. {flaiR} requires Flair NLP. Please ensure Flair NLP is installed in Python manually.") |
171 | 86 | }
|
172 | 87 | } else {
|
173 |
| - 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", |
174 |
| - paste("\033[1m\033[33m", flair_version[[3]], "\033[39m\033[22m", sep = ""))) |
| 88 | + 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 = ""))) |
175 | 89 | }
|
176 | 90 | }
|
| 91 | + |
| 92 | + |
| 93 | +# .onAttach <- function(...) { |
| 94 | +# # Docker 環境檢查及 Python 設置 |
| 95 | +# Sys.unsetenv("RETICULATE_PYTHON") |
| 96 | +# in_docker <- file.exists("/.dockerenv") |
| 97 | +# |
| 98 | +# if (in_docker) { |
| 99 | +# # Docker 環境使用固定路徑 |
| 100 | +# python_path <- "/opt/venv/bin/python3" |
| 101 | +# Sys.setenv(RETICULATE_PYTHON = python_path) |
| 102 | +# } else { |
| 103 | +# # 非 Docker 環境 |
| 104 | +# python_cmd <- if (Sys.info()["sysname"] == "Windows") "python" else "python3" |
| 105 | +# python_path <- Sys.which(python_cmd) |
| 106 | +# } |
| 107 | +# |
| 108 | +# # 檢查 Python 路徑 |
| 109 | +# if (python_path == "") { |
| 110 | +# packageStartupMessage("Cannot locate Python executable. Ensure Python is installed and in PATH.") |
| 111 | +# return(invisible(NULL)) |
| 112 | +# } |
| 113 | +# |
| 114 | +# # 檢查 Python 版本 |
| 115 | +# tryCatch({ |
| 116 | +# python_version <- system(paste(python_path, "--version"), intern = TRUE) |
| 117 | +# if (!grepl("Python 3", python_version)) { |
| 118 | +# packageStartupMessage("Python 3 is required.") |
| 119 | +# return(invisible(NULL)) |
| 120 | +# } |
| 121 | +# }, error = function(e) { |
| 122 | +# packageStartupMessage(paste("Failed to check Python version:", e$message)) |
| 123 | +# return(invisible(NULL)) |
| 124 | +# }) |
| 125 | +# |
| 126 | +# # Version check functions |
| 127 | +# check_torch_version <- function() { |
| 128 | +# torch_version_command <- paste(python_path, "-c \"import torch; print(torch.__version__)\"") |
| 129 | +# result <- system(torch_version_command, intern = TRUE) |
| 130 | +# if (length(result) == 0 || result[1] == "ERROR" || is.na(result[1])) { |
| 131 | +# return(list(paste("PyTorch", paste0("\033[31m", "\u2717", "\033[39m"), sep = " "), FALSE)) |
| 132 | +# } |
| 133 | +# return(list(paste("PyTorch", paste0("\033[32m", "\u2713", "\033[39m"), result[1], sep = " "), TRUE, result[1])) |
| 134 | +# } |
| 135 | +# |
| 136 | +# check_flair_version <- function() { |
| 137 | +# flair_version_command <- paste(python_path, "-c \"import flair; print(flair.__version__)\"") |
| 138 | +# result <- system(flair_version_command, intern = TRUE) |
| 139 | +# if (length(result) == 0 || result[1] == "ERROR" || is.na(result[1])) { |
| 140 | +# return(list(paste("flair", paste0("\033[31m", "\u2717", "\033[39m"), sep = " "), FALSE)) |
| 141 | +# } |
| 142 | +# return(list(paste("flair", paste0("\033[32m", "\u2713", "\033[39m"), result[1], sep = " "), TRUE, result[1])) |
| 143 | +# } |
| 144 | +# |
| 145 | +# flair_version <- check_flair_version() |
| 146 | +# torch_version <- check_torch_version() |
| 147 | +# |
| 148 | +# if (isFALSE(flair_version[[2]])) { |
| 149 | +# packageStartupMessage(sprintf(" Flair %-50s", paste0("is installing from Python"))) |
| 150 | +# |
| 151 | +# # 安裝命令 |
| 152 | +# if (in_docker) { |
| 153 | +# commands <- c( |
| 154 | +# paste(python_path, "-m pip install --no-cache-dir numpy==1.26.4"), |
| 155 | +# paste(python_path, "-m pip install --no-cache-dir torch"), |
| 156 | +# paste(python_path, "-m pip install --no-cache-dir flair"), |
| 157 | +# paste(python_path, "-m pip install --no-cache-dir scipy==1.12.0") |
| 158 | +# ) |
| 159 | +# } else { |
| 160 | +# commands <- c( |
| 161 | +# paste(python_path, "-m pip install --upgrade pip"), |
| 162 | +# paste(python_path, "-m pip install torch"), |
| 163 | +# paste(python_path, "-m pip install flair"), |
| 164 | +# paste(python_path, "-m pip install scipy==1.12.0") |
| 165 | +# ) |
| 166 | +# } |
| 167 | +# |
| 168 | +# command_statuses <- vapply(commands, system, FUN.VALUE = integer(1)) |
| 169 | +# |
| 170 | +# flair_check_again <- check_flair_version() |
| 171 | +# if (isFALSE(flair_check_again[[2]])) { |
| 172 | +# packageStartupMessage("Failed to install Flair. Please install manually.") |
| 173 | +# } |
| 174 | +# } else { |
| 175 | +# 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", |
| 176 | +# paste("\033[1m\033[33m", flair_version[[3]], "\033[39m\033[22m", sep = ""))) |
| 177 | +# } |
| 178 | +# } |
0 commit comments