Skip to content

Commit

Permalink
linear error handling for codecov coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
madanucd committed Dec 10, 2024
1 parent 7e5119c commit 5193daf
Showing 1 changed file with 29 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,60 +18,35 @@ class SpacyImplementation(TextAnnotatorInterface):
grounding_implementation = None

def init_spacy(self, grounding_implementation: GroundingInterface):
try:
# Define the URL for the Spacy model
model_url = "https://s3-us-west-2.amazonaws.com/ai2-s2-scispacy/releases/v0.5.4/en_core_sci_sm-0.5.4.tar.gz"

# Use pystow.ensure to download and cache the model archive
try:
model_archive = pystow.ensure("spacy", "models", url=model_url)
except Exception as e:
raise RuntimeError(f"Failed to download or cache the Spacy model: {e}")

# Define the expected unpacked directory
model_dir = model_archive.parent / "en_core_sci"

# Unpack the model if it's not already unpacked
if not model_dir.exists():
try:
print("Unpacking Spacy model...")
with tarfile.open(model_archive, "r:gz") as tar:
tar.extractall(path=model_dir.parent)
except (tarfile.TarError, IOError) as e:
raise RuntimeError(f"Error while unpacking the Spacy model archive: {e}")

# Identify the unpacked directory dynamically
model_subdir = next(
(d for d in model_archive.parent.iterdir() if d.is_dir() and d.name.startswith("en_core_sci")), None
)
if not model_subdir:
raise FileNotFoundError(f"Unpacked directory not found in {model_dir}.")

inner_model_dir = next(
(d for d in model_subdir.iterdir() if
d.is_dir() and d.name.startswith("en_core_sci") and "egg-info" not in d.name), None
)
if not inner_model_dir:
raise FileNotFoundError(f"Inner 'en_core_sci' directory not found in {model_subdir}.")

# Load the model
try:
self.nlp = spacy.load(
str(model_archive.parent / model_subdir.name / inner_model_dir.name / model_subdir.name))
except Exception as e:
raise RuntimeError(f"Failed to load the Spacy model: {e}")

self.grounding_implementation = grounding_implementation

# Test the model
try:
self.nlp("Nystagmus, strabismus, fundus, ocular albinism, lewis.")
except Exception as e:
raise RuntimeError(f"Test run of Spacy NLP model failed: {e}")

except Exception as e:
print(f"An error occurred during Spacy initialization: {e}")
raise
# Define the URL for the Spacy model
model_url = "https://s3-us-west-2.amazonaws.com/ai2-s2-scispacy/releases/v0.5.4/en_core_sci_sm-0.5.4.tar.gz"

# Use pystow.ensure to download and cache the model archive
model_archive = pystow.ensure("spacy", "models", url=model_url)
# Define the expected unpacked directory
model_dir = model_archive.parent / "en_core_sci"

# Unpack the model if it's not already unpacked
if not model_dir.exists():
print("Unpacking Spacy model...")
with tarfile.open(model_archive, "r:gz") as tar:
tar.extractall(path=model_dir.parent)

model_subdir = next((d for d in model_archive.parent.iterdir() if d.is_dir() and d.name.startswith("en_core_sci")), None)

if model_subdir:
inner_model_dir = next((d for d in model_subdir.iterdir() if d.is_dir() and d.name.startswith("en_core_sci") and "egg-info" not in d.name), None)
if inner_model_dir:
# Load the model
self.nlp = spacy.load(str(str(model_archive.parent / model_subdir.name / inner_model_dir.name / model_subdir.name)))

# Assign the grounding implementation
self.grounding_implementation = grounding_implementation

# Test the model with a sample sentence
self.nlp("Nystagmus, strabismus, fundus, ocular albinism, lewis.")



def get_annotated_entities(self, text) -> List[TextAnnotationResult]:
"""Annotate text using SPACY"""
Expand Down

0 comments on commit 5193daf

Please sign in to comment.