-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
59 lines (51 loc) · 1.83 KB
/
app.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
54
55
56
57
58
59
"""
API of Text Generation Framework
"""
from flask import Flask, request, jsonify, Response
import json
from configuration import ModelConfig, DeployConfig
from datahandler import DataReader
from generator import TextGenerator
from src import (make_generation,
CommentGeneratorDataset,
SpacyNER,
PostgreSQLDatabase)
import keys
APP = Flask(__name__)
MODEL_CONFIG = ModelConfig().get_args()
DEPLOY_CONFIG = DeployConfig().get_args()
DATABASE = PostgreSQLDatabase(database=keys.DATABASE,
user=keys.USER,
password=keys.PASSWORD,
host=keys.HOST,
port=keys.PORT)
GENERATOR_MODEL = TextGenerator(MODEL_CONFIG, DEPLOY_CONFIG)
NER_MODEL = SpacyNER(model_name=MODEL_CONFIG.spacy_ner_model, ners=MODEL_CONFIG.ners)
@APP.route("/ping")
def ping():
return "testing"
@APP.route("/gen-text", methods=['POST'])
def gen_text():
"""
- requrest body:
{
"text": "this is a text",
"num-sequences": 3
}
- output
{
"result": ["text-1", "text-2", "text-3"]
}
"""
data = request.get_json(force=True)
text, num_sequences = data['text'], data['num-sequences']
predicts = make_generation(generator_model=GENERATOR_MODEL,
ner_model=NER_MODEL,
dataset=CommentGeneratorDataset,
input_text=text,
database=DATABASE,
num_sequences=num_sequences)
result = {'result':predicts}
return Response(json.dumps(result, ensure_ascii=False), content_type='application/json')
if __name__=="__main__":
APP.run(debug=False, host='0.0.0.0')