Spaces:
Sleeping
Sleeping
Download app.py from MikdadMrhij/MCP_SERVER: direct link, hf CLI and curl.
- Browser
- Download file 3.22 kB
-
https://huggingface.co/spaces/MikdadMrhij/MCP_SERVER/resolve/main/app.py
- Command line
-
hf download hf://spaces/MikdadMrhij/MCP_SERVER/app.py
-
curl -L -o app.py https://huggingface.co/spaces/MikdadMrhij/MCP_SERVER/resolve/main/app.py
3.22 kB
| import arxiv | |
| import json | |
| import os | |
| from typing import List | |
| import gradio as gr | |
| PAPER_DIR = "papers" | |
| def search_papers(topic: str, max_results: int = 5) -> List[str]: | |
| """ | |
| Search for papers on arXiv based on a topic and store their information. | |
| """ | |
| client = arxiv.Client() | |
| search = arxiv.Search( | |
| query=topic, | |
| max_results=max_results, | |
| sort_by=arxiv.SortCriterion.Relevance | |
| ) | |
| papers = client.results(search) | |
| # Create directory for this topic | |
| path = os.path.join(PAPER_DIR, topic.lower().replace(" ", "_")) | |
| os.makedirs(path, exist_ok=True) | |
| file_path = os.path.join(path, "papers_info.json") | |
| try: | |
| with open(file_path, "r") as json_file: | |
| papers_info = json.load(json_file) | |
| except (FileNotFoundError, json.JSONDecodeError): | |
| papers_info = {} | |
| paper_ids = [] | |
| for paper in papers: | |
| paper_ids.append(paper.get_short_id()) | |
| paper_info = { | |
| 'title': paper.title, | |
| 'authors': [author.name for author in paper.authors], | |
| 'summary': paper.summary, | |
| 'pdf_url': paper.pdf_url, | |
| 'published': str(paper.published.date()) | |
| } | |
| papers_info[paper.get_short_id()] = paper_info | |
| with open(file_path, "w") as json_file: | |
| json.dump(papers_info, json_file, indent=2) | |
| return paper_ids | |
| def extract_info(paper_id: str) -> str: | |
| """ | |
| Search for information about a specific paper across all topic directories. | |
| """ | |
| for item in os.listdir(PAPER_DIR): | |
| item_path = os.path.join(PAPER_DIR, item) | |
| if os.path.isdir(item_path): | |
| file_path = os.path.join(item_path, "papers_info.json") | |
| if os.path.isfile(file_path): | |
| try: | |
| with open(file_path, "r") as json_file: | |
| papers_info = json.load(json_file) | |
| if paper_id in papers_info: | |
| return json.dumps(papers_info[paper_id], indent=2) | |
| except (FileNotFoundError, json.JSONDecodeError) as e: | |
| print(f"Error reading {file_path}: {str(e)}") | |
| continue | |
| return f"There's no saved information related to paper {paper_id}." | |
| # ---------------- Gradio UI ---------------- | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## 📚 Research Paper Finder (arXiv)") | |
| with gr.Tab("Search Papers"): | |
| topic = gr.Textbox(label="Topic", placeholder="Enter a research topic") | |
| max_results = gr.Slider(1, 10, value=5, step=1, label="Max Results") | |
| output_ids = gr.Textbox(label="Paper IDs") | |
| search_btn = gr.Button("Search") | |
| search_btn.click(search_papers, inputs=[topic, max_results], outputs=output_ids) | |
| with gr.Tab("Extract Info"): | |
| paper_id = gr.Textbox(label="Paper ID", placeholder="Enter a paper ID") | |
| output_info = gr.Textbox(label="Paper Info (JSON)") | |
| extract_btn = gr.Button("Extract Info") | |
| extract_btn.click(extract_info, inputs=paper_id, outputs=output_info) | |
| # Launch Gradio app | |
| if __name__ == "__main__": | |
| demo.launch(mcp_server=True) | |