akhaliq HF staff commited on
Commit
2bc512d
·
verified ·
1 Parent(s): a7e2292

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -48
app.py CHANGED
@@ -47,25 +47,20 @@ def get_df() -> pd.DataFrame:
47
 
48
  # Prepare the DataFrame by removing 'abstract'
49
  logger.info("Removing 'abstract' column if present.")
50
- paper_info = []
51
- for _, row in tqdm.auto.tqdm(df.iterrows(), total=len(df)):
52
- info = row.copy()
53
- if "abstract" in info:
54
- del info["abstract"]
55
- paper_info.append(info)
56
- df_prepared = pd.DataFrame(paper_info)
57
 
58
  # Add 'paper_page' links
59
  logger.info("Adding 'paper_page' links.")
60
- df_prepared["paper_page"] = df_prepared["arxiv_id"].apply(lambda x: f"https://huggingface.co/papers/{x}")
61
 
62
  # Verify that 'date' column exists
63
- if 'date' not in df_prepared.columns:
64
- logger.error("'date' column is missing from the DataFrame.")
65
- df_prepared["date"] = datetime.datetime.now(timezone.utc).strftime("%Y-%m-%d")
66
 
67
  logger.info("DataFrame preparation complete.")
68
- return df_prepared
69
  except Exception as e:
70
  logger.error(f"Error in get_df: {e}")
71
  return pd.DataFrame() # Return empty DataFrame on error
@@ -157,7 +152,6 @@ class PaperManager:
157
  self.paper_list = paper_list
158
  self.papers_per_page = papers_per_page
159
  self.sort_method = "hot" # Default sort method
160
- self.top_time_frame = "all time" # Default time frame for "Top" sorting
161
  self.sort_papers()
162
  # 'current_page' and 'total_pages' are set in 'sort_papers()'
163
 
@@ -552,7 +546,7 @@ with demo:
552
 
553
  Once your paper is submitted, it will automatically appear in this demo.
554
  """)
555
- # Hacker News-like Header
556
  with gr.Row():
557
  gr.HTML("""
558
  <table border="0" cellpadding="0" cellspacing="0" class="header-table">
@@ -563,33 +557,26 @@ with demo:
563
  </span>
564
  </td>
565
  <td align="right">
566
- <!-- Future Navigation Links Can Be Added Here -->
 
 
 
567
  </td>
568
  </tr>
569
  </table>
