-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
87 lines (66 loc) Β· 2.07 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# -*- coding: utf-8 -*-
from flask import Flask,render_template,request, jsonify
from flask_cors import CORS
import torch
from sum_model import summarize_model
from ext import textrank_summarize
from qa_model import get_qa_model
app = Flask(__name__)
cors = CORS(app)
#home
@app.route('/')
def home():
return render_template('home.html')
#summary page
@app.route('/sum')
def index():
return render_template('index.html')
#general summarization
@app.route('/sum/gsummarize', methods=['POST'])
def gsummarize():
try:
data = request.get_json(force=True)
context = data['context']
gsum = summarize_model(context)
response = jsonify({'gsum': gsum})
except Exception as e:
response = jsonify({'error': str(e)})
return response
# keysentence extraction
@app.route('/sum/key',methods=['POST'])
def key():
try:
data = request.get_json(force=True)
context = data['context']
keytext = textrank_summarize(context,1) #λ¬Έμ₯ μ μ‘°μ νμ
response = jsonify({'keytext': keytext})
except Exception as e:
response = jsonify({'error': str(e)})
return response
#qa
@app.route('/sum/qa', methods=['POST'])
def qa_endpoint():
try:
data = request.get_json(force=True)
context = data['context']
question = data['question']
if question == "":
response = jsonify({'error': 'μ§λ¬Έμ μ
λ ₯ν΄μ£ΌμΈμ.'})
return response
to_predict = [{"context": context, "qas": [{"question": question, "id": "0"}]}]
result = qa_model.predict(to_predict)
answer = result[0][0]['answer'][0]
answer = "μ μ ν λ΅λ³μ μ°Ύμ μ μμ΅λλ€." if answer == '' else answer
response = jsonify({'answer': answer})
except Exception as e:
response = jsonify({'error': str(e)})
return response
if __name__ == '__main__':
model_path = 'model/checkpoint-1119-epoch-1'
qa_model = get_qa_model(model_path, use_cuda=False)
# general summarization
# summodel_path = 'model/model.pt'
# sum_model=get_sum_model(summodel_path)
# sum_model.load_state_dict(torch.load(summodel_path), strict=False)
# sum_model.eval()
app.run(host='127.0.0.1',port=5000,debug=True)