akhaliq HF staff commited on
Commit
dea1fc4
·
verified ·
1 Parent(s): 22d5f09

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -53
app.py CHANGED
@@ -195,13 +195,12 @@ class PaperList:
195
 
196
  class PaperManager:
197
  """
198
- Manages sorting, pagination, and search queries for the list of papers.
199
  """
200
  def __init__(self, paper_list: PaperList, papers_per_page=30):
201
  self.paper_list = paper_list
202
  self.papers_per_page = papers_per_page
203
  self.sort_method = "hot" # Default sort method
204
- self.top_time_frame = "all time" # Default time frame for "Top" sorting
205
  self.sort_papers()
206
  # 'current_page' and 'total_pages' are set in 'sort_papers()'
207
 
@@ -240,27 +239,6 @@ class PaperManager:
240
  df_sorted = df
241
  elif self.sort_method == "new":
242
  df_sorted = df.sort_values(by='date', ascending=False) # Sort by 'date'
243
- elif self.sort_method == "top":
244
- # Filter based on the selected time frame
245
- now = datetime.datetime.now(timezone.utc)
246
- if self.top_time_frame == "day":
247
- time_threshold = now - datetime.timedelta(days=1)
248
- elif self.top_time_frame == "week":
249
- time_threshold = now - datetime.timedelta(weeks=1)
250
- elif self.top_time_frame == "month":
251
- time_threshold = now - datetime.timedelta(days=30)
252
- elif self.top_time_frame == "year":
253
- time_threshold = now - datetime.timedelta(days=365)
254
- elif self.top_time_frame == "all time":
255
- time_threshold = datetime.datetime.min.replace(tzinfo=timezone.utc)
256
- else:
257
- time_threshold = datetime.datetime.min.replace(tzinfo=timezone.utc)
258
-
259
- # Convert 'date' column to datetime
260
- df_sorted = df.copy()
261
- df_sorted['date_parsed'] = pd.to_datetime(df_sorted['date'], errors='coerce').dt.tz_localize(timezone.utc, ambiguous='NaT', nonexistent='NaT')
262
- df_sorted = df_sorted[df_sorted['date_parsed'] >= time_threshold]
263
- df_sorted = df_sorted.sort_values(by='upvotes', ascending=False).drop(columns=['date_parsed'])
264
  elif self.sort_method == "most_models":
265
  df_sorted = df.sort_values(by='models_count', ascending=False)
266
  elif self.sort_method == "most_datasets":
@@ -278,17 +256,13 @@ class PaperManager:
278
 
279
  def set_sort_method(self, method, time_frame=None):
280
  """
281
- Sets the sort method ('hot', 'new', 'top', 'most_models', 'most_datasets', 'most_spaces') and re-sorts the papers.
282
- If 'top' is selected, also sets the time frame.
283
  """
284
- valid_methods = ["hot", "new", "top", "most_models", "most_datasets", "most_spaces"]
285
  if method not in valid_methods:
286
  method = "hot"
287
  logger.info(f"Setting sort method to: {method}")
288
  self.sort_method = method
289
- if method == "top" and time_frame:
290
- self.top_time_frame = time_frame.lower()
291
- logger.info(f"Setting top time frame to: {self.top_time_frame}")
292
  self.sort_papers()
293
  return True # Assume success
294
 
@@ -448,15 +422,18 @@ logger.info("Scheduler shutdown registered.")
448
 
449
  def change_sort_method_ui(method: str, time_frame: str = "all time") -> str:
450
  """
451
- Changes the sort method and, if 'top' is selected, sets the time frame.
452
  """
453
  logger.info(f"Changing sort method to: {method} with time frame: {time_frame}")
454
  if method.lower() in ["most_models", "most_datasets", "most_spaces"]:
455
  paper_manager.set_sort_method(method.lower())
456
- elif method.lower() == "top":
457
- paper_manager.set_sort_method(method.lower(), time_frame)
458
- else:
459
  paper_manager.set_sort_method(method.lower())
 
 
 
 
 
460
  return paper_manager.get_current_page_papers()
461
 
462
 
@@ -625,28 +602,22 @@ with demo:
625
  </tr>
626
  </table>
