petkar commited on
Commit
734b741
·
verified ·
1 Parent(s): d26b093

Update app.py

Browse files

replaced everything

Files changed (1) hide show
  1. app.py +46 -48
app.py CHANGED
@@ -1,45 +1,48 @@
1
- from smolagents import CodeAgent, 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
 
9
- from Gradio_UI import GradioUI
 
 
 
 
 
10
 
11
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
12
- #@tool
13
- #def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
14
- # #Keep this format for the description / args / args description but feel free to modify the tool
15
- # """A tool that does nothing yet
16
- # Args:
17
- # arg1: the first argument
18
- # arg2: the second argument
19
- # """
20
- # return "What magic will you build ?"
21
 
22
- # Load the DuckDuckGo search tool
23
- search_model = HfApiModel("Intelligent-Internet/II-Search-4B", return_text=True)
 
24
 
25
  @tool
26
  def my_custom_tool(city1: str, city2: str) -> str:
27
  """
28
  A tool that searches for the top 5 sightseeing locations in two cities.
29
- Args:
30
- city1: The name of the first city.
31
- city2: The name of the second city.
32
- Returns:
33
- A string listing the top 5 sightseeing locations in each city.
34
  """
 
 
 
 
 
 
 
35
 
36
- # Search for top 5 sightseeing locations in city1
37
- results_city1 = search_model(f"top 5 sightseeing locations in {city1}").split("\n")
38
- # Search for top 5 sightseeing locations in city2
39
- results_city2 = search_model(f"top 5 sightseeing locations in {city2}").split("\n")
40
- print(results_city1)
41
-
42
- # Format the results as a string
43
  output = f"Top 5 sightseeing locations in {city1}:\n"
44
  output += "\n".join(results_city1[:5]) + "\n\n"
45
  output += f"Top 5 sightseeing locations in {city2}:\n"
@@ -49,14 +52,8 @@ def my_custom_tool(city1: str, city2: str) -> str:
49
 
50
  @tool
51
  def get_current_time_in_timezone(timezone: str) -> str:
52
- """A tool that fetches the current local time in a specified timezone.
53
- Args:
54
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
55
- """
56
  try:
57
- # Create timezone object
58
  tz = pytz.timezone(timezone)
59
- # Get current time in that timezone
60
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
61
  return f"The current local time in {timezone} is: {local_time}"
62
  except Exception as e:
@@ -65,26 +62,21 @@ def get_current_time_in_timezone(timezone: str) -> str:
65
 
66
  final_answer = FinalAnswerTool()
67
 
68
- # 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:
69
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
70
-
71
- model = HfApiModel(
72
- max_tokens=2096,
73
- temperature=0.5,
74
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
75
- custom_role_conversions=None,
76
- )
77
-
78
-
79
- # Import tool from Hub
80
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
81
 
 
 
 
82
  with open("prompts.yaml", 'r') as stream:
83
  prompt_templates = yaml.safe_load(stream)
84
-
 
 
 
85
  agent = CodeAgent(
86
  model=model,
87
- tools=[final_answer, get_current_time_in_timezone,my_custom_tool ], ## add your tools here (don't remove final answer)
88
  max_steps=6,
89
  verbosity_level=1,
90
  grammar=None,
@@ -94,6 +86,12 @@ agent = CodeAgent(
94
  prompt_templates=prompt_templates
95
  )
96
 
97
- my_custom_tool("Berlin","Bremen")
 
 
 
 
 
98
 
99
- GradioUI(agent).launch()
 
 
1
+ from smolagents import CodeAgent, HfApiModel, load_tool, tool
2
  import datetime
3
  import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
+ from Gradio_UI import GradioUI
8
 
9
+ # -------------------------------
10
+ # 1️⃣ Initialize models
11
+ # -------------------------------
12
 
13
+ # Plain Hugging Face search model
14
+ search_model = HfApiModel(
15
+ "Intelligent-Internet/II-Search-4B",
16
+ return_text=True,
17
+ streaming=False # ensure it returns a string
18
+ )
19
 
20
+ # Main agent model
21
+ model = HfApiModel(
22
+ max_tokens=2096,
23
+ temperature=0.5,
24
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
25
+ custom_role_conversions=None
26
+ )
 
 
 
27
 
28
+ # -------------------------------
29
+ # 2️⃣ Define tools
30
+ # -------------------------------
31
 
32
  @tool
33
  def my_custom_tool(city1: str, city2: str) -> str:
34
  """
35
  A tool that searches for the top 5 sightseeing locations in two cities.
 
 
 
 
 
36
  """
37
+ # Query the search model as plain text
38
+ text_city1 = search_model(f"List the top 5 sightseeing locations in {city1}")
39
+ text_city2 = search_model(f"List the top 5 sightseeing locations in {city2}")
40
+
41
+ # Split by lines to get individual results
42
+ results_city1 = text_city1.split("\n")
43
+ results_city2 = text_city2.split("\n")
44
 
45
+ # Format the output
 
 
 
 
 
 
46
  output = f"Top 5 sightseeing locations in {city1}:\n"
47
  output += "\n".join(results_city1[:5]) + "\n\n"
48
  output += f"Top 5 sightseeing locations in {city2}:\n"
 
52
 
53
  @tool
54
  def get_current_time_in_timezone(timezone: str) -> str:
 
 
 
 
55
  try:
 
56
  tz = pytz.timezone(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:
 
62
 
63
  final_answer = FinalAnswerTool()
64
 
65
+ # Image generation tool from Hugging Face Hub
 
 
 
 
 
 
 
 
 
 
 
66
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
67
 
68
+ # -------------------------------
69
+ # 3️⃣ Load prompt templates
70
+ # -------------------------------
71
  with open("prompts.yaml", 'r') as stream:
72
  prompt_templates = yaml.safe_load(stream)
73
+
74
+ # -------------------------------
75
+ # 4️⃣ Create agent
76
+ # -------------------------------
77
  agent = CodeAgent(
78
  model=model,
79
+ tools=[final_answer, get_current_time_in_timezone, my_custom_tool],
80
  max_steps=6,
81
  verbosity_level=1,
82
  grammar=None,
 
86
  prompt_templates=prompt_templates
87
  )
88
 
89
+ # -------------------------------
90
+ # 5️⃣ Test the tool
91
+ # -------------------------------
92
+ if __name__ == "__main__":
93
+ result = my_custom_tool("Berlin", "Bremen")
94
+ print(result)
95
 
96
+ # Launch Gradio UI
97
+ GradioUI(agent).launch()