alexander1198 commited on
Commit
a01a593
·
verified ·
1 Parent(s): 8c5c24b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -39
app.py CHANGED
@@ -1,69 +1,99 @@
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 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 does nothing yet
15
  Args:
16
- arg1: the first argument
17
- arg2: the second argument
18
  """
19
- return "What magic will you build ?"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
 
 
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
23
- """A tool that fetches the current local time in a specified timezone.
24
  Args:
25
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
26
  """
27
  try:
28
- # Create timezone object
29
  tz = pytz.timezone(timezone)
30
- # Get current time in that timezone
31
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
32
- return f"The current local time in {timezone} is: {local_time}"
33
  except Exception as e:
34
- return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
 
 
 
 
 
36
 
37
- final_answer = FinalAnswerTool()
 
38
 
39
- # 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:
40
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
41
 
 
 
 
42
  model = HfApiModel(
43
- max_tokens=2096,
44
- temperature=0.5,
45
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
46
- custom_role_conversions=None,
47
  )
48
 
 
 
 
 
 
49
 
50
- # Import tool from Hub
51
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
52
-
53
- with open("prompts.yaml", 'r') as stream:
54
- prompt_templates = yaml.safe_load(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,
62
- planning_interval=None,
63
- name=None,
64
- description=None,
65
  prompt_templates=prompt_templates
66
  )
67
 
68
-
69
- GradioUI(agent).launch()
 
 
 
 
1
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
2
+ from tools.final_answer import FinalAnswerTool
3
+ from Gradio_UI import GradioUI
4
  import datetime
 
5
  import pytz
6
+ import requests
7
  import yaml
 
8
 
9
+ # -------------------------
10
+ # 1) Real custom tool: translate text via LibreTranslate API
11
+ # -------------------------
12
  @tool
13
+ def translate_text(text: str, target_lang: str) -> str:
14
+ """Translate a given text into the target language using LibreTranslate.
 
15
  Args:
16
+ text: the source text to translate
17
+ target_lang: ISO code of the target language (e.g. 'it', 'en', 'es')
18
  """
19
+ try:
20
+ response = requests.post(
21
+ 'https://libretranslate.com/translate',
22
+ json={
23
+ 'q': text,
24
+ 'source': 'auto',
25
+ 'target': target_lang,
26
+ 'format': 'text'
27
+ },
28
+ timeout=5
29
+ )
30
+ data = response.json()
31
+ return data.get('translatedText', 'Errore nella traduzione')
32
+ except Exception as e:
33
+ return f"Translation error: {e}"
34
 
35
+ # -------------------------
36
+ # 2) Prebuilt tool: current time in timezone
37
+ # -------------------------
38
  @tool
39
  def get_current_time_in_timezone(timezone: str) -> str:
40
+ """Fetch the current local time in a specified timezone.
41
  Args:
42
+ timezone: e.g., 'Europe/Rome', 'America/New_York'
43
  """
44
  try:
 
45
  tz = pytz.timezone(timezone)
 
46
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
47
+ return f"Current local time in {timezone}: {local_time}"
48
  except Exception as e:
49
+ return f"Error fetching time: {e}"
50
 
51
+ # -------------------------
52
+ # 3) Load additional tools
53
+ # -------------------------
54
+ # DuckDuckGo web search tool
55
+ duck_search = DuckDuckGoSearchTool()
56
 
57
+ # Image generation tool from Hub
58
+ image_gen_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
59
 
60
+ # Final answer formatter
61
+ tool_final = FinalAnswerTool()
62
 
63
+ # -------------------------
64
+ # 4) Model setup
65
+ # -------------------------
66
  model = HfApiModel(
67
+ max_tokens=1500,
68
+ temperature=0.7,
69
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
 
70
  )
71
 
72
+ # -------------------------
73
+ # 5) Load prompt templates
74
+ # -------------------------
75
+ with open("prompts.yaml", 'r') as f:
76
+ prompt_templates = yaml.safe_load(f)
77
 
78
+ # -------------------------
79
+ # 6) Agent construction
80
+ # -------------------------
 
 
 
81
  agent = CodeAgent(
82
  model=model,
83
+ tools=[
84
+ tool_final,
85
+ translate_text,
86
+ get_current_time_in_timezone,
87
+ duck_search,
88
+ image_gen_tool,
89
+ ],
90
+ max_steps=8,
91
  verbosity_level=1,
 
 
 
 
92
  prompt_templates=prompt_templates
93
  )
94
 
95
+ # -------------------------
96
+ # 7) Launch Gradio UI
97
+ # -------------------------
98
+ if __name__ == "__main__":
99
+ GradioUI(agent).launch()