-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextBot.py
More file actions
139 lines (125 loc) · 4.97 KB
/
Copy pathTextBot.py
File metadata and controls
139 lines (125 loc) · 4.97 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from transformers import pipeline, set_seed
import os
from pytube import YouTube
import json
import torch
import math
class TextBot:
def __init__(self):
self.transcribe_model = pipeline(model="facebook/wav2vec2-large-960h-lv60-self", device=0, framework="pt")
self.summarizer_model = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6", device=0,
framework="pt")
self.nlp = pipeline('question-answering', model="deepset/roberta-large-squad2",
tokenizer="deepset/roberta-large-squad2", device=0, framework="pt")
self.text_generator = pipeline('text-generation', model='gpt2-large', device=0)
self.url = None
self.title = None
self.res = {}
self.yt = None
self.audio_format = '.wav'
self.output_format = '.json'
self.temp_location = './temp/'
torch.cuda.empty_cache()
def process_url(self, url):
self.url = url
self.yt = YouTube(self.url)
title = self.yt.title
title = title.replace('.', '')
title = title.replace('(', '')
title = title.replace(')', '')
title = title.replace('?', '')
title = title.replace('|', '')
title = title.replace(':', '')
print(title)
title = title.replace(' ', '_')
self.title = title
def check_cache(self):
prev_file = self.temp_location + self.title + self.output_format
if os.path.isfile(prev_file):
with open(prev_file) as json_file:
self.res = json.load(json_file)
return self.res
else:
return None
def download_video(self):
title = self.title + self.audio_format
if not os.path.isfile(self.temp_location + title):
video = self.yt.streams.filter(only_audio=True).first()
out_file = video.download(output_path=self.temp_location)
base, ext = os.path.splitext(out_file)
title = self.temp_location + title
try:
os.rename(out_file, title)
except FileExistsError:
os.remove(title)
os.rename(out_file, title)
return title
print('downloaded video->', title)
print('location->', self.temp_location + title)
return './temp/' + title
def transcribe_audio(self):
new_file = self.download_video()
text = self.transcribe_model(new_file, chunk_length_s=60)['text']
self.res['text'] = text.lower()
print('transcribe done')
torch.cuda.empty_cache()
def generate_summary(self):
prev = 0
text = self.res['text']
self.res['summary'] = []
if len(text) > 3000:
batches = math.ceil(len(text) / 3000)
for i in range(1, batches + 1):
summary = self.summarizer_model(text[prev:i * 3000], max_length=150, min_length=30, do_sample=False)
self.res['summary'].append(summary[0]['summary_text'].lower())
prev += 3000
else:
summary = self.summarizer_model(text, max_length=150, min_length=30, do_sample=False)
self.res['summary'].append(summary[0]['summary_text'].lower())
print('summarization done')
torch.cuda.empty_cache()
def summary(self,text):
prev = 0
summary = []
if len(text) > 3000:
batches = math.ceil(len(text) / 3000)
for i in range(1, batches + 1):
t_sum = self.summarizer_model(text[prev:i * 3000], max_length=150, min_length=30, do_sample=False)
summary.append(t_sum[0]['summary_text'].lower())
prev += 3000
else:
t_sum = self.summarizer_model(text, max_length=150, min_length=30, do_sample=False)
summary.append(t_sum[0]['summary_text'].lower())
print('summarization done')
torch.cuda.empty_cache()
return summary
def store_output(self):
with open(self.temp_location + self.title + self.output_format, "w") as outfile:
json.dump(self.res, outfile)
def ask_qa_bot(self, question, context):
qa_input = {
'question': question,
'context': context
}
ans = self.nlp(qa_input)
torch.cuda.empty_cache()
return ans
def text_generation(self, text, multiple):
num = 1
if multiple:
num = 3
resp = self.text_generator(text, max_length=150, num_return_sequences=num, top_k=0,
temperature=0.8, do_sample=True, )
torch.cuda.empty_cache()
return resp
def run_pipeline(self, url, t_only):
self.process_url(url)
cache_hit = self.check_cache()
if cache_hit is not None:
return cache_hit
self.transcribe_audio()
if not t_only:
self.generate_summary()
self.store_output()
torch.cuda.empty_cache()
return self.res