找不到我的模型在flas中的路径

2024-10-04 03:18:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我的目录结构如下所示:

- 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@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,这样它就可以找到“测试模型”。我做错什么了

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

编辑:我试过:

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.


Tags: topathpytestprojecthomemodelserver
1条回答
网友
1楼 · 发布于 2024-10-04 03:18:43

创建一个新的py文件,比如modelload.py,然后在这个函数中创建一个函数,比如load_model,加载空间模型

示例代码

# 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`"""

相关问题 更多 >