awacke1 commited on
Commit
fd22ee8
·
verified ·
1 Parent(s): a20388c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -92
app.py CHANGED
@@ -5,86 +5,8 @@ import gradio as gr
5
  import time
6
  import os
7
  import json
8
- from datetime import datetime
9
 
10
- def get_rank_papers(url, progress=gr.Progress(track_tqdm=True)):
11
- base_url = "https://paperswithcode.com"
12
-
13
- session = requests.Session()
14
- headers = {
15
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
16
- 'Cache-Control': 'no-cache'
17
- }
18
- print("Time run at : ", time.ctime())
19
- offset = 0
20
- data_list = {}
21
- break_duplicate = 10
22
-
23
- while True:
24
- response = session.get(url, headers=headers, params={'page': offset})
25
- if response.status_code != 200:
26
- print('Failed to retrieve data')
27
- break
28
- soup = BeautifulSoup(response.text, 'html.parser')
29
- paper_info = soup.find_all('div', class_='row infinite-item item paper-card')
30
- if not paper_info:
31
- break
32
- for ppr in paper_info:
33
- title = ppr.find('h1').text.strip()
34
-
35
- if "paper" in ppr.find('a')['href']:
36
- link = base_url + ppr.find('a')['href']
37
- else:
38
- link = ppr.find('a')['href']
39
- Github_Star = ppr.find('span', class_='badge badge-secondary').text.strip().replace(',', '')
40
- pdf_link = ''
41
- try:
42
- response_link = session.get(link, headers=headers)
43
- soup_link = BeautifulSoup(response_link.text, 'html.parser')
44
- paper_info_link = soup_link.find_all('div', class_='paper-abstract')
45
- pdf_link = paper_info_link[0].find('div', class_='col-md-12').find('a')['href']
46
- except:
47
- pass
48
- if title not in data_list:
49
- data_list[title] = {'link': link, 'Github Star': int(Github_Star), 'pdf_link': pdf_link.strip()}
50
- else:
51
- break_duplicate -= 1
52
- if break_duplicate == 0:
53
- return data_list
54
- offset += 1
55
- progress.update(offset)
56
- print('Data retrieval complete')
57
- return data_list
58
-
59
- def load_cached_data(cache_file):
60
- if os.path.exists(cache_file):
61
- with open(cache_file, 'r') as f:
62
- return json.load(f)
63
- return None
64
-
65
- def save_cached_data(data, cache_file):
66
- with open(cache_file, 'w') as f:
67
- json.dump(data, f)
68
-
69
- def format_dataframe(data):
70
- df = pd.DataFrame(data).T
71
- df['title'] = df.index
72
- df = df[['title', 'Github Star', 'link', 'pdf_link']]
73
- df['link'] = df['link'].apply(lambda x: f'<a href="{x}" target="_blank">Link</a>')
74
- df['pdf_link'] = df['pdf_link'].apply(lambda x: f'<a href="{x}" target="_blank">{x}</a>')
75
- return df
76
-
77
- def load_and_cache_data(url, cache_file):
78
- cached_data = load_cached_data(cache_file)
79
-
80
- if cached_data:
81
- print(f"Loading cached data from {cache_file}")
82
- return cached_data
83
-
84
- print(f"Fetching new data from {url}")
85
- new_data = get_rank_papers(url)
86
- save_cached_data(new_data, cache_file)
87
- return new_data
88
 
89
  def update_display(category):
90
  cache_file = f"{category}_papers_cache.json"
@@ -95,31 +17,36 @@ def update_display(category):
95
 
96
  return len(df), df.to_html(escape=False, index=False)
97
 
 
 
 
 
 
 
98
  with gr.Blocks() as demo:
99
  gr.Markdown("<h1><center>Papers Leaderboard</center></h1>")
100
 
101
  with gr.Tab("Top Trending Papers"):
102
- top_output = [gr.Textbox(label="Number of Papers Fetched"),
103
- gr.HTML()]
104
  top_button = gr.Button("Refresh Leaderboard")
105
- top_button.click(fn=lambda: update_display("top"), inputs=None, outputs=top_output)
106
 
107
  with gr.Tab("New Papers"):
108
- new_output = [gr.Textbox(label="Number of Papers Fetched"),
109
- gr.HTML()]
110
  new_button = gr.Button("Refresh Leaderboard")
111
- new_button.click(fn=lambda: update_display("latest"), inputs=None, outputs=new_output)
112
 
113
  with gr.Tab("Greatest Papers"):
114
- greatest_output = [gr.Textbox(label="Number of Papers Fetched"),
115
- gr.HTML()]
116
  greatest_button = gr.Button("Refresh Leaderboard")
117
- greatest_button.click(fn=lambda: update_display("greatest"), inputs=None, outputs=greatest_output)
118
 
119
  # Load initial data for all tabs
120
- demo.load(fn=lambda: (update_display("top"), update_display("latest"), update_display("greatest")),
121
- inputs=None,
122
- outputs=[top_output, new_output, greatest_output])
123
 
124
  # Launch the Gradio interface
125
- demo.launch()
 
 
5
  import time
6
  import os
7
  import json
 
8
 
9
+ # ... (keep all the previous functions unchanged)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  def update_display(category):
12
  cache_file = f"{category}_papers_cache.json"
 
17
 
18
  return len(df), df.to_html(escape=False, index=False)
19
 
20
+ def load_all_data():
21
+ top_count, top_html = update_display("top")
22
+ new_count, new_html = update_display("latest")
23
+ greatest_count, greatest_html = update_display("greatest")
24
+ return top_count, top_html, new_count, new_html, greatest_count, greatest_html
25
+
26
  with gr.Blocks() as demo:
27
  gr.Markdown("<h1><center>Papers Leaderboard</center></h1>")
28
 
29
  with gr.Tab("Top Trending Papers"):
30
+ top_count = gr.Textbox(label="Number of Papers Fetched")
31
+ top_html = gr.HTML()
32
  top_button = gr.Button("Refresh Leaderboard")
33
+ top_button.click(fn=lambda: update_display("top"), inputs=None, outputs=[top_count, top_html])
34
 
35
  with gr.Tab("New Papers"):
36
+ new_count = gr.Textbox(label="Number of Papers Fetched")
37
+ new_html = gr.HTML()
38
  new_button = gr.Button("Refresh Leaderboard")
39
+ new_button.click(fn=lambda: update_display("latest"), inputs=None, outputs=[new_count, new_html])
40
 
41
  with gr.Tab("Greatest Papers"):
42
+ greatest_count = gr.Textbox(label="Number of Papers Fetched")
43
+ greatest_html = gr.HTML()
44
  greatest_button = gr.Button("Refresh Leaderboard")
45
+ greatest_button.click(fn=lambda: update_display("greatest"), inputs=None, outputs=[greatest_count, greatest_html])
46
 
47
  # Load initial data for all tabs
48
+ demo.load(fn=load_all_data, outputs=[top_count, top_html, new_count, new_html, greatest_count, greatest_html])
 
 
49
 
50
  # Launch the Gradio interface
51
+ demo.launch()
52
+