Victoria Shevchenko commited on
Commit
050d686
·
1 Parent(s): 3d1237b

new tools + timezone tool in agent

Browse files
Files changed (1) hide show
  1. app.py +45 -1
app.py CHANGED
@@ -1,11 +1,15 @@
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
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
@@ -18,6 +22,46 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
18
  """
19
  return "What magic will you build ?"
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
23
  """A tool that fetches the current local time in a specified timezone.
@@ -51,7 +95,7 @@ with open("prompts.yaml", 'r') as stream:
51
 
52
  agent = CodeAgent(
53
  model=model,
54
- tools=[final_answer], ## add your tools here (don't remove final answer)
55
  max_steps=6,
56
  verbosity_level=1,
57
  grammar=None,
 
1
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
  import datetime
3
+ import fitz
4
  import requests
5
+ import spacy
6
+ import pandas as pd
7
  import pytz
8
  import yaml
9
  from tools.final_answer import FinalAnswerTool
10
 
11
  from Gradio_UI import GradioUI
12
+ nlp = spacy.load("en_core_web_sm")
13
 
14
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
15
  @tool
 
22
  """
23
  return "What magic will you build ?"
24
 
25
+ @tool
26
+ def extract_text_from_pdf(pdf_path):
27
+ """A tool that extracts text from a PDF file.
28
+ Args:
29
+ pdf_path: A string representing the path to the PDF file.
30
+ """
31
+ document = fitz.open(pdf_path)
32
+ text = ""
33
+
34
+ # Iterate through each page
35
+ for page_num in range(len(document)):
36
+ page = document.load_page(page_num)
37
+ text += page.get_text()
38
+
39
+ return text
40
+
41
+ @tool
42
+ def extract_entities(text, desired_entities):
43
+ # Process the text with spaCy
44
+ doc = nlp(text)
45
+
46
+ # Extract entities that match the desired types
47
+ entities = [(ent.text, ent.label_) for ent in doc.ents if ent.label_ in desired_entities]
48
+ return entities
49
+
50
+ @tool
51
+ def create_table_with_specific_entities(text, desired_entities):
52
+ # Extract specific entities
53
+ entities = extract_entities(text, desired_entities)
54
+
55
+ # Create a DataFrame with desired entities as columns
56
+ df = pd.DataFrame(columns=desired_entities)
57
+
58
+ # Populate the DataFrame with entity values
59
+ for entity, label in entities:
60
+ df.loc[len(df), label] = entity
61
+
62
+ return df
63
+
64
+
65
  @tool
66
  def get_current_time_in_timezone(timezone: str) -> str:
67
  """A tool that fetches the current local time in a specified timezone.
 
95
 
96
  agent = CodeAgent(
97
  model=model,
98
+ tools=[get_current_time_in_timezone, final_answer], ## add your tools here (don't remove final answer)
99
  max_steps=6,
100
  verbosity_level=1,
101
  grammar=None,