Spaces:
Running
Running
raannakasturi
commited on
Commit
·
3ec5aa6
1
Parent(s):
41d4301
Add initial implementation for paper summarization and data fetching
Browse files- .gitignore +4 -0
- fetch_data.py +69 -0
- main.py +51 -0
- post_blog.py +156 -0
- summarize_paper.py +17 -0
.gitignore
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
.env
|
2 |
+
/__*
|
3 |
+
*.json
|
4 |
+
/BLOGGER
|
fetch_data.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from gradio_client import Client
|
2 |
+
import json
|
3 |
+
from concurrent.futures import ThreadPoolExecutor, as_completed
|
4 |
+
|
5 |
+
def fetch_category_ids(cat_ids_api_key):
|
6 |
+
"""Fetch category IDs using the category API."""
|
7 |
+
if not cat_ids_api_key:
|
8 |
+
raise ValueError("API access key not found. Please check your environment variables.")
|
9 |
+
cat_ids_api_client = Client("raannakasturi/ReXploreIDFetchingAPI")
|
10 |
+
try:
|
11 |
+
result = cat_ids_api_client.predict(
|
12 |
+
user_access_key=cat_ids_api_key,
|
13 |
+
api_name="/fetch_paper_ids"
|
14 |
+
)
|
15 |
+
cat_ids = json.loads(result)
|
16 |
+
if cat_ids['status'] == 'success':
|
17 |
+
return cat_ids['data']
|
18 |
+
else:
|
19 |
+
return None
|
20 |
+
except Exception as e:
|
21 |
+
print(f"Exception while fetching category IDs: {str(e)}")
|
22 |
+
return None
|
23 |
+
|
24 |
+
def fetch_single_paper_data(paper_id):
|
25 |
+
paper_data_api_client = Client("raannakasturi/ReXplorePaperDataFetcher")
|
26 |
+
try:
|
27 |
+
result = paper_data_api_client.predict(
|
28 |
+
id=paper_id,
|
29 |
+
api_name="/fetch_paper_data"
|
30 |
+
)
|
31 |
+
paper_data = json.loads(result)
|
32 |
+
if paper_data['status'] == 'success':
|
33 |
+
return paper_id, paper_data['data']
|
34 |
+
else:
|
35 |
+
print(f"Failed to fetch data for paper ID {paper_id}: {paper_data.get('message', 'Unknown error')}")
|
36 |
+
return paper_id, None
|
37 |
+
except Exception as e:
|
38 |
+
print(f"Exception while fetching data for paper ID {paper_id}: {str(e)}")
|
39 |
+
return paper_id, None
|
40 |
+
|
41 |
+
def fetch_paper_data_concurrently(paper_ids, max_threads=12):
|
42 |
+
paper_id_data = {}
|
43 |
+
with ThreadPoolExecutor(max_workers=max_threads) as executor:
|
44 |
+
future_to_paper_id = {executor.submit(fetch_single_paper_data, paper_id): paper_id for paper_id in paper_ids}
|
45 |
+
for future in as_completed(future_to_paper_id):
|
46 |
+
paper_id = future_to_paper_id[future]
|
47 |
+
try:
|
48 |
+
paper_id, data = future.result()
|
49 |
+
if data:
|
50 |
+
paper_id_data[paper_id] = data
|
51 |
+
except Exception as e:
|
52 |
+
print(f"Error fetching data for paper ID {paper_id}: {str(e)}")
|
53 |
+
return paper_id_data
|
54 |
+
|
55 |
+
def fetch_paper_data_with_category(cat_ids_api_key):
|
56 |
+
data = {}
|
57 |
+
try:
|
58 |
+
cat_ids = fetch_category_ids(cat_ids_api_key)
|
59 |
+
if cat_ids:
|
60 |
+
for category, ids in cat_ids.items():
|
61 |
+
print(f"Fetching data for category: {category}")
|
62 |
+
paper_data = fetch_paper_data_concurrently(ids['ids'])
|
63 |
+
print(paper_data)
|
64 |
+
if paper_data:
|
65 |
+
data[category] = paper_data
|
66 |
+
return json.dumps(data, indent=4, ensure_ascii=False)
|
67 |
+
except Exception as e:
|
68 |
+
print(f"Exception while fetching paper data by category: {str(e)}")
|
69 |
+
return None
|
main.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
import dotenv
|
4 |
+
from summarize_paper import summarize_paper
|
5 |
+
from fetch_data import fetch_paper_data_with_category
|
6 |
+
from post_blog import post_blog
|
7 |
+
|
8 |
+
# Load environment variables
|
9 |
+
dotenv.load_dotenv()
|
10 |
+
cat_ids_api_key = os.getenv("DATA_API_ACCESS_KEY")
|
11 |
+
summarizer_api_key = os.getenv("SUMMARIZER_API_KEY")
|
12 |
+
|
13 |
+
def paper_data(paper_data):
|
14 |
+
data = {"status": "success"}
|
15 |
+
data['data'] = {}
|
16 |
+
paper_data = json.loads(paper_data)
|
17 |
+
for category, papers in paper_data.items():
|
18 |
+
print(f"Processing category: {category}")
|
19 |
+
data['data'][category] = {}
|
20 |
+
for paper_id, details in papers.items():
|
21 |
+
doi = details.get("doi")
|
22 |
+
pdf_url = details.get("pdf_url")
|
23 |
+
title = details.get("title")
|
24 |
+
citation = details.get("citation")
|
25 |
+
if not all([paper_id, doi, pdf_url, title, citation]):
|
26 |
+
print(f"Skipping paper with ID: {paper_id} (missing details)")
|
27 |
+
continue
|
28 |
+
summary, mindmap = summarize_paper(pdf_url, paper_id, summarizer_api_key)
|
29 |
+
post_blog(title, category, summary, mindmap, citation, os.getenv('ACCESS_KEY'))
|
30 |
+
data['data'][category][paper_id] = {
|
31 |
+
"id": paper_id,
|
32 |
+
"doi": doi,
|
33 |
+
"title": title,
|
34 |
+
"category": category,
|
35 |
+
"citation": citation,
|
36 |
+
"summary": summary,
|
37 |
+
"mindmap": mindmap,
|
38 |
+
}
|
39 |
+
output_file = "paper_data_with_summary.json"
|
40 |
+
data = json.dumps(data, indent=4, ensure_ascii=False)
|
41 |
+
with open(output_file, "w", encoding="utf-8") as file:
|
42 |
+
json.dump(data, file, indent=4)
|
43 |
+
print(f"Processed data saved to {output_file}")
|
44 |
+
return data
|
45 |
+
|
46 |
+
if __name__ == "__main__":
|
47 |
+
data = fetch_paper_data_with_category(cat_ids_api_key)
|
48 |
+
with open("paper_data.json", "w", encoding="utf-8") as file:
|
49 |
+
json.dump(data, file, indent=4, ensure_ascii=False)
|
50 |
+
pdata = paper_data(data)
|
51 |
+
print(pdata)
|
post_blog.py
ADDED
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
import os
|
3 |
+
import requests
|
4 |
+
import base64
|
5 |
+
import dotenv
|
6 |
+
import mistune
|
7 |
+
|
8 |
+
dotenv.load_dotenv()
|
9 |
+
api_endpoint = os.getenv('API_ENDPOINT')
|
10 |
+
password = os.getenv('BLOG_PASSWORD')
|
11 |
+
username = os.getenv('BLOG_USERNAME')
|
12 |
+
|
13 |
+
def generate_post_html(title, summary, mindmap, citation):
|
14 |
+
html_summary = mistune.html(summary)
|
15 |
+
post = f"""
|
16 |
+
<!-- wp:html -->
|
17 |
+
<div>
|
18 |
+
<script src="https://cdn.jsdelivr.net/npm/markmap-autoloader@latest"></script>
|
19 |
+
<style>
|
20 |
+
.markmap {{
|
21 |
+
position: relative;
|
22 |
+
}}
|
23 |
+
.markmap > svg {{
|
24 |
+
width: 100%;
|
25 |
+
border: 2px solid #000;
|
26 |
+
height: 80dvh;
|
27 |
+
}}
|
28 |
+
</style>
|
29 |
+
<p id="paper_summary" data="{summary.replace("&", "&")}">
|
30 |
+
{html_summary.replace("&", "&")}
|
31 |
+
</p>
|
32 |
+
<br>
|
33 |
+
<br>
|
34 |
+
<h2>Mindmap</h2>
|
35 |
+
<div class="markmap" data="{mindmap.replace("&", "&")}">
|
36 |
+
<script type="text/template">
|
37 |
+
# {title.replace("&", "&")}
|
38 |
+
{mindmap.replace("&", "&")}
|
39 |
+
</script>
|
40 |
+
</div>
|
41 |
+
<br>
|
42 |
+
<br>
|
43 |
+
<h2>Citation</h2>
|
44 |
+
<p id="paper_citation" data="{citation.replace("&", "&")}">
|
45 |
+
{mistune.html(citation.replace("&", "&"))}
|
46 |
+
</p>
|
47 |
+
</div>
|
48 |
+
<!-- /wp:html -->
|
49 |
+
"""
|
50 |
+
return post
|
51 |
+
|
52 |
+
def sanitize_citation(citation):
|
53 |
+
pattern = r"(https://doi\.org/\S+)"
|
54 |
+
sanitized_citation = re.sub(
|
55 |
+
pattern,
|
56 |
+
lambda match: f"[{match.group(1)}](https://doi.org/{match.group(1).split('/')[-1]})",
|
57 |
+
citation
|
58 |
+
)
|
59 |
+
return sanitized_citation
|
60 |
+
|
61 |
+
|
62 |
+
def create_post(title, category, summary, mindmap, citation):
|
63 |
+
post_title = f"{title}"
|
64 |
+
post_category = f"{category}"
|
65 |
+
post_body = generate_post_html(title, summary, mindmap, sanitize_citation(citation))
|
66 |
+
return post_title, post_category, post_body
|
67 |
+
|
68 |
+
def create_category_if_not_exists(category_name, headers):
|
69 |
+
categories_url = f"{api_endpoint}/categories"
|
70 |
+
response = requests.get(categories_url, headers=headers)
|
71 |
+
if response.status_code == 200:
|
72 |
+
categories = response.json()
|
73 |
+
for category in categories:
|
74 |
+
if category['name'].lower() == category_name.lower():
|
75 |
+
return category['id']
|
76 |
+
create_response = requests.post(
|
77 |
+
categories_url,
|
78 |
+
headers=headers,
|
79 |
+
json={"name": category_name}
|
80 |
+
)
|
81 |
+
if create_response.status_code == 201:
|
82 |
+
return create_response.json()['id']
|
83 |
+
else:
|
84 |
+
print(f"Error creating category: {create_response.text}")
|
85 |
+
return None
|
86 |
+
|
87 |
+
def post_post(title, category, body):
|
88 |
+
credentials = f"{username}:{password}"
|
89 |
+
url = f"{api_endpoint}/posts"
|
90 |
+
auth_key = base64.b64encode(credentials.encode('utf-8')).decode('utf-8')
|
91 |
+
headers = {
|
92 |
+
"Authorization": f"Basic {auth_key}"
|
93 |
+
}
|
94 |
+
category_ids = []
|
95 |
+
category_id = create_category_if_not_exists(category, headers)
|
96 |
+
if category_id:
|
97 |
+
category_ids.append(category_id)
|
98 |
+
post_data = {
|
99 |
+
"title": title,
|
100 |
+
"status": "publish",
|
101 |
+
"categories": category_ids,
|
102 |
+
"content": body
|
103 |
+
}
|
104 |
+
response = requests.post(url, headers=headers, json=post_data)
|
105 |
+
print(response.status_code)
|
106 |
+
if response.status_code == 201:
|
107 |
+
print(f"Posted to blog... [{category}] {title}")
|
108 |
+
return True
|
109 |
+
else:
|
110 |
+
print(f"Failed to post to blog: {response.text}")
|
111 |
+
return False
|
112 |
+
|
113 |
+
def post_blog(title, category, summary, mindmap, citation, access_key):
|
114 |
+
if access_key != os.getenv('ACCESS_KEY'):
|
115 |
+
return False
|
116 |
+
try:
|
117 |
+
post_title, post_category, post_body = create_post(title, category, summary, mindmap, citation)
|
118 |
+
status = post_post(post_title, post_category, post_body)
|
119 |
+
if status:
|
120 |
+
print('Post created successfully')
|
121 |
+
return True
|
122 |
+
else:
|
123 |
+
print('Failed to create post')
|
124 |
+
return False
|
125 |
+
except Exception as e:
|
126 |
+
print('An error occurred:', str(e))
|
127 |
+
return False
|
128 |
+
|
129 |
+
if __name__ == '__main__':
|
130 |
+
data = {
|
131 |
+
"status": "success",
|
132 |
+
"Astrophysics": {
|
133 |
+
"2412.16344": {
|
134 |
+
"id": "2412.16344",
|
135 |
+
"doi": "https://doi.org/10.48550/arXiv.2412.16344",
|
136 |
+
"title": "Focal Plane of the Arcus Probe X-Ray Spectrograph",
|
137 |
+
"category": "Astrophysics",
|
138 |
+
"citation": "Grant, C. E., Bautz, M. W., Miller, E. D., Foster, R. F., LaMarr, B., Malonis, A., Prigozhin, G., Schneider, B., Leitz, C., & Falcone, A. D. (2024). Focal Plane of the Arcus Probe X-Ray Spectrograph. ArXiv. https://doi.org/10.48550/ARXIV.2412.16344",
|
139 |
+
"summary": "## Summary\nThe Arcus Probe mission concept provides high-resolution soft X-ray and UV spectroscopy to study the universe. The X-ray Spectrograph (XRS) uses two CCD focal planes to detect and record X-ray photons. Laboratory performance results meet observatory requirements.\n\n## Highlights\n- The Arcus Probe mission concept explores the formation and evolution of clusters, galaxies, and stars.\n- The XRS instrument includes four parallel optical channels and two detector focal plane arrays.\n- The CCDs are designed and manufactured by MIT Lincoln Laboratory (MIT/LL).\n- The XRS focal plane utilizes high heritage MIT/LL CCDs with proven technologies.\n- Laboratory testing confirms CCID-94 performance meets required spectral resolution and readout noise.\n- The Arcus mission includes two co-aligned instruments working simultaneously.\n- The XRS Instrument Control Unit (XICU) controls the activities of the XRS.\n\n## Key Insights\n- The Arcus Probe mission concept provides a significant improvement in sensitivity and resolution over previous missions, enabling breakthrough science in understanding the universe.\n- The XRS instrument's design, including the use of two CCD focal planes and four parallel optical channels, allows for high-resolution spectroscopy and efficient detection of X-ray photons.\n- The CCDs used in the XRS instrument are designed and manufactured by MIT Lincoln Laboratory (MIT/LL), which has a proven track record of producing high-quality CCDs for space missions.\n- The laboratory performance results of the CCID-94 device demonstrate that it meets the required spectral resolution and readout noise for the Arcus mission, indicating that the instrument is capable of achieving its scientific goals.\n- The XRS Instrument Control Unit (XICU) plays a crucial role in controlling the activities of the XRS, including gathering and storing data, and processing event recognition.\n- The Arcus mission's use of two co-aligned instruments working simultaneously allows for a wide range of scientific investigations, including the study of time-domain science and the physics of time-dependent phenomena.\n- The high heritage MIT/LL CCDs used in the XRS focal plane provide a reliable and efficient means of detecting X-ray photons, enabling the instrument to achieve its scientific goals.",
|
140 |
+
"mindmap": "## Arcus Probe Mission Concept\n- Explores formation and evolution of clusters, galaxies, stars\n- High-resolution soft X-ray and UV spectroscopy\n- Agile response capability for time-domain science\n\n## X-Ray Spectrograph (XRS) Instrument\n- Two nearly identical CCD focal planes\n- Detects and records X-ray photons from dispersed spectra\n- Zero-order of critical angle transmission gratings\n\n## XRS Focal Plane Characteristics\n- Frametransfer X-ray CCDs\n- 8-CCD array per Detector Assembly\n- FWHM < 70 eV @ 0.5 keV\n- System read noise ≤ 4 e- RMS @ 625 kpixels/sec\n\n## Detector Assembly\n- Eight CCDs in a linear array\n- Tilted to match curved focal surface\n- Gaps minimized between CCDs\n- Alignment optimized with XRS optics\n\n## Detector Electronics\n- Programmable analog clock waveforms and biases\n- Low-noise analog signal processing and digitization\n- 1 second frame time for negligible pileup\n\n## XRS Instrument Control Unit (XICU)\n- Controls XRS activities and data transfer\n- Event Recognition Processor (ERP) extracts X-ray events\n- Reduces data rate by many orders of magnitude\n\n## CCD X-Ray Performance\n- Measured readout noise 2-3 e- RMS\n- Spectral resolution meets Arcus requirements\n- FWHM < 70 eV at 0.5 keV\n\n## CCID-94 Characteristics\n- Back-illuminated frame-transfer CCDs\n- 2048 × 1024 pixel imaging array\n- 24 × 24 µm image area pixel size\n- 50 µm detector thickness\n\n## Contamination Blocking Filter (CBF)\n- Protects detectors from molecular contamination\n- 45 nm polyimide + 30 nm Al\n- Maintained above +20°C by heater control\n\n## Optical Blocking Filter (OBF)\n- Attenuates visible/IR stray light\n- 40 nm Al on-chip filter\n- Works in conjunction with CBF"
|
141 |
+
}
|
142 |
+
}
|
143 |
+
}
|
144 |
+
if data['status'] != 'success':
|
145 |
+
print('Failed to fetch data')
|
146 |
+
else:
|
147 |
+
for category, catdata in data.items():
|
148 |
+
if category != 'status':
|
149 |
+
for paper_id, paperdata in catdata.items():
|
150 |
+
title = paperdata['title']
|
151 |
+
category = paperdata['category']
|
152 |
+
summary = paperdata['summary']
|
153 |
+
mindmap = paperdata['mindmap']
|
154 |
+
citation = paperdata['citation']
|
155 |
+
access_key = os.getenv('ACCESS_KEY')
|
156 |
+
post_blog(title, category, summary, mindmap, citation, access_key)
|
summarize_paper.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
from gradio_client import Client
|
3 |
+
|
4 |
+
def summarize_paper(pdf_url, paper_id, access_key):
|
5 |
+
mindmap = None
|
6 |
+
summary = None
|
7 |
+
summarizer_client = Client("raannakasturi/ReXploreAPI")
|
8 |
+
result = summarizer_client.predict(
|
9 |
+
url=pdf_url,
|
10 |
+
id=paper_id,
|
11 |
+
access_key=access_key,
|
12 |
+
api_name="/rexplore_summarizer"
|
13 |
+
)
|
14 |
+
data = json.loads(result[0])
|
15 |
+
mindmap = data.get('mindmap')
|
16 |
+
summary = data.get('summary')
|
17 |
+
return summary, mindmap
|