Commit
·
20e59fb
1
Parent(s):
b4ee178
catch error
Browse files
app.py
CHANGED
@@ -1,22 +1,30 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
-
from cachetools import cached,TTLCache
|
|
|
|
|
4 |
|
5 |
-
CACHE_TIME = 60*60*12 # 12 hours
|
6 |
|
7 |
def parse_arxiv_id_from_paper_url(url):
|
8 |
return url.split("/")[-1]
|
9 |
|
|
|
10 |
@cached(cache=TTLCache(maxsize=500, ttl=CACHE_TIME))
|
11 |
def get_recommendations_from_semantic_scholar(semantic_scholar_id: str):
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
|
22 |
def filter_recommendations(recommendations, max_paper_count=5):
|
@@ -28,6 +36,7 @@ def filter_recommendations(recommendations, max_paper_count=5):
|
|
28 |
arxiv_paper = arxiv_paper[:max_paper_count]
|
29 |
return arxiv_paper
|
30 |
|
|
|
31 |
@cached(cache=TTLCache(maxsize=500, ttl=CACHE_TIME))
|
32 |
def get_paper_title_from_arxiv_id(arxiv_id):
|
33 |
try:
|
@@ -42,7 +51,7 @@ def get_paper_title_from_arxiv_id(arxiv_id):
|
|
42 |
def format_recommendation_into_markdown(arxiv_id, recommendations):
|
43 |
# title = get_paper_title_from_arxiv_id(arxiv_id)
|
44 |
# url = f"https://huggingface.co/papers/{arxiv_id}"
|
45 |
-
|
46 |
comment = "The following papers were recommended by the Semantic Scholar API \n\n"
|
47 |
for r in recommendations:
|
48 |
hub_paper_url = f"https://huggingface.co/papers/{r['externalIds']['ArXiv']}"
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
+
from cachetools import cached, TTLCache
|
4 |
+
|
5 |
+
CACHE_TIME = 60 * 60 * 12 # 12 hours
|
6 |
|
|
|
7 |
|
8 |
def parse_arxiv_id_from_paper_url(url):
|
9 |
return url.split("/")[-1]
|
10 |
|
11 |
+
|
12 |
@cached(cache=TTLCache(maxsize=500, ttl=CACHE_TIME))
|
13 |
def get_recommendations_from_semantic_scholar(semantic_scholar_id: str):
|
14 |
+
try:
|
15 |
+
r = requests.post(
|
16 |
+
"https://api.semanticscholar.org/recommendations/v1/papers/",
|
17 |
+
json={
|
18 |
+
"positivePaperIds": [semantic_scholar_id],
|
19 |
+
},
|
20 |
+
params={"fields": "externalIds,title,year", "limit": 10},
|
21 |
+
)
|
22 |
+
return r.json()["recommendedPapers"]
|
23 |
+
except KeyError as e:
|
24 |
+
raise gr.Error(
|
25 |
+
"Error getting recommendations, if this is a new paper it may not yet have"
|
26 |
+
" been indexed by Semantic Scholar."
|
27 |
+
) from e
|
28 |
|
29 |
|
30 |
def filter_recommendations(recommendations, max_paper_count=5):
|
|
|
36 |
arxiv_paper = arxiv_paper[:max_paper_count]
|
37 |
return arxiv_paper
|
38 |
|
39 |
+
|
40 |
@cached(cache=TTLCache(maxsize=500, ttl=CACHE_TIME))
|
41 |
def get_paper_title_from_arxiv_id(arxiv_id):
|
42 |
try:
|
|
|
51 |
def format_recommendation_into_markdown(arxiv_id, recommendations):
|
52 |
# title = get_paper_title_from_arxiv_id(arxiv_id)
|
53 |
# url = f"https://huggingface.co/papers/{arxiv_id}"
|
54 |
+
# comment = f"Recommended papers for [{title}]({url})\n\n"
|
55 |
comment = "The following papers were recommended by the Semantic Scholar API \n\n"
|
56 |
for r in recommendations:
|
57 |
hub_paper_url = f"https://huggingface.co/papers/{r['externalIds']['ArXiv']}"
|