ScholarAgent / app.py
pdx97's picture
Updated new
2e6775a verified
raw
history blame
2.79 kB
from smolagents import CodeAgent, HfApiModel, load_tool, tool
import datetime
import requests
import pytz
import yaml
from tools.final_answer import FinalAnswerTool
from scholarly import scholarly
import gradio as gr
@tool
def fetch_latest_research_papers(keywords: list, num_results: int = 5) -> list:
"""Fetches the latest research papers from Google Scholar based on provided keywords.
Args:
keywords: A list of keywords to search for relevant papers.
num_results: The number of papers to fetch (default is 5).
"""
try:
query = " ".join(keywords)
search_results = scholarly.search_pubs(query)
papers = []
for i in range(num_results):
paper = next(search_results, None)
if paper:
papers.append({
"title": paper['bib'].get('title', 'No Title'),
"authors": paper['bib'].get('author', 'Unknown Authors'),
"year": paper['bib'].get('pub_year', 'Unknown Year'),
"abstract": paper['bib'].get('abstract', 'No Abstract Available'),
"link": paper.get('pub_url', 'No Link Available')
})
return papers
except Exception as e:
return [f"Error fetching research papers: {str(e)}"]
final_answer = FinalAnswerTool()
model = HfApiModel(
max_tokens=2096,
temperature=0.5,
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
custom_role_conversions=None,
)
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
agent = CodeAgent(
model=model,
tools=[final_answer, fetch_latest_research_papers],
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
name="ScholarAgent",
description="An AI agent that fetches the latest research papers from Google Scholar based on user-defined keywords and filters.",
prompt_templates=prompt_templates
)
def search_papers(user_input):
keywords = user_input.split(",") # Split input by commas for multiple keywords
results = fetch_latest_research_papers(keywords, num_results=5)
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])
# Create a simple Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# Google Scholar Research Paper Fetcher")
keyword_input = gr.Textbox(label="Enter keywords (comma-separated)", placeholder="e.g., deep learning, reinforcement learning")
output_display = gr.Markdown()
search_button = gr.Button("Search")
search_button.click(search_papers, inputs=[keyword_input], outputs=[output_display])
demo.launch()