Update app.py
Browse files
app.py
CHANGED
@@ -7,23 +7,37 @@ API_URL = "https://huggingface.co/api/daily_papers"
|
|
7 |
class PaperManager:
|
8 |
def __init__(self, papers_per_page=10):
|
9 |
self.papers_per_page = papers_per_page
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
return
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
def format_paper(self, paper):
|
|
|
|
|
|
|
27 |
title = paper.get('title', 'No title')
|
28 |
paper_id = paper.get('paper', {}).get('id', '')
|
29 |
url = f"https://huggingface.co/papers/{paper_id}" if paper_id else "#"
|
@@ -38,152 +52,4 @@ class PaperManager:
|
|
38 |
time_ago = "Unknown"
|
39 |
|
40 |
return f"""<div style='border-bottom: 1px solid #eee; padding: 10px 0;'>
|
41 |
-
<a href='{url}' target='_blank' style='
|
42 |
-
<div style='font-size: 0.8em; color: #666; margin-top: 5px;'>
|
43 |
-
{upvotes} upvotes | by {authors} | {time_ago} days ago | {comments} comments
|
44 |
-
</div>
|
45 |
-
</div>"""
|
46 |
-
|
47 |
-
def render_papers(self, papers):
|
48 |
-
if not papers:
|
49 |
-
return "<div>No papers available for this page.</div>"
|
50 |
-
|
51 |
-
return "".join([self.format_paper(paper) for paper in papers])
|
52 |
-
|
53 |
-
def search_papers(self, papers, query):
|
54 |
-
if not query:
|
55 |
-
return self.render_papers(papers)
|
56 |
-
|
57 |
-
filtered_papers = [paper for paper in papers if query.lower() in paper.get('title', '').lower()]
|
58 |
-
if not filtered_papers:
|
59 |
-
return "<div>No papers match your search query.</div>"
|
60 |
-
return self.render_papers(filtered_papers)
|
61 |
-
|
62 |
-
css = """
|
63 |
-
html, body {
|
64 |
-
height: 100%;
|
65 |
-
margin: 0;
|
66 |
-
padding: 0;
|
67 |
-
display: flex;
|
68 |
-
justify-content: center;
|
69 |
-
align-items: center;
|
70 |
-
background-color: #f0f0f0;
|
71 |
-
}
|
72 |
-
.container {
|
73 |
-
font-family: Arial, sans-serif;
|
74 |
-
max-width: 800px;
|
75 |
-
width: 100%;
|
76 |
-
background-color: white;
|
77 |
-
padding: 20px;
|
78 |
-
border-radius: 10px;
|
79 |
-
box-shadow: 0 0 10px rgba(0,0,0,0.1);
|
80 |
-
}
|
81 |
-
.paper-list {
|
82 |
-
max-height: 400px;
|
83 |
-
overflow-y: auto;
|
84 |
-
border: 1px solid #eee;
|
85 |
-
border-radius: 5px;
|
86 |
-
padding: 10px;
|
87 |
-
margin-bottom: 10px;
|
88 |
-
}
|
89 |
-
.search-row {
|
90 |
-
display: flex;
|
91 |
-
gap: 10px;
|
92 |
-
margin-bottom: 20px;
|
93 |
-
}
|
94 |
-
.title {
|
95 |
-
text-align: center;
|
96 |
-
color: #333;
|
97 |
-
}
|
98 |
-
.footer {
|
99 |
-
display: flex;
|
100 |
-
justify-content: space-between;
|
101 |
-
align-items: center;
|
102 |
-
margin-top: 10px;
|
103 |
-
}
|
104 |
-
"""
|
105 |
-
|
106 |
-
paper_manager = PaperManager()
|
107 |
-
|
108 |
-
def initialize():
|
109 |
-
papers, total_pages = paper_manager.fetch_papers(page=1)
|
110 |
-
html = paper_manager.render_papers(papers)
|
111 |
-
return html, 1, total_pages, papers # paper_list, current_page, total_pages, papers
|
112 |
-
|
113 |
-
def refresh_papers(current_page, query):
|
114 |
-
# Refresh always starts from page 1
|
115 |
-
papers, total_pages = paper_manager.fetch_papers(page=1)
|
116 |
-
if query:
|
117 |
-
html = paper_manager.search_papers(papers, query)
|
118 |
-
else:
|
119 |
-
html = paper_manager.render_papers(papers)
|
120 |
-
return html, 1, total_pages, papers
|
121 |
-
|
122 |
-
def search_papers(query, papers):
|
123 |
-
html = paper_manager.search_papers(papers, query)
|
124 |
-
return html
|
125 |
-
|
126 |
-
def change_page(direction, current_page, total_pages, papers, query):
|
127 |
-
if direction == "next" and current_page < total_pages:
|
128 |
-
new_page = current_page + 1
|
129 |
-
elif direction == "prev" and current_page > 1:
|
130 |
-
new_page = current_page - 1
|
131 |
-
else:
|
132 |
-
new_page = current_page # No change if limits are reached
|
133 |
-
|
134 |
-
papers, total_pages = paper_manager.fetch_papers(page=new_page)
|
135 |
-
if query:
|
136 |
-
html = paper_manager.search_papers(papers, query)
|
137 |
-
else:
|
138 |
-
html = paper_manager.render_papers(papers)
|
139 |
-
return html, new_page, total_pages, papers
|
140 |
-
|
141 |
-
def go_prev(current_page, total_pages, papers, query):
|
142 |
-
return change_page("prev", current_page, total_pages, papers, query)
|
143 |
-
|
144 |
-
def go_next(current_page, total_pages, papers, query):
|
145 |
-
return change_page("next", current_page, total_pages, papers, query)
|
146 |
-
|
147 |
-
demo = gr.Blocks(css=css)
|
148 |
-
|
149 |
-
with demo:
|
150 |
-
with gr.Column(elem_classes=["container"]):
|
151 |
-
gr.Markdown("# Daily Papers - HackerNews Style", elem_classes=["title"])
|
152 |
-
with gr.Row(elem_classes=["search-row"]):
|
153 |
-
search_input = gr.Textbox(label="Search papers", placeholder="Enter search term...")
|
154 |
-
refresh_button = gr.Button("Refresh")
|
155 |
-
paper_list = gr.HTML(elem_classes=["paper-list"])
|
156 |
-
|
157 |
-
with gr.Row(elem_classes=["footer"]):
|
158 |
-
prev_button = gr.Button("Previous Page")
|
159 |
-
page_info = gr.Markdown("Page 1 of 1")
|
160 |
-
next_button = gr.Button("Next Page")
|
161 |
-
|
162 |
-
# Hidden states
|
163 |
-
current_page_state = gr.State(1)
|
164 |
-
total_pages_state = gr.State(1)
|
165 |
-
papers_state = gr.State([])
|
166 |
-
|
167 |
-
# Initialize the app
|
168 |
-
demo.load(initialize, outputs=[paper_list, current_page_state, total_pages_state, papers_state])
|
169 |
-
|
170 |
-
# Search functionality
|
171 |
-
search_input.submit(search_papers, inputs=[search_input, papers_state], outputs=[paper_list])
|
172 |
-
search_input.change(search_papers, inputs=[search_input, papers_state], outputs=[paper_list])
|
173 |
-
|
174 |
-
# Refresh functionality
|
175 |
-
refresh_button.click(refresh_papers,
|
176 |
-
inputs=[current_page_state, search_input],
|
177 |
-
outputs=[paper_list, current_page_state, total_pages_state, papers_state])
|
178 |
-
|
179 |
-
# Previous Page
|
180 |
-
prev_button.click(go_prev,
|
181 |
-
inputs=[current_page_state, total_pages_state, papers_state, search_input],
|
182 |
-
outputs=[paper_list, current_page_state, total_pages_state, papers_state])
|
183 |
-
|
184 |
-
# Next Page
|
185 |
-
next_button.click(go_next,
|
186 |
-
inputs=[current_page_state, total_pages_state, papers_state, search_input],
|
187 |
-
outputs=[paper_list, current_page_state, total_pages_state, papers_state])
|
188 |
-
|
189 |
-
demo.launch()
|
|
|
7 |
class PaperManager:
|
8 |
def __init__(self, papers_per_page=10):
|
9 |
self.papers_per_page = papers_per_page
|
10 |
+
self.cache = []
|
11 |
+
self.last_fetch_date = None
|
12 |
+
self.total_pages = 1
|
13 |
+
|
14 |
+
def fetch_papers(self):
|
15 |
+
"""
|
16 |
+
Fetch papers from the API if not already fetched today.
|
17 |
+
"""
|
18 |
+
today = datetime.now(timezone.utc).date()
|
19 |
+
if self.last_fetch_date == today and self.cache:
|
20 |
+
print("Using cached papers.")
|
21 |
+
return True
|
22 |
+
else:
|
23 |
+
try:
|
24 |
+
# Attempt to fetch all papers at once. Adjust 'limit' as per API's capability.
|
25 |
+
response = requests.get(f"{API_URL}?page=1&limit=1000")
|
26 |
+
response.raise_for_status()
|
27 |
+
data = response.json()
|
28 |
+
self.cache = sorted(data, key=lambda x: x.get('paper', {}).get('upvotes', 0), reverse=True)
|
29 |
+
self.last_fetch_date = today
|
30 |
+
self.total_pages = (len(self.cache) + self.papers_per_page - 1) // self.papers_per_page
|
31 |
+
print(f"Fetched {len(self.cache)} papers. Total pages: {self.total_pages}")
|
32 |
+
return True
|
33 |
+
except requests.RequestException as e:
|
34 |
+
print(f"Error fetching papers: {e}")
|
35 |
+
return False
|
36 |
|
37 |
def format_paper(self, paper):
|
38 |
+
"""
|
39 |
+
Format a single paper's information into HTML.
|
40 |
+
"""
|
41 |
title = paper.get('title', 'No title')
|
42 |
paper_id = paper.get('paper', {}).get('id', '')
|
43 |
url = f"https://huggingface.co/papers/{paper_id}" if paper_id else "#"
|
|
|
52 |
time_ago = "Unknown"
|
53 |
|
54 |
return f"""<div style='border-bottom: 1px solid #eee; padding: 10px 0;'>
|
55 |
+
<a href='{url}' target='_blank' style='co
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|