pdx97 commited on
Commit
0f668a0
·
verified ·
1 Parent(s): 304758c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -6
app.py CHANGED
@@ -9,31 +9,58 @@ import gradio as gr
9
 
10
  @tool
11
  def fetch_latest_research_papers(keywords: list, num_results: int = 5) -> 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
  query = " ".join(keywords)
19
  search_results = scholarly.search_pubs(query)
 
20
  papers = []
21
- for i in range(num_results):
22
  paper = next(search_results, None)
23
  if paper:
 
 
 
 
 
 
 
 
 
 
 
 
24
  papers.append({
25
  "title": paper['bib'].get('title', 'No Title'),
26
  "authors": paper['bib'].get('author', 'Unknown Authors'),
27
- "year": paper['bib'].get('pub_year', 'Unknown Year'),
28
  "abstract": paper['bib'].get('abstract', 'No Abstract Available'),
29
  "link": paper.get('pub_url', 'No Link Available')
30
  })
31
- return papers
 
 
 
 
 
 
32
  except Exception as e:
 
33
  return [f"Error fetching research papers: {str(e)}"]
34
 
35
 
36
 
 
37
  final_answer = FinalAnswerTool()
38
 
39
  model = HfApiModel(
 
9
 
10
  @tool
11
  def fetch_latest_research_papers(keywords: list, num_results: int = 5) -> list:
12
+ """
13
+ Fetches the latest research papers from Google Scholar based on provided keywords.
14
+
15
  Args:
16
+ keywords (list of str): A list of keywords to search for relevant papers.
17
+ num_results (int): The number of papers to fetch. Default is 5.
18
+
19
+ Returns:
20
+ list: A list of dictionaries containing paper details.
21
  """
22
  try:
23
+ print(f"DEBUG: Searching papers with keywords: {keywords}") # Debug input
24
  query = " ".join(keywords)
25
  search_results = scholarly.search_pubs(query)
26
+
27
  papers = []
28
+ for _ in range(num_results * 2): # Fetch extra papers to ensure we get recent ones
29
  paper = next(search_results, None)
30
  if paper:
31
+ scholarly.fill(paper) # Fetch additional metadata
32
+ pub_year = paper['bib'].get('pub_year', 'Unknown Year')
33
+
34
+ # Ensure year is an integer
35
+ if pub_year != 'Unknown Year':
36
+ try:
37
+ pub_year = int(pub_year)
38
+ except ValueError:
39
+ pub_year = 0 # Handle invalid years
40
+
41
+ print(f"DEBUG: Found paper - {paper['bib'].get('title', 'No Title')} ({pub_year})")
42
+
43
  papers.append({
44
  "title": paper['bib'].get('title', 'No Title'),
45
  "authors": paper['bib'].get('author', 'Unknown Authors'),
46
+ "year": pub_year,
47
  "abstract": paper['bib'].get('abstract', 'No Abstract Available'),
48
  "link": paper.get('pub_url', 'No Link Available')
49
  })
50
+
51
+ # Sort by the latest publication year
52
+ papers = sorted(papers, key=lambda x: x["year"] if isinstance(x["year"], int) else 0, reverse=True)
53
+
54
+ # Return only the latest `num_results` papers
55
+ return papers[:num_results]
56
+
57
  except Exception as e:
58
+ print(f"ERROR: {str(e)}") # Debug errors
59
  return [f"Error fetching research papers: {str(e)}"]
60
 
61
 
62
 
63
+
64
  final_answer = FinalAnswerTool()
65
 
66
  model = HfApiModel(