570
  """)
571
- # Sort Options and Time Frame (conditionally visible)
572
- with gr.Row():
573
- sort_radio = gr.Radio(
574
- choices=["Hot", "New", "Top"],
575
- value="Hot",
576
- label="Sort By",
577
- interactive=True
578
- )
579
- time_frame_dropdown = gr.Dropdown(
580
- choices=["day", "week", "month", "year", "all time"],
581
- value="all time",
582
- label="Time Frame for Top",
583
- visible=False,
584
- interactive=True
585
- )
586
  # Paper list
587
  paper_list = gr.HTML()
588
  # Navigation Buttons
589
  with gr.Row():
590
  prev_button = gr.Button("Prev")
591
  next_button = gr.Button("Next")
592
-
593
  # Load papers on app start
594
  demo.load(
595
  fn=lambda: paper_manager.get_current_page_papers(),
@@ -600,30 +587,51 @@ with demo:
600
  prev_button.click(paper_manager.prev_page, outputs=[paper_list])
601
  next_button.click(paper_manager.next_page, outputs=[paper_list])
602
 
603
- # Sort option change: Toggle visibility of time_frame_dropdown based on sort method
604
- sort_radio.change(
605
- fn=lambda method: gr.update(visible=True) if method.lower() == "top" else gr.update(visible=False),
606
- inputs=[sort_radio],
607
- outputs=[time_frame_dropdown]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
608
  )
609
 
610
- # Sort option change: Apply sorting method with time frame if applicable
611
- sort_radio.change(
612
- fn=change_sort_method_ui,
613
- inputs=[sort_radio, time_frame_dropdown],
 
 
 
 
 
614
  outputs=[paper_list]
615
  )
616
 
617
- # Footer
618
- gr.Markdown("""
619
- Related useful Spaces:
620
- - [Semantic Scholar Paper Recommender](https://huggingface.co/spaces/librarian-bots/recommend_similar_papers) by [davanstrien](https://huggingface.co/davanstrien)
621
- - [ArXiv CS RAG](https://huggingface.co/spaces/bishmoy/Arxiv-CS-RAG) by [bishmoy](https://huggingface.co/bishmoy)
622
- - [Paper Q&A](https://huggingface.co/spaces/chansung/paper_qa) by [chansung](https://huggingface.co/chansung)
623
- """)
624
 
625
 
626
  # --- Launch the App ---
627
 
628
  if __name__ == "__main__":
629
- demo.launch()
 
47
 
48
  # Prepare the DataFrame by removing 'abstract'
49
  logger.info("Removing 'abstract' column if present.")
50
+ if 'abstract' in df.columns:
51
+ df = df.drop(columns=['abstract'])
 
 
 
 
 
52
 
53
  # Add 'paper_page' links
54
  logger.info("Adding 'paper_page' links.")
55
+ df["paper_page"] = df["arxiv_id"].apply(lambda x: f"https://huggingface.co/papers/{x}")
56
 
57
  # Verify that 'date' column exists
58
+ if 'date' not in df.columns:
59
+ logger.error("'date' column is missing from the DataFrame. Filling with current date.")
60
+ df["date"] = datetime.datetime.now(timezone.utc).strftime("%Y-%m-%d")
61
 
62
  logger.info("DataFrame preparation complete.")
63
+ return df
64
  except Exception as e:
65
  logger.error(f"Error in get_df: {e}")
66
  return pd.DataFrame() # Return empty DataFrame on error
 
152
  self.paper_list = paper_list
153
  self.papers_per_page = papers_per_page
154
  self.sort_method = "hot" # Default sort method
 
155
  self.sort_papers()
156
  # 'current_page' and 'total_pages' are set in 'sort_papers()'
157
 
 
546
 
547
  Once your paper is submitted, it will automatically appear in this demo.
548
  """)
549
+ # Hacker News-like Header with "Hot" and "New" sort options
550
  with gr.Row():
551
  gr.HTML("""
552
  <table border="0" cellpadding="0" cellspacing="0" class="header-table">
 
557
  </span>
558
  </td>
559
  <td align="right">
560
+ <span class="pagetop">
561
+ <a href="#" id="hot_sort">Hot</a> |
562
+ <a href="#" id="new_sort">New</a>
563
+ </span>
564
  </td>
565
  </tr>
566
  </table>
567
  """)
568
+ # Hidden components for Gradio to capture sort clicks
569
+ hot_sort = gr.Button("Hot", visible=False)
570
+ new_sort = gr.Button("New", visible=False)
571
+ # Sort Options and Time Frame (conditionally visible) - Removed "Top" sort
572
+ # Removed the time_frame_dropdown as "Top" sort is removed
 
 
 
 
 
 
 
 
 
 
573
  # Paper list
574
  paper_list = gr.HTML()
575
  # Navigation Buttons
576
  with gr.Row():
577
  prev_button = gr.Button("Prev")
578
  next_button = gr.Button("Next")
579
+
580
  # Load papers on app start
581
  demo.load(
582
  fn=lambda: paper_manager.get_current_page_papers(),
 
587
  prev_button.click(paper_manager.prev_page, outputs=[paper_list])
588
  next_button.click(paper_manager.next_page, outputs=[paper_list])
589
 
590
+ # Sort option changes via header links
591
+ # Since Gradio cannot capture clicks on HTML directly, use hidden buttons and trigger them via JavaScript
592
+ # Add JavaScript to trigger Gradio button clicks when header links are clicked
593
+ demo.load(
594
+ fn=lambda: None,
595
+ outputs=[],
596
+ _js="""
597
+ const hotLink = document.getElementById('hot_sort');
598
+ const newLink = document.getElementById('new_sort');
599
+ const hotButton = document.querySelector('button#component-__block-0--hot_sort');
600
+ const newButton = document.querySelector('button#component-__block-0--new_sort');
601
+
602
+ if (hotLink && hotButton) {
603
+ hotLink.addEventListener('click', (e) => {
604
+ e.preventDefault();
605
+ hotButton.click();
606
+ });
607
+ }
608
+
609
+ if (newLink && newButton) {
610
+ newLink.addEventListener('click', (e) => {
611
+ e.preventDefault();
612
+ newButton.click();
613
+ });
614
+ }
615
+ """
616
  )
617
 
618
+ # Assign hidden buttons to trigger sort methods
619
+ hot_sort.click(
620
+ fn=lambda: change_sort_method_ui("hot"),
621
+ inputs=[],
622
+ outputs=[paper_list]
623
+ )
624
+ new_sort.click(
625
+ fn=lambda: change_sort_method_ui("new"),
626
+ inputs=[],
627
  outputs=[paper_list]
628
  )
629
 
630
+ # Footer - Removed as per request
631
+ # Removed the footer markdown section
 
 
 
 
 
632
 
633
 
634
  # --- Launch the App ---
635
 
636
  if __name__ == "__main__":
637
+ demo.launch()