File size: 7,483 Bytes
c5b0bb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Sematnic_Scholar_tab.py
# Description: contains the code to create the Semantic Scholar tab in the Gradio UI.
#
# Imports
#
# External Libraries
import gradio as gr
#
# Internal Libraries
from App_Function_Libraries.Third_Party.Semantic_Scholar import search_and_display, FIELDS_OF_STUDY, PUBLICATION_TYPES


#
######################################################################################################################
# Functions
def create_semantic_scholar_tab():
    """Create the Semantic Scholar tab for the Gradio UI"""
    with gr.Tab("Semantic Scholar Search"):
        with gr.Row():
            with gr.Column(scale=2):
                gr.Markdown("""

                    ## Semantic Scholar Paper Search



                    This interface allows you to search for academic papers using the Semantic Scholar API with advanced filtering options:



                    ### Search Options

                    - **Keywords**: Search across titles, abstracts, and other paper content

                    - **Year Range**: Filter papers by publication year (e.g., "2020-2023" or "2020")

                    - **Venue**: Filter by publication venue (journal or conference)

                    - **Minimum Citations**: Filter papers by minimum citation count

                    - **Fields of Study**: Filter papers by academic field

                    - **Publication Types**: Filter by type of publication

                    - **Open Access**: Option to show only papers with free PDF access



                    ### Results Include

                    - Paper title

                    - Author list

                    - Publication year and venue

                    - Citation count

                    - Publication types

                    - Abstract

                    - Links to PDF (when available) and Semantic Scholar page

                    """)
            with gr.Column(scale=2):
                gr.Markdown("""

                    ### Pagination

                    - 10 results per page

                    - Navigate through results using Previous/Next buttons

                    - Current page number and total results displayed



                    ### Usage Tips

                    - Combine multiple filters for more specific results

                    - Use specific terms for more focused results

                    - Try different combinations of filters if you don't find what you're looking for

                    """)
        with gr.Row():
            with gr.Column(scale=2):
                search_input = gr.Textbox(
                    label="Search Query",
                    placeholder="Enter keywords to search for papers...",
                    lines=1
                )

                # Advanced search options
                with gr.Row():
                    year_range = gr.Textbox(
                        label="Year Range",
                        placeholder="e.g., 2020-2023 or 2020",
                        lines=1
                    )
                    venue = gr.Textbox(
                        label="Venue",
                        placeholder="e.g., Nature, Science",
                        lines=1
                    )
                    min_citations = gr.Number(
                        label="Minimum Citations",
                        value=0,
                        minimum=0,
                        step=1
                    )

                with gr.Row():
                    fields_of_study = gr.Dropdown(
                        choices=FIELDS_OF_STUDY,
                        label="Fields of Study",
                        multiselect=True,
                        value=[]
                    )
                    publication_types = gr.Dropdown(
                        choices=PUBLICATION_TYPES,
                        label="Publication Types",
                        multiselect=True,
                        value=[]
                    )

                open_access_only = gr.Checkbox(
                    label="Open Access Only",
                    value=False
                )

            with gr.Column(scale=1):
                search_button = gr.Button("Search", variant="primary")

                # Pagination controls
                with gr.Row():
                    prev_button = gr.Button("← Previous")
                    current_page = gr.Number(value=0, label="Page", minimum=0, step=1)
                    max_page = gr.Number(value=0, label="Max Page", visible=False)
                    next_button = gr.Button("Next →")

                total_results = gr.Textbox(
                    label="Total Results",
                    value="0",
                    interactive=False
                )

        output_text = gr.Markdown(
            label="Results",
            value="Use the search options above to find papers."
        )

        def update_page(direction, current, maximum):
            new_page = current + direction
            if new_page < 0:
                return 0
            if new_page > maximum:
                return maximum
            return new_page

        # Handle search and pagination
        def search_from_button(query, fields_of_study, publication_types, year_range, venue, min_citations,

                               open_access_only):
            """Wrapper to always search from page 0 when search button is clicked"""
            return search_and_display(
                query=query,
                page=0,  # Force page 0 for new searches
                fields_of_study=fields_of_study,
                publication_types=publication_types,
                year_range=year_range,
                venue=venue,
                min_citations=min_citations,
                open_access_only=open_access_only
            )
        normal_search = search_and_display

        search_button.click(
            fn=search_from_button,
            inputs=[
                search_input, fields_of_study, publication_types,
                year_range, venue, min_citations, open_access_only
            ],
            outputs=[output_text, current_page, max_page, total_results]
        )

        prev_button.click(
            fn=lambda curr, max_p: update_page(-1, curr, max_p),
            inputs=[current_page, max_page],
            outputs=current_page
        ).then(
            fn=normal_search,
            inputs=[
                search_input, current_page, fields_of_study, publication_types,
                year_range, venue, min_citations, open_access_only
            ],
            outputs=[output_text, current_page, max_page, total_results]
        )

        next_button.click(
            fn=lambda curr, max_p: update_page(1, curr, max_p),
            inputs=[current_page, max_page],
            outputs=current_page
        ).then(
            fn=normal_search,
            inputs=[
                search_input, current_page, fields_of_study, publication_types,
                year_range, venue, min_citations, open_access_only
            ],
            outputs=[output_text, current_page, max_page, total_results]
        )

#
# End of Semantic_Scholar_tab.py
######################################################################################################################