@@ -89,7 +89,12 @@ def __init__(self, config: DeidentificationConfig = DeidentificationConfig()):
8989 torch .load = self .__safe_load
9090 spacy .prefer_gpu ()
9191 if not Deidentification .nlp :
92- Deidentification .nlp = spacy .load (self .config .spacy_model )
92+ try :
93+ Deidentification .nlp = spacy .load (self .config .spacy_model )
94+ except OSError as err :
95+ self .model_not_found_error (str (err ))
96+ except Exception as err :
97+ raise err
9398
9499 def __str__ (self ) -> str :
95100 """Return a string representation of the Deidentification instance.
@@ -107,6 +112,26 @@ def __str__(self) -> str:
107112 ]
108113 return "\n " .join (program_info ) + "\n " + str (self .config )
109114
115+ def model_not_found_error (self , err : str ):
116+ """Handles errors related to missing spaCy models and provides installation instructions.
117+
118+ This function processes spaCy model errors, specifically handling cases where
119+ a required model cannot be found. If the error indicates a missing model,
120+ it prints installation instructions to stderr and exits the program.
121+
122+ Args:
123+ err: Error message string from the spaCy library.
124+ """
125+ print (file = sys .stderr )
126+ print (str (err ), file = sys .stderr )
127+ if "Can't find model" in str (err ):
128+ print (file = sys .stderr )
129+ print ("Please manually run the following command one time to download the required model:" , file = sys .stderr )
130+ print (file = sys .stderr )
131+ print (f"python -m spacy download { self .config .spacy_model } " , file = sys .stderr )
132+ print (file = sys .stderr )
133+ sys .exit (1 )
134+
110135 def deidentify (self , text : str ) -> str :
111136 """De-identify personal information in the input text.
112137
0 commit comments