|
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 | | - |
92 | 22 | .onAttach <- function(...) { |
93 | | - # Clear any existing RETICULATE_PYTHON setting first |
94 | | - Sys.unsetenv("RETICULATE_PYTHON") |
95 | | - |
96 | 23 | # Determine Python command |
97 | 24 | python_cmd <- if (Sys.info()["sysname"] == "Windows") "python" else "python3" |
98 | 25 | python_path <- Sys.which(python_cmd) |
99 | 26 |
|
100 | | - # If python_path is empty, try to find Python from common locations |
101 | | - if (python_path == "") { |
102 | | - common_paths <- c( |
103 | | - "/usr/bin/python3", |
104 | | - "/usr/local/bin/python3", |
105 | | - Sys.getenv("CONDA_PYTHON_EXE"), # Check for conda environment |
106 | | - file.path(Sys.getenv("USERPROFILE"), "Anaconda3", "python.exe"), # Windows Anaconda |
107 | | - file.path(Sys.getenv("HOME"), "anaconda3", "bin", "python") # Unix Anaconda |
108 | | - ) |
109 | | - |
110 | | - for (path in common_paths) { |
111 | | - if (file.exists(path)) { |
112 | | - python_path <- path |
113 | | - break |
114 | | - } |
115 | | - } |
116 | | - } |
117 | | - |
118 | 27 | # Check Python path |
119 | 28 | if (python_path == "") { |
120 | 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.")) |
121 | | - return(invisible(NULL)) |
| 30 | + return(invisible(NULL)) # Exit .onAttach without stopping package loading |
122 | 31 | } |
123 | 32 |
|
124 | | - # Set RETICULATE_PYTHON to the found Python path |
125 | | - Sys.setenv(RETICULATE_PYTHON = python_path) |
126 | | - |
127 | | - # Try to initialize reticulate with the found Python |
128 | | - tryCatch({ |
129 | | - if (!requireNamespace("reticulate", quietly = TRUE)) { |
130 | | - install.packages("reticulate") |
131 | | - } |
132 | | - reticulate::use_python(python_path, required = TRUE) |
133 | | - }, error = function(e) { |
134 | | - packageStartupMessage(paste("Failed to initialize Python environment:", e$message)) |
135 | | - return(invisible(NULL)) |
136 | | - }) |
137 | | - |
138 | | - # Check Python version |
| 33 | + # Check Python versio Try to get Python version |
139 | 34 | tryCatch({ |
140 | 35 | python_version <- system(paste(python_path, "--version"), intern = TRUE) |
141 | 36 | if (!grepl("Python 3", python_version)) { |
142 | 37 | packageStartupMessage("Python 3 is required, but a different version was found. Please install Python 3. flaiR functionality requiring Python will not be available.") |
143 | | - return(invisible(NULL)) |
| 38 | + return(invisible(NULL)) # Exit .onAttach without stopping package loading |
144 | 39 | } |
145 | 40 | }, error = function(e) { |
146 | 41 | packageStartupMessage(paste("Failed to get Python version with path:", python_path, "Error:", e$message, ". flaiR functionality requiring Python will not be available.")) |
147 | | - return(invisible(NULL)) |
| 42 | + return(invisible(NULL)) # Exit .onAttach without stopping package loading |
148 | 43 | }) |
149 | 44 |
|
150 | | - # Version check functions |
| 45 | + # Check if PyTorch is installed |
151 | 46 | check_torch_version <- function() { |
| 47 | + # torch_version_command <- paste(python_path, "-c 'import torch; print(torch.__version__)'") |
152 | 48 | torch_version_command <- paste(python_path, "-c \"import torch; print(torch.__version__)\"") |
153 | 49 | result <- system(torch_version_command, intern = TRUE) |
154 | 50 | if (length(result) == 0 || result[1] == "ERROR" || is.na(result[1])) { |
155 | 51 | return(list(paste("PyTorch", paste0("\033[31m", "\u2717", "\033[39m"), sep = " "), FALSE)) |
156 | 52 | } |
157 | | - 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])) |
158 | 55 | } |
159 | 56 |
|
| 57 | + # Check if flair is installed |
160 | 58 | check_flair_version <- function() { |
| 59 | + # flair_version_command <- paste(python_path, "-c 'import flair; print(flair.__version__)'") |
161 | 60 | flair_version_command <- paste(python_path, "-c \"import flair; print(flair.__version__)\"") |
162 | 61 | result <- system(flair_version_command, intern = TRUE) |
163 | 62 | if (length(result) == 0 || result[1] == "ERROR" || is.na(result[1])) { |
164 | 63 | return(list(paste("flair", paste0("\033[31m", "\u2717", "\033[39m"), sep = " "), FALSE)) |
165 | 64 | } |
166 | | - 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])) |
167 | 67 | } |
168 | 68 |
|
169 | | - # Check versions and install if needed |
170 | 69 | flair_version <- check_flair_version() |
171 | 70 | torch_version <- check_torch_version() |
172 | 71 |
|
173 | 72 | if (isFALSE(flair_version[[2]])) { |
174 | 73 | packageStartupMessage(sprintf(" Flair %-50s", paste0("is installing from Python"))) |
| 74 | + |
175 | 75 | commands <- c( |
176 | 76 | paste(python_path, "-m pip install --upgrade pip"), |
177 | 77 | paste(python_path, "-m pip install torch"), |
178 | 78 | paste(python_path, "-m pip install flair"), |
179 | 79 | paste(python_path, "-m pip install scipy==1.12.0") |
180 | 80 | ) |
181 | 81 | command_statuses <- vapply(commands, system, FUN.VALUE = integer(1)) |
| 82 | + |
182 | 83 | flair_check_again <- check_flair_version() |
183 | 84 | if (isFALSE(flair_check_again[[2]])) { |
184 | 85 | packageStartupMessage("Failed to install Flair. {flaiR} requires Flair NLP. Please ensure Flair NLP is installed in Python manually.") |
185 | 86 | } |
186 | 87 | } else { |
187 | | - 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", |
188 | | - 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 = ""))) |
189 | 89 | } |
190 | 90 | } |
191 | 91 |
|
|
0 commit comments