mriusero commited on
Commit
fe23b95
·
1 Parent(s): 8d1eb96

feat: refacto

Browse files
Files changed (3) hide show
  1. app.py +3 -38
  2. tools/calculate.py +24 -0
  3. tools/get_timezone.py +19 -0
app.py CHANGED
@@ -5,9 +5,12 @@ import requests
5
  import urllib.parse
6
  import pytz
7
  import yaml
 
8
  from tools.final_answer import FinalAnswerTool
9
  from tools.web_search import DuckDuckGoSearchTool
10
  from tools.visit_webpage import VisitWebpageTool
 
 
11
 
12
  from Gradio_UI import GradioUI
13
 
@@ -22,44 +25,6 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
22
  """
23
  return "What magic will you build ?"
24
 
25
- @tool
26
- def calculator(operation: str, expression: str) -> str:
27
- """
28
- A tool that performs advanced mathematical operations using the Newton API.
29
- Args:
30
- operation: The mathematical operation to perform (e.g., 'derive', 'integrate').
31
- expression: The mathematical expression to operate on.
32
- Returns:
33
- The result of the mathematical operation as a string.
34
- """
35
- encoded_expression = urllib.parse.quote(expression)
36
- url = f"https://newton.now.sh/api/v2/{operation}/{encoded_expression}"
37
-
38
- response = requests.get(url)
39
-
40
- if response.status_code == 200:
41
- result = response.json().get("result")
42
- return result
43
- else:
44
- return f"Error: Unable to fetch result. Status code: {response.status_code}"
45
-
46
-
47
- @tool
48
- def get_current_time_in_timezone(timezone: str) -> str:
49
- """A tool that fetches the current local time in a specified timezone.
50
- Args:
51
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
52
- """
53
- try:
54
- # Create timezone object
55
- tz = pytz.timezone(timezone)
56
- # Get current time in that timezone
57
- local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
58
- return f"The current local time in {timezone} is: {local_time}"
59
- except Exception as e:
60
- return f"Error fetching time for timezone '{timezone}': {str(e)}"
61
-
62
-
63
  final_answer = FinalAnswerTool()
64
  web_search = DuckDuckGoSearchTool()
65
  visit_webpage = VisitWebpageTool()
 
5
  import urllib.parse
6
  import pytz
7
  import yaml
8
+
9
  from tools.final_answer import FinalAnswerTool
10
  from tools.web_search import DuckDuckGoSearchTool
11
  from tools.visit_webpage import VisitWebpageTool
12
+ from tools.get_timezone import get_current_time_in_timezone
13
+ from tools.calculate import calculator
14
 
15
  from Gradio_UI import GradioUI
16
 
 
25
  """
26
  return "What magic will you build ?"
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  final_answer = FinalAnswerTool()
29
  web_search = DuckDuckGoSearchTool()
30
  visit_webpage = VisitWebpageTool()
tools/calculate.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import urllib.parse
3
+ from smolagents import tool
4
+
5
+ @tool
6
+ def calculator(operation: str, expression: str) -> str:
7
+ """
8
+ A tool that performs advanced mathematical operations using the Newton API.
9
+ Args:
10
+ operation: The mathematical operation to perform (e.g., 'derive', 'integrate').
11
+ expression: The mathematical expression to operate on.
12
+ Returns:
13
+ The result of the mathematical operation as a string.
14
+ """
15
+ encoded_expression = urllib.parse.quote(expression)
16
+ url = f"https://newton.now.sh/api/v2/{operation}/{encoded_expression}"
17
+
18
+ response = requests.get(url)
19
+
20
+ if response.status_code == 200:
21
+ result = response.json().get("result")
22
+ return result
23
+ else:
24
+ return f"Error: Unable to fetch result. Status code: {response.status_code}"
tools/get_timezone.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datetime
2
+ import pytz
3
+ from smolagents import tool
4
+
5
+ @tool
6
+ def get_current_time_in_timezone(timezone: str) -> str:
7
+ """A tool that fetches the current local time in a specified timezone.
8
+ Args:
9
+ timezone: A string representing a valid timezone (e.g., 'America/New_York').
10
+ """
11
+ try:
12
+ # Create timezone object
13
+ tz = pytz.timezone(timezone)
14
+ # Get current time in that timezone
15
+ local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
16
+ return f"The current local time in {timezone} is: {local_time}"
17
+ except Exception as e:
18
+ return f"Error fetching time for timezone '{timezone}': {str(e)}"
19
+