MarcoM003 commited on
Commit
4d0f4a0
·
verified ·
1 Parent(s): a3272b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -72
app.py CHANGED
@@ -1,88 +1,42 @@
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
12
- def get_random_quote() -> str:
13
- """A tool that fetches a random quote from ZenQuotes API.
14
- """
15
- try:
16
- response = requests.get("https://zenquotes.io/api/random")
17
- data = response.json()
18
- quote = data[0]["q"]
19
- author = data[0]["a"]
20
- return f"\"{quote}\" - {author}"
21
- except Exception as e:
22
- return f"Error fetching quote: {str(e)}"
23
-
24
 
 
25
  @tool
26
- def search_wikipedia(query: str) -> str:
27
- """A tool that searches for information on Wikipedia.
28
- Args:
29
- query: The search query.
30
- """
31
- try:
32
- response = requests.get(f"https://en.wikipedia.org/w/api.php?action=opensearch&search={query}&limit=1&format=json")
33
- data = response.json()
34
- title = data[1][0]
35
- summary = data[2][0]
36
- return f"**{title}**: {summary}"
37
- except Exception as e:
38
- return f"Error searching Wikipedia: {str(e)}"
39
-
40
- @tool
41
- def get_current_time_in_timezone(timezone: str) -> str:
42
- """A tool that fetches the current local time in a specified timezone.
43
- Args:
44
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
45
- """
46
- try:
47
- # Create timezone object
48
- tz = pytz.timezone(timezone)
49
- # Get current time in that timezone
50
- local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
51
- return f"The current local time in {timezone} is: {local_time}"
52
- except Exception as e:
53
- return f"Error fetching time for timezone '{timezone}': {str(e)}"
54
-
55
-
56
- final_answer = FinalAnswerTool()
57
 
58
- # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
59
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
 
 
 
 
 
60
 
 
61
  model = HfApiModel(
62
- max_tokens=2096,
63
- temperature=0.5,
64
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
65
- custom_role_conversions=None,
66
  )
67
 
68
-
69
- # Import tool from Hub
70
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
71
-
72
- with open("prompts.yaml", 'r') as stream:
73
- prompt_templates = yaml.safe_load(stream)
74
-
75
-
76
  agent = CodeAgent(
77
  model=model,
78
- tools=[final_answer, get_random_quote, get_current_time_in_timezone, search_wikipedia],
79
- max_steps=6,
80
  verbosity_level=1,
81
- grammar=None,
82
- planning_interval=None,
83
- name=None,
84
- description=None,
85
- prompt_templates=prompt_templates
86
  )
87
 
 
 
88
  GradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent, HfApiModel
2
+ from datetime import datetime
 
3
  import pytz
 
 
4
 
5
+ # Definimos una herramienta que dice hola
 
 
6
  @tool
7
+ def say_hello() -> str:
8
+ """Una herramienta que dice hola."""
9
+ return "Hola, ¿cómo estás?"
 
 
 
 
 
 
 
 
 
10
 
11
+ # Definimos una herramienta que dice adiós
12
  @tool
13
+ def say_goodbye() -> str:
14
+ """Una herramienta que dice adiós."""
15
+ return "Adiós, hasta luego."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
+ # Definimos una herramienta que da la hora actual
18
+ @tool
19
+ def get_current_time() -> str:
20
+ """Una herramienta que da la hora actual."""
21
+ tz = pytz.timezone('America/New_York')
22
+ current_time = datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
23
+ return f"La hora actual es: {current_time}"
24
 
25
+ # Creamos el modelo de lenguaje
26
  model = HfApiModel(
27
+ max_tokens=1024,
28
+ temperature=0.5,
29
+ model_id='t5-base',
 
30
  )
31
 
32
+ # Creamos el agente
 
 
 
 
 
 
 
33
  agent = CodeAgent(
34
  model=model,
35
+ tools=[say_hello, say_goodbye, get_current_time],
36
+ max_steps=3,
37
  verbosity_level=1,
 
 
 
 
 
38
  )
39
 
40
+ # Lanzamos el agente
41
+ from Gradio_UI import GradioUI
42
  GradioUI(agent).launch()