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abbd
 
 
 
 
 
 
ffff3d1
 
 
 
 
 
 
 
 
 
ca3e112
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import gradio as gr
import copy
import datasets

STYLE = """

.main {
  width: 90% !important;
  margin: 0 auto; /* Center the container */
}

.small-font{
  font-size: 12pt !important;
}

.small-font:hover {
  font-size: 20px !important;
  transition: font-size 0.3s ease-out;
  transition-delay: 0.8s;
}

.group {
  padding-top: 10px;
  padding-left: 10px;
  padding-right: 10px;
  padding-bottom: 10px;
  border: 2px dashed gray;
  border-radius: 20px;
  box-shadow: 5px 3px 10px 1px rgba(0, 0, 0, 0.4) !important;
}

.accordion > button > span{
  font-size: 12pt !important;
}

.accordion {
  border-style: dashed !important;
  border-left-width: 2px !important;
  border-bottom-width: 2.5px !important;
  border-top: none !important;
  border-right: none !important;
  box-shadow: none !important;
}

.no-gap {
    gap: 0px;
}

.no-radius {
    border-radius: 0px;    
}

#search_input > label > span {
    display: none;
}

#exp-type > span {
    display: none;
}
"""

dataset_repo_id = "chansung/auto-paper-qa2"
ds = datasets.load_dataset(dataset_repo_id)

title2qna = {}
date2qna = {}
longest_qans = 0

def count_nans(row):
    count = 0

    for _, (k, v) in enumerate(data.items()):
        if v is None:
            count = count + 1

    return count

for data in ds["train"]:
    date = data["target_date"].strftime("%Y-%m-%d")

    if date in date2qna:
        papers = copy.deepcopy(date2qna[date])
        for paper in papers:
            if paper["title"] == data["title"]:
                if count_nans(paper) > count_nans(data):
                    date2qna[date].remove(paper)
        
        date2qna[date].append(data)
        del papers
    else:
        date2qna[date] = [data]

for date in date2qna:
    papers = date2qna[date]
    for paper in papers:
        title2qna[paper["title"]] = paper

titles = title2qna.keys()

sorted_dates = sorted(date2qna.keys())
last_date = sorted_dates[-1]
last_papers = date2qna[last_date]
selected_paper = last_papers[0]

def get_papers(date):
    papers = [paper["title"] for paper in date2qna[date]]
    return gr.Dropdown(
        papers,
        value=papers[0]
    )

