此程序或服务器运行非常慢(加载5-10秒)。我不知道如何让这更快,你知道吗?请帮帮我。非常感谢。这是一个机器学习模型,顺便说一句,特别是LinearSVC。
词干分析器=雪球词干分析器(“英语”)
text_list=[]
categories=['python','javascript','java','c','r','while\u loop','for\u loop']
def cleanPunc(sentence): #function to clean the word of unnecessary punctuation or special characters using re library or regex
cleaned = re.sub(r'[?|!|,|~|^]',r'',sentence)
cleaned = cleaned.strip()
cleaned = cleaned.replace("\n"," ")
return cleaned
def stemming(sentence):
stemSentence = ""
for word in sentence.split():
stem = stemmer.stem(word)
stemSentence += stem
stemSentence += " "
stemSentence = stemSentence.strip()
return stemSentence
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
@app.route('/_get_text_input/', methods=['POST'])
def get_text_input():
#Get new text data
text_list = []
text = request.get_data().decode("utf-8")
text = cleanPunc(text.lower())
text = stemming(text)
text_list.append(text)
print(text_list)
vectorizer = pickle.load(open('vectorizer.sav', 'rb'))
vectorized_text = vectorizer.transform(text_list)
predicted_tags = []
for category in categories:
#load model
filename = 'svc-' + category + '.sav'
loaded_model = pickle.load(open(filename, 'rb'))
result = loaded_model.predict(vectorized_text)
if result[0] == 1:
predicted_tags.append(category)
if len(predicted_tags) == 0:
predicted_tags = {'none'}
print(predicted_tags)
# return jsonify({'data' : predicted_tags})
return jsonify({'data': render_template('response.html', predicted_tags=predicted_tags), 'predicted_tags' : predicted_tags})
if __name__ == "__main__":
app.run(debug=True, threaded=True)
目前没有回答
相关问题 更多 >
编程相关推荐