aristidescc commited on
Commit
d0d5833
·
verified ·
1 Parent(s): f66908a

Update app.py

Browse files

Setup CodeAgent with available tools and added yoda_english_transform tool

Files changed (1) hide show
  1. app.py +35 -1
app.py CHANGED
@@ -1,5 +1,6 @@
1
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
  import datetime
 
3
  import requests
4
  import pytz
5
  import yaml
@@ -7,6 +8,8 @@ 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
@@ -33,6 +36,37 @@ def get_current_time_in_timezone(timezone: str) -> str:
33
  except Exception as e:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  final_answer = FinalAnswerTool()
38
 
@@ -55,7 +89,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,
 
1
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
  import datetime
3
+ import spacy
4
  import requests
5
  import pytz
6
  import yaml
 
8
 
9
  from Gradio_UI import GradioUI
10
 
11
+ nlp = spacy.load("en_core_web_sm")
12
+
13
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
14
  @tool
15
  def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
 
36
  except Exception as e:
37
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
38
 
39
+ @tool
40
+ def yoda_english_transform(sentence: str) -> str:
41
+ """A tool that converts an english sentence into the way Yoda talks.
42
+ Args:
43
+ sentence: A string representing the sentence to convert.
44
+ """
45
+ doc = nlp(sentence)
46
+
47
+ subject = ""
48
+ verb = ""
49
+ obj = ""
50
+ others = []
51
+
52
+ for token in doc:
53
+ if token.dep_ == "nsubj":
54
+ subject = token.text
55
+ elif token.dep_ == "ROOT":
56
+ verb = token.text
57
+ elif token.dep_ in ["dobj", "attr", "prep", "pobj"]:
58
+ obj += token.text + " "
59
+ else:
60
+ others.append(token.text)
61
+
62
+ # Construcción Yoda Style
63
+ if subject and verb:
64
+ yoda_sentence = f"{obj.strip()}, {verb} {subject} {' '.join(others)}"
65
+ else:
66
+ yoda_sentence = f"{sentence}... hmmm."
67
+
68
+ return yoda_sentence
69
+
70
 
71
  final_answer = FinalAnswerTool()
72
 
 
89
 
90
  agent = CodeAgent(
91
  model=model,
92
+ tools=[final_answer, image_generation_tool, get_current_time_in_timezone, yoda_english_transform], ## add your tools here (don't remove final answer)
93
  max_steps=6,
94
  verbosity_level=1,
95
  grammar=None,