akhaliq HF staff commited on
Commit
0c9b0cc
·
verified ·
1 Parent(s): 4b38ab3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -1
app.py CHANGED
@@ -5,7 +5,73 @@ from datetime import datetime, timezone
5
  API_URL = "https://huggingface.co/api/daily_papers"
6
 
7
  class PaperManager:
8
- # ... [The PaperManager class remains unchanged] ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  css = """
11
  body {
 
5
  API_URL = "https://huggingface.co/api/daily_papers"
6
 
7
  class PaperManager:
8
+ def __init__(self, papers_per_page=10):
9
+ self.papers_per_page = papers_per_page
10
+ self.current_page = 1
11
+ self.papers = []
12
+ self.total_pages = 1
13
+
14
+ def fetch_papers(self):
15
+ try:
16
+ response = requests.get(f"{API_URL}?limit=50")
17
+ response.raise_for_status()
18
+ data = response.json()
19
+ self.papers = sorted(data, key=lambda x: x.get('paper', {}).get('upvotes', 0), reverse=True)
20
+ self.total_pages = (len(self.papers) + self.papers_per_page - 1) // self.papers_per_page
21
+ self.current_page = 1
22
+ return True
23
+ except requests.RequestException as e:
24
+ print(f"Error fetching papers: {e}")
25
+ return False
26
+
27
+ def format_paper(self, paper):
28
+ title = paper.get('title', 'No title')
29
+ url = f"https://huggingface.co/papers/{paper['paper'].get('id', '')}"
30
+ authors = ', '.join([author.get('name', '') for author in paper['paper'].get('authors', [])])
31
+ upvotes = paper.get('paper', {}).get('upvotes', 0)
32
+ comments = paper.get('numComments', 0)
33
+ published_time = datetime.fromisoformat(paper.get('publishedAt', datetime.now(timezone.utc).isoformat()).replace('Z', '+00:00'))
34
+ time_ago = (datetime.now(timezone.utc) - published_time).days
35
+
36
+ return f"""
37
+ <div class="paper-item">
38
+ <h3><a href="{url}" target="_blank">{title}</a></h3>
39
+ <p class="paper-meta">
40
+ <span class="upvotes">{upvotes} ▲</span>
41
+ <span class="authors">{authors}</span>
42
+ <span class="time-ago">{time_ago}d</span>
43
+ <span class="comments">{comments}💬</span>
44
+ </p>
45
+ </div>
46
+ """
47
+
48
+ def render_papers(self):
49
+ start = (self.current_page - 1) * self.papers_per_page
50
+ end = start + self.papers_per_page
51
+ current_papers = self.papers[start:end]
52
+
53
+ if not current_papers:
54
+ return "<div class='no-papers'>No papers available for this page.</div>"
55
+
56
+ return "".join([self.format_paper(paper) for paper in current_papers])
57
+
58
+ def search_papers(self, query):
59
+ if not query:
60
+ self.current_page = 1
61
+ return self.render_papers()
62
+
63
+ filtered_papers = [paper for paper in self.papers if query.lower() in paper.get('title', '').lower()]
64
+ return "".join([self.format_paper(paper) for paper in filtered_papers[:self.papers_per_page]])
65
+
66
+ def next_page(self):
67
+ if self.current_page < self.total_pages:
68
+ self.current_page += 1
69
+ return self.render_papers(), f"Page {self.current_page} of {self.total_pages}"
70
+
71
+ def prev_page(self):
72
+ if self.current_page > 1:
73
+ self.current_page -= 1
74
+ return self.render_papers(), f"Page {self.current_page} of {self.total_pages}"
75
 
76
  css = """
77
  body {