Linux python flask restful calls the post method to report an error no such file

from __future__ import unicode_literals
from flask import Flask
from flask_restful import reqparse,Api, Resource
from flask import Flask,jsonify,request
from flask import abort  

from flask import make_response,Response  
import json

-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp==========

-sharp-sharp-sharp-sharp-sharp11.41
import jieba
import os
import jieba.analyse
        
app = Flask(__name__)
app.debug = True
app.config.update(RESTFUL_JSON=dict(ensure_ascii=False))
api = Api(app)

os.chdir("/home/nlp/model/IF")
from sklearn.externals import joblib
clf = joblib.load("model.m")
vec = joblib.load("vec.m")
transformer = joblib.load("tfidf.m")
ch2 = joblib.load("ch2.m")

@app.route("/")
def hello_world():  
    return "hello world"

@app.route("/add_task/", methods=["POST"])
def add_task():
       
    url = request.json["siteDomain"]
    if ("guba" in url) or ("" in "text"):
        lable = 0  
    else:
        text = request.json["content"]
        title = request.json["title"]
        content = title + text       
        word_cut = jieba.lcut(content.strip(), cut_all = False)       
        news1 = []
        news1.append(" ".join(word_cut))                            
        x_test11 = vec.transform(news1)               
        x_test21 = transformer.transform(x_test11)
        X_test1 = ch2.transform(x_test21)          
        y1 = clf.predict(X_test1)
        if y1[0] == 0:
            lable = 0 
        else:
            lable = 1       
    rt = {"number":lable}
    return json.dumps(rt)

if __name__ == "__main__":
    app.run(host = "0.0.0.0")

run the above code to report an error (/ usr/bin/python3: can"t open file "RFIFL.py": [Errno 2] No such file or directory)
specific errors are as follows

/usr/local/python/lib/python3.6/site-packages/sklearn/base.py:311: UserWarning: Trying to unpickle estimator SVC from version 0.18.1 when using version 0.19.1. This might lead to breaking code or invalid results. Use at your own risk.
  UserWarning)
/usr/local/python/lib/python3.6/site-packages/sklearn/base.py:311: UserWarning: Trying to unpickle estimator GridSearchCV from version 0.18.1 when using version 0.19.1. This might lead to breaking code or invalid results. Use at your own risk.
  UserWarning)
/usr/local/python/lib/python3.6/site-packages/sklearn/base.py:311: UserWarning: Trying to unpickle estimator CountVectorizer from version 0.18.1 when using version 0.19.1. This might lead to breaking code or invalid results. Use at your own risk.
  UserWarning)
/usr/local/python/lib/python3.6/site-packages/sklearn/base.py:311: UserWarning: Trying to unpickle estimator TfidfTransformer from version 0.18.1 when using version 0.19.1. This might lead to breaking code or invalid results. Use at your own risk.
  UserWarning)
/usr/local/python/lib/python3.6/site-packages/sklearn/base.py:311: UserWarning: Trying to unpickle estimator SelectKBest from version 0.18.1 when using version 0.19.1. This might lead to breaking code or invalid results. Use at your own risk.
  UserWarning)
 * Serving Flask app "RFIFL" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
 * Restarting with stat
/usr/bin/python3: can"t open file "RFIFL.py": [Errno 2] No such file or directory

but when I replace the code with this

from __future__ import unicode_literals
from flask import Flask
from flask_restful import reqparse,Api, Resource
from flask import Flask,jsonify,request
from flask import abort  

from flask import make_response,Response  
import json

-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp==========

-sharp-sharp-sharp-sharp-sharp11.41
import jieba
import os
import jieba.analyse
        
app = Flask(__name__)
app.debug = True
app.config.update(RESTFUL_JSON=dict(ensure_ascii=False))
api = Api(app)

@app.route("/")
def hello_world():  
    return "hello world"

@app.route("/add_task/", methods=["POST"])
def add_task():
    os.chdir("/home/nlp/model/IF")
    from sklearn.externals import joblib
    clf = joblib.load("model.m")
    vec = joblib.load("vec.m")
    transformer = joblib.load("tfidf.m")
    ch2 = joblib.load("ch2.m")   
    url = request.json["siteDomain"]
    if ("guba" in url) or ("" in "text"):
        lable = 0  
    else:
        text = request.json["content"]
        title = request.json["title"]
        content = title + text       
        word_cut = jieba.lcut(content.strip(), cut_all = False)       
        news1 = []
        news1.append(" ".join(word_cut))                            
        x_test11 = vec.transform(news1)               
        x_test21 = transformer.transform(x_test11)
        X_test1 = ch2.transform(x_test21)          
        y1 = clf.predict(X_test1)
        if y1[0] == 0:
            lable = 0 
        else:
            lable = 1       
    rt = {"number":lable}
    return json.dumps(rt)

if __name__ == "__main__":
    app.run(host = "0.0.0.0")

that is

os.chdir("/home/nlp/model/IF")
from sklearn.externals import joblib
clf = joblib.load("model.m")
vec = joblib.load("vec.m")
transformer = joblib.load("tfidf.m")
ch2 = joblib.load("ch2.m") 

this code does not work properly when placed outside the post method at the beginning. When it is placed in the function under the post method, the program can run normally. Why is this?
I want to put this code outside the post method. I don"t want to load these files every time I call the post method. I want to put it in the cache for the post method to call directly. What should I do? Ask the boss to let me know. A good man is safe all his life

Mar.10,2021

I suggest putting

def abspath(filename):
    basedir = '/home/nlp/model/IF'
    return os.path.join(basedir, filename)

vec = joblib.load(abspath("vec.m"))

the reason why can't open file 'RFIFL.py': [Errno 2] No such file or directory appears is that the base path is changed at first with os.chdir () , while app = Flask (_ name__) , we cannot find / home/nlp/model/IF/RFIFL.py entry file in the modified path.

Menu