Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -50,6 +50,228 @@ class PaperManager:
|
|
| 50 |
time_ago = (datetime.now(timezone.utc) - published_time).days
|
| 51 |
except ValueError:
|
| 52 |
time_ago = "Unknown"
|
| 53 |
-
|
|
|
|
| 54 |
return f"""<div style='border-bottom: 1px solid #eee; padding: 10px 0;'>
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
time_ago = (datetime.now(timezone.utc) - published_time).days
|
| 51 |
except ValueError:
|
| 52 |
time_ago = "Unknown"
|
| 53 |
+
|
| 54 |
+
# Ensure the triple-quoted string is properly closed
|
| 55 |
return f"""<div style='border-bottom: 1px solid #eee; padding: 10px 0;'>
|
| 56 |
+
<a href='{url}' target='_blank' style='color: #000; text-decoration: none; font-weight: bold;'>{title}</a>
|
| 57 |
+
<div style='font-size: 0.8em; color: #666; margin-top: 5px;'>
|
| 58 |
+
{upvotes} upvotes | by {authors} | {time_ago} days ago | {comments} comments
|
| 59 |
+
</div>
|
| 60 |
+
</div>"""
|
| 61 |
+
|
| 62 |
+
def render_papers(self, papers, page=1):
|
| 63 |
+
"""
|
| 64 |
+
Render HTML for a list of papers based on the current page.
|
| 65 |
+
"""
|
| 66 |
+
if not papers:
|
| 67 |
+
return "<div>No papers available for this page.</div>"
|
| 68 |
+
start = (page - 1) * self.papers_per_page
|
| 69 |
+
end = start + self.papers_per_page
|
| 70 |
+
page_papers = papers[start:end]
|
| 71 |
+
return "".join([self.format_paper(paper) for paper in page_papers])
|
| 72 |
+
|
| 73 |
+
def search_papers(self, query):
|
| 74 |
+
"""
|
| 75 |
+
Filter papers based on the search query.
|
| 76 |
+
"""
|
| 77 |
+
if not query:
|
| 78 |
+
return self.cache
|
| 79 |
+
return [paper for paper in self.cache if query.lower() in paper.get('title', '').lower()]
|
| 80 |
+
|
| 81 |
+
css = """
|
| 82 |
+
html, body {
|
| 83 |
+
height: 100%;
|
| 84 |
+
margin: 0;
|
| 85 |
+
padding: 0;
|
| 86 |
+
display: flex;
|
| 87 |
+
justify-content: center;
|
| 88 |
+
align-items: center;
|
| 89 |
+
background-color: #f0f0f0;
|
| 90 |
+
}
|
| 91 |
+
.container {
|
| 92 |
+
font-family: Arial, sans-serif;
|
| 93 |
+
max-width: 800px;
|
| 94 |
+
width: 100%;
|
| 95 |
+
background-color: white;
|
| 96 |
+
padding: 20px;
|
| 97 |
+
border-radius: 10px;
|
| 98 |
+
box-shadow: 0 0 10px rgba(0,0,0,0.1);
|
| 99 |
+
}
|
| 100 |
+
.paper-list {
|
| 101 |
+
max-height: 400px;
|
| 102 |
+
overflow-y: auto;
|
| 103 |
+
border: 1px solid #eee;
|
| 104 |
+
border-radius: 5px;
|
| 105 |
+
padding: 10px;
|
| 106 |
+
margin-bottom: 10px;
|
| 107 |
+
}
|
| 108 |
+
.search-row {
|
| 109 |
+
display: flex;
|
| 110 |
+
gap: 10px;
|
| 111 |
+
margin-bottom: 20px;
|
| 112 |
+
}
|
| 113 |
+
.title {
|
| 114 |
+
text-align: center;
|
| 115 |
+
color: #333;
|
| 116 |
+
}
|
| 117 |
+
.footer {
|
| 118 |
+
display: flex;
|
| 119 |
+
justify-content: space-between;
|
| 120 |
+
align-items: center;
|
| 121 |
+
margin-top: 10px;
|
| 122 |
+
}
|
| 123 |
+
"""
|
| 124 |
+
|
| 125 |
+
# Instantiate the PaperManager
|
| 126 |
+
paper_manager = PaperManager()
|
| 127 |
+
|
| 128 |
+
def initialize():
|
| 129 |
+
"""
|
| 130 |
+
Initialize the app by fetching papers and setting initial states.
|
| 131 |
+
"""
|
| 132 |
+
success = paper_manager.fetch_papers()
|
| 133 |
+
if success:
|
| 134 |
+
papers = paper_manager.cache
|
| 135 |
+
page = 1
|
| 136 |
+
total_pages = paper_manager.total_pages
|
| 137 |
+
page_papers = paper_manager.render_papers(papers, page)
|
| 138 |
+
page_info_text = f"Page {page} of {total_pages}"
|
| 139 |
+
return page_papers, page, total_pages, papers, page_info_text
|
| 140 |
+
else:
|
| 141 |
+
error_html = "<div>Error fetching papers. Please try again later.</div>"
|
| 142 |
+
return error_html, 1, 1, [], "Page 1 of 1"
|
| 143 |
+
|
| 144 |
+
def refresh_papers(current_page, query):
|
| 145 |
+
"""
|
| 146 |
+
Refresh the papers. Fetch new data if a new day has started.
|
| 147 |
+
"""
|
| 148 |
+
success = paper_manager.fetch_papers()
|
| 149 |
+
if success:
|
| 150 |
+
# Reset to first page on refresh
|
| 151 |
+
new_page = 1
|
| 152 |
+
if query:
|
| 153 |
+
filtered = paper_manager.search_papers(query)
|
| 154 |
+
total_pages = (len(filtered) + paper_manager.papers_per_page - 1) // paper_manager.papers_per_page if filtered else 1
|
| 155 |
+
page_papers = paper_manager.render_papers(filtered, new_page)
|
| 156 |
+
page_info_text = f"Page {new_page} of {total_pages}"
|
| 157 |
+
return page_papers, new_page, total_pages, filtered, page_info_text
|
| 158 |
+
else:
|
| 159 |
+
papers = paper_manager.cache
|
| 160 |
+
total_pages = paper_manager.total_pages
|
| 161 |
+
page_papers = paper_manager.render_papers(papers, new_page)
|
| 162 |
+
page_info_text = f"Page {new_page} of {total_pages}"
|
| 163 |
+
return page_papers, new_page, total_pages, papers, page_info_text
|
| 164 |
+
else:
|
| 165 |
+
error_html = "<div>Error fetching papers. Please try again later.</div>"
|
| 166 |
+
return error_html, current_page, paper_manager.total_pages, [], f"Page {current_page} of {paper_manager.total_pages}"
|
| 167 |
+
|
| 168 |
+
def search_papers(query, papers):
|
| 169 |
+
"""
|
| 170 |
+
Search for papers based on the query and update the display.
|
| 171 |
+
"""
|
| 172 |
+
if query:
|
| 173 |
+
filtered = paper_manager.search_papers(query)
|
| 174 |
+
else:
|
| 175 |
+
filtered = papers
|
| 176 |
+
total_pages = (len(filtered) + paper_manager.papers_per_page - 1) // paper_manager.papers_per_page if filtered else 1
|
| 177 |
+
page = 1
|
| 178 |
+
page_papers = paper_manager.render_papers(filtered, page)
|
| 179 |
+
page_info_text = f"Page {page} of {total_pages}"
|
| 180 |
+
return page_papers, page, total_pages, filtered, page_info_text
|
| 181 |
+
|
| 182 |
+
def change_page(direction, current_page, total_pages, papers, query):
|
| 183 |
+
"""
|
| 184 |
+
Change the current page based on the direction ('next' or 'prev').
|
| 185 |
+
"""
|
| 186 |
+
if direction == "next" and current_page < total_pages:
|
| 187 |
+
new_page = current_page + 1
|
| 188 |
+
elif direction == "prev" and current_page > 1:
|
| 189 |
+
new_page = current_page - 1
|
| 190 |
+
else:
|
| 191 |
+
new_page = current_page # No change if limits are reached
|
| 192 |
+
|
| 193 |
+
if not papers:
|
| 194 |
+
page_papers = "<div>No papers available for this page.</div>"
|
| 195 |
+
else:
|
| 196 |
+
page_papers = paper_manager.render_papers(papers, new_page)
|
| 197 |
+
page_info_text = f"Page {new_page} of {total_pages}"
|
| 198 |
+
return page_papers, new_page, total_pages, papers, page_info_text
|
| 199 |
+
|
| 200 |
+
def go_prev(current_page, total_pages, papers, query):
|
| 201 |
+
"""
|
| 202 |
+
Handle the 'Previous Page' button click.
|
| 203 |
+
"""
|
| 204 |
+
return change_page("prev", current_page, total_pages, papers, query)
|
| 205 |
+
|
| 206 |
+
def go_next(current_page, total_pages, papers, query):
|
| 207 |
+
"""
|
| 208 |
+
Handle the 'Next Page' button click.
|
| 209 |
+
"""
|
| 210 |
+
return change_page("next", current_page, total_pages, papers, query)
|
| 211 |
+
|
| 212 |
+
# Define the Gradio Blocks interface
|
| 213 |
+
demo = gr.Blocks(css=css)
|
| 214 |
+
|
| 215 |
+
with demo:
|
| 216 |
+
with gr.Column(elem_classes=["container"]):
|
| 217 |
+
# Title
|
| 218 |
+
gr.Markdown("# Daily Papers - HackerNews Style", elem_classes=["title"])
|
| 219 |
+
|
| 220 |
+
# Search and Refresh Row
|
| 221 |
+
with gr.Row(elem_classes=["search-row"]):
|
| 222 |
+
search_input = gr.Textbox(label="Search papers", placeholder="Enter search term...")
|
| 223 |
+
refresh_button = gr.Button("Refresh")
|
| 224 |
+
|
| 225 |
+
# Paper List Display
|
| 226 |
+
paper_list = gr.HTML(elem_classes=["paper-list"])
|
| 227 |
+
|
| 228 |
+
# Pagination Controls
|
| 229 |
+
with gr.Row(elem_classes=["footer"]):
|
| 230 |
+
prev_button = gr.Button("Previous Page")
|
| 231 |
+
page_info = gr.Markdown("Page 1 of 1")
|
| 232 |
+
next_button = gr.Button("Next Page")
|
| 233 |
+
|
| 234 |
+
# Hidden States
|
| 235 |
+
current_page_state = gr.State(1)
|
| 236 |
+
total_pages_state = gr.State(1)
|
| 237 |
+
papers_state = gr.State([])
|
| 238 |
+
|
| 239 |
+
# Initialize the app on load
|
| 240 |
+
demo.load(
|
| 241 |
+
initialize,
|
| 242 |
+
outputs=[paper_list, current_page_state, total_pages_state, papers_state, page_info]
|
| 243 |
+
)
|
| 244 |
+
|
| 245 |
+
# Search Functionality
|
| 246 |
+
search_input.submit(
|
| 247 |
+
search_papers,
|
| 248 |
+
inputs=[search_input, papers_state],
|
| 249 |
+
outputs=[paper_list, current_page_state, total_pages_state, papers_state, page_info]
|
| 250 |
+
)
|
| 251 |
+
search_input.change(
|
| 252 |
+
search_papers,
|
| 253 |
+
inputs=[search_input, papers_state],
|
| 254 |
+
outputs=[paper_list, current_page_state, total_pages_state, papers_state, page_info]
|
| 255 |
+
)
|
| 256 |
+
|
| 257 |
+
# Refresh Functionality
|
| 258 |
+
refresh_button.click(
|
| 259 |
+
refresh_papers,
|
| 260 |
+
inputs=[current_page_state, search_input],
|
| 261 |
+
outputs=[paper_list, current_page_state, total_pages_state, papers_state, page_info]
|
| 262 |
+
)
|
| 263 |
+
|
| 264 |
+
# Pagination Buttons
|
| 265 |
+
prev_button.click(
|
| 266 |
+
go_prev,
|
| 267 |
+
inputs=[current_page_state, total_pages_state, papers_state, search_input],
|
| 268 |
+
outputs=[paper_list, current_page_state, total_pages_state, papers_state, page_info]
|
| 269 |
+
)
|
| 270 |
+
next_button.click(
|
| 271 |
+
go_next,
|
| 272 |
+
inputs=[current_page_state, total_pages_state, papers_state, search_input],
|
| 273 |
+
outputs=[paper_list, current_page_state, total_pages_state, papers_state, page_info]
|
| 274 |
+
)
|
| 275 |
+
|
| 276 |
+
# Launch the Gradio app
|
| 277 |
+
demo.launch()
|