Update app.py
Browse files
app.py
CHANGED
@@ -5,73 +5,7 @@ from datetime import datetime, timezone
|
|
5 |
API_URL = "https://huggingface.co/api/daily_papers"
|
6 |
|
7 |
class PaperManager:
|
8 |
-
|
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 {
|
@@ -85,6 +19,10 @@ body {
|
|
85 |
margin: 0 auto;
|
86 |
background-color: white;
|
87 |
padding: 1rem;
|
|
|
|
|
|
|
|
|
88 |
}
|
89 |
.title {
|
90 |
text-align: center;
|
@@ -102,11 +40,12 @@ body {
|
|
102 |
flex-grow: 1;
|
103 |
}
|
104 |
.paper-list {
|
105 |
-
|
106 |
overflow-y: auto;
|
107 |
border: 1px solid #e2e8f0;
|
108 |
border-radius: 8px;
|
109 |
padding: 0.5rem;
|
|
|
110 |
}
|
111 |
.paper-item {
|
112 |
border-bottom: 1px solid #e2e8f0;
|
@@ -138,7 +77,7 @@ body {
|
|
138 |
display: flex;
|
139 |
justify-content: space-between;
|
140 |
align-items: center;
|
141 |
-
|
142 |
}
|
143 |
button, .button {
|
144 |
background-color: #4299e1;
|
|
|
5 |
API_URL = "https://huggingface.co/api/daily_papers"
|
6 |
|
7 |
class PaperManager:
|
8 |
+
# ... [The PaperManager class remains unchanged] ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
css = """
|
11 |
body {
|
|
|
19 |
margin: 0 auto;
|
20 |
background-color: white;
|
21 |
padding: 1rem;
|
22 |
+
box-sizing: border-box;
|
23 |
+
display: flex;
|
24 |
+
flex-direction: column;
|
25 |
+
min-height: 100vh;
|
26 |
}
|
27 |
.title {
|
28 |
text-align: center;
|
|
|
40 |
flex-grow: 1;
|
41 |
}
|
42 |
.paper-list {
|
43 |
+
flex-grow: 1;
|
44 |
overflow-y: auto;
|
45 |
border: 1px solid #e2e8f0;
|
46 |
border-radius: 8px;
|
47 |
padding: 0.5rem;
|
48 |
+
margin-bottom: 1rem;
|
49 |
}
|
50 |
.paper-item {
|
51 |
border-bottom: 1px solid #e2e8f0;
|
|
|
77 |
display: flex;
|
78 |
justify-content: space-between;
|
79 |
align-items: center;
|
80 |
+
padding-top: 0.5rem;
|
81 |
}
|
82 |
button, .button {
|
83 |
background-color: #4299e1;
|