Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| def arxiv_search(*kwargs): | |
| kwargs = ('AND',) + kwargs | |
| print(kwargs) | |
| N = len(kwargs) | |
| assert N % 3 == 0, "Number of arguments must be a multiple of 3." | |
| part_size = N // 3 | |
| logics = kwargs[:part_size] | |
| terms = kwargs[part_size:2 * part_size] | |
| fields = kwargs[2 * part_size:] | |
| base_url = "https://arxiv.org/search/advanced?advanced=" | |
| # Ensure the lengths of logics, terms, and fields are the same | |
| assert len(logics) == len(terms) == len(fields), "Lengths of logic operators, terms, and fields must be the same." | |
| # Construct the query parameters dynamically | |
| params = {} | |
| for i, (logic, term, field) in enumerate(zip(logics, terms, fields)): | |
| params[f"terms-{i}-operator"] = logic | |
| params[f"terms-{i}-term"] = term | |
| params[f"terms-{i}-field"] = field.lower() | |
| params.update({ | |
| "classification-physics_archives": "all", | |
| "classification-include_cross_list": "include", | |
| "date-filter_by": "all_dates", | |
| "date-year": "", | |
| "date-from_date": "", | |
| "date-to_date": "", | |
| "date-date_type": "submitted_date", | |
| "abstracts": "show", | |
| "size": 50, | |
| "order": "-announced_date_first" | |
| }) | |
| # Construct the URL without making a request | |
| url_params = "&".join([f"{key}={value}" for key, value in params.items()]) | |
| result_url = base_url + url_params | |
| logical_expression, api_url = convert_to_logical_expression(logics, terms, fields) | |
| # Generate HTML link with explanatory text, larger font size, and centered alignment | |
| html_link = ( | |
| "<div style='text-align:center; font-size:18px;'>" | |
| "<p>Click the link below to view the search results on arXiv:</p>" | |
| f"<a href='{result_url}' target='_blank'>View ArXiv Search Results</a>" | |
| f"<p>Or you can use the ArXiv Search API:</p><a href='{api_url}' target='_blank'>View ArXiv Search API Results</a><p>{logical_expression}</p>" | |
| "</div>" | |
| ) | |
| # print(html_link) | |
| return html_link | |
| def convert_to_logical_expression(logics, keywords, fields): | |
| # Ensure the lengths of logics, keywords, and fields are the same | |
| assert len(logics) == len(keywords) == len(fields), "Lengths of logics, keywords, and fields must be the same." | |
| # 映射的字典 | |
| field_mapping = { | |
| "Title": "ti", | |
| "Abstract": "abs", | |
| "All": "all" | |
| } | |
| # Create a list of expressions for each logic, keyword, and field | |
| expressions = [f'{logic}+{field_mapping[field]}:"{keyword}"' for logic, keyword, field in zip(logics, keywords, fields) if keyword != ''] | |
| # Combine expressions using logic operators | |
| logical_expression = f"+".join(expressions) | |
| logical_expression = '+'.join(logical_expression.split('+')[1:]) | |
| api_url = f'https://export.arxiv.org/api/query?search_query={logical_expression}&sortBy=submittedDate&sortOrder=descending&start=0&max_results=20' | |
| return logical_expression, api_url | |
| with gr.Blocks(title='Arxiv Search🔍️') as demo: | |
| gr.HTML(''' | |
| <center> | |
| <h1>Arxiv Search🔍️</h1> | |
| <p>Click <a href="https://arxiv.org/search/advanced" target="_blank">here</a> to go to the official search engine.</p> | |
| </center> | |
| ''') | |
| Logics, Keywords, Fields = [], [], [] | |
| for i in range(5): | |
| with gr.Row(): | |
| if i == 0: | |
| with gr.Column(scale=5): | |
| keyword = gr.Textbox(lines=1, label='Search Term') | |
| Keywords.append(keyword) | |
| with gr.Column(scale=5): | |
| tag = gr.Dropdown(['Title', 'Abstract', 'All'], label='Search Type', value='Title') | |
| Fields.append(tag) | |
| else: | |
| with gr.Column(scale=1): | |
| logics = gr.Dropdown(['AND', 'OR', 'NOT'], label='Logic', value = "OR") | |
| Logics.append(logics) | |
| with gr.Column(scale=6): | |
| keyword = gr.Textbox(lines=1, label='Search Term') | |
| Keywords.append(keyword) | |
| with gr.Column(scale=3): | |
| tag = gr.Dropdown(['Title', 'Abstract', 'All'], label='Search Type', value='Title') | |
| Fields.append(tag) | |
| arxiv_res = gr.HTML(label = 'Arxiv Search Results') | |
| submit = gr.Button(label='Search', variant='primary') | |
| clear = gr.ClearButton(label='Clear') | |
| submit.click(fn=arxiv_search, inputs=[*Logics, *Keywords, *Fields], outputs=arxiv_res) | |
| clear.click(outputs=[*Logics, *Keywords, *Fields], fn=lambda: ['OR']*4 + ['']*5 + ['Title']*5, show_progress=False) | |
| gr.Examples([ | |
| [ "NeRF", "Title", "AND", "GAN", "Abstract"], | |
| [ "GAN", "All", "OR", "Diffusion", "All"], | |
| ], inputs=[ Keywords[0], Fields[0], Logics[0], Keywords[1], Fields[1]]) | |
| demo.launch(debug = True) | |