-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
43 lines (34 loc) · 1.44 KB
/
server.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
from flask import Flask, request, jsonify
from summarizer import Summarizer
from flask_cors import CORS
from PyPDF2 import PdfReader
from transformers import BartForConditionalGeneration, BartTokenizer, PegasusForConditionalGeneration, PegasusTokenizer
app = Flask(__name__)
CORS(app)
bert_model = Summarizer()
model = BartForConditionalGeneration.from_pretrained('facebook/bart-large-cnn')
tokenizer = BartTokenizer.from_pretrained('facebook/bart-large-cnn')
@app.route('/', methods = ['GET', 'POST'])
def home():
if(request.method == 'GET'):
data = "hello world"
return jsonify({'data': data})
@app.route('/summarize', methods=['POST'])
def summarize():
if 'file' not in request.files:
return jsonify({'error': 'No file part'})
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'})
pdf_reader = PdfReader(file)
text = ''
for page in pdf_reader.pages:
text += page.extract_text()
inputs = tokenizer.encode(text, return_tensors="pt", max_length=1024, truncation=True)
summary_ids = model.generate(inputs, num_beams=4, min_length=50, max_length=400, early_stopping=True)
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
return summary
def bert_summarize(text):
return ''.join(bert_model(text, min_length=100, max_length=1000))
if __name__ == '__main__':
app.run(host= '0.0.0.0',port=8080)