def set_paper(date, paper_title):
    selected_paper = None
    for paper in date2qna[date]:
        if paper["title"] == paper_title:
            selected_paper = paper
            break

    return (
        gr.Markdown(f"# {selected_paper['title']}"), gr.Markdown(selected_paper["summary"]),

        gr.Markdown(f"## πŸ™‹ {selected_paper['0_question']}"), 
        gr.Markdown(f"β†ͺ **(ELI5)** {selected_paper['0_answers:eli5']}"), 
        gr.Markdown(f"β†ͺ **(Technical)** {selected_paper['0_answers:expert']}"),
        gr.Markdown(f"## πŸ™‹πŸ™‹ {selected_paper['0_additional_depth_q:follow up question']}"),
        gr.Markdown(f"β†ͺ **(ELI5)** {selected_paper['0_additional_depth_q:answers:eli5']}"), 
        gr.Markdown(f"β†ͺ **(Technical)** {selected_paper['0_additional_depth_q:answers:expert']}"),
        gr.Markdown(f"## πŸ™‹πŸ™‹ {selected_paper['0_additional_breath_q:follow up question']}"), 
        gr.Markdown(f"β†ͺ **(ELI5)** {selected_paper['0_additional_breath_q:answers:eli5']}"), 
        gr.Markdown(f"β†ͺ **(Technical)** {selected_paper['0_additional_breath_q:answers:expert']}"),

        gr.Markdown(f"## πŸ™‹ {selected_paper['1_question']}"), 
        gr.Markdown(f"β†ͺ **(ELI5)** {selected_paper['1_answers:eli5']}"), 
        gr.Markdown(f"β†ͺ **(Technical)** {selected_paper['1_answers:expert']}"),
        gr.Markdown(f"## πŸ™‹πŸ™‹ {selected_paper['1_additional_depth_q:follow up question']}"), 
        gr.Markdown(f"β†ͺ **(ELI5)** {selected_paper['1_additional_depth_q:answers:eli5']}"), 
        gr.Markdown(f"β†ͺ **(Technical)** {selected_paper['1_additional_depth_q:answers:expert']}"),
        gr.Markdown(f"## πŸ™‹πŸ™‹ {selected_paper['1_additional_breath_q:follow up question']}"), 
        gr.Markdown(f"β†ͺ **(ELI5)** {selected_paper['1_additional_breath_q:answers:eli5']}"), 
        gr.Markdown(f"β†ͺ **(Technical)** {selected_paper['1_additional_breath_q:answers:expert']}"),

        gr.Markdown(f"## πŸ™‹ {selected_paper['2_question']}"), 
        gr.Markdown(f"β†ͺ **(ELI5)** {selected_paper['2_answers:eli5']}"), 
        gr.Markdown(f"β†ͺ **(Technical)** {selected_paper['2_answers:expert']}"),
        gr.Markdown(f"## πŸ™‹πŸ™‹ {selected_paper['2_additional_depth_q:follow up question']}"), 
        gr.Markdown(f"β†ͺ **(ELI5)** {selected_paper['2_additional_depth_q:answers:eli5']}"), 
        gr.Markdown(f"β†ͺ **(Technical)** {selected_paper['2_additional_depth_q:answers:expert']}"),
        gr.Markdown(f"## πŸ™‹πŸ™‹ {selected_paper['2_additional_breath_q:follow up question']}"), 
        gr.Markdown(f"β†ͺ **(ELI5)** {selected_paper['2_additional_breath_q:answers:eli5']}"), 
        gr.Markdown(f"β†ͺ **(Technical)** {selected_paper['2_additional_breath_q:answers:expert']}"),
    )

def change_exp_type(exp_type):
    if exp_type == "ELI5":
        return (
            gr.Markdown(visible=True), gr.Markdown(visible=False), gr.Markdown(visible=True), gr.Markdown(visible=False), gr.Markdown(visible=True), gr.Markdown(visible=False),
            gr.Markdown(visible=True), gr.Markdown(visible=False), gr.Markdown(visible=True), gr.Markdown(visible=False), gr.Markdown(visible=True), gr.Markdown(visible=False),
            gr.Markdown(visible=True), gr.Markdown(visible=False), gr.Markdown(visible=True), gr.Markdown(visible=False), gr.Markdown(visible=True), gr.Markdown(visible=False),
        )
    else:
        return (
            gr.Markdown(visible=False), gr.Markdown(visible=True), gr.Markdown(visible=False), gr.Markdown(visible=True), gr.Markdown(visible=False), gr.Markdown(visible=True),
            gr.Markdown(visible=False), gr.Markdown(visible=True), gr.Markdown(visible=False), gr.Markdown(visible=True), gr.Markdown(visible=False), gr.Markdown(visible=True),
            gr.Markdown(visible=False), gr.Markdown(visible=True), gr.Markdown(visible=False), gr.Markdown(visible=True), gr.Markdown(visible=False), gr.Markdown(visible=True),
        )        

def search(search_in, max_results=3):
    results = []

    for title in titles:
        if len(results) > 3:
            break
        else:
            if search_in in title:
                results.append(title)

    return (
        gr.Textbox(
            visible=True if len(results) > 0 else False,
            value=results[0] if len(results) > 0 else ""
        ),
        gr.Textbox(
            visible=True if len(results) > 1 else False,
            value=results[1] if len(results) > 1 else ""
        ),
        gr.Textbox(
            visible=True if len(results) > 2 else False,
            value=results[2] if len(results) > 2 else ""
        )
    )

