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)
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"]
lable = ""
if "guba" in url:
lable = ""
print ("svm file contain guba")
else:
text = request.json["content"]
title = request.json["title"]
print ("svm data receive successful")
content = title + text
word_cut = jieba.lcut(content.strip(), cut_all = False)
news1 = []
news1.append(" ".join(word_cut))
x_test11 = IFvec.transform(news1)
x_test21 = IFtransformer.transform(x_test11)
X_test1 = IFch2.transform(x_test21)
y1 = IFclf.predict(X_test1)
print ("svm model judge successful")
if y1[0] == 0:
key = re.findall(keywords,title)
if key:
lable = lable + ""
else:
lable = ""
else:
lable = lable + ""
rt = {"SVM_TextSort":lable}
print ("svm change json successful")
time_end=time.time()
print ("svm totally cost",time_end-time_start)
return json.dumps(rt)
if __name__ == "__main__":
app.run(host = "0.0.0.0")
Hello, guys. I wrote an interface with the above method. When calling this method on the java side, the processing speed is very slow, sometimes reaching 120s. I test the method with python locally, and the time required for a text is about 0.02s. Why does it take so long to call through java? ask the boss to tell me that the following is the code for calling the py restful interface on the java side
.import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
/**
*
* POST
*
* @author XAYQ-ZhaoXiaoXu
* @create 2018/5/7
* @since 1.0.0
*/
public class PostTest {
private static String URL = "http://172.22.11.41:5000/SVM_TextSort/";
// private static String URL = "http://172.22.11.41:5000/lable_person/";
// private static String URL = "http://172.22.8.81:5000/lable_person/";
public static void main(String[] args) {
Map<String, Object> params = new HashMap<String, Object>();
params.put("siteDomain", "www.sugarinfo.net");
params.put("title", "");
// params.put("content", " 118(NYSE:PPDF)CEO"2018" 10"" "" 14 10\n" +
// "P2P "" (FinTech)(Smart Finance):; 10");
params.put("content", "11111111111111111111");
System.out.println(System.currentTimeMillis());
try {
//
URL url = new URL(URL);//
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestMethod("POST"); //
connection.setRequestProperty("Accept", "application/json"); //
connection.setRequestProperty("Content-Type", "application/json"); //
connection.connect();
//
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8
out.append(JSON.toJSONString(params));
out.flush();
out.close();
int code = connection.getResponseCode();
InputStream is;
if (code == 200) {
System.out.println("");
is = connection.getInputStream();
} else {
System.out.println(":" + code);
is = connection.getErrorStream();
return;
}
System.out.println(System.currentTimeMillis());
//
int length = connection.getContentLength();//
if (length != -1) {
byte[] data = new byte[length];
byte[] temp = new byte[512];
int readLen = 0;
int destPos = 0;
while ((readLen = is.read(temp)) > 0) {
System.arraycopy(temp, 0, data, destPos, readLen);
destPos += readLen;
}
String result = new String(data, "UTF-8"); // utf-8
Map<String, Object> ret = JSON.parseObject(result, new TypeReference<Map<String, Object>>(){});
System.out.println(ret);
// if (ret.get("SVM_TextSort") == null) {
// System.out.println("11111111111111111");
// } else {
// System.out.println("2122222222222222222");
// }
// System.out.println(ret.get("SVM_TextSort"));
//SVM_CHANNEL
List<String> svmChannelList = new ArrayList<String>();
if (svmChannelList.isEmpty()) {
System.out.println("222222222222222222");
}
//SVM_CHANNEL
if (ret.get("SVM_TextSort") != null) {
String str = ret.get("SVM_TextSort").toString().trim();
if (str.contains(",")) {
//
svmChannelList = Arrays.asList(str.split(","));
} else {
//
svmChannelList = Collections.singletonList(str);
}
if (!svmChannelList.isEmpty()) {
System.out.println("11111111111111111111111");
}
}
}
System.out.println(System.currentTimeMillis());
} catch (IOException e) {
e.printStackTrace();
}
}
}