ScholarAgent / app.py
pdx97's picture
Update app.py
7233de1 verified
raw
history blame
4.22 kB
import feedparser
import urllib.parse
import yaml
import gradio as gr
from typing import List, Dict
from smolagents import CodeAgent, HfApiModel, tool
@tool
def fetch_latest_arxiv_papers(keywords: List[str], num_results: int = 3) -> List[Dict[str, str]]:
"""
Fetches the latest research papers from arXiv.
Args:
keywords (List[str]): A list of search keywords to filter relevant papers.
num_results (int): The maximum number of research papers to fetch. Default is 3.
Returns:
List[Dict[str, str]]: A list of dictionaries where each dictionary contains:
- "title" (str): The title of the research paper.
- "authors" (str): The authors of the paper.
- "year" (str): The publication year.
- "abstract" (str): A summary of the research paper.
- "link" (str): A direct link to the paper on arXiv.
"""
try:
print(f"DEBUG: Searching arXiv papers with keywords: {keywords}") # Debug input
# ✅ Properly format query with +AND+ for multiple keywords
query = "+AND+".join([f"all:{kw}" for kw in keywords])
query_encoded = urllib.parse.quote(query) # Encode spaces and special characters
url = f"http://export.arxiv.org/api/query?search_query={query_encoded}&start=0&max_results={num_results}&sortBy=submittedDate&sortOrder=descending"
print(f"DEBUG: Query URL - {url}") # Debug URL
feed = feedparser.parse(url)
papers = []
for entry in feed.entries:
papers.append({
"title": entry.title,
"authors": ", ".join(author.name for author in entry.authors),
"year": entry.published[:4], # Extract year
"abstract": entry.summary,
"link": entry.link
})
return papers
except Exception as e:
print(f"ERROR: {str(e)}") # Debug errors
return [{"error": f"Error fetching research papers: {str(e)}"}]
# ✅ Define the AI Model
model = HfApiModel(
max_tokens=2096,
temperature=0.5,
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
custom_role_conversions=None,
)
# ✅ Load prompt templates
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
# ✅ Create the AI Agent
agent = CodeAgent(
model=model,
tools=[fetch_latest_arxiv_papers], # Properly registered tool
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
name="ScholarAgent",
description="An AI agent that fetches the latest research papers from arXiv based on user-defined keywords and filters.",
prompt_templates=prompt_templates
)
# ✅ Define Gradio Search Function
def search_papers(user_input):
keywords = [kw.strip() for kw in user_input.split(",") if kw.strip()] # Ensure valid keywords
print(f"DEBUG: Received input keywords - {keywords}") # Debug user input
if not keywords:
print("DEBUG: No valid keywords provided.")
return "Error: Please enter at least one valid keyword."
results = fetch_latest_arxiv_papers(keywords, num_results=3) # Fetch 3 results
print(f"DEBUG: Results received - {results}") # Debug function output
if isinstance(results, list) and results and isinstance(results[0], dict):
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
])
print("DEBUG: No results found.")
return "No results found. Try different keywords."
# ✅ Create Gradio UI
with gr.Blocks() as demo:
gr.Markdown("# arXiv 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])
print("DEBUG: Gradio UI is running. Waiting for user input...")
# ✅ Launch Gradio App
demo.launch()