Create util.py
Browse files
util.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import warnings
|
2 |
+
import torch
|
3 |
+
import gradio
|
4 |
+
from transformers import pipeline
|
5 |
+
from bs4 import BeautifulSoup
|
6 |
+
import requests
|
7 |
+
|
8 |
+
summarizer = pipeline("summarization", model="stevhliu/my_awesome_billsum_model")
|
9 |
+
url = "https://medium.com/analytics-vidhya/openai-gpt-3-language-models-are-few-shot-learners-82531b3d3122"
|
10 |
+
r = requests.get(url)
|
11 |
+
soup = BeautifulSoup(r.text, 'html.parser')
|
12 |
+
results = soup.find_all(['hl', 'p'])
|
13 |
+
text = [result.text for result in results]
|
14 |
+
Article = ''.join(text)
|
15 |
+
max_chunk = 500
|
16 |
+
Article = Article.replace('.', '')
|
17 |
+
Article = Article.replace('?', '')
|
18 |
+
Article = Article.replace('!', '')
|
19 |
+
|
20 |
+
def SUMMARIZE(Url):
|
21 |
+
Url = "https://medium.com/analytics-vidhya/openai-gpt-3-language-models-are-few-shot-learners-82531b3d3122"
|
22 |
+
summarizer = pipeline("summarization", model="stevhliu/my_awesome_billsum_model")
|
23 |
+
r = requests.get(Url)
|
24 |
+
soup = BeautifulSoup(r.text, 'html.parser')
|
25 |
+
results = soup.find_all(['hl', 'p'])
|
26 |
+
text = [result.text for result in results]
|
27 |
+
Article = ''.join(text)
|
28 |
+
sentences = Article.split(' ')
|
29 |
+
current_chunk = 0
|
30 |
+
chunks = []
|
31 |
+
for sentence in sentences:
|
32 |
+
if len(chunks) == current_chunk + 1:
|
33 |
+
if len(chunks[current_chunk]) + len(sentence.split(' ')) <= max_chunk:
|
34 |
+
chunks[current_chunk].extend(sentence.split(' '))
|
35 |
+
else:
|
36 |
+
current_chunk += 1
|
37 |
+
chunks.append(sentence.split(' '))
|
38 |
+
else:
|
39 |
+
#print(current_chunk)
|
40 |
+
chunks.append(sentence.split(' '))
|
41 |
+
|
42 |
+
for chunk_id in range(len(chunks)):
|
43 |
+
chunks[chunk_id] = ' '.join(chunks[chunk_id])
|
44 |
+
res = summarizer(chunks, max_length=120, min_length=30, do_sample=False)
|
45 |
+
for i in range(len(res)):
|
46 |
+
return res[i].values()
|