-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
69 lines (53 loc) · 2.14 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
import json
from flask import Flask, request
from flask_cors import CORS, cross_origin
from prometheus_flask_exporter import PrometheusMetrics
from transformers import pipeline
from transformers import BertTokenizerFast, EncoderDecoderModel
import torch
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
# tokenizer = BertTokenizerFast.from_pretrained('mrm8488/bert-small2bert-small-finetuned-cnn_daily_mail-summarization')
# model = EncoderDecoderModel.from_pretrained('mrm8488/bert-small2bert-small-finetuned-cnn_daily_mail-summarization')
# def generate_summary(text):
# # cut off at BERT max length 512
# inputs = tokenizer([text], padding="max_length", truncation=True, max_length=512, return_tensors="pt")
# input_ids = inputs.input_ids
# attention_mask = inputs.attention_mask
# output = model.generate(input_ids, attention_mask=attention_mask)
# return tokenizer.decode(output[0], skip_special_tokens=True)
metrics = PrometheusMetrics(app)
@app.route("/", methods=['GET'])
def welcome():
return "Welcome to summarization closed beta 0.1"
@app.route("/summarize", methods=['POST'])
@cross_origin()
def similarity_route():
request_data = request.get_json()
text = request_data.get("text")
if text is None or type(text) is not str:
return "Wrong args", 400
num_of_words = len(text.split())
if num_of_words < 30:
return "Less than 30 words", 400
### TODO:
# - split text + merge output
if num_of_words > 1000:
return "Only support 1000 words now", 400
else:
try:
result = summarizer(text, max_length=max(130, num_of_words // 3) , min_length=30, do_sample=False)
# result = generate_summary(text)
# result = {'summary': result}
except:
return "Failed to summarize", 500
response = app.response_class(
response=json.dumps(result),
status=200,
mimetype='application/json'
)
return response
if __name__ == "__main__":
app.run(host='0.0.0.0', port=3001, debug=True)