File size: 4,817 Bytes
e0b448e
2ec100f
 
 
 
9b5b26a
 
 
c19d193
6aae614
e0b448e
 
8fe992b
e0b448e
9b5b26a
2ec100f
 
b402b11
 
88de546
b402b11
 
2ec100f
b402b11
2ec100f
2363498
e2fff14
 
 
 
2ec100f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0b448e
 
2ec100f
 
 
 
 
 
 
 
 
 
 
 
e0b448e
 
2ec100f
 
 
 
 
b402b11
2ec100f
b402b11
e2fff14
 
 
9b5b26a
e2fff14
9b5b26a
e2fff14
 
 
 
 
 
9b5b26a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c01ffb
 
6aae614
ae7a494
e121372
e0b448e
 
 
13d500a
8c01ffb
9b5b26a
 
8c01ffb
861422e
 
e0b448e
8c01ffb
8fe992b
e0b448e
8c01ffb
 
 
 
e2fff14
8c01ffb
861422e
8fe992b
 
e0b448e
2ec100f
 
 
 
 
 
 
 
 
e0b448e
2ec100f
9b5b26a
e0b448e
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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()