-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
80 lines (67 loc) · 2.55 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
from flask import Flask, request
from youtube_transcript_api import YouTubeTranscriptApi, NoTranscriptAvailable, VideoUnavailable, TranscriptsDisabled
from youtube_transcript_api.formatters import TextFormatter
from transformers import T5ForConditionalGeneration, T5Tokenizer, BartForConditionalGeneration, BartTokenizer, BartConfig
from flask_cors import CORS
import nltk
import json
nltk.download('punkt')
nltk.download('stopwords')
app = Flask(__name__)
CORS(app)
app.app_context().push()
def get_summary(str1):
model = BartForConditionalGeneration.from_pretrained(
"facebook/bart-large-cnn", output_past=True)
tokenizer = BartTokenizer.from_pretrained(
"facebook/bart-large", output_past=True)
device = 'cuda'
nested = nest_sentences(str1)
input_tokenized = tokenizer.encode(
' '.join(nested[0]), truncation=True, return_tensors='pt')
input_tokenized = input_tokenized.to(device)
summary_ids = model.to("cuda").generate(
input_tokenized, length_penalty=3.0, min_length=30, max_length=100)
output = [tokenizer.decode(g, skip_special_tokens=True,
clean_up_tokenization_spaces=False) for g in summary_ids]
return output
def format_text(document):
formatter = TextFormatter()
transcript = formatter.format_transcript(document).replace("\n", ". ")
return transcript
def nest_sentences(document):
nested = []
sent = []
l = 0
for i in nltk.sent_tokenize(document):
l += len(i)
if (l < 1024):
sent.append(i)
else:
nested.append(sent)
sent = []
l = 0
if (sent):
nested.append(sent)
return nested
def get_youtube_data(youtube_uri):
str1 = ""
try:
data = (YouTubeTranscriptApi.get_transcript(youtube_uri, languages=["en"]))
str1 = format_text(data)
except NoTranscriptAvailable:
return json.dumps({"response" : "No Transcript available for following video"})
except VideoUnavailable:
return json.dumps({"response" : "Video unavailable"})
except TranscriptsDisabled:
return json.dumps({"response" : "No Transcript available for following video"})
print(get_summary(str1))
return json.dumps({"response" : get_summary(str1)[0]})
@app.route("/api/summarize", methods=["GET"])
def summarize():
param = request.args.get("youtube_url")
youtube_uri_arr = param.split("=")
youtube_id = youtube_uri_arr[1]
return (get_youtube_data(youtube_id))
if __name__ == "__main__":
app.run(debug=True, port=2000)