UPDATE_SEARCH_RESULTS = f"""
function search(searchIn, maxResults = 3) {{
    if (searchIn.trim().length > 0) {{
        const results = [];
        let titles = {list(titles)};

        for (const title of titles) {{ // Assuming 'titles' is an array defined elsewhere
            if (results.length > 3) {{
                break;
            }} else {{
                if (title.toLowerCase().includes(searchIn.toLowerCase())) {{ // JavaScript's equivalent to Python's 'in'
                    results.push(title);
                }}
            }}
        }}

        // Handle UI elements (Explanation below)
        const resultElements = [1, 2, 3].map(index => {{
            return results[index - 1] || '';
        }});

        if (resultElements[0] == '') {{
            document.getElementById('search_r1').style.display = 'none';
        }} else {{
            document.getElementById('search_r1').style.display = 'block';
        }}

        if (resultElements[1] == '') {{
            document.getElementById('search_r2').style.display = 'none';
        }} else {{
            document.getElementById('search_r2').style.display = 'block';
        }}

        if (resultElements[2] == '') {{
            document.getElementById('search_r3').style.display = 'none';
        }} else {{
            document.getElementById('search_r3').style.display = 'block';
        }}

        return resultElements; 
    }} else {{
        document.getElementById('search_r1').style.display = 'none';
        document.getElementById('search_r2').style.display = 'none';
        document.getElementById('search_r3').style.display = 'none';

        return ['', '', '']
    }}
}}
"""

def set_date(title):
    paper = title2qna[title]
    date = paper["target_date"].strftime("%Y-%m-%d")
    return date

def set_papers(date, title):
    papers = [paper["title"] for paper in date2qna[date]]
    return (
        gr.Dropdown(choices=papers, value=title),
        gr.Textbox("")
    )

