pdx97 commited on
Commit
c02e3db
·
verified ·
1 Parent(s): e6df082

Updated app.y to arxiv API

Browse files
Files changed (1) hide show
  1. app.py +27 -47
app.py CHANGED
@@ -1,60 +1,38 @@
1
  from smolagents import CodeAgent, HfApiModel, load_tool, tool
2
- import datetime
3
- import requests
4
- import pytz
5
  import yaml
6
- from tools.final_answer import FinalAnswerTool
7
- from scholarly import scholarly
8
  import gradio as gr
9
 
10
  @tool
11
- def fetch_latest_research_papers(keywords: list, num_results: int = 1) -> list:
12
- """Fetches the latest research papers from Google Scholar based on provided keywords.
13
  Args:
14
  keywords: A list of keywords to search for relevant papers.
15
  num_results: The number of papers to fetch (default is 5).
16
  """
17
  try:
18
- print(f"DEBUG: Searching papers with keywords: {keywords}") # Debug input
19
- query = " ".join([kw.strip() for kw in keywords if kw.strip()]) # Ensure clean query
20
- search_results = scholarly.search_pubs(query)
21
- papers = []
22
- for _ in range(num_results): # Fetch extra papers to ensure we get recent ones
23
- paper = next(search_results, None)
24
- if paper:
25
- scholarly.fill(paper) # Fetch additional metadata
26
- pub_year = paper['bib'].get('pub_year', 'Unknown Year')
27
-
28
- # Ensure year is an integer
29
- if pub_year != 'Unknown Year':
30
- try:
31
- pub_year = int(pub_year)
32
- except ValueError:
33
- pub_year = 0 # Handle invalid years
34
-
35
- print(f"DEBUG: Found paper - {paper['bib'].get('title', 'No Title')} ({pub_year})")
36
 
37
- papers.append({
38
- "title": paper['bib'].get('title', 'No Title'),
39
- "authors": paper['bib'].get('author', 'Unknown Authors'),
40
- "year": pub_year,
41
- "abstract": paper['bib'].get('abstract', 'No Abstract Available'),
42
- "link": paper.get('pub_url', 'No Link Available')
43
- })
44
-
45
- # Sort by the latest publication year
46
- papers = sorted(papers, key=lambda x: x["year"] if isinstance(x["year"], int) else 0, reverse=True)
47
 
48
- # Return only the latest `num_results` papers
49
- return papers[:num_results]
50
 
51
  except Exception as e:
52
  print(f"ERROR: {str(e)}") # Debug errors
53
  return [f"Error fetching research papers: {str(e)}"]
54
 
55
 
