davanstrien HF staff commited on
Commit
3a0a966
1 Parent(s): 7d95e35

add some caching

Browse files
Files changed (3) hide show
  1. app.py +4 -2
  2. requirements.in +1 -0
  3. requirements.txt +2 -0
app.py CHANGED
@@ -1,11 +1,13 @@
1
  import gradio as gr
2
  import requests
 
3
 
 
4
 
5
  def parse_arxiv_id_from_paper_url(url):
6
  return url.split("/")[-1]
7
 
8
-
9
  def get_recommendations_from_semantic_scholar(semantic_scholar_id: str):
10
  r = requests.post(
11
  "https://api.semanticscholar.org/recommendations/v1/papers/",
@@ -26,7 +28,7 @@ def filter_recommendations(recommendations, max_paper_count=5):
26
  arxiv_paper = arxiv_paper[:max_paper_count]
27
  return arxiv_paper
28
 
29
-
30
  def get_paper_title_from_arxiv_id(arxiv_id):
31
  try:
32
  return requests.get(f"https://huggingface.co/api/papers/{arxiv_id}").json()[
 
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
  r = requests.post(
13
  "https://api.semanticscholar.org/recommendations/v1/papers/",
 
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:
34
  return requests.get(f"https://huggingface.co/api/papers/{arxiv_id}").json()[
requirements.in CHANGED
@@ -1,2 +1,3 @@
 
1
  gradio
2
  requests
 
1
+ cachetools
2
  gradio
3
  requests
requirements.txt CHANGED
@@ -19,6 +19,8 @@ attrs==23.1.0
19
  # via
20
  # jsonschema
21
  # referencing
 
 
22
  certifi==2023.7.22
23
  # via
24
  # httpcore
 
19
  # via
20
  # jsonschema
21
  # referencing
22
+ cachetools==5.3.1
23
+ # via -r requirements.in
24
  certifi==2023.7.22
25
  # via
26
  # httpcore