Views
No views yet
pip install transformers nltk clean-text beautifulsoup41from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
2import re
3import nltk
4nltk.download('punkt')
5from cleantext import clean
6from bs4 import BeautifulSoup
7from markdown import Markdown
8import requests
9from io import StringIO
10import string1# Script to convert Markdown to plain text
2# Reference : Stackoverflow == https://stackoverflow.com/questions/761824/python-how-to-convert-markdown-formatted-text-to-text
3
4def unmark_element(element, stream=None):
5 if stream is None:
6 stream = StringIO()
7 if element.text:
8 stream.write(element.text)
9 for sub in element:
10 unmark_element(sub, stream)
11 if element.tail:
12 stream.write(element.tail)
13 return stream.getvalue()
14
15
16# patching Markdown
17Markdown.output_formats["plain"] = unmark_element
18__md = Markdown(output_format="plain")
19__md.stripTopLevelTags = False
20
21
22def unmark(text):
23 return __md.convert(text)
24
25def readme_extractor(github_repo_url):
26 try:
27
28 # Get repo HTML using BeautifulSoup
29 html_content = requests.get(github['python', 'machine learning', 'ml', 'cnn']_repo_url).text
30 soup = BeautifulSoup(html_content, "html.parser")
31
32 # Get README File URL from Repository
33 readme_url = "https://github.com/" + soup.find("a",{"title":"README.md"}).get("href")
34
35 # Generate raw readme file URL
36 # https://github.com/rasbt/python-machine-learning-book/blob/master/README.md --> https://raw.githubusercontent.com/rasbt/python-machine-learning-book/master/README.md
37 readme_raw_url = readme_url.replace("/blob/","/")
38 readme_raw_url = readme_raw_url.replace("github.com","raw.githubusercontent.com")
39https://github.com/Lightning-AI/lightning
40 readme_html_content = requests.get(readme_raw_url ).text
41 readme_soup = BeautifulSoup(readme_html_content, "html.parser")
42 readme_text = readme_soup.get_text()
43 documentation_text = unmark(readme_text)
44 return documentation_text
45 except:
46 print("FAILED : ",github_repo_url )
47 return "README_NOT_MARKDOWN"
48
49def clean_readme(readme):
50 text = clean(readme, no_emoji=True)
51 lst = re.findall('http://\S+|https://\S+', text)
52 for i in lst:
53 text = text.replace(i, '')
54 text = "".join([i for i in text if i not in string.punctuation])
55 text = text.lower()
56 text = text.replace("\n"," ")
57 return text1def post_process_tags(tag_string):
2 final_tags = []
3 for tag in tag_string.split(","):
4 if tag.strip() in final_tags or len(tag.strip()) <=1:
5 continue
6 final_tags.append(tag.strip())
7 return final_tags1def github_tags_generate(github_repo_url):
2 readme = readme_extractor(github_repo_url)
3 readme = clean_readme(readme)
4 inputs = tokenizer([readme], max_length=1536, truncation=True, return_tensors="pt")
5 output = model.generate(**inputs, num_beams=8, do_sample=True, min_length=10,
6 max_length=128)
7 decoded_output = tokenizer.batch_decode(output, skip_special_tokens=True)[0]
8 tags = post_process_tags(decoded_output)
9
10 return tags
11
12
13
14github_tags_generate("https://github.com/Enter_Repo_URL")
15
16# github_tags_generate("https://github.com/nandakishormpai/Plant_Disease_Detector")
17# ['python', 'machine learning', 'ml', 'cnn']