egarciag commited on
Commit
8ab983b
·
verified ·
1 Parent(s): 2979069

Update app.py

Browse files

Tools implementation

Files changed (1) hide show
  1. app.py +129 -16
app.py CHANGED
@@ -1,30 +1,146 @@
1
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
 
2
  import datetime
3
  import requests
4
  import pytz
5
  import yaml
 
6
  from tools.final_answer import FinalAnswerTool
7
 
8
  from Gradio_UI import GradioUI
9
 
10
  @tool
11
- def search_articles(month:str, text2search:str)-> str:
12
- """A tool that search articles in the site arxiv.org,
13
- filtered by month and a text used for the search
 
14
  Args:
15
- month: month of the article
16
- text2search: text to search for filtering the articles
 
 
 
 
 
17
  """
18
-
19
- return "..." #TBI (To Be Implemented)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  @tool
22
- def generate_markdown(data:str)-> str:
23
- """A tool that convert to markdown the text dad receive
 
 
24
  Args:
25
- data: the first argument
 
 
 
 
26
  """
27
- return "..." #TBD (To Be Implemented)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  @tool
30
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -54,17 +170,14 @@ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may
54
  custom_role_conversions=None,
55
  )
56
 
57
-
58
  # Import tool from Hub
59
  search_tool = DuckDuckGoSearchTool()
60
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
61
 
62
- with open("prompts.yaml", 'r') as stream:
63
- prompt_templates = yaml.safe_load(stream)
64
 
65
  agent = CodeAgent(
66
  model=model,
67
- tools=[search_tool, image_generation_tool, final_answer],
68
  max_steps=6,
69
  verbosity_level=1,
70
  grammar=None,
 
1
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
+ from shutil import make_archive
3
  import datetime
4
  import requests
5
  import pytz
6
  import yaml
7
+ import os
8
  from tools.final_answer import FinalAnswerTool
9
 
10
  from Gradio_UI import GradioUI
11
 
12
  @tool
13
+ def search_articles(text2search: str, start: int, end: int, output_md: str = "arxiv_articles.md") -> str:
14
+ """
15
+ A tool that searches articles on arxiv.org using a search term and saves the results as a Markdown file.
16
+
17
  Args:
18
+ text2search (str): Search query to filter articles.
19
+ start (int): Number of articles to skip.
20
+ end (int): Max results.
21
+ output_md (str): Path to save the Markdown file. Default is 'arxiv_articles.md'.
22
+
23
+ Returns:
24
+ str: Path to the generated Markdown file.
25
  """
26
+ try:
27
+ # Construct the ArXiv API query
28
+ search_url = (
29
+ f"http://export.arxiv.org/api/query?search_query=all:{text2search}"
30
+ f"&start={start}&max_results=5&sortBy=submittedDate&sortOrder=descending"
31
+ )
32
+
33
+ response = requests.get(search_url)
34
+ if response.status_code != 200:
35
+ return "Error: Failed to fetch articles from ArXiv."
36
+
37
+ # Extract and filter results
38
+ articles = []
39
+ entries = response.text.split("<entry>")[1:] # Splitting XML response
40
+ for entry in entries:
41
+ title_start = entry.find("<title>") + 7
42
+ title_end = entry.find("</title>")
43
+ title = entry[title_start:title_end].strip()
44
+
45
+ link_start = entry.find("<id>") + 4
46
+ link_end = entry.find("</id>")
47
+ link = entry[link_start:link_end].strip()
48
+
49
+ published_start = entry.find("<published>") + 11
50
+ published_end = entry.find("</published>")
51
+ published_date = entry[published_start:published_end][:10] # Extract YYYY-MM-DD
52
+
53
+ articles.append(f"### [{title}]({link})\n**Published Date:** {published_date}\n")
54
+
55
+ if not articles:
56
+ return f"No articles found for '{text2search}'."
57
+
58
+ # Save results to a Markdown file
59
+ with open(output_md, "w", encoding="utf-8") as md_file:
60
+ md_file.write(f"# ArXiv Articles on '{text2search}'\n\n")
61
+ md_file.writelines("\n".join(articles))
62
+
63
+ return f"Markdown file created: {os.path.abspath(output_md)}"
64
+
65
+ except Exception as e:
66
+ return f"Error: {str(e)}"
67
+
68
 
69
  @tool
70
+ def download_articles(article_links: list, save_folder: str = "downloads") -> str:
71
+ """
72
+ A tool that downloads articles from arxiv.org given a list of links.
73
+
74
  Args:
75
+ article_links (list): List of article links from arXiv.
76
+ save_folder (str): Folder to save downloaded articles. Default is 'downloads'.
77
+
78
+ Returns:
79
+ str: Success or error message.
80
  """
81
+ if not article_links:
82
+ return "Error: No article links provided."
83
+
84
+ # Ensure the save folder exists
85
+ os.makedirs(save_folder, exist_ok=True)
86
+
87
+ downloaded_files = []
88
+
89
+ for link in article_links:
90
+ try:
91
+ # Extract the article ID from the link
92
+ article_id = link.split("/")[-1]
93
+
94
+ # Construct the PDF download URL
95
+ pdf_url = f"https://arxiv.org/pdf/{article_id}.pdf"
96
+
97
+ # Download the PDF
98
+ response = requests.get(pdf_url, stream=True)
99
+ if response.status_code != 200:
100
+ return f"Error: Failed to download {pdf_url}"
101
+
102
+ # Save the file
103
+ file_path = os.path.join(save_folder, f"{article_id}.pdf")
104
+ with open(file_path, "wb") as file:
105
+ for chunk in response.iter_content(1024):
106
+ file.write(chunk)
107
+
108
+ downloaded_files.append(file_path)
109
+
110
+ except Exception as e:
111
+ return f"Error downloading {link}: {str(e)}"
112
+
113
+ return f"Downloaded articles:\n" + "\n".join(downloaded_files)
114
+
115
+ @tool
116
+ def create_zip_package(articles_folder: str, output_zip: str) -> str:
117
+ """
118
+ A tool that packages all downloaded articles (PDFs) from a folder into a zip file.
119
+
120
+ Args:
121
+ articles_folder (str): Folder containing downloaded articles.
122
+ output_zip (str): The name of the output zip file.
123
+
124
+ Returns:
125
+ str: Path to the created zip file.
126
+ """
127
+ if not os.path.exists(articles_folder):
128
+ return f"Error: Folder '{articles_folder}' does not exist."
129
+
130
+ file_paths = [os.path.join(articles_folder, f) for f in os.listdir(articles_folder) if f.endswith(".pdf")]
131
+
132
+ if not file_paths:
133
+ return "Error: No PDF files found for zipping."
134
+
135
+ try:
136
+ with zipfile.ZipFile(output_zip, 'w', zipfile.ZIP_DEFLATED) as zipf:
137
+ for file in file_paths:
138
+ zipf.write(file, os.path.basename(file))
139
+
140
+ return f"Zip package created successfully: {output_zip}"
141
+
142
+ except Exception as e:
143
+ return f"Error: {str(e)}"
144
 
145
  @tool
146
  def get_current_time_in_timezone(timezone: str) -> str:
 
170
  custom_role_conversions=None,
171
  )
172
 
 
173
  # Import tool from Hub
174
  search_tool = DuckDuckGoSearchTool()
 
175
 
176
+ my_prompt = ""
 
177
 
178
  agent = CodeAgent(
179
  model=model,
180
+ tools=[search_articles, download_articles, create_zip_package, final_answer],
181
  max_steps=6,
182
  verbosity_level=1,
183
  grammar=None,