Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,106 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
|
|
|
1 |
+
# How to use: YTVideoToText("https://www.youtube.com/watch?v=jQL0ZeHtXFc")
|
2 |
+
def YTVideoToText(video_link):
|
3 |
+
# installing & importing libraries
|
4 |
+
from transformers import pipeline
|
5 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
6 |
+
|
7 |
+
# fetching video transcript
|
8 |
+
video_id = video_link.split("=")[1]
|
9 |
+
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
10 |
+
|
11 |
+
# iterating throughout and adding all text together
|
12 |
+
result = ""
|
13 |
+
for i in transcript:
|
14 |
+
result += ' ' + i['text']
|
15 |
+
|
16 |
+
# summarize text
|
17 |
+
summarizerfb = pipeline("summarization", model="facebook/bart-large-cnn")
|
18 |
+
|
19 |
+
num_iters = int(len(result)/1000)
|
20 |
+
summarized_text = []
|
21 |
+
summarized_text2 = []
|
22 |
+
for i in range(0, num_iters + 1):
|
23 |
+
start = 0
|
24 |
+
start = i * 1000
|
25 |
+
end = (i + 1) * 1000
|
26 |
+
out = summarizerfb(result[start:end], max_length=130, min_length=30, do_sample=False)
|
27 |
+
out = out[0]
|
28 |
+
out = out['summary_text']
|
29 |
+
summarized_text.append(out)
|
30 |
+
summarized_text2 = ' '.join(summarized_text)
|
31 |
+
|
32 |
+
# returning summary
|
33 |
+
return summarized_text2;
|
34 |
+
|
35 |
+
|
36 |
+
# How to use: postSummaryWithBart("https://ethereum.org/en/what-is-ethereum/")
|
37 |
+
def postSummaryWithBart(blog_link):
|
38 |
+
# importing libraries
|
39 |
+
from transformers import pipeline
|
40 |
+
from bs4 import BeautifulSoup
|
41 |
+
import requests
|
42 |
+
|
43 |
+
# loading summarization pipeline
|
44 |
+
summarizer = pipeline("summarization")
|
45 |
+
|
46 |
+
# getting our blog post
|
47 |
+
URL = blog_link
|
48 |
+
r = requests.get(URL)
|
49 |
+
soup = BeautifulSoup(r.text, 'html.parser')
|
50 |
+
results = soup.find_all(['h1', 'p'])
|
51 |
+
text = [result.text for result in results]
|
52 |
+
ARTICLE = ' '.join(text)
|
53 |
|
54 |
+
# replacing punctuations with end-of-sentence tags
|
55 |
+
ARTICLE = ARTICLE.replace('.', '.')
|
56 |
+
ARTICLE = ARTICLE.replace('?', '?')
|
57 |
+
ARTICLE = ARTICLE.replace('!', '!')
|
58 |
+
sentences = ARTICLE.split('')
|
59 |
|
60 |
+
# chunking text
|
61 |
+
max_chunk = 500
|
62 |
+
current_chunk = 0
|
63 |
+
chunks = []
|
64 |
+
for sentence in sentences:
|
65 |
+
# checking if we have an empty chunk
|
66 |
+
if len(chunks) == current_chunk + 1:
|
67 |
+
if len(chunks[current_chunk]) + len(sentence.split(' ')) <= max_chunk:
|
68 |
+
chunks[current_chunk].extend(sentence.split(' '))
|
69 |
+
else:
|
70 |
+
current_chunk += 1
|
71 |
+
chunks.append(sentence.split(' '))
|
72 |
+
else:
|
73 |
+
print(current_chunk)
|
74 |
+
chunks.append(sentence.split(' '))
|
75 |
+
for chunk_id in range(len(chunks)):
|
76 |
+
chunks[chunk_id] = ' '.join(chunks[chunk_id])
|
77 |
+
|
78 |
+
# summarizing text
|
79 |
+
res = summarizer(chunks, max_length=70, min_length=30, do_sample=False)
|
80 |
+
text = ''.join([summ['summary_text'] for summ in res])
|
81 |
+
|
82 |
+
# returning summary
|
83 |
+
return text;
|
84 |
+
|
85 |
+
|
86 |
+
# How to use: abstractiveSummaryWithPegasus("""Sample text to be summarized""")
|
87 |
+
def abstractiveSummaryWithPegasus(words):
|
88 |
+
# importing & loading model
|
89 |
+
from transformers import PegasusForConditionalGeneration, PegasusTokenizer
|
90 |
+
tokenizer = PegasusTokenizer.from_pretrained("google/pegasus-xsum")
|
91 |
+
model = PegasusForConditionalGeneration.from_pretrained("google/pegasus-xsum")
|
92 |
+
|
93 |
+
# perform summarization
|
94 |
+
tokens = tokenizer(words, truncation=True, padding="longest", return_tensors="pt")
|
95 |
+
summary = model.generate(**tokens)
|
96 |
+
actual_summ = tokenizer.decode(summary[0])
|
97 |
+
|
98 |
+
# returning summary
|
99 |
+
print(actual_summ)
|
100 |
+
|
101 |
+
|
102 |
+
import gradio as gr
|
103 |
+
def process(context, question):
|
104 |
+
pass # Implement your question-answering model here...
|
105 |
|
106 |
+
gr.Interface(fn=process, inputs=["text", "text"], outputs=["textbox", "text"]).launch()
|