ameerazam08 commited on
Commit
3e26d53
·
verified ·
1 Parent(s): a1ded45

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +124 -0
app.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ from bs4 import BeautifulSoup
3
+ import pandas as pd
4
+ import gradio as gr
5
+ import time
6
+
7
+ def get_rank_papers(url,progress=gr.Progress(track_tqdm=True)):
8
+ base_url = "https://paperswithcode.com"
9
+
10
+ session = requests.Session()
11
+ headers = {
12
+ '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',
13
+ 'Cache-Control': 'no-cache'
14
+ }
15
+ print("Time run at : ",time.ctime())
16
+ offset = 0
17
+ data_list = {}
18
+ break_duplicate = 10
19
+
20
+
21
+ while True:
22
+ response = session.get(url, headers=headers, params={'page': offset})
23
+ if response.status_code != 200:
24
+ print('Failed to retrieve data')
25
+ break
26
+ soup = BeautifulSoup(response.text, 'html.parser')
27
+ paper_info = soup.find_all('div', class_='row infinite-item item paper-card')
28
+ if not paper_info:
29
+ break
30
+ for ppr in paper_info:
31
+ title = ppr.find('h1').text.strip()
32
+
33
+ if "paper" in ppr.find('a')['href']:
34
+ link = base_url + ppr.find('a')['href']
35
+ else:
36
+ link = ppr.find('a')['href']
37
+ Github_Star = ppr.find('span', class_='badge badge-secondary').text.strip().replace(',', '')
38
+ pdf_link = ''
39
+ try:
40
+ response_link = session.get(link, headers=headers)
41
+ soup_link = BeautifulSoup(response_link.text, 'html.parser')
42
+ paper_info_link = soup_link.find_all('div', class_='paper-abstract')
43
+ pdf_link = paper_info_link[0].find('div', class_='col-md-12').find('a')['href']
44
+ except:
45
+ pass
46
+ if title not in data_list:
47
+ data_list[title] = {'link': link, 'Github Star': int(Github_Star), 'pdf_link': pdf_link.strip()}
48
+ else:
49
+ break_duplicate -= 1
50
+ if break_duplicate == 0:
51
+ return data_list
52
+ offset += 1
53
+ progress.update(offset)
54
+ print('Data retrieval complete')
55
+ return data_list
56
+
57
+ def reload_gradio_top():
58
+ # Retrieve rank papers
59
+ print('Retrieving data .. top papers')
60
+ rank_paper_df = get_rank_papers(url="https://paperswithcode.com/")
61
+ rank_paper_df = dict(sorted(rank_paper_df.items(), key=lambda x: x[1]['Github Star'], reverse=True))
62
+ rank_paper_df = pd.DataFrame(rank_paper_df).T
63
+ # Add title column
64
+ rank_paper_df['title'] = rank_paper_df.index
65
+ rank_paper_df = rank_paper_df[['title', 'Github Star', 'link', 'pdf_link']]
66
+ # Convert link column to HTML links
67
+ rank_paper_df['link'] = rank_paper_df['link'].apply(lambda x: f'<a href="{x}" target="_blank">Link</a>')
68
+ rank_paper_df['pdf_link'] = rank_paper_df['pdf_link'].apply(lambda x: f'<a href="{x}" target="_blank">{x}</a>')
69
+ rank_paper = rank_paper_df.to_html(escape=False, index=False)
70
+ return len(rank_paper_df),rank_paper
71
+
72
+
73
+ def reload_gradio_new():
74
+ # Retrieve rank papers
75
+ print('Retrieving data .. new papers')
76
+ rank_paper_df = get_rank_papers(url="https://paperswithcode.com/latest")
77
+ rank_paper_df = dict(sorted(rank_paper_df.items(), key=lambda x: x[1]['Github Star'], reverse=True))
78
+ rank_paper_df = pd.DataFrame(rank_paper_df).T
79
+ # Add title column
80
+ rank_paper_df['title'] = rank_paper_df.index
81
+ rank_paper_df = rank_paper_df[['title', 'Github Star', 'link', 'pdf_link']]
82
+ # Convert link column to HTML links
83
+ rank_paper_df['link'] = rank_paper_df['link'].apply(lambda x: f'<a href="{x}" target="_blank">Link</a>')
84
+ rank_paper_df['pdf_link'] = rank_paper_df['pdf_link'].apply(lambda x: f'<a href="{x}" target="_blank">{x}</a>')
85
+ rank_paper = rank_paper_df.to_html(escape=False, index=False)
86
+ return len(rank_paper_df),rank_paper
87
+
88
+
89
+ def reload_gradio_latest():
90
+ # Retrieve rank papers
91
+ print('Retrieving data .. latest papers')
92
+ rank_paper_df = get_rank_papers(url="https://paperswithcode.com/greatest")
93
+ rank_paper_df = dict(sorted(rank_paper_df.items(), key=lambda x: x[1]['Github Star'], reverse=True))
94
+ rank_paper_df = pd.DataFrame(rank_paper_df).T
95
+ # Add title column
96
+ rank_paper_df['title'] = rank_paper_df.index
97
+ rank_paper_df = rank_paper_df[['title', 'Github Star', 'link', 'pdf_link']]
98
+ # Convert link column to HTML links
99
+ rank_paper_df['link'] = rank_paper_df['link'].apply(lambda x: f'<a href="{x}" target="_blank">Link</a>')
100
+ rank_paper_df['pdf_link'] = rank_paper_df['pdf_link'].apply(lambda x: f'<a href="{x}" target="_blank">{x}</a>')
101
+ rank_paper = rank_paper_df.to_html(escape=False, index=False)
102
+ return len(rank_paper_df),rank_paper
103
+
104
+
105
+ with gr.Blocks() as demo:
106
+ gr.Markdown("<h1><center>Papers Leaderboard</center>")
107
+ with gr.Tab("Top Trending Papers"):
108
+ button = gr.Button("Load Leaderboard")
109
+ output = [gr.Textbox(label="Number of Papers Fetch"),
110
+ gr.HTML()]
111
+ button.click(fn=reload_gradio_top, inputs=None, outputs=output)
112
+ with gr.Tab("New Papers"):
113
+ button = gr.Button("Load Leaderboard")
114
+ output = [gr.Textbox(label="Number of Papers Fetch"),
115
+ gr.HTML()]
116
+ button.click(fn=reload_gradio_new, inputs=None, outputs=output)
117
+ with gr.Tab("Latest Papers"):
118
+ button = gr.Button("Load Leaderboard")
119
+ output = [gr.Textbox(label="Number of Papers Fetch"),
120
+ gr.HTML()]
121
+ button.click(fn=reload_gradio_latest, inputs=None, outputs=output)
122
+
123
+ # Launch the Gradio interface
124
+ demo.launch()