Spaces:
Sleeping
Sleeping
Create SemanticScholar.py
Browse files- tools/SemanticScholar.py +41 -0
tools/SemanticScholar.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import Tool
|
| 2 |
+
from semanticscholar import SemanticScholar
|
| 3 |
+
|
| 4 |
+
class AcademicPaperSearchTool(Tool):
|
| 5 |
+
name = "academic_paper_search"
|
| 6 |
+
description = "Searches academic papers via Semantic Scholar and returns the most relevant titles and abstracts."
|
| 7 |
+
inputs = {
|
| 8 |
+
"query": {"type": "string", "description": "Search query for academic papers (title, keywords, etc.)"}
|
| 9 |
+
}
|
| 10 |
+
output_type = "string"
|
| 11 |
+
|
| 12 |
+
def forward(self, query: str) -> str:
|
| 13 |
+
sch = SemanticScholar()
|
| 14 |
+
try:
|
| 15 |
+
# Use query=query to avoid potential keyword issues with the SemanticScholar method
|
| 16 |
+
papers = sch.search_paper(query, limit=3) # get top-3 matching papers
|
| 17 |
+
except Exception as e:
|
| 18 |
+
return f"An error occurred during search: {e}"
|
| 19 |
+
|
| 20 |
+
if not papers:
|
| 21 |
+
return "No papers found."
|
| 22 |
+
|
| 23 |
+
# Format results concisely
|
| 24 |
+
lines = []
|
| 25 |
+
for p in papers:
|
| 26 |
+
# --- THE FIX IS HERE ---
|
| 27 |
+
# Access attributes directly using dot notation (p.title, p.year, p.abstract)
|
| 28 |
+
# instead of the dictionary method p.get(...)
|
| 29 |
+
title = p.title
|
| 30 |
+
year = p.year
|
| 31 |
+
abstract = p.abstract if p.abstract else "" # Check if abstract exists
|
| 32 |
+
|
| 33 |
+
# Only take the first sentence or two of the abstract
|
| 34 |
+
abstract_snip = abstract.split(". ")[0] if abstract else ""
|
| 35 |
+
|
| 36 |
+
# Ensure 'year' is handled gracefully (it can sometimes be None)
|
| 37 |
+
year_str = str(year) if year else "N/A"
|
| 38 |
+
|
| 39 |
+
lines.append(f"**{title}** ({year_str}): {abstract_snip}...")
|
| 40 |
+
|
| 41 |
+
return "\n".join(lines)
|