Spaces:
Sleeping
Sleeping
app
Browse files- Dockerfile +32 -0
- app.py +53 -0
- main.py +27 -2
- requirements.txt +13 -0
- supervisord.conf +20 -0
- zotero_refactor.ipynb +0 -746
Dockerfile
CHANGED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.10
|
2 |
+
|
3 |
+
RUN useradd -m -u 1000 user
|
4 |
+
USER user
|
5 |
+
ENV HOME=/home/user \
|
6 |
+
PATH=/home/user/.local/bin:$PATH
|
7 |
+
|
8 |
+
# Set the working directory
|
9 |
+
WORKDIR $HOME/app
|
10 |
+
|
11 |
+
COPY requirements.txt .
|
12 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
13 |
+
RUN git config --global credential.helper store
|
14 |
+
|
15 |
+
COPY . .
|
16 |
+
COPY supervisord.conf .
|
17 |
+
|
18 |
+
# Set permissions on the log file
|
19 |
+
USER root
|
20 |
+
RUN touch $HOME/app/mylog.log $HOME/app/supervisord.log && chmod a+rwx $HOME/app/mylog.log $HOME/app/supervisord.log
|
21 |
+
USER user
|
22 |
+
|
23 |
+
RUN mkdir -p /tmp/cache/
|
24 |
+
RUN mkdir -p /.cache
|
25 |
+
RUN chmod a+rwx -R /tmp/cache/
|
26 |
+
RUN chmod a+rwx -R /.cache
|
27 |
+
ENV HF_HUB_CACHE=HF_HOME
|
28 |
+
|
29 |
+
ENV PYTHONUNBUFFERED=1 PORT=7860
|
30 |
+
|
31 |
+
# Run supervisord
|
32 |
+
CMD ["supervisord", "-c", "supervisord.conf"]
|
app.py
CHANGED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
import dotenv
|
4 |
+
import pandas as pd
|
5 |
+
from datasets import load_dataset
|
6 |
+
from fasthtml.common import *
|
7 |
+
|
8 |
+
dotenv.load_dotenv()
|
9 |
+
|
10 |
+
app, rt = fast_app()
|
11 |
+
from huggingface_hub import login, whoami
|
12 |
+
|
13 |
+
login(token=os.environ.get("HF_TOKEN"))
|
14 |
+
|
15 |
+
hf_user = whoami(os.environ.get("HF_TOKEN"))["name"]
|
16 |
+
HF_REPO_ID = f"{hf_user}/zotero-answer-ai-articles"
|
17 |
+
|
18 |
+
|
19 |
+
@rt("/")
|
20 |
+
async def get():
|
21 |
+
"""Dataset viewer home page"""
|
22 |
+
return Titled(
|
23 |
+
"Zotero Dataset Viewer",
|
24 |
+
Form(
|
25 |
+
Input(id="dataset_subset", placeholder="Enter dataset subset"),
|
26 |
+
Button("Load Dataset"),
|
27 |
+
hx_post="/load_dataset",
|
28 |
+
hx_target="#dataset_content",
|
29 |
+
),
|
30 |
+
Div(id="dataset_content"),
|
31 |
+
)
|
32 |
+
|
33 |
+
|
34 |
+
@rt("/load_dataset")
|
35 |
+
async def post(dataset_subset: str):
|
36 |
+
"""Load and display dataset"""
|
37 |
+
try:
|
38 |
+
dataset = load_dataset(HF_REPO_ID, dataset_subset, split="train")
|
39 |
+
df = pd.DataFrame(dataset[:10]) # Load first 10 rows
|
40 |
+
table = df.to_html(classes="table", index=False)
|
41 |
+
|
42 |
+
return Div(
|
43 |
+
H2(f"Dataset: {dataset_name}"),
|
44 |
+
P(f"Number of rows: {len(dataset)}"),
|
45 |
+
P(f"Columns: {', '.join(df.columns)}"),
|
46 |
+
Div(NotStr(table), cls="table-responsive"),
|
47 |
+
)
|
48 |
+
|
49 |
+
except Exception as e:
|
50 |
+
return Div(f"Error loading dataset: {str(e)}")
|
51 |
+
|
52 |
+
|
53 |
+
serve()
|
main.py
CHANGED
@@ -1,9 +1,11 @@
|
|
1 |
import os
|
2 |
import re
|
|
|
3 |
|
4 |
import dotenv
|
5 |
import pandas as pd
|
6 |
import requests
|
|
|
7 |
import srsly
|
8 |
from bs4 import BeautifulSoup
|
9 |
from datasets import Dataset, load_dataset
|
@@ -372,8 +374,11 @@ def main():
|
|
372 |
processed_arxiv_ids.add(item["arxiv_id"])
|
373 |
except Exception as e:
|
374 |
print(f"Failed to parse html for {item['arxiv_id']}: {e}")
|
|
|
|
|
|
|
375 |
print("Extracting from pdf...")
|
376 |
-
md_content = get_pdf_text(item["arxiv_id"])
|
377 |
if md_content:
|
378 |
item["contents"] = parse_markdown_content(md_content, item["arxiv_id"])
|
379 |
processed_arxiv_ids.add(item["arxiv_id"])
|
@@ -431,5 +436,25 @@ def main():
|
|
431 |
srsly.write_json("data/processed_arxiv_ids.json", processed_arxiv_ids)
|
432 |
|
433 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
434 |
if __name__ == "__main__":
|
435 |
-
|
|
|
1 |
import os
|
2 |
import re
|
3 |
+
import time
|
4 |
|
5 |
import dotenv
|
6 |
import pandas as pd
|
7 |
import requests
|
8 |
+
import schedule
|
9 |
import srsly
|
10 |
from bs4 import BeautifulSoup
|
11 |
from datasets import Dataset, load_dataset
|
|
|
374 |
processed_arxiv_ids.add(item["arxiv_id"])
|
375 |
except Exception as e:
|
376 |
print(f"Failed to parse html for {item['arxiv_id']}: {e}")
|
377 |
+
item["contents"] = []
|
378 |
+
|
379 |
+
if len(item["contents"]) == 0:
|
380 |
print("Extracting from pdf...")
|
381 |
+
md_content = get_pdf_text(item["arxiv_id"]) # fix this
|
382 |
if md_content:
|
383 |
item["contents"] = parse_markdown_content(md_content, item["arxiv_id"])
|
384 |
processed_arxiv_ids.add(item["arxiv_id"])
|
|
|
436 |
srsly.write_json("data/processed_arxiv_ids.json", processed_arxiv_ids)
|
437 |
|
438 |
|
439 |
+
def schedule_periodic_task():
|
440 |
+
"""
|
441 |
+
Schedule the main task to run at the user-defined frequency
|
442 |
+
"""
|
443 |
+
main() # run once initially
|
444 |
+
|
445 |
+
frequency = "daily" # TODO: env
|
446 |
+
if frequency == "hourly":
|
447 |
+
print("Scheduling tasks to run every hour at the top of the hour")
|
448 |
+
schedule.every().hour.at(":00").do(main)
|
449 |
+
elif frequency == "daily":
|
450 |
+
start_time = "10:00"
|
451 |
+
print("Scheduling tasks to run every day at: {start_time} UTC+00")
|
452 |
+
schedule.every().day.at(start_time).do(main)
|
453 |
+
|
454 |
+
while True:
|
455 |
+
schedule.run_pending()
|
456 |
+
time.sleep(1)
|
457 |
+
|
458 |
+
|
459 |
if __name__ == "__main__":
|
460 |
+
schedule_periodic_task()
|
requirements.txt
CHANGED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fasthtml-hf>=0.1.1
|
2 |
+
python-fasthtml>=0.0.8
|
3 |
+
huggingface-hub>=0.20.0
|
4 |
+
uvicorn>=0.29
|
5 |
+
schedule==1.2.0
|
6 |
+
supervisor==4.2.5
|
7 |
+
requests
|
8 |
+
srsly
|
9 |
+
python-dotenv
|
10 |
+
beautifulsoup4
|
11 |
+
retry
|
12 |
+
pandas
|
13 |
+
datasets
|
supervisord.conf
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[supervisord]
|
2 |
+
nodaemon=true
|
3 |
+
|
4 |
+
[program:main]
|
5 |
+
command=python main.py
|
6 |
+
stdout_logfile=/dev/stdout
|
7 |
+
stdout_logfile_maxbytes=0
|
8 |
+
stderr_logfile=/dev/stderr
|
9 |
+
stderr_logfile_maxbytes=0
|
10 |
+
autostart=true
|
11 |
+
# autorestart=true
|
12 |
+
|
13 |
+
[program:app]
|
14 |
+
command=python app.py
|
15 |
+
stdout_logfile=/dev/null
|
16 |
+
stdout_logfile_maxbytes=0
|
17 |
+
stderr_logfile=/dev/stderr
|
18 |
+
stderr_logfile_maxbytes=0
|
19 |
+
autostart=true
|
20 |
+
autorestart=true
|
zotero_refactor.ipynb
DELETED
@@ -1,746 +0,0 @@
|
|
1 |
-
{
|
2 |
-
"cells": [
|
3 |
-
{
|
4 |
-
"cell_type": "code",
|
5 |
-
"execution_count": 1,
|
6 |
-
"id": "bfd057e4",
|
7 |
-
"metadata": {},
|
8 |
-
"outputs": [],
|
9 |
-
"source": [
|
10 |
-
"%ai reset"
|
11 |
-
]
|
12 |
-
},
|
13 |
-
{
|
14 |
-
"cell_type": "code",
|
15 |
-
"execution_count": 2,
|
16 |
-
"id": "1c3aa2dc",
|
17 |
-
"metadata": {},
|
18 |
-
"outputs": [],
|
19 |
-
"source": [
|
20 |
-
"%%ai -c\n",
|
21 |
-
"I'm refactoring the following code to ingest and process items from a zotero lib.\n",
|
22 |
-
"\n",
|
23 |
-
"```python\n",
|
24 |
-
"import re\n",
|
25 |
-
"\n",
|
26 |
-
"import requests\n",
|
27 |
-
"import srsly\n",
|
28 |
-
"from bs4 import BeautifulSoup\n",
|
29 |
-
"from retry import retry\n",
|
30 |
-
"from tqdm.auto import tqdm\n",
|
31 |
-
"\n",
|
32 |
-
"\n",
|
33 |
-
"\n",
|
34 |
-
"def get_pdf_text(arxiv_id):\n",
|
35 |
-
" url = \"http://147.189.194.113:80/extract\"\n",
|
36 |
-
"\n",
|
37 |
-
" try:\n",
|
38 |
-
" response = requests.get(url, params={\"arxiv_id\": arxiv_id})\n",
|
39 |
-
" response = response.json()\n",
|
40 |
-
" if \"text\" in response:\n",
|
41 |
-
" return response['text']\n",
|
42 |
-
" return None\n",
|
43 |
-
" except Exception as e:\n",
|
44 |
-
" print(e)\n",
|
45 |
-
" return None\n",
|
46 |
-
" \n",
|
47 |
-
"\n",
|
48 |
-
"\n",
|
49 |
-
"def get_zotero_items():\n",
|
50 |
-
" \"\"\"fetch all items from zotero library\"\"\"\n",
|
51 |
-
"\n",
|
52 |
-
" GROUP_ID = \"5004697\"\n",
|
53 |
-
" API_KEY = \"HFfTT8uuL7z9tN0L5VCHe7Ow\"\n",
|
54 |
-
" BASE_URL = f\"https://api.zotero.org/groups/{GROUP_ID}/items\"\n",
|
55 |
-
" LIMIT = 100\n",
|
56 |
-
"\n",
|
57 |
-
" headers = {\"Zotero-API-Key\": API_KEY, \"Content-Type\": \"application/json\"}\n",
|
58 |
-
"\n",
|
59 |
-
" items = []\n",
|
60 |
-
" start = 0\n",
|
61 |
-
"\n",
|
62 |
-
" ITEM_TYPES = [\n",
|
63 |
-
" \"journalArticle\",\n",
|
64 |
-
" \"report\",\n",
|
65 |
-
" \"conferencePaper\",\n",
|
66 |
-
" \"manuscript\",\n",
|
67 |
-
" \"book\",\n",
|
68 |
-
" \"thesis\",\n",
|
69 |
-
" ] # Replace or extend with desired item types\n",
|
70 |
-
"\n",
|
71 |
-
" i = 1\n",
|
72 |
-
" while True:\n",
|
73 |
-
" i += 1\n",
|
74 |
-
" params = {\"limit\": LIMIT, \"start\": start}\n",
|
75 |
-
" response = requests.get(BASE_URL, headers=headers, params=params)\n",
|
76 |
-
" page_items = response.json()\n",
|
77 |
-
" if ITEM_TYPES:\n",
|
78 |
-
" params[\"itemType\"] = \",\".join(ITEM_TYPES)\n",
|
79 |
-
"\n",
|
80 |
-
" if not page_items:\n",
|
81 |
-
" break\n",
|
82 |
-
"\n",
|
83 |
-
" items.extend(page_items)\n",
|
84 |
-
" start += LIMIT\n",
|
85 |
-
" print(f\"# items fetched {len(items)}\")\n",
|
86 |
-
" # break # debug\n",
|
87 |
-
"\n",
|
88 |
-
" return items\n",
|
89 |
-
"\n",
|
90 |
-
"\n",
|
91 |
-
"def extract_arxiv_links_and_pdfs(items):\n",
|
92 |
-
" \"\"\"Extract mapping between arxiv & pdf urls for a list of zotero items\n",
|
93 |
-
"\n",
|
94 |
-
" Args:\n",
|
95 |
-
" items (list): list of zotero items\n",
|
96 |
-
"\n",
|
97 |
-
" Returns:\n",
|
98 |
-
" arxiv_info (dict): dictionary with arxiv links as keys and pdf links as values\n",
|
99 |
-
" \"\"\"\n",
|
100 |
-
" arxiv_info = {}\n",
|
101 |
-
" arxiv_pattern = re.compile(r\"arxiv.org/abs/(\\d+\\.\\d+)\")\n",
|
102 |
-
"\n",
|
103 |
-
" for item in items:\n",
|
104 |
-
" data = item.get(\"data\", {})\n",
|
105 |
-
" attachments = item.get(\"links\", {}).get(\"attachment\", {})\n",
|
106 |
-
" arxiv_url = None\n",
|
107 |
-
" pdf_url = None\n",
|
108 |
-
"\n",
|
109 |
-
" if \"url\" in data and \"arxiv.org\" in data[\"url\"]:\n",
|
110 |
-
" arxiv_match = arxiv_pattern.search(data[\"url\"])\n",
|
111 |
-
" if arxiv_match:\n",
|
112 |
-
" arxiv_url = data[\"url\"]\n",
|
113 |
-
"\n",
|
114 |
-
" if attachments:\n",
|
115 |
-
" pdf_url = attachments[\"href\"]\n",
|
116 |
-
"\n",
|
117 |
-
" if arxiv_url:\n",
|
118 |
-
" arxiv_info[arxiv_url] = pdf_url\n",
|
119 |
-
"\n",
|
120 |
-
" return arxiv_info\n",
|
121 |
-
"\n",
|
122 |
-
"\n",
|
123 |
-
"@retry(tries=3, delay=15, backoff=2)\n",
|
124 |
-
"def get_arxiv_with_retry(arxiv_id):\n",
|
125 |
-
" return requests.get(f\"https://ar5iv.labs.arxiv.org/html/{arxiv_id}\")\n",
|
126 |
-
"\n",
|
127 |
-
"\n",
|
128 |
-
"def get_arxiv(arxiv_ids):\n",
|
129 |
-
" \"\"\"\n",
|
130 |
-
" Fetch html version of arxiv papers\n",
|
131 |
-
" \"\"\"\n",
|
132 |
-
" html5ivs = {}\n",
|
133 |
-
" for i, arxiv_id in tqdm(enumerate(arxiv_ids)):\n",
|
134 |
-
" if \"v\" in arxiv_id:\n",
|
135 |
-
" arxiv_id = arxiv_id.split(\"v\")[0]\n",
|
136 |
-
" response = get_arxiv_with_retry(arxiv_id)\n",
|
137 |
-
" html5ivs[arxiv_id] = response.text\n",
|
138 |
-
" if response.status_code != 200:\n",
|
139 |
-
" html5ivs[arxiv_id] = \"ERROR\"\n",
|
140 |
-
" return html5ivs\n",
|
141 |
-
"\n",
|
142 |
-
"\n",
|
143 |
-
"def extract_html_content(html):\n",
|
144 |
-
" \"\"\"\n",
|
145 |
-
" Extract content from arxiv html\n",
|
146 |
-
" \"\"\"\n",
|
147 |
-
" arxiv_id_match = re.search(r\"\\[(\\d+\\.\\d+(v\\d+)?)\\]\", html)\n",
|
148 |
-
" arxiv_id = arxiv_id_match.group(1) if arxiv_id_match else None\n",
|
149 |
-
" soup = BeautifulSoup(html, \"html.parser\")\n",
|
150 |
-
" result = []\n",
|
151 |
-
"\n",
|
152 |
-
" # Extract paper title\n",
|
153 |
-
" paper_title = soup.find(\"h1\", class_=\"ltx_title ltx_title_document\").get_text(strip=True)\n",
|
154 |
-
"\n",
|
155 |
-
" for math in soup.find_all(\"math\"):\n",
|
156 |
-
" math.decompose()\n",
|
157 |
-
" for cite in soup.find_all(\"cite\"):\n",
|
158 |
-
" cite.decompose()\n",
|
159 |
-
"\n",
|
160 |
-
" # Extract abstract\n",
|
161 |
-
" abstract = soup.find(\"div\", class_=\"ltx_abstract\")\n",
|
162 |
-
" if abstract:\n",
|
163 |
-
" result.append(\n",
|
164 |
-
" {\n",
|
165 |
-
" \"content\": \" \".join(p.get_text(strip=True) for p in abstract.find_all(\"p\")).replace(\")\", \") \"),\n",
|
166 |
-
" \"title\": \"Abstract\",\n",
|
167 |
-
" \"paper_title\": paper_title,\n",
|
168 |
-
" \"content_type\": \"abstract\",\n",
|
169 |
-
" }\n",
|
170 |
-
" )\n",
|
171 |
-
"\n",
|
172 |
-
" # Extract introduction\n",
|
173 |
-
" intro = soup.find(\"section\", id=\"S1\")\n",
|
174 |
-
" if intro:\n",
|
175 |
-
" section_title = \"Introduction\"\n",
|
176 |
-
" paragraphs = intro.find_all(\"div\", class_=\"ltx_para\")\n",
|
177 |
-
" for para in paragraphs:\n",
|
178 |
-
" result.append(\n",
|
179 |
-
" {\n",
|
180 |
-
" \"content\": para.get_text(strip=True).replace(\")\", \") \"),\n",
|
181 |
-
" \"title\": section_title,\n",
|
182 |
-
" \"paper_title\": paper_title,\n",
|
183 |
-
" \"content_type\": \"introduction\",\n",
|
184 |
-
" }\n",
|
185 |
-
" )\n",
|
186 |
-
"\n",
|
187 |
-
" # Extract other sections (excluding introduction and conclusion)\n",
|
188 |
-
" sections = soup.find_all(\"section\", class_=\"ltx_section\")\n",
|
189 |
-
" for section in sections[1:-1]:\n",
|
190 |
-
" section_title = section.find(\"h2\", class_=\"ltx_title ltx_title_section\").get_text(strip=True)\n",
|
191 |
-
" paragraphs = section.find_all(\"div\", class_=\"ltx_para\")\n",
|
192 |
-
" for para in paragraphs:\n",
|
193 |
-
" result.append(\n",
|
194 |
-
" {\n",
|
195 |
-
" \"content\": para.get_text(strip=True).replace(\")\", \") \"),\n",
|
196 |
-
" \"title\": section_title,\n",
|
197 |
-
" \"paper_title\": paper_title,\n",
|
198 |
-
" \"content_type\": \"body\",\n",
|
199 |
-
" }\n",
|
200 |
-
" )\n",
|
201 |
-
"\n",
|
202 |
-
" # Extract conclusion\n",
|
203 |
-
" conclusion = soup.find(\"section\", id=\"S4\")\n",
|
204 |
-
" if conclusion:\n",
|
205 |
-
" result.append(\n",
|
206 |
-
" {\n",
|
207 |
-
" \"content\": \" \".join(\n",
|
208 |
-
" p.get_text(strip=True) for p in conclusion.find_all(\"div\", class_=\"ltx_para\")\n",
|
209 |
-
" ).replace(\")\", \") \"),\n",
|
210 |
-
" \"title\": conclusion.find(\"h2\", class_=\"ltx_title ltx_title_section\").get_text(strip=True),\n",
|
211 |
-
" \"paper_title\": paper_title,\n",
|
212 |
-
" \"content_type\": \"conclusion\",\n",
|
213 |
-
" }\n",
|
214 |
-
" )\n",
|
215 |
-
"\n",
|
216 |
-
" for c in result:\n",
|
217 |
-
" c[\"arxiv_id\"] = arxiv_id\n",
|
218 |
-
"\n",
|
219 |
-
" return result\n",
|
220 |
-
"\n",
|
221 |
-
"\n",
|
222 |
-
"def pool_html_content(parsed, pool_size=3, step_size=2):\n",
|
223 |
-
" \"\"\"\n",
|
224 |
-
" Pool together individual bits of contents from \"body\" and \"introduction\" sections where the \"title\" is the same.\n",
|
225 |
-
" It pools them 'pool_size' by 'pool_size' as a sliding window with a step size of 'step_size'.\n",
|
226 |
-
" Keeps all other attributes for each element and includes abstract and conclusion without pooling.\n",
|
227 |
-
"\n",
|
228 |
-
" Args:\n",
|
229 |
-
" parsed (list): List of dictionaries containing parsed content.\n",
|
230 |
-
" pool_size (int): Size of the pool for the sliding window.\n",
|
231 |
-
" step_size (int): Step size for the sliding window.\n",
|
232 |
-
"\n",
|
233 |
-
" Returns:\n",
|
234 |
-
" list: A list of pooled contents including non-pooled abstract and conclusion.\n",
|
235 |
-
" \"\"\"\n",
|
236 |
-
" pooled_contents = []\n",
|
237 |
-
" i = 0\n",
|
238 |
-
" while i < len(parsed) - pool_size + 1:\n",
|
239 |
-
" current_window = parsed[i : i + pool_size]\n",
|
240 |
-
" # Check if all entries in the current window have the same title and are from 'body' or 'introduction'\n",
|
241 |
-
" if all(\n",
|
242 |
-
" entry[\"content_type\"] in [\"body\", \"introduction\"] and entry[\"title\"] == current_window[0][\"title\"]\n",
|
243 |
-
" for entry in current_window\n",
|
244 |
-
" ):\n",
|
245 |
-
" pooled_content = \"\\n\".join(entry[\"content\"] for entry in current_window)\n",
|
246 |
-
" if \"bit\" not in current_window[0][\"title\"][:5]:\n",
|
247 |
-
" title = current_window[0][\"title\"].lstrip(\"0123456789\")\n",
|
248 |
-
" pooled_content = f\"{current_window[0]['paper_title']}\\n{title}\\n\\n\" + pooled_content\n",
|
249 |
-
" pooled_entry = {\n",
|
250 |
-
" \"title\": title,\n",
|
251 |
-
" \"text\": pooled_content,\n",
|
252 |
-
" \"paper_title\": current_window[0][\"paper_title\"],\n",
|
253 |
-
" \"content_type\": current_window[0][\"content_type\"],\n",
|
254 |
-
" \"arxiv_id\": current_window[0][\"arxiv_id\"],\n",
|
255 |
-
" }\n",
|
256 |
-
" pooled_contents.append(pooled_entry)\n",
|
257 |
-
" i += step_size\n",
|
258 |
-
"\n",
|
259 |
-
" # Include abstract and conclusion without pooling\n",
|
260 |
-
" for entry in parsed:\n",
|
261 |
-
" if entry[\"content_type\"] in [\"abstract\", \"conclusion\"]:\n",
|
262 |
-
" if \"content\" in entry:\n",
|
263 |
-
" entry[\"text\"] = entry[\"content\"]\n",
|
264 |
-
" del entry[\"content\"]\n",
|
265 |
-
" if \"bit\" not in entry[\"title\"][:5]:\n",
|
266 |
-
" entry[\"title\"] = entry[\"title\"].lstrip(\"0123456789\")\n",
|
267 |
-
" entry[\"text\"] = f\"{entry['paper_title']}\\n{entry['title']}\\n\\n\" + entry[\"text\"]\n",
|
268 |
-
" pooled_contents.append(entry)\n",
|
269 |
-
"\n",
|
270 |
-
" return pooled_contents\n",
|
271 |
-
"\n",
|
272 |
-
"\n",
|
273 |
-
"def parse_markdown_content(md_content, arxiv_id):\n",
|
274 |
-
" \"\"\"\n",
|
275 |
-
" Parses markdown content to identify and extract sections based on headers.\n",
|
276 |
-
" \"\"\"\n",
|
277 |
-
"\n",
|
278 |
-
" lines = md_content.split(\"\\n\")\n",
|
279 |
-
" parsed = []\n",
|
280 |
-
" current_section = None\n",
|
281 |
-
" content = []\n",
|
282 |
-
" paper_title = None\n",
|
283 |
-
" current_title = None\n",
|
284 |
-
"\n",
|
285 |
-
" # Identify sections based on headers\n",
|
286 |
-
" for line in lines:\n",
|
287 |
-
" if line.startswith(\"#\"):\n",
|
288 |
-
" if paper_title is None:\n",
|
289 |
-
" paper_title = line.lstrip(\"#\").lstrip(\"#\").strip()\n",
|
290 |
-
" continue\n",
|
291 |
-
" if content:\n",
|
292 |
-
" if not current_title:\n",
|
293 |
-
" print(\"skipping pre-abstract!\")\n",
|
294 |
-
" parsed.append(\n",
|
295 |
-
" {\n",
|
296 |
-
" \"title\": current_title,\n",
|
297 |
-
" \"text\": content,\n",
|
298 |
-
" \"paper_title\": paper_title,\n",
|
299 |
-
" \"content_type\": \"body\"\n",
|
300 |
-
" if current_section not in [\"Abstract\", \"Introduction\", \"Conclusion\"]\n",
|
301 |
-
" else current_section.lower(),\n",
|
302 |
-
" \"arxiv_id\": arxiv_id,\n",
|
303 |
-
" }\n",
|
304 |
-
" )\n",
|
305 |
-
" content = []\n",
|
306 |
-
"\n",
|
307 |
-
" if \"Introduction\" in line:\n",
|
308 |
-
" current_section = \"Introduction\"\n",
|
309 |
-
" elif \"Abstract\" in line:\n",
|
310 |
-
" current_section = \"Abstract\"\n",
|
311 |
-
" elif \"Conclusion\" in line:\n",
|
312 |
-
" current_section = \"Conclusion\"\n",
|
313 |
-
" elif \"Reference\" in line:\n",
|
314 |
-
" break\n",
|
315 |
-
" else:\n",
|
316 |
-
" current_section = \"body\"\n",
|
317 |
-
"\n",
|
318 |
-
" current_title = line.lstrip(\"#\").lstrip(\"#\").lstrip()\n",
|
319 |
-
"\n",
|
320 |
-
" if \"bit\" not in current_title:\n",
|
321 |
-
" current_title = (\n",
|
322 |
-
" current_title.lstrip(\"123456789\")\n",
|
323 |
-
" .lstrip()\n",
|
324 |
-
" .lstrip(\".\")\n",
|
325 |
-
" .lstrip()\n",
|
326 |
-
" .lstrip(\"123456789\")\n",
|
327 |
-
" .lstrip()\n",
|
328 |
-
" .lstrip(\".\")\n",
|
329 |
-
" .lstrip()\n",
|
330 |
-
" )\n",
|
331 |
-
" else:\n",
|
332 |
-
" content.append(line)\n",
|
333 |
-
"\n",
|
334 |
-
" if content:\n",
|
335 |
-
" parsed.append(\n",
|
336 |
-
" {\n",
|
337 |
-
" \"title\": current_section,\n",
|
338 |
-
" \"text\": content,\n",
|
339 |
-
" \"paper_title\": paper_title,\n",
|
340 |
-
" \"content_type\": \"body\"\n",
|
341 |
-
" if current_section not in [\"Abstract\", \"Introduction\", \"Conclusion\"]\n",
|
342 |
-
" else current_section.lower(),\n",
|
343 |
-
" \"arxiv_id\": arxiv_id,\n",
|
344 |
-
" }\n",
|
345 |
-
" )\n",
|
346 |
-
"\n",
|
347 |
-
" return parsed\n",
|
348 |
-
"\n",
|
349 |
-
"\n",
|
350 |
-
"def pool_markdown(parsed, pool_size=3, step_size=2):\n",
|
351 |
-
" \"\"\"\n",
|
352 |
-
" pool together markdown contents with sliding window of size 'pool_size' and step size of 'step_size'.\n",
|
353 |
-
"\n",
|
354 |
-
" Args:\n",
|
355 |
-
" parsed (list): List of dictionaries containing parsed content.\n",
|
356 |
-
" pool_size (int): Size of the pool for the sliding window.\n",
|
357 |
-
" step_size (int): Step size for the sliding window.\n",
|
358 |
-
"\n",
|
359 |
-
" Returns:\n",
|
360 |
-
" list: A list of pooled contents including non-pooled abstract and conclusion.\n",
|
361 |
-
" \"\"\"\n",
|
362 |
-
" cleaned_contents = []\n",
|
363 |
-
" for section in parsed:\n",
|
364 |
-
" cleaned_lines = [line for line in section[\"text\"] if line and \"|\" not in line and len(line) >= 40]\n",
|
365 |
-
" section[\"text\"] = cleaned_lines\n",
|
366 |
-
" cleaned_contents.append(section)\n",
|
367 |
-
"\n",
|
368 |
-
" pooled_contents = []\n",
|
369 |
-
" for section in cleaned_contents:\n",
|
370 |
-
" if section[\"content_type\"] in [\"body\", \"introduction\"]:\n",
|
371 |
-
" i = 0\n",
|
372 |
-
" while i <= len(section[\"text\"]) - pool_size:\n",
|
373 |
-
" pooled_content = \"\\n\".join(section[\"text\"][i : i + pool_size])\n",
|
374 |
-
" pooled_content = f\"{section['paper_title']}\\n{section['title']}\\n\\n\" + pooled_content\n",
|
375 |
-
" pooled_entry = {\n",
|
376 |
-
" \"title\": section[\"title\"],\n",
|
377 |
-
" \"text\": pooled_content,\n",
|
378 |
-
" \"paper_title\": section[\"paper_title\"],\n",
|
379 |
-
" \"content_type\": section[\"content_type\"],\n",
|
380 |
-
" \"arxiv_id\": section[\"arxiv_id\"],\n",
|
381 |
-
" }\n",
|
382 |
-
" pooled_contents.append(pooled_entry)\n",
|
383 |
-
" i += step_size\n",
|
384 |
-
" else:\n",
|
385 |
-
" section[\"text\"] = f\"{section['paper_title']}\\n\" + \"\\n\".join(section[\"text\"])\n",
|
386 |
-
" pooled_contents.append(section)\n",
|
387 |
-
" return pooled_contents\n",
|
388 |
-
"\n",
|
389 |
-
"\n",
|
390 |
-
"def clean_and_pool_markdown_content(md, arxiv_id):\n",
|
391 |
-
" md = parse_markdown_content(md, arxiv_id)\n",
|
392 |
-
" return pool_markdown(md)\n",
|
393 |
-
"\n",
|
394 |
-
"\n",
|
395 |
-
"if __name__ == \"__main__\":\n",
|
396 |
-
" items = get_zotero_items()\n",
|
397 |
-
" print(f\"# of items fetched from zotero: {len(items)}\")\n",
|
398 |
-
" arxiv_info = extract_arxiv_links_and_pdfs(items)\n",
|
399 |
-
"\n",
|
400 |
-
" # extract arxiv information ---\n",
|
401 |
-
" arxiv_ids = set()\n",
|
402 |
-
" for arxiv_url, pdf_url in arxiv_info.items():\n",
|
403 |
-
" arxiv_ids.add(arxiv_url.split(\"/\")[-1])\n",
|
404 |
-
" print(f\"# of arxiv papers: {len(arxiv_ids)}\")\n",
|
405 |
-
"\n",
|
406 |
-
" html5ivs = get_arxiv(arxiv_ids)\n",
|
407 |
-
"\n",
|
408 |
-
" broken = []\n",
|
409 |
-
" extracted = {}\n",
|
410 |
-
" extracted_from_pdf = {}\n",
|
411 |
-
" broken_tex = {}\n",
|
412 |
-
"\n",
|
413 |
-
" pbar = tqdm(range(len(html5ivs)))\n",
|
414 |
-
" for arxiv_id, x in html5ivs.items():\n",
|
415 |
-
" try:\n",
|
416 |
-
" extracted[arxiv_id] = extract_html_content(x)\n",
|
417 |
-
" except Exception as e:\n",
|
418 |
-
" # print(e)\n",
|
419 |
-
" print(f'getting pdf text for {arxiv_id}...')\n",
|
420 |
-
" pdf_text = get_pdf_text(arxiv_id) \n",
|
421 |
-
" if pdf_text is not None:\n",
|
422 |
-
" extracted_from_pdf[arxiv_id] = pdf_text # get_pdf_text(arxiv_id) # <- original\n",
|
423 |
-
" else:\n",
|
424 |
-
" broken.append(arxiv_id)\n",
|
425 |
-
" pbar.update(1)\n",
|
426 |
-
" pbar.close()\n",
|
427 |
-
"\n",
|
428 |
-
" print(f\"# of arxiv papers extracted: {len(extracted)}\")\n",
|
429 |
-
" print(f\"# of arxiv papers extracted from pdf: {len(extracted_from_pdf)}\")\n",
|
430 |
-
" print(f\"# of broken links: {len(broken)}\")\n",
|
431 |
-
"\n",
|
432 |
-
" pooled = [pool_html_content(x) for _, x in extracted.items()]\n",
|
433 |
-
" pooled += [\n",
|
434 |
-
" clean_and_pool_markdown_content(x, arxiv_id) for arxiv_id, x in extracted_from_pdf.items()\n",
|
435 |
-
" ] # No pdf content yet, no op for now\n",
|
436 |
-
" flat_pool = [item for sublist in pooled for item in sublist]\n",
|
437 |
-
"\n",
|
438 |
-
" id_to_abstract = {}\n",
|
439 |
-
" for doc in pooled:\n",
|
440 |
-
" for entry in doc:\n",
|
441 |
-
" if entry[\"content_type\"] == \"abstract\":\n",
|
442 |
-
" id_to_abstract[entry[\"arxiv_id\"]] = entry[\"text\"]\n",
|
443 |
-
" break\n",
|
444 |
-
"\n",
|
445 |
-
" srsly.write_json(\"id_to_abstract.json\", id_to_abstract)\n",
|
446 |
-
" srsly.write_json(\"flatpool.json\", flat_pool)\n",
|
447 |
-
"```\n",
|
448 |
-
"go through it and reply OK."
|
449 |
-
]
|
450 |
-
},
|
451 |
-
{
|
452 |
-
"cell_type": "markdown",
|
453 |
-
"id": "24e7ef23",
|
454 |
-
"metadata": {},
|
455 |
-
"source": [
|
456 |
-
"OK. I've reviewed the code you provided for ingesting and processing items from a Zotero library."
|
457 |
-
]
|
458 |
-
},
|
459 |
-
{
|
460 |
-
"cell_type": "markdown",
|
461 |
-
"id": "84628bb2",
|
462 |
-
"metadata": {},
|
463 |
-
"source": [
|
464 |
-
"OK. I've reviewed the code you provided for ingesting and processing items from a Zotero library."
|
465 |
-
]
|
466 |
-
},
|
467 |
-
{
|
468 |
-
"cell_type": "code",
|
469 |
-
"execution_count": 3,
|
470 |
-
"id": "bada6931",
|
471 |
-
"metadata": {},
|
472 |
-
"outputs": [],
|
473 |
-
"source": [
|
474 |
-
"%%ai\n",
|
475 |
-
"Let's work step by step. I currently have\n",
|
476 |
-
"\n",
|
477 |
-
"```\n",
|
478 |
-
"import os\n",
|
479 |
-
"import re\n",
|
480 |
-
"\n",
|
481 |
-
"import dotenv\n",
|
482 |
-
"import requests\n",
|
483 |
-
"from bs4 import BeautifulSoup\n",
|
484 |
-
"from retry import retry\n",
|
485 |
-
"from tqdm.auto import tqdm\n",
|
486 |
-
"\n",
|
487 |
-
"dotenv.load_dotenv()\n",
|
488 |
-
"\n",
|
489 |
-
"\n",
|
490 |
-
"########################################################\n",
|
491 |
-
"### GET ZOTERO ITEMS\n",
|
492 |
-
"########################################################\n",
|
493 |
-
"@retry(tries=3, delay=8)\n",
|
494 |
-
"def _fetch_one_zotero_batch(url, headers, params):\n",
|
495 |
-
" \"\"\"\n",
|
496 |
-
" Fetch articles from Zotero API\n",
|
497 |
-
" \"\"\"\n",
|
498 |
-
" response = requests.get(url, headers=headers, params=params)\n",
|
499 |
-
" response.raise_for_status()\n",
|
500 |
-
" return response.json()\n",
|
501 |
-
"\n",
|
502 |
-
"\n",
|
503 |
-
"def get_zotero_items(debug=False):\n",
|
504 |
-
" \"\"\"\n",
|
505 |
-
" fetch items from zotero library\n",
|
506 |
-
" \"\"\"\n",
|
507 |
-
"\n",
|
508 |
-
" GROUP_ID = os.getenv(\"GROUP_ID\")\n",
|
509 |
-
" API_KEY = os.getenv(\"API_KEY\")\n",
|
510 |
-
" BASE_URL = f\"https://api.zotero.org/groups/{GROUP_ID}/items\"\n",
|
511 |
-
" LIMIT = 100\n",
|
512 |
-
"\n",
|
513 |
-
" headers = {\"Zotero-API-Key\": API_KEY, \"Content-Type\": \"application/json\"}\n",
|
514 |
-
"\n",
|
515 |
-
" items = []\n",
|
516 |
-
" start = 0\n",
|
517 |
-
"\n",
|
518 |
-
" i = 1\n",
|
519 |
-
" while True:\n",
|
520 |
-
" i += 1\n",
|
521 |
-
" params = {\"limit\": LIMIT, \"start\": start}\n",
|
522 |
-
" page_items = _fetch_one_zotero_batch(BASE_URL, headers, params)\n",
|
523 |
-
"\n",
|
524 |
-
" if not page_items:\n",
|
525 |
-
" break\n",
|
526 |
-
"\n",
|
527 |
-
" items.extend(page_items)\n",
|
528 |
-
" start += LIMIT\n",
|
529 |
-
" print(f\"# items fetched {len(items)}\")\n",
|
530 |
-
"\n",
|
531 |
-
" if debug:\n",
|
532 |
-
" break\n",
|
533 |
-
"\n",
|
534 |
-
" return items\n",
|
535 |
-
"\n",
|
536 |
-
"\n",
|
537 |
-
"########################################################\n",
|
538 |
-
"### EXTRACT ARXIV LINKS AND PDFs\n",
|
539 |
-
"########################################################\n",
|
540 |
-
"def get_arxiv_items(items):\n",
|
541 |
-
" arxiv_items = []\n",
|
542 |
-
" arxiv_pattern = re.compile(r\"arxiv.org/abs/(\\d+\\.\\d+)\")\n",
|
543 |
-
"\n",
|
544 |
-
" for item in items:\n",
|
545 |
-
" data = item.get(\"data\", {})\n",
|
546 |
-
" attachments = item.get(\"links\", {}).get(\"attachment\", {})\n",
|
547 |
-
"\n",
|
548 |
-
" arxiv_url = None\n",
|
549 |
-
" pdf_url = None\n",
|
550 |
-
"\n",
|
551 |
-
" if \"url\" in data and \"arxiv.org\" in data[\"url\"]:\n",
|
552 |
-
" arxiv_match = arxiv_pattern.search(data[\"url\"])\n",
|
553 |
-
" if arxiv_match:\n",
|
554 |
-
" arxiv_url = data[\"url\"]\n",
|
555 |
-
"\n",
|
556 |
-
" if attachments:\n",
|
557 |
-
" pdf_url = attachments[\"href\"]\n",
|
558 |
-
"\n",
|
559 |
-
" if arxiv_url:\n",
|
560 |
-
" arxiv_items.append(\n",
|
561 |
-
" {\n",
|
562 |
-
" \"arxiv_id\": arxiv_url.split(\"/\")[-1],\n",
|
563 |
-
" \"arxiv_url\": arxiv_url,\n",
|
564 |
-
" \"pdf_url\": pdf_url,\n",
|
565 |
-
" \"added_by\": item[\"meta\"][\"createdByUser\"][\"username\"],\n",
|
566 |
-
" \"date_added\": data.get(\"dateAdded\", \"\"),\n",
|
567 |
-
" }\n",
|
568 |
-
" )\n",
|
569 |
-
"\n",
|
570 |
-
" return arxiv_items\n",
|
571 |
-
"\n",
|
572 |
-
"\n",
|
573 |
-
"@retry(tries=3, delay=15, backoff=2)\n",
|
574 |
-
"def fetch_arxiv_html(arxiv_id):\n",
|
575 |
-
" url = f\"https://ar5iv.labs.arxiv.org/html/{arxiv_id.split('v')[0]}\"\n",
|
576 |
-
" response = requests.get(url)\n",
|
577 |
-
" return response.text if response.status_code == 200 else None\n",
|
578 |
-
"\n",
|
579 |
-
"\n",
|
580 |
-
"def get_arxiv_html(arxiv_items):\n",
|
581 |
-
" for item in tqdm(arxiv_items):\n",
|
582 |
-
" html = fetch_arxiv_html(item[\"arxiv_id\"])\n",
|
583 |
-
" if html:\n",
|
584 |
-
" item[\"raw_html\"] = html\n",
|
585 |
-
" else:\n",
|
586 |
-
" print(f\"failed to fetch html for {item['arxiv_id']}\")\n",
|
587 |
-
"\n",
|
588 |
-
" return arxiv_items\n",
|
589 |
-
"\n",
|
590 |
-
"\n",
|
591 |
-
"if __name__ == \"__main__\":\n",
|
592 |
-
" items = get_zotero_items(debug=True)\n",
|
593 |
-
" print(f\"# of items fetched from zotero: {len(items)}\")\n",
|
594 |
-
" arxiv_items = get_arxiv_items(items)\n",
|
595 |
-
" print(f\"# of arxiv papers: {len(arxiv_items)}\")\n",
|
596 |
-
" arxiv_htmls = get_arxiv_html(arxiv_items) # raw html files\n",
|
597 |
-
"--\n",
|
598 |
-
"review for any mistake until now. What shall we do next."
|
599 |
-
]
|
600 |
-
},
|
601 |
-
{
|
602 |
-
"cell_type": "markdown",
|
603 |
-
"id": "60ab1dfc",
|
604 |
-
"metadata": {},
|
605 |
-
"source": [
|
606 |
-
"The code looks good so far. You've implemented functions to fetch items from Zotero, extract ArXiv-related items, and fetch the HTML content for ArXiv papers. \n",
|
607 |
-
"\n",
|
608 |
-
"Next steps could include:\n",
|
609 |
-
"\n",
|
610 |
-
"1. Parse the HTML content to extract structured information from the papers.\n",
|
611 |
-
"2. Implement error handling for failed HTML fetches.\n",
|
612 |
-
"3. Add a function to extract text from PDFs for papers where HTML fetch fails.\n",
|
613 |
-
"4. Store the extracted data in a suitable format (e.g., JSON, database).\n",
|
614 |
-
"5. Implement content pooling as in your original code.\n",
|
615 |
-
"\n",
|
616 |
-
"Which of these would you like to focus on first?"
|
617 |
-
]
|
618 |
-
},
|
619 |
-
{
|
620 |
-
"cell_type": "code",
|
621 |
-
"execution_count": 5,
|
622 |
-
"id": "696318f6",
|
623 |
-
"metadata": {},
|
624 |
-
"outputs": [],
|
625 |
-
"source": [
|
626 |
-
"%%aip\n",
|
627 |
-
"Let's work on step 1. Let's follow the original code as much as possible.\n",
|
628 |
-
"Note: these are arxiv htmls. Let's also keep full body text."
|
629 |
-
]
|
630 |
-
},
|
631 |
-
{
|
632 |
-
"cell_type": "code",
|
633 |
-
"execution_count": 6,
|
634 |
-
"id": "e7e4a5a6",
|
635 |
-
"metadata": {},
|
636 |
-
"outputs": [
|
637 |
-
{
|
638 |
-
"ename": "NameError",
|
639 |
-
"evalue": "name 'arxiv_htmls' is not defined",
|
640 |
-
"output_type": "error",
|
641 |
-
"traceback": [
|
642 |
-
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
643 |
-
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
|
644 |
-
"Cell \u001b[0;32mIn[6], line 18\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mprocess_arxiv_items\u001b[39m(arxiv_items):\n\u001b[1;32m 16\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m [\u001b[38;5;28mdict\u001b[39m(item, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mextract_html_content(item[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mraw_html\u001b[39m\u001b[38;5;124m\"\u001b[39m])) \u001b[38;5;28;01mfor\u001b[39;00m item \u001b[38;5;129;01min\u001b[39;00m arxiv_items \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mraw_html\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m item]\n\u001b[0;32m---> 18\u001b[0m processed_items \u001b[38;5;241m=\u001b[39m process_arxiv_items(\u001b[43marxiv_htmls\u001b[49m)\n",
|
645 |
-
"\u001b[0;31mNameError\u001b[0m: name 'arxiv_htmls' is not defined"
|
646 |
-
]
|
647 |
-
}
|
648 |
-
],
|
649 |
-
"source": [
|
650 |
-
"def extract_html_content(html):\n",
|
651 |
-
" soup = BeautifulSoup(html, \"html.parser\")\n",
|
652 |
-
" arxiv_id = re.search(r\"\\[(\\d+\\.\\d+(v\\d+)?)\\]\", html).group(1) if re.search(r\"\\[(\\d+\\.\\d+(v\\d+)?)\\]\", html) else None\n",
|
653 |
-
" paper_title = soup.find(\"h1\", class_=\"ltx_title ltx_title_document\").get_text(strip=True)\n",
|
654 |
-
" [m.decompose() for m in soup.find_all([\"math\", \"cite\"])]\n",
|
655 |
-
" abstract = \" \".join(p.get_text(strip=True) for p in soup.find(\"div\", class_=\"ltx_abstract\").find_all(\"p\")).replace(\")\", \") \") if soup.find(\"div\", class_=\"ltx_abstract\") else \"\"\n",
|
656 |
-
" sections = soup.find_all(\"section\", class_=\"ltx_section\")\n",
|
657 |
-
" body = []\n",
|
658 |
-
" for section in sections:\n",
|
659 |
-
" title = section.find(\"h2\", class_=\"ltx_title ltx_title_section\").get_text(strip=True)\n",
|
660 |
-
" content = \" \".join(p.get_text(strip=True).replace(\")\", \") \") for p in section.find_all(\"div\", class_=\"ltx_para\"))\n",
|
661 |
-
" body.append({\"title\": title, \"content\": content})\n",
|
662 |
-
" return {\"arxiv_id\": arxiv_id, \"paper_title\": paper_title, \"abstract\": abstract, \"body\": body, \"full_text\": soup.get_text(strip=True)}\n",
|
663 |
-
"\n",
|
664 |
-
"def process_arxiv_items(arxiv_items):\n",
|
665 |
-
" return [dict(item, **extract_html_content(item[\"raw_html\"])) for item in arxiv_items if \"raw_html\" in item]\n",
|
666 |
-
"\n",
|
667 |
-
"processed_items = process_arxiv_items(arxiv_htmls)"
|
668 |
-
]
|
669 |
-
},
|
670 |
-
{
|
671 |
-
"cell_type": "code",
|
672 |
-
"execution_count": 8,
|
673 |
-
"id": "70a3caaa",
|
674 |
-
"metadata": {},
|
675 |
-
"outputs": [],
|
676 |
-
"source": [
|
677 |
-
"%%aip\n",
|
678 |
-
"the above doesn't work and missed may details of the original implementation. Do not focus on pooling now\n",
|
679 |
-
"just do extract_html_content better\n"
|
680 |
-
]
|
681 |
-
},
|
682 |
-
{
|
683 |
-
"cell_type": "code",
|
684 |
-
"execution_count": null,
|
685 |
-
"id": "4040257d",
|
686 |
-
"metadata": {},
|
687 |
-
"outputs": [],
|
688 |
-
"source": [
|
689 |
-
"def extract_html_content(html):\n",
|
690 |
-
" soup, result = BeautifulSoup(html, \"html.parser\"), []\n",
|
691 |
-
" arxiv_id = re.search(r\"\\[(\\d+\\.\\d+(v\\d+)?)\\]\", html)\n",
|
692 |
-
" arxiv_id = arxiv_id.group(1) if arxiv_id else None\n",
|
693 |
-
" paper_title = soup.find(\"h1\", class_=\"ltx_title ltx_title_document\").get_text(strip=True)\n",
|
694 |
-
" [m.decompose() for m in soup.find_all([\"math\", \"cite\"])]\n",
|
695 |
-
" abstract = soup.find(\"div\", class_=\"ltx_abstract\")\n",
|
696 |
-
" if abstract:\n",
|
697 |
-
" result.append({\"content\": \" \".join(p.get_text(strip=True) for p in abstract.find_all(\"p\")).replace(\")\", \") \"), \"title\": \"Abstract\", \"paper_title\": paper_title, \"content_type\": \"abstract\"})\n",
|
698 |
-
" intro = soup.find(\"section\", id=\"S1\")\n",
|
699 |
-
" if intro:\n",
|
700 |
-
" for para in intro.find_all(\"div\", class_=\"ltx_para\"):\n",
|
701 |
-
" result.append({\"content\": para.get_text(strip=True).replace(\")\", \") \"), \"title\": \"Introduction\", \"paper_title\": paper_title, \"content_type\": \"introduction\"})\n",
|
702 |
-
" for section in soup.find_all(\"section\", class_=\"ltx_section\")[1:-1]:\n",
|
703 |
-
" section_title = section.find(\"h2\", class_=\"ltx_title ltx_title_section\").get_text(strip=True)\n",
|
704 |
-
" for para in section.find_all(\"div\", class_=\"ltx_para\"):\n",
|
705 |
-
" result.append({\"content\": para.get_text(strip=True).replace(\")\", \") \"), \"title\": section_title, \"paper_title\": paper_title, \"content_type\": \"body\"})\n",
|
706 |
-
" conclusion = soup.find(\"section\", id=\"S4\")\n",
|
707 |
-
" if conclusion:\n",
|
708 |
-
" result.append({\"content\": \" \".join(p.get_text(strip=True) for p in conclusion.find_all(\"div\", class_=\"ltx_para\")).replace(\")\", \") \"), \"title\": conclusion.find(\"h2\", class_=\"ltx_title ltx_title_section\").get_text(strip=True), \"paper_title\": paper_title, \"content_type\": \"conclusion\"})\n",
|
709 |
-
" for c in result:\n",
|
710 |
-
" c[\"arxiv_id\"] = arxiv_id\n",
|
711 |
-
" return result\n",
|
712 |
-
"\n",
|
713 |
-
"processed_items = [extract_html_content(item[\"raw_html\"]) for item in arxiv_items if \"raw_html\" in item]"
|
714 |
-
]
|
715 |
-
},
|
716 |
-
{
|
717 |
-
"cell_type": "code",
|
718 |
-
"execution_count": null,
|
719 |
-
"id": "0242f021",
|
720 |
-
"metadata": {},
|
721 |
-
"outputs": [],
|
722 |
-
"source": []
|
723 |
-
}
|
724 |
-
],
|
725 |
-
"metadata": {
|
726 |
-
"kernelspec": {
|
727 |
-
"display_name": "Python 3 (ipykernel)",
|
728 |
-
"language": "python",
|
729 |
-
"name": "python3"
|
730 |
-
},
|
731 |
-
"language_info": {
|
732 |
-
"codemirror_mode": {
|
733 |
-
"name": "ipython",
|
734 |
-
"version": 3
|
735 |
-
},
|
736 |
-
"file_extension": ".py",
|
737 |
-
"mimetype": "text/x-python",
|
738 |
-
"name": "python",
|
739 |
-
"nbconvert_exporter": "python",
|
740 |
-
"pygments_lexer": "ipython3",
|
741 |
-
"version": "3.12.4"
|
742 |
-
}
|
743 |
-
},
|
744 |
-
"nbformat": 4,
|
745 |
-
"nbformat_minor": 5
|
746 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|