627
  """)
628
- # Sort Options and Time Frame (conditionally visible)
629
  with gr.Row():
630
  sort_radio = gr.Radio(
631
- choices=["Hot", "New", "Top", "Most Models", "Most Datasets", "Most Spaces"],
632
  value="Hot",
633
  label="Sort By",
634
  interactive=True
635
  )
636
- time_frame_dropdown = gr.Dropdown(
637
- choices=["day", "week", "month", "year", "all time"],
638
- value="all time",
639
- label="Time Frame for Top",
640
- visible=False,
641
- interactive=True
642
- )
643
  # Paper list
644
  paper_list = gr.HTML()
645
  # Navigation Buttons
646
  with gr.Row():
647
  prev_button = gr.Button("Prev")
648
  next_button = gr.Button("Next")
649
-
650
  # Load papers on app start
651
  demo.load(
652
  fn=lambda: paper_manager.get_current_page_papers(),
@@ -657,17 +628,10 @@ with demo:
657
  prev_button.click(paper_manager.prev_page, outputs=[paper_list])
658
  next_button.click(paper_manager.next_page, outputs=[paper_list])
659
 
660
- # Sort option change: Toggle visibility of time_frame_dropdown based on sort method
661
- sort_radio.change(
662
- fn=lambda method: gr.update(visible=True) if method.lower() == "top" else gr.update(visible=False),
663
- inputs=[sort_radio],
664
- outputs=[time_frame_dropdown]
665
- )
666
-
667
- # Sort option change: Apply sorting method with time frame if applicable
668
  sort_radio.change(
669
  fn=change_sort_method_ui,
670
- inputs=[sort_radio, time_frame_dropdown],
671
  outputs=[paper_list]
672
  )
673
 
@@ -683,4 +647,4 @@ with demo:
683
  # --- Launch the App ---
684
 
685
  if __name__ == "__main__":
686
- demo.launch()
 
195
 
196
  class PaperManager:
197
  """
198
+ Manages sorting, pagination, and repository-based sorting for the list of papers.
199
  """
200
  def __init__(self, paper_list: PaperList, papers_per_page=30):
201
  self.paper_list = paper_list
202
  self.papers_per_page = papers_per_page
203
  self.sort_method = "hot" # Default sort method
 
204
  self.sort_papers()
205
  # 'current_page' and 'total_pages' are set in 'sort_papers()'
206
 
 
239
  df_sorted = df
240
  elif self.sort_method == "new":
241
  df_sorted = df.sort_values(by='date', ascending=False) # Sort by 'date'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
242
  elif self.sort_method == "most_models":
243
  df_sorted = df.sort_values(by='models_count', ascending=False)
244
  elif self.sort_method == "most_datasets":
 
256
 
257
  def set_sort_method(self, method, time_frame=None):
258
  """
259
+ Sets the sort method ('hot', 'new', 'most_models', 'most_datasets', 'most_spaces') and re-sorts the papers.
 
260
  """
261
+ valid_methods = ["hot", "new", "most_models", "most_datasets", "most_spaces"]
262
  if method not in valid_methods:
263
  method = "hot"
264
  logger.info(f"Setting sort method to: {method}")
265
  self.sort_method = method
 
 
 
266
  self.sort_papers()
267
  return True # Assume success
268
 
 
422
 
423
  def change_sort_method_ui(method: str, time_frame: str = "all time") -> str:
424
  """
425
+ Changes the sort method and, if applicable, sets additional parameters.
426
  """
427
  logger.info(f"Changing sort method to: {method} with time frame: {time_frame}")
428
  if method.lower() in ["most_models", "most_datasets", "most_spaces"]:
429
  paper_manager.set_sort_method(method.lower())
430
+ elif method.lower() == "hot":
 
 
431
  paper_manager.set_sort_method(method.lower())
432
+ elif method.lower() == "new":
433
+ paper_manager.set_sort_method(method.lower())
434
+ else:
435
+ # Default to 'hot' if method is unrecognized
436
+ paper_manager.set_sort_method("hot")
437
  return paper_manager.get_current_page_papers()
438
 
439
 
 
602
  </tr>
603
  </table>
604
  """)
605
+ # Sort Options (Removed "Top" and its timeframe)
606
  with gr.Row():
607
  sort_radio = gr.Radio(
608
+ choices=["Hot", "New", "Most Models", "Most Datasets", "Most Spaces"],
609
  value="Hot",
610
  label="Sort By",
611
  interactive=True
612
  )
613
+ # Removed time_frame_dropdown as "Top" sort is removed
 
 
 
 
 
 
614
  # Paper list
615
  paper_list = gr.HTML()
616
  # Navigation Buttons
617
  with gr.Row():
618
  prev_button = gr.Button("Prev")
619
  next_button = gr.Button("Next")
620
+
621
  # Load papers on app start
622
  demo.load(
623
  fn=lambda: paper_manager.get_current_page_papers(),
 
628
  prev_button.click(paper_manager.prev_page, outputs=[paper_list])
629
  next_button.click(paper_manager.next_page, outputs=[paper_list])
630
 
631
+ # Sort option change: Apply sorting method
 
 
 
 
 
 
 
632
  sort_radio.change(
633
  fn=change_sort_method_ui,
634
+ inputs=[sort_radio, None], # Pass None since time_frame_dropdown is removed
635
  outputs=[paper_list]
636
  )
637
 
 
647
  # --- Launch the App ---
648
 
649
  if __name__ == "__main__":
650
+ demo.launch()