56
- final_answer = FinalAnswerTool()
57
-
58
  model = HfApiModel(
59
  max_tokens=2096,
60
  temperature=0.5,
@@ -64,16 +42,16 @@ model = HfApiModel(
64
 
65
  with open("prompts.yaml", 'r') as stream:
66
  prompt_templates = yaml.safe_load(stream)
67
-
68
  agent = CodeAgent(
69
  model=model,
70
- tools=[final_answer, fetch_latest_research_papers],
71
  max_steps=6,
72
  verbosity_level=1,
73
  grammar=None,
74
  planning_interval=None,
75
  name="ScholarAgent",
76
- description="An AI agent that fetches the latest research papers from Google Scholar based on user-defined keywords and filters.",
77
  prompt_templates=prompt_templates
78
  )
79
 
@@ -85,11 +63,14 @@ def search_papers(user_input):
85
  print("DEBUG: No valid keywords provided.")
86
  return "Error: Please enter at least one valid keyword."
87
 
88
- results = fetch_latest_research_papers(keywords, num_results=1)
89
  print(f"DEBUG: Results received - {results}") # Debug function output
90
 
91
  if isinstance(results, list) and results and isinstance(results[0], dict):
92
- return "\n\n".join([f"**Title:** {paper['title']}\n**Authors:** {paper['authors']}\n**Year:** {paper['year']}\n**Abstract:** {paper['abstract']}\n[Read More]({paper['link']})" for paper in results])
 
 
 
93
 
94
  print("DEBUG: No results found.")
95
  return "No results found. Try different keywords."
@@ -97,14 +78,13 @@ def search_papers(user_input):
97
 
98
  # Create a simple Gradio interface
99
  with gr.Blocks() as demo:
100
- gr.Markdown("# Google Scholar Research Paper Fetcher")
101
  keyword_input = gr.Textbox(label="Enter keywords (comma-separated)", placeholder="e.g., deep learning, reinforcement learning")
102
  output_display = gr.Markdown()
103
  search_button = gr.Button("Search")
104
-
105
  search_button.click(search_papers, inputs=[keyword_input], outputs=[output_display])
106
 
107
-
108
  print("DEBUG: Gradio UI is running. Waiting for user input...")
109
 
110
  demo.launch()
 
1
  from smolagents import CodeAgent, HfApiModel, load_tool, tool
 
 
 
2
  import yaml
3
+ import feedparser
 
4
  import gradio as gr
5
 
6
  @tool
7
+ def fetch_latest_arxiv_papers(keywords: list, num_results: int = 1) -> list:
8
+ """Fetches the latest research papers from arXiv based on provided keywords.
9
  Args:
10
  keywords: A list of keywords to search for relevant papers.
11
  num_results: The number of papers to fetch (default is 5).
12
  """
13
  try:
14
+ print(f"DEBUG: Searching arXiv papers with keywords: {keywords}") # Debug input
15
+ query = "+".join(keywords)
16
+ url = f"http://export.arxiv.org/api/query?search_query=all:{query}&start=0&max_results={num_results}&sortBy=submittedDate&sortOrder=descending"
17
+ feed = feedparser.parse(url)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
+ papers = []
20
+ for entry in feed.entries:
21
+ papers.append({
22
+ "title": entry.title,
23
+ "authors": ", ".join(author.name for author in entry.authors),
24
+ "year": entry.published[:4], # Extract year
25
+ "abstract": entry.summary,
26
+ "link": entry.link
27
+ })
 
28
 
29
+ return papers
 
30
 
31
  except Exception as e:
32
  print(f"ERROR: {str(e)}") # Debug errors
33
  return [f"Error fetching research papers: {str(e)}"]
34
 
35
 
 
 
36
  model = HfApiModel(
37
  max_tokens=2096,
38
  temperature=0.5,
 
42
 
43
  with open("prompts.yaml", 'r') as stream:
44
  prompt_templates = yaml.safe_load(stream)
45
+
46
  agent = CodeAgent(
47
  model=model,
48
+ tools=[fetch_latest_arxiv_papers],
49
  max_steps=6,
50
  verbosity_level=1,
51
  grammar=None,
52
  planning_interval=None,
53
  name="ScholarAgent",
54
+ description="An AI agent that fetches the latest research papers from arXiv based on user-defined keywords and filters.",
55
  prompt_templates=prompt_templates
56
  )
57
 
 
63
  print("DEBUG: No valid keywords provided.")
64
  return "Error: Please enter at least one valid keyword."
65
 
66
+ results = fetch_latest_arxiv_papers(keywords, num_results=3) # Fetch 3 results
67
  print(f"DEBUG: Results received - {results}") # Debug function output
68
 
69
  if isinstance(results, list) and results and isinstance(results[0], dict):
70
+ return "\n\n".join([
71
+ f"**Title:** {paper['title']}\n**Authors:** {paper['authors']}\n**Year:** {paper['year']}\n**Abstract:** {paper['abstract']}\n[Read More]({paper['link']})"
72
+ for paper in results
73
+ ])
74
 
75
  print("DEBUG: No results found.")
76
  return "No results found. Try different keywords."
 
78
 
79
  # Create a simple Gradio interface
80
  with gr.Blocks() as demo:
81
+ gr.Markdown("# arXiv Research Paper Fetcher")
82
  keyword_input = gr.Textbox(label="Enter keywords (comma-separated)", placeholder="e.g., deep learning, reinforcement learning")
83
  output_display = gr.Markdown()
84
  search_button = gr.Button("Search")
85
+
86
  search_button.click(search_papers, inputs=[keyword_input], outputs=[output_display])
87
 
 
88
  print("DEBUG: Gradio UI is running. Waiting for user input...")
89
 
90
  demo.launch()