with gr.Blocks(css=STYLE) as demo:
    gr.Markdown("# Let's explore papers with auto generated Q&As")
    
    with gr.Column(elem_classes=["group"]):
        with gr.Row():
            date_dd = gr.Dropdown(
                sorted_dates, 
                value=last_date, 
                label="Select date", 
                interactive=True,
                scale=3,
            )
            papers_dd = gr.Dropdown(
                [paper["title"] for paper in last_papers],
                value=selected_paper["title"],
                label="Select paper title", 
                interactive=True,
                scale=7,
            )

        with gr.Column(elem_classes=["no-gap"]):
            search_in = gr.Textbox("", placeholder="Enter keywords to search...", elem_id="search_input")
            search_r1 = gr.Button(visible=False, elem_id="search_r1", elem_classes=["no-radius"])
            search_r2 = gr.Button(visible=False, elem_id="search_r2", elem_classes=["no-radius"])
            search_r3 = gr.Button(visible=False, elem_id="search_r3", elem_classes=["no-radius"])

    title = gr.Markdown(f"# {selected_paper['title']}")
    summary = gr.Markdown(f"{selected_paper['summary']}", elem_classes=["small-font"])

    with gr.Row():
        with gr.Column(scale=7):
            gr.Markdown("## Auto generated Questions & Answers")

        exp_type = gr.Radio(choices=["ELI5", "Technical"], value="ELI5", elem_id="exp-type", scale=3)

    # 1
    with gr.Column(elem_classes=["group"], visible=True) as q_0:
        basic_q_0 = gr.Markdown(f"## πŸ™‹ {selected_paper['0_question']}")
        basic_q_eli5_0 = gr.Markdown(f"β†ͺ **(ELI5)** {selected_paper['0_answers:eli5']}", elem_classes=["small-font"]) 
        basic_q_expert_0 = gr.Markdown(f"β†ͺ **(Technical)** {selected_paper['0_answers:expert']}", visible=False, elem_classes=["small-font"]) 

        with gr.Accordion("Additional question #1", open=False, elem_classes=["accordion"]) as aq_0_0:
            depth_q_0 = gr.Markdown(f"## πŸ™‹πŸ™‹ {selected_paper['0_additional_depth_q:follow up question']}")
            depth_q_eli5_0 = gr.Markdown(f"β†ͺ **(ELI5)** {selected_paper['0_additional_depth_q:answers:eli5']}", elem_classes=["small-font"])
            depth_q_expert_0 = gr.Markdown(f"β†ͺ **(Technical)** {selected_paper['0_additional_depth_q:answers:expert']}", visible=False, elem_classes=["small-font"])

        with gr.Accordion("Additional question #2", open=False, elem_classes=["accordion"]) as aq_0_1:
            breath_q_0 = gr.Markdown(f"## πŸ™‹πŸ™‹ {selected_paper['0_additional_breath_q:follow up question']}")
            breath_q_eli5_0 = gr.Markdown(f"β†ͺ **(ELI5)** {selected_paper['0_additional_breath_q:answers:eli5']}", elem_classes=["small-font"])
            breath_q_expert_0 = gr.Markdown(f"β†ͺ **(Technical)** {selected_paper['0_additional_breath_q:answers:expert']}", visible=False, elem_classes=["small-font"])

    # 2
    with gr.Column(elem_classes=["group"], visible=True) as q_1:
        basic_q_1 = gr.Markdown(f"## πŸ™‹ {selected_paper['1_question']}")
        basic_q_eli5_1 = gr.Markdown(f"β†ͺ **(ELI5)** {selected_paper['1_answers:eli5']}", elem_classes=["small-font"]) 
        basic_q_expert_1 = gr.Markdown(f"β†ͺ **(Technical)** {selected_paper['1_answers:expert']}", visible=False, elem_classes=["small-font"]) 

        with gr.Accordion("Additional question #1", open=False, elem_classes=["accordion"]) as aq_1_0:
            depth_q_1 = gr.Markdown(f"## πŸ™‹πŸ™‹ {selected_paper['1_additional_depth_q:follow up question']}")
            depth_q_eli5_1 = gr.Markdown(f"β†ͺ **(ELI5)** {selected_paper['1_additional_depth_q:answers:eli5']}", elem_classes=["small-font"])
            depth_q_expert_1 = gr.Markdown(f"β†ͺ **(Technical)** {selected_paper['1_additional_depth_q:answers:expert']}", visible=False, elem_classes=["small-font"])

        with gr.Accordion("Additional question #2", open=False, elem_classes=["accordion"]) as aq_1_1:
            breath_q_1 = gr.Markdown(f"## πŸ™‹πŸ™‹ {selected_paper['1_additional_breath_q:follow up question']}")
            breath_q_eli5_1 = gr.Markdown(f"β†ͺ **(ELI5)** {selected_paper['1_additional_breath_q:answers:eli5']}", elem_classes=["small-font"])
            breath_q_expert_1 = gr.Markdown(f"β†ͺ **(Technical)** {selected_paper['1_additional_breath_q:answers:expert']}", visible=False, elem_classes=["small-font"])

    # 3
    with gr.Column(elem_classes=["group"], visible=True) as q_2:
        basic_q_2 = gr.Markdown(f"## πŸ™‹ {selected_paper['2_question']}")
        basic_q_eli5_2 = gr.Markdown(f"β†ͺ **(ELI5)** {selected_paper['2_answers:eli5']}", elem_classes=["small-font"]) 
        basic_q_expert_2 = gr.Markdown(f"β†ͺ **(Technical)** {selected_paper['2_answers:expert']}", visible=False, elem_classes=["small-font"]) 

        with gr.Accordion("Additional question #1", open=False, elem_classes=["accordion"]) as aq_2_0:
            depth_q_2 = gr.Markdown(f"## πŸ™‹πŸ™‹ {selected_paper['2_additional_depth_q:follow up question']}")
            depth_q_eli5_2 = gr.Markdown(f"β†ͺ **(ELI5)** {selected_paper['2_additional_depth_q:answers:eli5']}", elem_classes=["small-font"])
            depth_q_expert_2 = gr.Markdown(f"β†ͺ **(Technical)** {selected_paper['2_additional_depth_q:answers:expert']}", visible=False, elem_classes=["small-font"])

        with gr.Accordion("Additional question #2", open=False, elem_classes=["accordion"]) as aq_2_1:
            breath_q_2 = gr.Markdown(f"## πŸ™‹πŸ™‹ {selected_paper['2_additional_breath_q:follow up question']}")
            breath_q_eli5_2 = gr.Markdown(f"β†ͺ **(ELI5)** {selected_paper['2_additional_breath_q:answers:eli5']}", elem_classes=["small-font"])
            breath_q_expert_2 = gr.Markdown(f"β†ͺ **(Technical)** {selected_paper['2_additional_breath_q:answers:expert']}", visible=False, elem_classes=["small-font"])

    gr.Markdown("The target papers are collected from [Hugging Face πŸ€— Daily Papers](https://huggingface.co/papers) on a daily basis. "
                "The entire data is generated by [Google's Gemini 1.0](https://deepmind.google/technologies/gemini/) Pro. "
                "If you are curious how it is done, visit the [Auto Paper Q&A Generation project repository](https://github.com/deep-diver/auto-paper-analysis) "
                "Also, the generated dataset is hosted on Hugging Face πŸ€— Dataset repository as well([Link](https://huggingface.co/datasets/chansung/auto-paper-qa2)). ")
    
    search_r1.click(
        set_date,
        search_r1,
        date_dd
    ).then(
        set_papers,
        inputs=[date_dd, search_r1],
        outputs=[papers_dd, search_in]
    )

    search_r2.click(
        set_date,
        search_r2,
        date_dd
    ).then(
        set_papers,
        inputs=[date_dd, search_r2],
        outputs=[papers_dd, search_in]
    )

    search_r3.click(
        set_date,
        search_r3,
        date_dd
    ).then(
        set_papers,
        inputs=[date_dd, search_r3],
        outputs=[papers_dd, search_in]
    )

    date_dd.input(
        get_papers,
        date_dd,
        papers_dd
    ).then(
        set_paper,
        [date_dd, papers_dd],
        [
            title, summary,
            basic_q_0, basic_q_eli5_0, basic_q_expert_0,
            depth_q_0, depth_q_eli5_0, depth_q_expert_0,
            breath_q_0, breath_q_eli5_0, breath_q_expert_0,

            basic_q_1, basic_q_eli5_1, basic_q_expert_1,
            depth_q_1, depth_q_eli5_1, depth_q_expert_1,
            breath_q_1, breath_q_eli5_1, breath_q_expert_1,

            basic_q_2, basic_q_eli5_2, basic_q_expert_2,
            depth_q_2, depth_q_eli5_2, depth_q_expert_2,
            breath_q_2, breath_q_eli5_2, breath_q_expert_2
        ]        
    )

    papers_dd.change(
        set_paper,
        [date_dd, papers_dd],
        [
            title, summary,
            basic_q_0, basic_q_eli5_0, basic_q_expert_0,
            depth_q_0, depth_q_eli5_0, depth_q_expert_0,
            breath_q_0, breath_q_eli5_0, breath_q_expert_0,

            basic_q_1, basic_q_eli5_1, basic_q_expert_1,
            depth_q_1, depth_q_eli5_1, depth_q_expert_1,
            breath_q_1, breath_q_eli5_1, breath_q_expert_1,

            basic_q_2, basic_q_eli5_2, basic_q_expert_2,
            depth_q_2, depth_q_eli5_2, depth_q_expert_2,
            breath_q_2, breath_q_eli5_2, breath_q_expert_2
        ]
    )

    search_in.change(
        inputs=[search_in],
        outputs=[search_r1, search_r2, search_r3],
        js=UPDATE_SEARCH_RESULTS,
        fn=None
    )

    exp_type.select(
        change_exp_type,
        exp_type,
        [
            basic_q_eli5_0, basic_q_expert_0, depth_q_eli5_0, depth_q_expert_0, breath_q_eli5_0, breath_q_expert_0,
            basic_q_eli5_1, basic_q_expert_1, depth_q_eli5_1, depth_q_expert_1, breath_q_eli5_1, breath_q_expert_1,
            basic_q_eli5_2, basic_q_expert_2, depth_q_eli5_2, depth_q_expert_2, breath_q_eli5_2, breath_q_expert_2
        ]
    )

demo.launch(share=True)