-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
31 lines (23 loc) · 1.11 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
from flask import Flask, render_template, request
from transformers import PegasusForConditionalGeneration, PegasusTokenizer
import torch
app = Flask(__name__)
model_name = "google/pegasus-xsum"
tokenizer = PegasusTokenizer.from_pretrained(model_name)
device = "cuda" if torch.cuda.is_available() else "cpu"
device = "cpu"
model = PegasusForConditionalGeneration.from_pretrained(model_name).to(device)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/text-summarization', methods=["POST"])
def summarize():
if request.method == "POST":
inputtext = request.form["inputtext_"]
input_text = "summarize: " + inputtext
tokenized_text = tokenizer.encode(input_text, return_tensors='pt', max_length=512).to(device)
summary_ = model.generate(tokenized_text, min_length=30, max_length=300)
summary = tokenizer.decode(summary_[0], skip_special_tokens=True)
return render_template("output.html", data = {"summary": summary})
if __name__ == '__main__': # It Allows You to Execute Code When the File Runs as a Script
app.run()