hcliao commited on
Commit
c8b2fa7
·
verified ·
1 Parent(s): 874edda

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -18
app.py CHANGED
@@ -9,27 +9,33 @@ 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
13
- #Keep this format for the description / args / args description but feel free to modify the tool
14
- """A tool that translates simple English words to French or Spanish.
15
  Args:
16
- arg1: The English word to translate.
17
  arg2: Language choice (0 = French, 1 = Spanish).
18
  """
19
- translations = {
20
- "hello": ["bonjour", "hola"],
21
- "goodbye": ["au revoir", "adiós"],
22
- "yes": ["oui", "si"],
23
- "no": ["non", "no"]
 
 
24
  }
25
- if arg1.lower() not in translations:
26
- return "Error: Word not in dictionary."
27
- if arg2 == 0:
28
- return translation[arg1.lower()][0] # French
29
- elif arg2 == 1:
30
- return translations[arg1.lower()[1]] # Spanish
31
- else:
32
- return "Error: Use 0 for French or 1 for Spanish"
 
 
 
 
 
33
 
34
 
35
  @tool
@@ -69,7 +75,7 @@ with open("prompts.yaml", 'r') as stream:
69
 
70
  agent = CodeAgent(
71
  model=model,
72
- tools=[final_answer, get_current_time_in_timezone, my_custom_tool], ## add your tools here (don't remove final answer)
73
  max_steps=6,
74
  verbosity_level=1,
75
  grammar=None,
 
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:
13
+ """A tool that fetches the current time in a timezone and adds a greeting in French or Spanish.
 
14
  Args:
15
+ arg1: A string representing a valid timezone (e.g., 'America/New_York').
16
  arg2: Language choice (0 = French, 1 = Spanish).
17
  """
18
+ import datetime
19
+ import pytz
20
+
21
+ # 定義多語言問候語
22
+ greetings = {
23
+ 0: "Bonjour, l'heure actuelle à {timezone} est : {time}", # French
24
+ 1: "Hola, la hora actual en {timezone} es : {time}" # Spanish
25
  }
26
+
27
+ try:
28
+ # 創建時區對象並獲取當前時間
29
+ tz = pytz.timezone(arg1)
30
+ local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
31
+
32
+ # 根據語言選擇問候語
33
+ if arg2 in greetings:
34
+ return greetings[arg2].format(timezone=arg1, time=local_time)
35
+ else:
36
+ return "Error: Use 0 for French or 1 for Spanish."
37
+ except Exception as e:
38
+ return f"Error fetching time for timezone '{arg1}': {str(e)}"
39
 
40
 
41
  @tool
 
75
 
76
  agent = CodeAgent(
77
  model=model,
78
+ tools=[final_answer, my_custom_tool], ## add your tools here (don't remove final answer)
79
  max_steps=6,
80
  verbosity_level=1,
81
  grammar=None,