from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool import xml.etree.ElementTree as ET from wordcloud import WordCloud from collections import Counter import re import datetime import requests import pytz import yaml from tools.final_answer import FinalAnswerTool import gradio as gr import os # Below is an example of a tool that does nothing. Amaze us with your creativity! @tool def search_arxiv(query: str): """Searches arXiv for academic papers and returns structured results. Args: query (str): The topic or keywords to search for. Returns: list: A list of tuples containing titles, summaries, and links. """ max_results = 5 url = f"http://export.arxiv.org/api/query?search_query={query}&max_results={max_results}" response = requests.get(url) if response.status_code == 200: papers = [] root = ET.fromstring(response.text) for entry in root.findall("{http://www.w3.org/2005/Atom}entry"): title = entry.find("{http://www.w3.org/2005/Atom}title").text summary = entry.find("{http://www.w3.org/2005/Atom}summary").text link = entry.find("{http://www.w3.org/2005/Atom}id").text papers.append((title, summary, link)) return papers return [] def generate_visuals(query): results = search_arxiv(query) if not results: return "No papers found.", None, None # Extract text data titles = [title for title, _, _ in results] summaries = " ".join(summary for _, summary, _ in results) # Generate Bar Chart for Keyword Frequency in Titles words = [word.lower() for title in titles for word in re.findall(r'\b\w+\b', title) if len(word) > 3] word_counts = Counter(words).most_common(10) # Save Bar Chart Image bar_chart_path = "/tmp/bar_chart.png" plt.figure(figsize=(8, 5)) plt.bar(*zip(*word_counts), color='skyblue') plt.xticks(rotation=45) plt.title("Top Keywords in Titles") plt.xlabel("Keywords") plt.ylabel("Frequency") plt.tight_layout() plt.savefig(bar_chart_path) plt.close() # Generate Word Cloud for Summary Text wordcloud = WordCloud(width=500, height=300, background_color="white").generate(summaries) wordcloud_path = "/tmp/wordcloud.png" wordcloud.to_file(wordcloud_path) # Display Search Results as Clickable Links markdown_text = "\n\n".join( [f"**[{title}]({link})**\n\n{summary}" for title, summary, link in results] ) return markdown_text, bar_chart_path, wordcloud_path @tool def summarize_text(text: str) -> str: """Summarizes long academic papers or articles. Args: text: The text to summarize. """ model = HfApiModel( max_tokens=512, temperature=0.5, model_id='Qwen/Qwen2.5-Coder-32B-Instruct', ) return model.generate(f"Summarize this research paper: {text}") @tool def get_current_time_in_timezone(timezone: str) -> str: """A tool that fetches the current local time in a specified timezone. Args: timezone: A string representing a valid timezone (e.g., 'America/New_York'). """ try: # Create timezone object tz = pytz.timezone(timezone) # Get current time in that timezone local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S") return f"The current local time in {timezone} is: {local_time}" except Exception as e: return f"Error fetching time for timezone '{timezone}': {str(e)}" final_answer = FinalAnswerTool() model = HfApiModel( max_tokens=2096, temperature=0.5, model_id='Qwen/Qwen2.5-Coder-32B-Instruct', ) # Import tool from Hub image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) with open("prompts.yaml", 'r') as stream: prompt_templates = yaml.safe_load(stream) agent = CodeAgent( model=model, tools=[final_answer, search_arxiv, summarize_text], # add your tools here (don't remove final answer) max_steps=6, verbosity_level=1, grammar=None, planning_interval=None, name="Research Assistant", description=None, prompt_templates=prompt_templates ) # Gradio Interface for arXiv research search and visualization iface = gr.Interface( fn=generate_visuals, inputs="text", outputs=["markdown", "image", "image"], title="🔎 arXiv Research Paper Search", description="Enter a topic or keywords to search for academic papers on arXiv. Get a list of papers with visual analysis.", examples=[["Machine Learning"], ["Quantum Computing"], ["Climate Change"]] ) # Launch Gradio Interface iface.launch() # The Gradio UI component (not needed if you already have the Gradio interface launched above) # GradioUI(agent).launch()