File size: 4,765 Bytes
9b5b26a
 
 
 
c19d193
6aae614
9b5b26a
1c474be
 
 
 
0928ebc
5df72d6
1c474be
 
9b5b26a
1c474be
 
9b5b26a
1c474be
 
 
 
 
 
 
 
 
 
 
 
 
 
9b5b26a
1c474be
 
 
9b5b26a
1c474be
 
 
9b5b26a
1c474be
9b5b26a
1c474be
 
9b5b26a
1c474be
 
 
 
 
 
 
 
 
 
 
9b5b26a
1c474be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0928ebc
1c474be
0928ebc
1c474be
0928ebc
 
1c474be
0928ebc
 
 
1c474be
 
 
 
 
 
7b88ea9
1c474be
 
 
7b88ea9
 
8097c77
 
1c474be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c01ffb
 
38b4b88
8bf73b6
 
 
6aae614
e121372
bf6d34c
 
0043127
fe328e0
13d500a
8c01ffb
 
1c474be
8c01ffb
861422e
 
9b5b26a
8c01ffb
8fe992b
7b88ea9
8c01ffb
 
 
 
 
 
861422e
8fe992b
 
9b5b26a
8c01ffb
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
150
151
152
153
154
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
import datetime
import requests
import pytz
import yaml
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI
from bs4 import BeautifulSoup
import arxiv
from PyPDF2 import PdfReader
from xml.etree import ElementTree
import io
# Below is an example of a tool that does nothing. Amaze us with your creativity !


@tool
def get_top_paper()-> str: 
    """A tool that fetches the most upvoted paper on Hugging Face daily papers.
    """
    url = "https://huggingface.co/papers"
    try:
        res = requests.get(url)
        res.raise_for_status() 
        # Parse the HTML response
        soup = BeautifulSoup(response.text, "html.parser")

        #inspect h3 the selector 
        top_paper = soup.find("h3")

        if top_paper_element:
            return top_paper.text.strip()
        else:
            return "Paper not found"

    except Exception as e:
        return f"Error fetching top paper: {str(e)}"
        
@tool
def get_paper_link(title:str)->str:
    """
    A Tool that finds the Hugging Face paper link given its title.
    Args:
        title: A string representing the title of the paper (eg., 'Competitive Programming with Large Reasoning Models').
    """
    url = "https://huggingface.co/papers"
    
    try:
        res = requests.get(url)
        res.raise_for_status()
        soup = BeautifulSoup(response.text, "html.parser")
        paper_links = soup.find("h3")
        
        for paper in paper_links:
            if paper.text.strip() == title:
                return "https://huggingface.co" + paper["href"] 
        
        return "Paper link not found."

    except Exception as e:
        return f"Error fetching paper link: {str(e)}"
        
@tool    
def get_paper_content(link:str)->str:
    """
    A tool that reads the first four pages of a paper and returns its content as a string given its link.
    Args:
        link: A string representing the URL of the paper (eg., 'https://huggingface.co/papers/2502.06807').
    
    """
    try:
        #Get the id from the Hugging face URL 
        paper_id = link.split("/papers/")[-1]
        paper = next(arxiv.Client().results(arxiv.Search(id_list=[paper_id])))

        #Get the PDF URL of the paper from arXiv
        pdf_url = paper.entry_id.replace("abs", "pdf") + ".pdf"
        response = requests.get(pdf_url)
        response.raise_for_status()
        pdf_buffer = io.BytesIO(response.content)

        # Extract text from the first four pages
        content = ""
        reader = PdfReader(pdf_buffer)
        pages = reader.pages[:4]
        for page in pages:
            content += page.extract_text() or ""  

        return content.strip()
    
    except Exception as e:
        return f"Error reading paper: {str(e)}"

        
@tool 
def get_related_papers(title:str, max_results:int)->list:
    """
    A tool that searches for related papers on arXiv based on the title of the query paper.
    Args:
        title: A string representing the title of the query paper to find related papers for.
        max_results: A integer representing the number of related papers to return.
    Returns:
        list: A list of dictionaries, each containing a related paper's title and URL.
    """
    try:
        search_url = f"http://export.arxiv.org/api/query?search_query=title:{title}&start=0&max_results={max_results}"
        resp = requests.get(search_url)
        if resp.status_code != 200:
            return f"Error: Failed to retrieve papers from arXiv. Status code: {response.status_code}"
        root = ElementTree.fromstring(resp.text)
        papers = []
        
        for entry in root.findall("{http://www.w3.org/2005/Atom}entry"):
            paper_title = entry.find("{http://www.w3.org/2005/Atom}title").text
            paper_url = entry.find("{http://www.w3.org/2005/Atom}id").text
            papers.append({"title": paper_title, "url": paper_url})
        
        return papers
    except Exception as e:
        return f"Error: {str(e)}"
        






final_answer = FinalAnswerTool()
model = HfApiModel(
max_tokens=2096,
temperature=0.5,
model_id='https://wxknx1kg971u7k1n.us-east-1.aws.endpoints.huggingface.cloud',
custom_role_conversions=None,
)




with open("prompts.yaml", 'r') as stream:
    prompt_templates = yaml.safe_load(stream)
    
agent = CodeAgent(
    model=model,
    tools=[final_answer,get_top_paper,get_paper_link,get_paper_content,get_related_papers], ## add your tools here (don't remove final answer)
    max_steps=6,
    verbosity_level=1,
    grammar=None,
    planning_interval=None,
    name=None,
    description=None,
    prompt_templates=prompt_templates
)


GradioUI(agent).launch()