from __future__ import unicode_literals
-sharpfrom flask_restful import reqparse,Api, Resource
from flask_restful import Api
from flask import Flask,request
-sharpfrom flask import abort
-sharpfrom flask import make_response,Response
import json
-sharpimport tab
import time
-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharptest/product==========
import re
-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)
test_content = ""
jieba.lcut(test_content.strip(), cut_all = False)
def abspath(filename):
basedir = "/home/nlp/model/IF"
return os.path.join(basedir, filename)
print ("IFFile is loading ......")
from sklearn.externals import joblib
IFclf = joblib.load(abspath("model.m"))
IFvec = joblib.load(abspath("vec.m"))
IFtransformer = joblib.load(abspath("tfidf.m"))
IFch2 = joblib.load(abspath("ch2.m"))
print ("IFFile loading successful")
print ("IFkeyword is loading ......")
keywords = "P2P|p2p|||||||.*||.*\
||||||||"
print ("IFkeyword loading successful")
@app.route("/")
def hello_world():
return "hello world"
@app.route("/SVM_TextSort/", methods=["POST"])
def add_task():
time_start=time.time()
url = request.json["siteDomain"]
-sharpurl = request.form.get("siteDomain")
lable = ""
if "guba" in url:
lable = ""
print ("svm file contain guba")
else:
text = request.json["content"]
title = request.json["title"]
-sharptext = request.form.get("content")
-sharptitle = request.form.get("title")
print ("svm data receive successful")
content = title + text
word_cut = jieba.lcut(content.strip(), cut_all = False)
news1 = []
news1.append(" ".join(word_cut))
print ("===IF judge====")
vec_testif = IFvec.transform(news1)
tf_testif = IFtransformer.transform(vec_testif)
ch2_testif = IFch2.transform(tf_testif)
yif = IFclf.predict(ch2_testif)
print ("svm model judge successful")
if yif[0] == 0:
key = re.findall(keywords,title)
if key:
lable = ""
else:
lable = ""
print ("====im IF rules====")
rt = {"SVM_TextSort":lable}
print ("svm change json successful")
time_end=time.time()
print ("svm totally cost",time_end-time_start)
print (rt)
return json.dumps(rt)
if __name__ == "__main__":
app.run(host = "0.0.0.0")
name the above code RFIFL.py, and place it in the linux background with the following code
nohup python3 -u /home/nlp/pyfoot/IF/RFIFL.py > /home/nlp/pyfoot/IF/RFIFL.log 2>&1 &
when accessing through other computers with the following code, the whole processing process is slow. At first, it is distributed in the background of this computer, and it only takes 0.0002s to access the whole process through the local computer. After being arranged in the background of linux, and accessed through other computers, the processing flow reaches about 2s, and the efficiency is too slow. May I ask how to solve the problem
import requests
user_info ={
"siteDomain": "letian",
"title": "ST:",
"content": """20181272018126:5350
"""
}
requests.post("http://127.0.0.1:5000/SVM_TextSort/", data=user_info)