-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.py
More file actions
35 lines (29 loc) · 1.01 KB
/
common.py
File metadata and controls
35 lines (29 loc) · 1.01 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
def toknize_gpt2(text):
from transformers import GPT2TokenizerFast
tokenizer = GPT2TokenizerFast.from_pretrained("gpt2")
return tokenizer.encode(text)
def detokenize_gpt2(tokenized):
from transformers import GPT2TokenizerFast
tokenizer = GPT2TokenizerFast.from_pretrained("gpt2")
return tokenizer.decode(tokenized)
def download(url):
import requests
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0'
}
response = requests.get(url, headers=headers)
response.encoding = 'UTF-8'
return response
def get_text_from_html(html):
from bs4 import BeautifulSoup
soup = BeautifulSoup(html.text, 'html.parser')
title = soup.find('title')
return title.get_text(), soup.get_text()
def split_text(tokenized, budget):
split = []
for i in range(0, len(tokenized), budget):
split.append(tokenized[i:i+budget])
if len(split[-1]) < budget and len(split) > 1:
remaining_budget = budget - len(split[-1])
split[-1] = split[-2][:remaining_budget] + split[-1]
return split