numpy icon indicating copy to clipboard operation
numpy copied to clipboard

BUG: DLL load failed while importing numpy_ops: The specified module could not be found.

Open ComEngAlishba opened this issue 2 years ago • 1 comments

Describe the issue:

from .numpy_ops import NumpyOps ImportError: DLL load failed while importing numpy_ops: The specified module could not be found.

Reproduce the code example:

import numpy as np
import spacy

# Load the Spacy model
nlp = spacy.load('en_core_web_sm')

def process(text):
    doc = nlp(text)
    sents = list(doc.sents)
    vecs = np.stack([sent.vector / sent.vector_norm for sent in sents])

    return sents, vecs

def cluster_text(sents, vecs, threshold):
    clusters = [[0]]
    for i in range(1, len(sents)):
        if np.dot(vecs[i], vecs[i-1]) < threshold:
            clusters.append([])
        clusters[-1].append(i)
    
    return clusters

def clean_text(text):
    # Add your text cleaning process here
    return text

def main():
    input_file = r"C:\Users\Alishba\Documents\MSc Project\CyberGuideAI\Selection of pdf test extraction library\CIS Controls - pdfminersix.txt"
    output_file = r"C:\Users\Alishba\Documents\MSc Project\CyberGuideAI\TextChunksOutput.txt"

    with open(input_file, "r", encoding="utf-8") as file:
        text = file.read()

    # Initialize the clusters lengths list and final texts list
    clusters_lens = []
    final_texts = []

    # Process the chunk
    threshold = 0.3
    sents, vecs = process(text)

    # Cluster the sentences
    clusters = cluster_text(sents, vecs, threshold)

    with open(output_file, "w", encoding="utf-8") as output:
        chunk_label = 1
        for cluster in clusters:
            cluster_txt = clean_text(' '.join([sents[i].text for i in cluster]))
            cluster_len = len(cluster_txt)

            # Check if the cluster is too short
            if cluster_len < 60:
                continue

            # Check if the cluster is too long
            elif cluster_len > 3000:
                threshold = 0.6
                sents_div, vecs_div = process(cluster_txt)
                reclusters = cluster_text(sents_div, vecs_div, threshold)

                for subcluster in reclusters:
                    div_txt = clean_text(' '.join([sents_div[i].text for i in subcluster]))
                    div_len = len(div_txt)

                    if div_len < 60 or div_len > 3000:
                        continue

                    clusters_lens.append(div_len)
                    final_texts.append(div_txt)
                    output.write(f"Chunk {chunk_label}:\n{div_txt}\n\n")
                    chunk_label += 1

            else:
                clusters_lens.append(cluster_len)
                final_texts.append(cluster_txt)
                output.write(f"Chunk {chunk_label}:\n{cluster_txt}\n\n")
                chunk_label += 1

if __name__ == "__main__":
    main()

Error message:

Traceback (most recent call last):
  File "c:\Users\2018c\Documents\GitHub\CyberGuideAI\AdjacentClusteringChunkingText.py", line 2, in <module>   
    import spacy
  File "C:\Users\2018c\AppData\Local\Programs\Python\Python312\Lib\site-packages\spacy\__init__.py", line 6, in <module>
    from .errors import setup_default_warnings
  File "C:\Users\2018c\AppData\Local\Programs\Python\Python312\Lib\site-packages\spacy\errors.py", line 3, in <module>
    from .compat import Literal
  File "C:\Users\2018c\AppData\Local\Programs\Python\Python312\Lib\site-packages\spacy\compat.py", line 39, in <module>
    from thinc.api import Optimizer  # noqa: F401
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\2018c\AppData\Local\Programs\Python\Python312\Lib\site-packages\thinc\api.py", line 1, in <module>
    from .backends import (
  File "C:\Users\2018c\AppData\Local\Programs\Python\Python312\Lib\site-packages\thinc\backends\__init__.py", line 18, in <module>
    from .mps_ops import MPSOps
  File "C:\Users\2018c\AppData\Local\Programs\Python\Python312\Lib\site-packages\thinc\backends\mps_ops.py", line 6, in <module>
    from .numpy_ops import NumpyOps
ImportError: DLL load failed while importing numpy_ops: The specified module could not be found.

Python and NumPy Versions:

Python is 3.12.4 and Numpy is 1.26.4

Runtime Environment:

No response

Context for the issue:

No response

ComEngAlishba avatar Jun 11 '24 17:06 ComEngAlishba

From the traceback, this error is coming from thinc. Please report the issue to them or to spacy. This is likely not a problem with numpy.

ngoldbaum avatar Jun 11 '24 20:06 ngoldbaum