linux python flask restful调用post方法报错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

#########测试==========

#####放在11.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')

运行以上代码报错(/usr/bin/python3: can't open file 'RFIFL.py': [Errno 2] No such file or directory)
具体错误如下

/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

但是当我把代码换成这样的

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

#########测试==========

#####放在11.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')

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") 

这段代码一开始放在post方法外程序是不能正常运行的,当放在post方法下的函数里,程序就可以正常运行,请问这是为什么?
我想把这段代码放在post方法外,不想每次调post方法都加载一遍这些文件,想放在缓存里供post方法直接调用,该怎么操作?求大佬告知。好人一生平安

开心每一天1111
浏览 1064回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python