Update app.py
Browse files
app.py
CHANGED
@@ -5,74 +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 |
-
# Sort papers by upvotes (descending order)
|
20 |
-
self.papers = sorted(data, key=lambda x: x.get('paper', {}).get('upvotes', 0), reverse=True)
|
21 |
-
self.total_pages = (len(self.papers) + self.papers_per_page - 1) // self.papers_per_page
|
22 |
-
self.current_page = 1 # Reset to first page after fetching
|
23 |
-
return True
|
24 |
-
except requests.RequestException as e:
|
25 |
-
print(f"Error fetching papers: {e}")
|
26 |
-
return False
|
27 |
-
|
28 |
-
def format_paper(self, paper):
|
29 |
-
title = paper.get('title', 'No title')
|
30 |
-
url = f"https://huggingface.co/papers/{paper['paper'].get('id', '')}"
|
31 |
-
authors = ', '.join([author.get('name', '') for author in paper['paper'].get('authors', [])])
|
32 |
-
upvotes = paper.get('paper', {}).get('upvotes', 0)
|
33 |
-
comments = paper.get('numComments', 0)
|
34 |
-
published_time = datetime.fromisoformat(paper.get('publishedAt', datetime.now(timezone.utc).isoformat()).replace('Z', '+00:00'))
|
35 |
-
time_ago = (datetime.now(timezone.utc) - published_time).days
|
36 |
-
|
37 |
-
return f"""
|
38 |
-
<div class="paper-item">
|
39 |
-
<h3><a href="{url}" target="_blank">{title}</a></h3>
|
40 |
-
<p class="paper-meta">
|
41 |
-
<span class="upvotes">{upvotes} upvotes</span> |
|
42 |
-
<span class="authors">by {authors}</span> |
|
43 |
-
<span class="time-ago">{time_ago} days ago</span> |
|
44 |
-
<span class="comments">{comments} comments</span>
|
45 |
-
</p>
|
46 |
-
</div>
|
47 |
-
"""
|
48 |
-
|
49 |
-
def render_papers(self):
|
50 |
-
start = (self.current_page - 1) * self.papers_per_page
|
51 |
-
end = start + self.papers_per_page
|
52 |
-
current_papers = self.papers[start:end]
|
53 |
-
|
54 |
-
if not current_papers:
|
55 |
-
return "<div class='no-papers'>No papers available for this page.</div>"
|
56 |
-
|
57 |
-
return "".join([self.format_paper(paper) for paper in current_papers])
|
58 |
-
|
59 |
-
def search_papers(self, query):
|
60 |
-
if not query:
|
61 |
-
self.current_page = 1
|
62 |
-
return self.render_papers()
|
63 |
-
|
64 |
-
filtered_papers = [paper for paper in self.papers if query.lower() in paper.get('title', '').lower()]
|
65 |
-
return "".join([self.format_paper(paper) for paper in filtered_papers[:self.papers_per_page]])
|
66 |
-
|
67 |
-
def next_page(self):
|
68 |
-
if self.current_page < self.total_pages:
|
69 |
-
self.current_page += 1
|
70 |
-
return self.render_papers(), f"Page {self.current_page} of {self.total_pages}"
|
71 |
-
|
72 |
-
def prev_page(self):
|
73 |
-
if self.current_page > 1:
|
74 |
-
self.current_page -= 1
|
75 |
-
return self.render_papers(), f"Page {self.current_page} of {self.total_pages}"
|
76 |
|
77 |
css = """
|
78 |
body {
|
@@ -99,6 +32,10 @@ body {
|
|
99 |
display: flex;
|
100 |
gap: 1rem;
|
101 |
margin-bottom: 1.5rem;
|
|
|
|
|
|
|
|
|
102 |
}
|
103 |
.paper-list {
|
104 |
max-height: 500px;
|
@@ -135,7 +72,7 @@ body {
|
|
135 |
align-items: center;
|
136 |
margin-top: 1rem;
|
137 |
}
|
138 |
-
button {
|
139 |
background-color: #4299e1;
|
140 |
color: white;
|
141 |
border: none;
|
@@ -143,8 +80,10 @@ button {
|
|
143 |
border-radius: 4px;
|
144 |
cursor: pointer;
|
145 |
transition: background-color 0.3s;
|
|
|
|
|
146 |
}
|
147 |
-
button:hover {
|
148 |
background-color: #3182ce;
|
149 |
}
|
150 |
.no-papers {
|
@@ -152,6 +91,15 @@ button:hover {
|
|
152 |
color: #718096;
|
153 |
padding: 1rem;
|
154 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
"""
|
156 |
|
157 |
paper_manager = PaperManager()
|
|
|
5 |
API_URL = "https://huggingface.co/api/daily_papers"
|
6 |
|
7 |
class PaperManager:
|
8 |
+
# ... [The PaperManager class remains unchanged] ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
css = """
|
11 |
body {
|
|
|
32 |
display: flex;
|
33 |
gap: 1rem;
|
34 |
margin-bottom: 1.5rem;
|
35 |
+
align-items: flex-end;
|
36 |
+
}
|
37 |
+
.search-row > div:first-child {
|
38 |
+
flex-grow: 1;
|
39 |
}
|
40 |
.paper-list {
|
41 |
max-height: 500px;
|
|
|
72 |
align-items: center;
|
73 |
margin-top: 1rem;
|
74 |
}
|
75 |
+
button, .button {
|
76 |
background-color: #4299e1;
|
77 |
color: white;
|
78 |
border: none;
|
|
|
80 |
border-radius: 4px;
|
81 |
cursor: pointer;
|
82 |
transition: background-color 0.3s;
|
83 |
+
font-size: 0.875rem;
|
84 |
+
line-height: 1.25rem;
|
85 |
}
|
86 |
+
button:hover, .button:hover {
|
87 |
background-color: #3182ce;
|
88 |
}
|
89 |
.no-papers {
|
|
|
91 |
color: #718096;
|
92 |
padding: 1rem;
|
93 |
}
|
94 |
+
#component-0 > div:first-child {
|
95 |
+
border-top-right-radius: 0;
|
96 |
+
border-bottom-right-radius: 0;
|
97 |
+
}
|
98 |
+
#component-1 {
|
99 |
+
height: 45px;
|
100 |
+
border-top-left-radius: 0;
|
101 |
+
border-bottom-left-radius: 0;
|
102 |
+
}
|
103 |
"""
|
104 |
|
105 |
paper_manager = PaperManager()
|