HenryY2023 commited on
Commit
5a6b105
·
verified ·
1 Parent(s): 8c5c24b

update app.py for henry first agent

Browse files
Files changed (1) hide show
  1. app.py +72 -1
app.py CHANGED
@@ -3,10 +3,81 @@ 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
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
  def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
@@ -55,7 +126,7 @@ with open("prompts.yaml", 'r') as stream:
55
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
 
3
  import requests
4
  import pytz
5
  import yaml
6
+ import os
7
+ import json
8
  from tools.final_answer import FinalAnswerTool
9
 
10
  from Gradio_UI import GradioUI
11
 
12
+ @tool
13
+ def search_arxiv_papers(topic: str, max_results: int = 5) -> str:
14
+ """Searches arXiv for papers related to the given topic and returns results as JSON.
15
+ Args:
16
+ topic: The search query for arXiv.
17
+ max_results: Maximum number of results to return (default 5).
18
+ """
19
+ try:
20
+ url = 'http://export.arxiv.org/api/query'
21
+ params = {
22
+ 'search_query': f'all:{topic}',
23
+ 'start': 0,
24
+ 'max_results': max_results,
25
+ 'sortBy': 'submittedDate',
26
+ 'sortOrder': 'descending'
27
+ }
28
+ response = requests.get(url, params=params)
29
+ response.raise_for_status()
30
+
31
+ root = xml.etree.ElementTree.fromstring(response.content)
32
+ entries = root.findall('{http://www.w3.org/2005/Atom}entry')
33
+ papers = []
34
+
35
+ for entry in entries:
36
+ title = entry.find('{http://www.w3.org/2005/Atom}title').text.strip()
37
+ authors = [author.find('{http://www.w3.org/2005/Atom}name').text
38
+ for author in entry.findall('{http://www.w3.org/2005/Atom}author')]
39
+ summary = entry.find('{http://www.w3.org/2005/Atom}summary').text.strip()
40
+ link = entry.find('{http://www.w3.org/2005/Atom}id').text
41
+
42
+ papers.append({
43
+ 'title': title,
44
+ 'authors': authors,
45
+ 'summary': summary,
46
+ 'link': link
47
+ })
48
+
49
+ return json.dumps(papers)
50
+ except Exception as e:
51
+ return f"Error searching arXiv: {str(e)}"
52
+
53
+ @tool
54
+ def save_papers_to_folder(papers_json: str, folder: str = "downloads") -> str:
55
+ """Saves papers data to specified folder as text files.
56
+ Args:
57
+ papers_json: JSON string containing papers data.
58
+ folder: Target folder path (default 'downloads').
59
+ """
60
+ try:
61
+ os.makedirs(folder, exist_ok=True)
62
+ papers = json.loads(papers_json)
63
+
64
+ for paper in papers:
65
+ # Sanitize filename
66
+ title = "".join([c if c.isalnum() else "_" for c in paper['title']])[:50]
67
+ filename = os.path.join(folder, f"{title}.txt")
68
+
69
+ with open(filename, 'w') as f:
70
+ f.write(f"Title: {paper['title']}\n")
71
+ f.write(f"Authors: {', '.join(paper['authors'])}\n")
72
+ f.write(f"Link: {paper['link']}\n")
73
+ f.write(f"Summary: {paper['summary']}\n\n")
74
+
75
+ return f"Saved {len(papers)} papers to {folder} directory."
76
+ except Exception as e:
77
+ return f"Error saving papers: {str(e)}"
78
+
79
+
80
+
81
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
82
  @tool
83
  def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
 
126
 
127
  agent = CodeAgent(
128
  model=model,
129
+ tools=[final_answer, search_arxiv_papers, save_papers_to_folder], ## add your tools here (don't remove final answer)
130
  max_steps=6,
131
  verbosity_level=1,
132
  grammar=None,