-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
561 lines (488 loc) · 19.8 KB
/
main.py
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
import os
import re
import json
import arxiv
import yaml
import logging
import argparse
import datetime
import requests
from typing import Optional
import time
import google.generativeai as genai
import requests
# https://github.com/google-gemini/cookbook/tree/main
# https://ai.google.dev/api?hl=zh-cn
class Translater:
def __init__(self, api_key: str):
self.api_key = api_key
genai.configure(api_key=self.api_key) # 填入自己的api_key
# 查询模型
for m in genai.list_models():
print(m.name)
print(m.supported_generation_methods)
sys_prompt = (
"You are a highly skilled translator specializing in artificial intelligence and computer science. \
You pride yourself on incredible accuracy and attention to detail. You always stick to the facts in the sources provided, and never make up new facts.\
Your translations are known for their accuracy, clarity, and fluency.\n\
Your task is to translate technical academic abstracts from English to Simplified Chinese.\
You will receive an English abstract, and you should produce a Chinese translation that adheres to the following:\n\
* **Accuracy:** All technical terms and concepts must be translated correctly.\n\
* **Clarity:** The translation should be easily understood by someone familiar with AI concepts.\n\
* **Fluency:** The translation should read naturally in Chinese.\n\
* **Output Format:** The returned text should not be bolded, not be separated into paragraphs, and remove all line breaks to merge into a single paragraph.\n \
Do not add your own opinions or interpretations; remain faithful to the original text while optimizing for readability. \
"
)
self.model = genai.GenerativeModel(
"gemini-1.5-pro-latest",
system_instruction=sys_prompt,
generation_config=genai.GenerationConfig(
# max_output_tokens=2000,
temperature=0.8,
),
)
# models/gemini-pro
# 输入令牌限制:30720
# 输出令牌限制:2048
# 模型安全:自动应用的安全设置,可由开发者调整。如需了解详情,请参阅安全设置
def translate(self, text: str):
response = self.model.generate_content(
f"Note output format, here is the abstract to translate:\n{text}"
)
return response.text
logging.basicConfig(
format="[%(asctime)s %(levelname)s] %(message)s",
datefmt="%m/%d/%Y %H:%M:%S",
level=logging.INFO,
)
base_url = "https://arxiv.paperswithcode.com/api/v0/papers/"
github_url = "https://api.github.com/search/repositories"
arxiv_url = "http://arxiv.org/"
def load_config(config_file: str) -> dict:
"""
config_file: input config file path
return: a dict of configuration
"""
# make filters pretty
def pretty_filters(**config) -> dict:
keywords = dict()
EXCAPE = '"'
QUOTA = "" # NO-USE
OR = " OR " # TODO
def parse_filters(filters: list):
ret = ""
for idx in range(0, len(filters)):
filter = filters[idx]
if len(filter.split()) > 1:
ret += EXCAPE + filter + EXCAPE
else:
ret += QUOTA + filter + QUOTA
if idx != len(filters) - 1:
ret += OR
return ret
for k, v in config["keywords"].items():
keywords[k] = parse_filters(v["filters"]) # {NeRF:}
return keywords
with open(config_file, "r") as f:
config = yaml.load(f, Loader=yaml.FullLoader)
config["kv"] = pretty_filters(**config)
logging.info(f"config = {config}")
return config
def get_authors(authors, first_author=False):
output = str()
if first_author == False:
output = ", ".join(str(author) for author in authors)
else:
output = authors[0]
return output
def sort_papers(papers):
output = dict()
keys = list(papers.keys())
keys.sort(reverse=True)
for key in keys:
output[key] = papers[key]
return output
def get_code_link(qword: str) -> str:
"""
This short function was auto-generated by ChatGPT.
I only renamed some params and added some comments.
@param qword: query string, eg. arxiv ids and paper titles
@return paper_code in github: string, if not found, return None
"""
# query = f"arxiv:{arxiv_id}"
query = f"{qword}"
params = {"q": query, "sort": "stars", "order": "desc"}
r = requests.get(github_url, params=params)
results = r.json()
code_link = None
if results["total_count"] > 0:
code_link = results["items"][0]["html_url"]
return code_link
def get_daily_papers(
topic, query="slam", max_results=2, translater: Optional[Translater] = None
):
"""
@param topic: str
@param query: str
@return paper_with_code: dict
"""
# output
content = dict()
content_to_web = dict()
print(f"query = {query}")
search_engine = arxiv.Search(
query=query, max_results=max_results, sort_by=arxiv.SortCriterion.SubmittedDate
)
for result in search_engine.results():
paper_id = result.get_short_id()
paper_title = result.title
paper_url = result.entry_id
code_url = base_url + paper_id # TODO
paper_abstract = result.summary.replace("\n", " ")
paper_authors = get_authors(result.authors)
paper_first_author = get_authors(result.authors, first_author=True)
primary_category = result.primary_category
publish_time = result.published.date()
update_time = result.updated.date()
comments = result.comment
if translater:
print(f"Translating {paper_title}")
retry_count = 0
retry_seconds = 60
NUM_RETRIES = 3
while retry_count < NUM_RETRIES:
try:
paper_abstract = translater.translate(paper_abstract)
break
except Exception as e:
print(f"Received {e} error, retry after {retry_seconds} seconds.")
time.sleep(retry_seconds)
retry_count += 1
# Here exponential backoff is employed to ensure the account doesn't get rate limited by making
# too many requests too quickly. This increases the time to wait between requests by a factor of 2.
retry_seconds *= 2
finally:
if retry_count == NUM_RETRIES:
print(
"Could not recover after making " f"{retry_count} attempts."
)
print(f"translatation failed. paper_abstract:\n {paper_abstract} ")
logging.info(f"Time = {update_time} title = {paper_title}")
paper_abstract = paper_abstract.rstrip() #删除末尾的指定字符,默认为空白符,包括空格、换行符、回车符、制表符。
# eg: 2108.09112v1 -> 2108.09112
ver_pos = paper_id.find("v")
if ver_pos == -1:
paper_key = paper_id
else:
paper_key = paper_id[0:ver_pos]
paper_url = arxiv_url + "abs/" + paper_key
try:
# source code link
r = requests.get(code_url).json()
repo_url = None
if "official" in r and r["official"]:
repo_url = r["official"]["url"]
# TODO: not found, two more chances
# else:
# repo_url = get_code_link(paper_title)
# if repo_url is None:
# repo_url = get_code_link(paper_key)
if repo_url is not None:
content[paper_key] = "|**{}**|[{}]({})|**[link]({})**|{}|\n".format(
update_time,
paper_title,
paper_url,
repo_url,
paper_abstract,
)
content_to_web[paper_key] = (
"- {}, Paper: [{}]({}), Code: **[{}]({})**,Abstract: {}".format(
update_time,
paper_title,
paper_url,
repo_url,
repo_url,
paper_abstract,
)
)
else:
content[paper_key] = "|**{}**|[{}]({})|null|{}|\n".format(
update_time, paper_title, paper_url, paper_abstract
)
content_to_web[paper_key] = "- {}, Paper: [{}]({}),{}".format(
update_time, paper_title, paper_url, paper_abstract
)
# TODO: select useful comments
comments = None
if comments != None:
content_to_web[paper_key] += f", {comments}\n"
else:
content_to_web[paper_key] += f"\n"
except Exception as e:
logging.error(f"exception: {e} with id: {paper_key}")
data = {topic: content}
data_web = {topic: content_to_web}
return data, data_web
def update_paper_links(filename):
"""
weekly update paper links in json file
"""
def parse_arxiv_string(s):
parts = s.split("|")
date = parts[1].strip()
title = parts[2].strip()
paper_url = parts[3].strip()
code = parts[4].strip()
abstract = parts[5].strip()
paper_url = re.sub(r"v\d+", "", paper_url)
return date, title, paper_url, code, abstract
with open(filename, "r") as f:
content = f.read()
if not content:
m = {}
else:
m = json.loads(content)
json_data = m.copy()
for keywords, v in json_data.items():
logging.info(f"keywords = {keywords}")
for paper_id, contents in v.items():
contents = str(contents)
(
update_time,
paper_title,
paper_url,
code_url,
abstract,
) = parse_arxiv_string(contents)
contents = "|{}|{}|{}|{}|{}|\n".format(
update_time, paper_title, paper_url, code_url, abstract
)
json_data[keywords][paper_id] = str(contents)
logging.info(f"paper_id = {paper_id}, contents = {contents}")
valid_link = False if "|null|" in contents else True
if valid_link:
continue
try:
code_url = base_url + paper_id # TODO
r = requests.get(code_url).json()
repo_url = None
if "official" in r and r["official"]:
repo_url = r["official"]["url"]
if repo_url is not None:
new_cont = contents.replace(
"|null|", f"|**[link]({repo_url})**|"
)
logging.info(f"ID = {paper_id}, contents = {new_cont}")
json_data[keywords][paper_id] = str(new_cont)
except Exception as e:
logging.error(f"exception: {e} with id: {paper_id}")
# dump to json file
with open(filename, "w") as f:
json.dump(json_data, f)
def update_json_file(filename, data_dict):
"""
daily update json file using data_dict
"""
with open(filename, "r") as f:
content = f.read()
if not content:
m = {}
else:
m = json.loads(content)
json_data = m.copy()
# update papers in each keywords
for data in data_dict:
for keyword in data.keys():
papers = data[keyword]
if keyword in json_data.keys():
json_data[keyword].update(papers)
else:
json_data[keyword] = papers
with open(filename, "w") as f:
json.dump(json_data, f)
def json_to_md(
filename,
md_filename,
task="",
to_web=False,
use_title=True,
use_tc=True,
use_b2t=True,
):
"""
@param filename: str
@param md_filename: str
@return None
"""
def pretty_math(s: str) -> str:
ret = ""
match = re.search(r"\$.*\$", s)
if match == None:
return s
math_start, math_end = match.span()
space_trail = space_leading = ""
if s[:math_start][-1] != " " and "*" != s[:math_start][-1]:
space_trail = " "
if s[math_end:][0] != " " and "*" != s[math_end:][0]:
space_leading = " "
ret += s[:math_start]
ret += f"{space_trail}${match.group()[1:-1].strip()}${space_leading}"
ret += s[math_end:]
return ret
DateNow = datetime.date.today()
DateNow = str(DateNow)
DateNow = DateNow.replace("-", ".")
with open(filename, "r") as f:
content = f.read()
if not content:
data = {}
else:
data = json.loads(content)
# clean README.md if daily already exist else create it
with open(md_filename, "w+") as f:
pass
# write data into README.md
with open(md_filename, "a+") as f:
if (use_title == True) and (to_web == True):
f.write("---\n" + "layout: default\n" + "---\n\n")
if use_title == True:
# f.write(("<p align="center"><h1 align="center"><br><ins>AI-ARXIV-DAILY"
# "</ins><br>Automatically Update AI Papers Daily</h1></p>\n"))
f.write("## Updated on " + DateNow + "\n")
else:
f.write("> Updated on " + DateNow + "\n")
# TODO: add usage
f.write("> Usage instructions: [here](./docs/README.md#usage)\n\n")
# Add: table of contents
if use_tc == True:
f.write("<details>\n")
f.write(" <summary>Table of Contents</summary>\n")
f.write(" <ol>\n")
for keyword in data.keys():
day_content = data[keyword]
if not day_content:
continue
kw = keyword.replace(" ", "-")
f.write(f" <li><a href=#{kw.lower()}>{keyword}</a></li>\n")
f.write(" </ol>\n")
f.write("</details>\n\n")
for keyword in data.keys():
day_content = data[keyword]
if not day_content:
continue
# the head of each part
f.write(f"## {keyword}\n\n")
if use_title == True:
if to_web == False:
f.write(
"|Publish Date|Title|Code|Abstract|\n"
+ "|---|---|---|--------------------------------------------------|\n"
)
else:
f.write("| Publish Date | Title | Code | Abstract |\n")
f.write(
"|:---------|:-----------------------|:------|:-------------------------------------------------|\n"
)
# sort papers by date
day_content = sort_papers(day_content)
for _, v in day_content.items():
if v is not None:
f.write(pretty_math(v)) # make latex pretty
f.write(f"\n")
# Add: back to top
if use_b2t:
top_info = f"#Updated on {DateNow}"
top_info = top_info.replace(" ", "-").replace(".", "")
f.write(
f"<p align=right>(<a href={top_info.lower()}>back to top</a>)</p>\n\n"
)
logging.info(f"{task} finished")
def demo(translater: Optional[Translater] = None, **config):
# TODO: use config
data_collector = []
data_collector_web = []
keywords = config["kv"]
max_results = config["max_results"]
publish_readme = config["publish_readme"]
publish_gitpage = config["publish_gitpage"]
b_update = config["update_paper_links"]
logging.info(f"Update Paper Link = {b_update}")
if config["update_paper_links"] == False:
logging.info(f"GET daily papers begin")
for topic, keyword in keywords.items():
logging.info(f"topic: {topic}, keyword: {keyword}")
data, data_web = get_daily_papers(
topic, query=keyword, max_results=max_results, translater=translater
)
data_collector.append(data)
data_collector_web.append(data_web)
print(f"data_collector:{data_collector}\n")
logging.info(f"GET daily papers end")
# 1. update README.md file
if publish_readme:
json_file = config["json_readme_path"]
md_file = config["md_readme_path"]
# update paper links
if config["update_paper_links"]:
update_paper_links(json_file)
else:
# update json data
update_json_file(json_file, data_collector)
# json data to markdown
json_to_md(json_file, md_file, task="Update Readme")
# 2. update docs/index.md file (to gitpage)
if publish_gitpage:
json_file = config["json_gitpage_path"]
md_file = config["md_gitpage_path"]
# TODO: duplicated update paper links!!!
if config["update_paper_links"]:
update_paper_links(json_file)
else:
update_json_file(json_file, data_collector)
json_to_md(
json_file,
md_file,
task="Update GitPage",
to_web=True,
use_tc=False,
use_b2t=False,
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--config_path", type=str, default="config.yaml", help="configuration file path"
)
parser.add_argument(
"--update_paper_links",
default=False,
action="store_true",
help="whether to update paper links etc.",
)
parser.add_argument(
"--google_api_key", type=str, default="", help="google ai api key."
)
args = parser.parse_args()
config = load_config(args.config_path)
# 覆盖从配置文件中读取的关键词,不采用从配置文件读取,而是直接写死
# This should be unencoded. Use `au:del_maestro AND ti:checkerboard`, not `au:del_maestro+AND+ti:checkerboard`.
config["kv"] = {
"多模态": 'abs:("Multi-modal Models" OR "Multimodal Model" OR "vision-language model"OR "Vision Language Models" \
"Vision-and-Language Pre-training" OR "Multimodal Learning" OR "multimodal pretraining") AND abs:("performance")',
"6DOF Object Pose": 'abs:("Object Pose Estimation" OR "object 6D pose estimation") AND abs:("performance")',
"nerf": 'abs:("Radiance Fields" AND "gaussian splatting") AND abs:("performance")',
"分类/检测/识别/分割": 'abs:("image classification" OR "object detection" OR "super resolution" OR "Object Tracking") AND abs:("performance")',
"生成模型": 'abs:("diffusion model" OR "text-to-video synthesis" OR "generative model")',
"LLM": 'abs:("state-of-the-art LLMs" OR "training language models") AND abs:("performance") OR ti:"large language Models"',
"Transformer": 'abs:("self-attention" OR "cross-attention" OR "cross attention" OR "Sparse attention" OR "attention") AND abs:("transformer") AND ti:("attention" OR "transformer")'
}
# config["kv"] = {
# "多模态": 'abs:("Multi-modal Models" OR "Multimodal Model" OR "vision-language model"OR "Vision Language Models") AND abs:("performance")'
# }
config = {**config, "update_paper_links": args.update_paper_links}
if args.google_api_key:
api = args.google_api_key
translater = Translater(api_key=api)
demo(translater, **config)
else:
demo(**config)