Spaces:
Runtime error
Runtime error
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool | |
import xml.etree.ElementTree as ET | |
import matplotlib.pyplot as plt | |
from wordcloud import WordCloud | |
from collections import Counter | |
import re | |
import datetime | |
import requests | |
import pytz | |
import yaml | |
from tools.final_answer import FinalAnswerTool | |
from Gradio_UI import GradioUI | |
# Below is an example of a tool that does nothing. Amaze us with your creativity ! | |
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) | |
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() | |
bar_chart_path = "bar_chart.png" | |
plt.savefig(bar_chart_path) | |
plt.close() | |
# Generate Word Cloud for Summary Text | |
wordcloud = WordCloud(width=500, height=300, background_color="white").generate(summaries) | |
plt.figure(figsize=(8, 5)) | |
plt.imshow(wordcloud, interpolation="bilinear") | |
plt.axis("off") | |
wordcloud_path = "wordcloud.png" | |
plt.savefig(wordcloud_path) | |
plt.close() | |
# 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 | |
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}") | |
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() | |
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder: | |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' | |
model = HfApiModel( | |
max_tokens=2096, | |
temperature=0.5, | |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded | |
custom_role_conversions=None, | |
) | |
# 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 | |
) | |
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"]] | |
) | |
iface.launch() | |
GradioUI(agent).launch() |