akhaliq HF staff commited on
Commit
55968d1
1 Parent(s): 13ff89e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -8
app.py CHANGED
@@ -11,21 +11,36 @@ class PaperManager:
11
  self.papers = []
12
  self.total_pages = 1
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  def fetch_papers(self):
15
  try:
16
  response = requests.get(f"{API_URL}?limit=100")
17
  response.raise_for_status()
18
  data = response.json()
19
 
20
- # Sort papers by 'publishedAt' descending, then by 'upvotes' descending
21
  self.papers = sorted(
22
  data,
23
- key=lambda x: (
24
- datetime.fromisoformat(
25
- x.get('publishedAt', datetime.now(timezone.utc).isoformat()).replace('Z', '+00:00')
26
- ),
27
- x.get('paper', {}).get('upvotes', 0)
28
- ),
29
  reverse=True
30
  )
31
 
@@ -311,4 +326,4 @@ with demo:
311
  </script>
312
  """)
313
 
314
- demo.launch()
 
11
  self.papers = []
12
  self.total_pages = 1
13
 
14
+ def calculate_score(self, paper):
15
+ """
16
+ Calculate the score of a paper based on upvotes and age.
17
+ This mimics the "hotness" algorithm used by platforms like Hacker News.
18
+ """
19
+ upvotes = paper.get('paper', {}).get('upvotes', 0)
20
+ published_at_str = paper.get('publishedAt', datetime.now(timezone.utc).isoformat())
21
+ try:
22
+ published_time = datetime.fromisoformat(published_at_str.replace('Z', '+00:00'))
23
+ except ValueError:
24
+ # If parsing fails, use current time to minimize the impact on sorting
25
+ published_time = datetime.now(timezone.utc)
26
+
27
+ time_diff = datetime.now(timezone.utc) - published_time
28
+ time_diff_hours = time_diff.total_seconds() / 3600 # Convert time difference to hours
29
+
30
+ # Avoid division by zero and apply the hotness formula
31
+ score = upvotes / ((time_diff_hours + 2) ** 1.5)
32
+ return score
33
+
34
  def fetch_papers(self):
35
  try:
36
  response = requests.get(f"{API_URL}?limit=100")
37
  response.raise_for_status()
38
  data = response.json()
39
 
40
+ # Sort papers by calculated score descending
41
  self.papers = sorted(
42
  data,
43
+ key=lambda x: self.calculate_score(x),
 
 
 
 
 
44
  reverse=True
45
  )
46
 
 
326
  </script>
327
  """)
328
 
329
+ demo.launch()