matterattetatte commited on
Commit
e13ea1d
·
verified ·
1 Parent(s): 71f6efc

Update tool.py

Browse files
Files changed (1) hide show
  1. tool.py +39 -31
tool.py CHANGED
@@ -1,38 +1,46 @@
1
  from smolagents import Tool
2
- from typing import Optional
3
 
4
  class SimpleTool(Tool):
5
- name = "get_travel_duration"
6
- description = "Gets the travel time between two places."
7
- inputs = {"start_location":{"type":"string","description":"the place from which you start your ride"},"destination_location":{"type":"string","description":"the place of arrival"},"transportation_mode":{"type":"string","nullable":True,"description":"The transportation mode, in 'driving', 'walking', 'bicycling', or 'transit'. Defaults to 'driving'."}}
8
- output_type = "string"
 
 
9
 
10
- def forward(self, start_location: str, destination_location: str, transportation_mode: Optional[str] = None) -> str:
11
- """Gets the travel time between two places.
 
 
 
 
 
 
 
12
 
13
- Args:
14
- start_location: the place from which you start your ride
15
- destination_location: the place of arrival
16
- transportation_mode: The transportation mode, in 'driving', 'walking', 'bicycling', or 'transit'. Defaults to 'driving'.
17
- """
18
- import os # All imports are placed within the function, to allow for sharing to Hub.
19
- import googlemaps
20
- from datetime import datetime
21
 
22
- gmaps = googlemaps.Client(os.getenv("GMAPS_API_KEY"))
 
 
 
23
 
24
- if transportation_mode is None:
25
- transportation_mode = "driving"
26
- try:
27
- directions_result = gmaps.directions(
28
- start_location,
29
- destination_location,
30
- mode=transportation_mode,
31
- departure_time=datetime(2025, 12, 6, 11, 0), # At 11, date far in the future
32
- )
33
- if len(directions_result) == 0:
34
- return "No way found between these places with the required transportation mode."
35
- return directions_result[0]["legs"][0]["duration"]["text"]
36
- except Exception as e:
37
- print(e)
38
- return e
 
 
1
  from smolagents import Tool
 
2
 
3
  class SimpleTool(Tool):
4
+ name = "pdf_extraction"
5
+ description = """Reads and extracts the text from all PDF files in the given folder and returns the combined text."""
6
+ inputs = {
7
+ "path": { "type": "string", "description": "Folder location of PDF files", "default": "pdfs", "nullable": True }
8
+ }
9
+ output_type = "any"
10
 
11
+ def __init__(self, *args, **kwargs):
12
+ super().__init__(*args, **kwargs)
13
+ try:
14
+ from pypdf import PdfReader
15
+ except ImportError:
16
+ raise ImportError(
17
+ "You must install package `pypdf` to run this tool: for instance, run `pip install pypdf`."
18
+ )
19
+ self.reader_class = PdfReader
20
 
21
+ def forward(self, path: str = "pdfs") -> str:
22
+ # Ensure the folder exists
23
+ if not os.path.exists(path):
24
+ return f"Error: The folder '{path}' does not exist."
 
 
 
 
25
 
26
+ # Find all PDF files in the folder
27
+ pdf_files = [file for file in os.listdir(path) if file.endswith(".pdf")]
28
+ if not pdf_files:
29
+ return f"No PDF files found in the folder '{path}'."
30
 
31
+ combined_text = []
32
+
33
+ # Iterate over each PDF file and extract its text
34
+ for pdf_file in pdf_files:
35
+ pdf_path = os.path.join(path, pdf_file)
36
+ try:
37
+ reader = self.reader_class(pdf_path)
38
+ file_text = ""
39
+ for page in reader.pages:
40
+ file_text += page.extract_text() # Extract text from each page
41
+ combined_text.append(f"### File: {pdf_file}\n{file_text.strip()}")
42
+ except Exception as e:
43
+ combined_text.append(f"### File: {pdf_file}\nError reading file: {str(e)}")
44
+
45
+ # Return all combined results
46
+ return "\n\n".join(combined_text)