-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Expand file tree
/
Copy patharxiv_retriever.py
More file actions
226 lines (195 loc) · 7.88 KB
/
arxiv_retriever.py
File metadata and controls
226 lines (195 loc) · 7.88 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
from .base import BaseRetriever, register_retriever
import arxiv
from arxiv import Result as ArxivResult
from ..protocol import Paper
from ..utils import extract_markdown_from_pdf, extract_tex_code_from_tar
from tempfile import TemporaryDirectory
import feedparser
from tqdm import tqdm
import multiprocessing
import os
from queue import Empty
from time import sleep
from typing import Any, Callable, TypeVar
from loguru import logger
import requests
T = TypeVar("T")
DOWNLOAD_TIMEOUT = (10, 60)
PDF_EXTRACT_TIMEOUT = 180
TAR_EXTRACT_TIMEOUT = 180
RETRYABLE_ARXIV_STATUSES = {429, 500, 502, 503, 504}
def _download_file(url: str, path: str) -> None:
with requests.get(url, stream=True, timeout=DOWNLOAD_TIMEOUT) as response:
response.raise_for_status()
with open(path, "wb") as file:
for chunk in response.iter_content(chunk_size=1024 * 1024):
if chunk:
file.write(chunk)
def _run_in_subprocess(
result_queue: Any,
func: Callable[..., T | None],
args: tuple[Any, ...],
) -> None:
try:
result_queue.put(("ok", func(*args)))
except Exception as exc:
result_queue.put(("error", f"{type(exc).__name__}: {exc}"))
def _run_with_hard_timeout(
func: Callable[..., T | None],
args: tuple[Any, ...],
*,
timeout: float,
operation: str,
paper_title: str,
) -> T | None:
start_methods = multiprocessing.get_all_start_methods()
context = multiprocessing.get_context("fork" if "fork" in start_methods else start_methods[0])
result_queue = context.Queue()
process = context.Process(target=_run_in_subprocess, args=(result_queue, func, args))
process.start()
try:
status, payload = result_queue.get(timeout=timeout)
except Empty:
if process.is_alive():
process.kill()
process.join(5)
result_queue.close()
result_queue.join_thread()
logger.warning(f"{operation} timed out for {paper_title} after {timeout} seconds")
return None
process.join(5)
result_queue.close()
result_queue.join_thread()
if status == "ok":
return payload
logger.warning(f"{operation} failed for {paper_title}: {payload}")
return None
def _extract_text_from_pdf_worker(pdf_url: str) -> str:
with TemporaryDirectory() as temp_dir:
path = os.path.join(temp_dir, "paper.pdf")
_download_file(pdf_url, path)
return extract_markdown_from_pdf(path)
def _extract_text_from_html_worker(html_url: str) -> str | None:
import trafilatura
downloaded = trafilatura.fetch_url(html_url)
if downloaded is None:
raise ValueError(f"Failed to download HTML from {html_url}")
text = trafilatura.extract(downloaded, include_comments=False, include_tables=False)
if not text:
raise ValueError(f"No text extracted from {html_url}")
return text
def _extract_text_from_tar_worker(source_url: str, paper_id: str, paper_title: str | None = None) -> str | None:
with TemporaryDirectory() as temp_dir:
path = os.path.join(temp_dir, "paper.tar.gz")
_download_file(source_url, path)
file_contents = extract_tex_code_from_tar(path, paper_id, paper_title=paper_title)
if not file_contents or "all" not in file_contents:
raise ValueError("Main tex file not found.")
return file_contents["all"]
@register_retriever("arxiv")
class ArxivRetriever(BaseRetriever):
def __init__(self, config):
super().__init__(config)
if self.config.source.arxiv.category is None:
raise ValueError("category must be specified for arxiv.")
def _retrieve_raw_papers(self) -> list[ArxivResult]:
client = arxiv.Client(num_retries=10, delay_seconds=10)
query = '+'.join(self.config.source.arxiv.category)
include_cross_list = self.config.source.arxiv.get("include_cross_list", False)
# Get the latest paper from arxiv rss feed
feed = feedparser.parse(f"https://rss.arxiv.org/atom/{query}")
if 'Feed error for query' in feed.feed.title:
raise Exception(f"Invalid ARXIV_QUERY: {query}.")
raw_papers = []
allowed_announce_types = {"new", "cross"} if include_cross_list else {"new"}
all_paper_ids = [
i.id.removeprefix("oai:arXiv.org:")
for i in feed.entries
if i.get("arxiv_announce_type", "new") in allowed_announce_types
]
if self.config.executor.debug:
all_paper_ids = all_paper_ids[:10]
# Get full information of each paper from arxiv api
bar = tqdm(total=len(all_paper_ids))
max_batch_retries = 5
batch_retry_delay = 30
for i in range(0, len(all_paper_ids), 20):
search = arxiv.Search(id_list=all_paper_ids[i:i + 20])
batch_succeeded = False
for attempt in range(max_batch_retries):
try:
batch = list(client.results(search))
bar.update(len(batch))
raw_papers.extend(batch)
batch_succeeded = True
break
except arxiv.HTTPError as exc:
status = getattr(exc, "status", None)
if status in RETRYABLE_ARXIV_STATUSES and attempt < max_batch_retries - 1:
wait = batch_retry_delay * (attempt + 1)
logger.warning(
f"arXiv API {status} on batch {i // 20}, retry {attempt + 1}/{max_batch_retries} in {wait}s"
)
sleep(wait)
elif status in RETRYABLE_ARXIV_STATUSES:
logger.warning(
f"Skipping batch {i // 20} after {max_batch_retries} retries due to arXiv API {status}"
)
break
else:
raise
if not batch_succeeded:
logger.warning(f"No papers retrieved for batch {i // 20}")
if i + 20 < len(all_paper_ids):
sleep(3)
bar.close()
return raw_papers
def convert_to_paper(self, raw_paper: ArxivResult) -> Paper:
title = raw_paper.title
authors = [a.name for a in raw_paper.authors]
abstract = raw_paper.summary
pdf_url = raw_paper.pdf_url
full_text = extract_text_from_tar(raw_paper)
if full_text is None:
full_text = extract_text_from_html(raw_paper)
if full_text is None:
full_text = extract_text_from_pdf(raw_paper)
return Paper(
source=self.name,
title=title,
authors=authors,
abstract=abstract,
url=raw_paper.entry_id,
pdf_url=pdf_url,
full_text=full_text,
)
def extract_text_from_html(paper: ArxivResult) -> str | None:
html_url = paper.entry_id.replace("/abs/", "/html/")
try:
return _extract_text_from_html_worker(html_url)
except Exception as exc:
logger.warning(f"HTML extraction failed for {paper.title}: {exc}")
return None
def extract_text_from_pdf(paper: ArxivResult) -> str | None:
if paper.pdf_url is None:
logger.warning(f"No PDF URL available for {paper.title}")
return None
return _run_with_hard_timeout(
_extract_text_from_pdf_worker,
(paper.pdf_url,),
timeout=PDF_EXTRACT_TIMEOUT,
operation="PDF extraction",
paper_title=paper.title,
)
def extract_text_from_tar(paper: ArxivResult) -> str | None:
source_url = paper.source_url()
if source_url is None:
logger.warning(f"No source URL available for {paper.title}")
return None
return _run_with_hard_timeout(
_extract_text_from_tar_worker,
(source_url, paper.entry_id, paper.title),
timeout=TAR_EXTRACT_TIMEOUT,
operation="Tar extraction",
paper_title=paper.title,
)