openfree commited on
Commit
edb3b94
ยท
verified ยท
1 Parent(s): c5b7f37

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +213 -0
app.py ADDED
@@ -0,0 +1,213 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ import datetime
4
+
5
+ import gradio as gr
6
+ import pandas as pd
7
+ from apscheduler.schedulers.background import BackgroundScheduler
8
+ from gradio_calendar import Calendar
9
+
10
+ from papers import PaperList, get_df
11
+
12
+ DESCRIPTION = "# [Papers Leaderboard](https://huggingface.co/papers)\nAn interactive interface to explore and filter the latest papers."
13
+ FOOT_NOTE = """\
14
+ Community: https://discord.gg/openfreeai
15
+ """
16
+
17
+ paper_list = PaperList(get_df())
18
+
19
+ def update_paper_list() -> None:
20
+ global paper_list # noqa: PLW0603
21
+ paper_list = PaperList(get_df())
22
+
23
+ scheduler = BackgroundScheduler()
24
+ scheduler.add_job(func=update_paper_list, trigger="cron", hour="*", timezone="UTC", misfire_grace_time=60)
25
+ scheduler.start()
26
+
27
+ def update_df() -> gr.Dataframe:
28
+ return gr.Dataframe(value=paper_list.df_prettified)
29
+
30
+ def update_num_papers(df: pd.DataFrame) -> str:
31
+ return f"{len(df)} / {len(paper_list.df_raw)}"
32
+
33
+ def search(
34
+ start_date: datetime.datetime,
35
+ end_date: datetime.datetime,
36
+ search_title: str,
37
+ search_abstract: str,
38
+ max_num_to_retrieve: int,
39
+ ) -> pd.DataFrame:
40
+ return paper_list.search(start_date, end_date, search_title, search_abstract, max_num_to_retrieve)
41
+
42
+
43
+ # ์ถ”๊ฐ€ํ•  CSS (Gradio Blocks์˜ css ํŒŒ๋ผ๋ฏธํ„ฐ)
44
+ css = """
45
+ /* ์ „์ฒด ๋ฐฐ๊ฒฝ์ด๋‚˜ ์ปจํ…Œ์ด๋„ˆ ์Šคํƒ€์ผ */
46
+ body {
47
+ margin: 0;
48
+ padding: 0;
49
+ background: linear-gradient(135deg, #eef2ff 0%, #fdfdfd 100%);
50
+ font-family: "Helvetica Neue", Arial, sans-serif;
51
+ }
52
+
53
+ /* Hero / Header ์„น์…˜ ์Šคํƒ€์ผ */
54
+ #hero-section {
55
+ background: linear-gradient(135deg, #3b82f6 0%, #9333ea 100%);
56
+ padding: 2rem;
57
+ border-radius: 0.5rem;
58
+ margin-bottom: 1rem;
59
+ color: white;
60
+ }
61
+ #hero-section h1 {
62
+ font-size: 2.2rem;
63
+ font-weight: bold;
64
+ margin-bottom: 0.5rem;
65
+ }
66
+ #hero-section p {
67
+ font-size: 1.1rem;
68
+ margin: 0;
69
+ }
70
+
71
+ /* ๊ฒ€์ƒ‰ ์„น์…˜ ์Šคํƒ€์ผ */
72
+ .search-container {
73
+ background-color: #ffffffdd;
74
+ backdrop-filter: blur(6px);
75
+ border-radius: 0.75rem;
76
+ padding: 1rem 1.5rem;
77
+ box-shadow: 0 3px 5px rgba(0,0,0,0.1);
78
+ margin-bottom: 1rem;
79
+ }
80
+ .search-container h2 {
81
+ margin-top: 0;
82
+ font-size: 1.4rem;
83
+ color: #374151;
84
+ }
85
+
86
+ /* ๊ทธ๋ฆฌ๋“œ๋‚˜ ์นด๋“œ ์Šคํƒ€์ผ */
87
+ .card {
88
+ background-color: #fff;
89
+ border-radius: 0.75rem;
90
+ padding: 1.5rem;
91
+ box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
92
+ margin-bottom: 1rem;
93
+ }
94
+ .card h3 {
95
+ margin-top: 0;
96
+ font-size: 1.2rem;
97
+ color: #111827;
98
+ }
99
+
100
+ /* ํ‘ธํ„ฐ/ํ•˜๋‹จ๋ถ€ ์Šคํƒ€์ผ */
101
+ .footer {
102
+ color: #6b7280;
103
+ font-size: 0.9rem;
104
+ text-align: center;
105
+ margin-top: 2rem;
106
+ }
107
+
108
+ /* DataFrame ํ…Œ์ด๋ธ” ์ถ”๊ฐ€ ์Šคํƒ€์ผ */
109
+ #table table {
110
+ border-collapse: collapse;
111
+ width: 100%;
112
+ background: #fafafa;
113
+ }
114
+ #table th, #table td {
115
+ border: 1px solid #e5e7eb;
116
+ padding: 0.5rem;
117
+ text-align: left;
118
+ }
119
+ #table thead {
120
+ background-color: #f3f4f6;
121
+ font-weight: 600;
122
+ }
123
+ """
124
+
125
+ with gr.Blocks(css=css) as demo:
126
+ # ์ƒ๋‹จ Hero ์„น์…˜
127
+ with gr.Box(elem_id="hero-section"):
128
+ gr.Markdown(
129
+ """
130
+ # Papers Leaderboard
131
+ Explore the latest papers and filter by date, title, or abstract keywords.
132
+ """
133
+ )
134
+ gr.Markdown("#### [View on HuggingFace](https://huggingface.co/papers)")
135
+
136
+ gr.Markdown("### Search & Filter", elem_classes="search-container")
137
+
138
+ # ๊ฒ€์ƒ‰ ์˜์—ญ
139
+ with gr.Group():
140
+ search_title = gr.Textbox(label="Search title")
141
+ with gr.Row():
142
+ with gr.Column(scale=4):
143
+ search_abstract = gr.Textbox(
144
+ label="Search abstract",
145
+ info="Search within abstracts (may not be fully accurate).",
146
+ )
147
+ with gr.Column(scale=1):
148
+ max_num_to_retrieve = gr.Slider(
149
+ label="Max number to retrieve",
150
+ info="Applies only to abstract-based searching",
151
+ minimum=1,
152
+ maximum=len(paper_list.df_raw),
153
+ step=1,
154
+ value=100,
155
+ )
156
+ with gr.Row():
157
+ start_date = Calendar(label="Start date", type="datetime", value="2023-05-05")
158
+ end_date = Calendar(label="End date", type="datetime")
159
+
160
+ # ๊ฒฐ๊ณผ ํ‘œ์‹œ
161
+ with gr.Group(elem_id="results-section"):
162
+ with gr.Box(elem_id="stats-container", _class="card"):
163
+ num_papers = gr.Textbox(
164
+ label="Number of papers",
165
+ value=update_num_papers(paper_list.df_raw),
166
+ interactive=False
167
+ )
168
+ # ํ…Œ์ด๋ธ”
169
+ df = gr.Dataframe(
170
+ value=paper_list.df_prettified,
171
+ datatype=paper_list.column_datatype,
172
+ type="pandas",
173
+ interactive=False,
174
+ max_height=1000,
175
+ elem_id="table",
176
+ column_widths=["10%", "10%", "60%", "10%", "5%", "5%"],
177
+ wrap=True,
178
+ )
179
+
180
+ gr.Markdown(FOOT_NOTE, elem_classes="footer")
181
+
182
+ # ์ด๋ฒคํŠธ ์—ฐ๊ฒฐ
183
+ gr.on(
184
+ triggers=[start_date.change, end_date.change, search_title.submit, search_abstract.submit],
185
+ fn=search,
186
+ inputs=[start_date, end_date, search_title, search_abstract, max_num_to_retrieve],
187
+ outputs=df,
188
+ api_name=False,
189
+ ).then(
190
+ fn=update_num_papers,
191
+ inputs=df,
192
+ outputs=num_papers,
193
+ queue=False,
194
+ api_name=False,
195
+ )
196
+
197
+ # ์ฒซ ๋กœ๋“œ ์‹œ df ๊ฐฑ์‹  -> ๋…ผ๋ฌธ ์ˆ˜ ๊ฐฑ์‹ 
198
+ demo.load(
199
+ fn=update_df,
200
+ outputs=df,
201
+ queue=False,
202
+ api_name=False,
203
+ ).then(
204
+ fn=update_num_papers,
205
+ inputs=df,
206
+ outputs=num_papers,
207
+ queue=False,
208
+ api_name=False,
209
+ )
210
+
211
+ if __name__ == "__main__":
212
+ # queue()๋กœ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ, API ๋น„๊ณต๊ฐœ
213
+ demo.queue(api_open=False).launch(show_api=False)