gpt4 book ai didi

python - 在 flask 中找不到我的模型的路径

转载 作者:行者123 更新时间:2023-12-01 06:47:37 24 4
gpt4 key购买 nike

我的目录结构如下所示:

- project
- server.py
- test-model (dir)

我的脚本如下所示:

@app.route("/spacy/entities", methods=['GET', 'POST'])
def spacy_entities():
import spacy
nlp = spacy.load('test-model') // also tried './test-model'

Can't find model 'test-model'. It doesn't seem to be a shortcut link, a Python package or a valid path to a data directory.

我正在 vagrant 中运行此脚本。

vagrant@homestead:~/Code/sideprojects/project$ pwd
/home/vagrant/Code/sideprojects/project

我尝试过的事情:

1. os.path.realpath('.') -> /home/vagrant/Code
2. os.path.realpath(__file__) -> /home/vagrant/Code/server.py
3. os.path.dirname(os.path.realpath(__file__)) -> /home/vagrant/Code

但它应该是 /home/vagrant/Code/sideprojects/project 以便它可以找到“test-model”。我做错了什么?

(我试图避免放置硬编码路径)

编辑:我尝试过:

path = os.path.dirname(os.path.realpath(__file__)) + '/sideprojects/project'
nlp = spacy.load(path + "/test-model")

Can't find model '/home/vagrant/Code/sideprojects/project/test-model'. It doesn't seem to be a shortcut link, a Python package or a valid path to a data directory.

完整的错误是:

File "server.py", line 40, in spacy_entities nlp = spacy.load(path + "/goal1") File "/home/vagrant/.local/lib/python3.6/site-packages/spacy/init.py", line 30, in load return util.load_model(name, **overrides) File "/home/vagrant/.local/lib/python3.6/site-packages/spacy/util.py", line 169, in load_model raise IOError(Errors.E050.format(name=name))

OSError: [E050] Can't find model '/home/vagrant/Code/sideprojects/project/test-model'. It doesn't seem to be a shortcut link, a Python package or a valid path to a data directory.

最佳答案

创建一个新的py文件,例如modelload.py,然后在其中创建一个函数,例如load_model,在此函数中加载spacy模型

示例代码

# modelload.py
# just make a script which load model and make a function to it

import spacy
model_path = 'test-model' # modifed this to your path and check if this is working fine and load correctly

def load_model(path=None):
if path is None:
path = model_path

nlp = spacy.load(path)
return nlp


# end

在您的路由脚本/server.py 文件中将此模型导入为这样的对象

# server.py

@app.route("/spacy/entities", methods=['GET', 'POST'])
def spacy_entities():
from modelload import load_model
nlp = load_model()
"""give other model names if you want to laod new model
by default it is `test-model`"""

关于python - 在 flask 中找不到我的模型的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59159024/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com