在Google Cloud Functions中无法加载Spacy,但本地可以加载。

huangapple go评论67阅读模式
英文:

Problem to load spacy in Google Cloud Functions but not locally

问题

我有一个类似这样的云函数:

import spacy

def entities():
 
    NER = spacy.load("en_core_web_sm")
    text='Looking for a Recruiting Data Analyst who can follow written instructions to correctly identify qualifying attributes from a candidates profile, load and properly code candidates in our recruiting database/CRM'
    text_entities= NER(text)
    entity_vector=[[word.text,word.label_] for word in text_entities.ents]

    return print('Function finished')

entities()

由于某种原因,如果我在我的计算机上执行它,没有问题,但如果我在我的函数中加载它,然后我会遇到以下问题:

OSError: 无法找到模型'en_core_web_sm'。它似乎不是一个Python包或有效的数据目录路径。
请访问https://cloud.google.com/functions/docs/troubleshooting以获取深入的故障排除文档。

我尝试直接导入'en_core_web_sm',但它引发了其他问题。我想知道是否可以手动获取Google函数保存spacy的位置,因为我猜当我搜索它时,环境在错误的位置查找。

英文:

I have a Cloud Function like this one:

import spacy

def entities():
 
    NER = spacy.load("en_core_web_sm")
    text='Looking for a Recruiting Data Analyst who can follow written instructions to correctly identify qualifying attributes from a candidates profile, load and properly code candidates in our recruiting database/CRM'
    text_entities= NER(text)
    entity_vector=[[word.text,word.label_] for word in text_entities.ents]

    return print('Function finished')

entities()

For some reason, If I execute this in my computer, there is no issues, but if I load it in my function, then I get:

> OSError: [E050] Can't find model 'en_core_web_sm'. It doesn't seem to be a Python package or a valid path to a data directory.
> Please visit https://cloud.google.com/functions/docs/troubleshooting for in-depth troubleshooting documentation.

I tried importing 'en_core_web_sm' directly, but I had other issues with it. I wonder If I can manually get where is Google Function saving spacy, becouse I guess that when I look for it, the environment is looking for it in the wrong place.

答案1

得分: 1

以下是翻译好的部分:

这可能会解决你的问题

import spacy
import os

os.system("python -m spacy download en_core_web_sm")

def entities():
 
    NER = spacy.load("en_core_web_sm")
    text='Looking for a Recruiting Data Analyst who can follow written instructions to correctly identify qualifying attributes from a candidates profile, load and properly code candidates in our recruiting database/CRM'
    text_entities= NER(text)
    entity_vector=[[word.text,word.label_] for word in text_entities.ents]

    return print('Function finished')

entities()
英文:

This may solve your problem:

import spacy
import os

os.system("python -m spacy download en_core_web_sm")

def entities():
 
    NER = spacy.load("en_core_web_sm")
    text='Looking for a Recruiting Data Analyst who can follow written instructions to correctly identify qualifying attributes from a candidates profile, load and properly code candidates in our recruiting database/CRM'
    text_entities= NER(text)
    entity_vector=[[word.text,word.label_] for word in text_entities.ents]

    return print('Function finished')

entities()

答案2

得分: 1

以下是要翻译的内容:

For me it worked to add the model to the requirements.txt (as the download python -m spacy command is not executed during deployment):

spacy<3.0.0
https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-2.3.0/en_core_web_sm-2.3.0.tar.gz#egg=en_core_web_sm

and then load it by

import en_core_web_sm
NER = en_core_web_sm.load()

Perhaps also the current version 3.5 of spaCy and the model works, but I use firebase and that version was causing a dependency conflict with the firebase_functions package, so I kept v2.3 for now...

英文:

For me it worked to add the model to the requirements.txt (as the download python -m spacy command is not executed during deployment:

spacy&lt;3.0.0
https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-2.3.0/en_core_web_sm-2.3.0.tar.gz#egg=en_core_web_sm

and then load it by

import en_core_web_sm
NER = en_core_web_sm.load()

Perhaps also the current version 3.5 of spaCy and the model works, but I use firebase and that version was causing a dependency conflict with the firebase_functions package, so I kept v2.3 for now...

huangapple
  • 本文由 发表于 2023年4月4日 16:08:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75926947.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定