-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
53 lines (38 loc) · 1.6 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from flask import Flask, render_template,request, jsonify, json
import logging
import play_with_model, models, data, main, postprocess
import numpy as np
import re
logging.basicConfig(level=logging.DEBUG)
app = Flask(__name__)
@app.route('/')
def hello():
return render_template('home.html')
@app.route('/punctuation',methods=['POST'])
def punctuate():
text = request.form.get('input_text',0,type=str)
is_process = request.form.get('post_process',0,type=str)
print("is process:", is_process)
print("text: " , text)
punctuated_data = punctuate(text)
print(punctuated_data)
if(is_process=="1"):
punctuated_data = postprocess.postProcess3(punctuated_data, 0, 3, 0, 0)
return jsonify(punctuated_data)
def punctuate(text_data):
model_file = "Model_models.py_h256_lr0.02.pcl"
vocab_len = len(data.read_vocabulary(data.WORD_VOCAB_FILE))
x_len = vocab_len if vocab_len < data.MAX_WORD_VOCABULARY_SIZE else data.MAX_WORD_VOCABULARY_SIZE + data.MIN_WORD_COUNT_IN_VOCAB
x = np.ones((x_len, main.MINIBATCH_SIZE)).astype(int)
print("Loading model parameters...")
net, _ = models.load(model_file, x)
print("Building model...")
word_vocabulary = net.x_vocabulary
punctuation_vocabulary = net.y_vocabulary
reverse_punctuation_vocabulary = {v:k for k,v in net.y_vocabulary.items()}
text = re.sub('[,?!।]', '', text_data)
# text = text_data
punctuated = play_with_model.punctuate(word_vocabulary, punctuation_vocabulary, reverse_punctuation_vocabulary, text, net)
return punctuated
if __name__ == "__main__":
app.run(debug=True)