File size: 5,166 Bytes
2db7cb0 | 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | import sys
import os
from pathlib import Path
sys.path.insert(0, os.path.dirname(__file__))
import requests
from bs4 import BeautifulSoup
import json
import time
from tqdm import tqdm
from clean import clean_text
PROJECT_ROOT = Path(__file__).resolve().parents[2]
DOCS_OUTPUT = PROJECT_ROOT / "data" / "docs.json"
URLS = [
"https://huggingface.co/docs/transformers/index/",
"https://huggingface.co/docs/transformers/installation/",
"https://huggingface.co/docs/transformers/quicktour/",
"https://huggingface.co/docs/transformers/weightconverter",
"https://huggingface.co/docs/transformers/models",
"https://huggingface.co/docs/transformers/custom_models",
"https://huggingface.co/docs/transformers/monkey_patching",
"https://huggingface.co/docs/transformers/fusion_mapping",
"https://huggingface.co/docs/transformers/how_to_hack_models",
"https://huggingface.co/docs/transformers/model_sharing",
"https://huggingface.co/docs/transformers/serialization"
]
def normalize_code_block(code: str) -> str:
lines = code.split("\n")
stripped = [l.rstrip() for l in lines]
# Drop leading/trailing blank lines
while stripped and not stripped[0]:
stripped.pop(0)
while stripped and not stripped[-1]:
stripped.pop()
if not stripped:
return ""
def fix_python_spacing(s: str) -> str:
import re
s = re.sub(r'\s*\(\s*', '(', s)
s = re.sub(r'\s*\)\s*', ')', s)
s = re.sub(r'\s*\[\s*', '[', s)
s = re.sub(r'\s*\]\s*', ']', s)
s = re.sub(r'\s*,\s*', ', ', s)
s = re.sub(r'\s*:\s*', ': ', s)
s = re.sub(r'\s*=\s*', '=', s)
s = re.sub(r'([^=!<>])=([^=])', r'\1 = \2', s)
s = re.sub(r'==', ' == ', s)
s = re.sub(r'!=', ' != ', s)
s = re.sub(r' +', ' ', s)
return s.strip()
if len(stripped) <= 4 and all(len(l.split()) <= 4 for l in stripped if l):
joined = " ".join(l for l in stripped if l)
return joined
fixed = []
for line in stripped:
fixed.append(fix_python_spacing(line))
return "\n".join(fixed)
def extract_main_text(soup):
main = soup.find("main")
if not main:
return ""
for tag in main.find_all(["nav", "footer", "aside", "script", "style"]):
tag.decompose()
for tag in main.find_all(True):
if tag.get_text(" ", strip=True).startswith("and get access to the augmented"):
tag.decompose()
break
texts = []
for tag in main.find_all(["h1", "h2", "h3", "p", "li", "pre"]):
if tag.name == "pre":
code_tag = tag.find("code")
raw = (code_tag or tag).get_text("\n")
lang = ""
cls = (code_tag or tag).get("class", [])
for c in cls:
if c.startswith("language-"):
lang = c.replace("language-", "")
break
normalized = normalize_code_block(raw)
if not normalized or len(normalized.strip()) < 3:
continue
texts.append(f"\n```{lang}\n{normalized}\n```\n")
continue
content = tag.get_text(" ", strip=True)
if len(content) < 30:
continue
if tag.name in ["h1", "h2", "h3"]:
texts.append(f"\n{content}\n")
elif tag.name == "li":
texts.append(f"- {content}")
else:
texts.append(content)
return "\n\n".join(texts)
def process_url(url, retries=3):
headers = {"User-Agent": "Mozilla/5.0 (compatible; RAG-doc-fetcher/1.0)"}
for attempt in range(retries):
try:
response = requests.get(url, timeout=10, headers=headers)
response.raise_for_status()
print(f"[SUCCESS] Fetched: {url} (Attempt {attempt+1})\n")
soup = BeautifulSoup(response.text, "html.parser")
text = extract_main_text(soup)
cleaned = clean_text(text)
title = soup.title.string if soup.title else "No Title"
return {
"text": cleaned,
"metadata": {
"source": "huggingface",
"url": url,
"title": title
}
}
except requests.RequestException as e:
print(f"[Attempt {attempt+1}/{retries}] Failed: {url}")
print(f"Error: {e}")
if attempt < retries - 1:
print("Waiting 5 seconds before retry...\n")
time.sleep(5)
else:
print(f"[FAILED] All retries failed for: {url}\n")
return None
def main():
docs = []
for i, url in enumerate(tqdm(URLS, desc="Processing URLs")):
doc = process_url(url)
if doc and doc["text"].strip():
docs.append(doc)
if i < len(URLS) - 1:
time.sleep(0.75)
DOCS_OUTPUT.parent.mkdir(parents=True, exist_ok=True)
with open(DOCS_OUTPUT, "w") as f:
json.dump(docs, f, indent=2)
print(f"Saved {len(docs)} docs to {DOCS_OUTPUT}")
if __name__ == "__main__":
main() |